DeveloperBreeze

Prerequisites

  • Access to an Ubuntu server.
  • Basic familiarity with using the command line.

Step-by-Step Guide

Step 1: Open Terminal

First, you'll need to open a terminal on your Ubuntu server. If you're accessing a remote server, you might use SSH to connect.

Step 2: Navigate to the Desired Directory

Navigate to the directory where you want to search for large files. If you want to search the entire file system, you can skip this step.

cd /path/to/directory

Replace /path/to/directory with the path to the directory you want to search.

Step 3: Use the find Command to Identify Large Files

Use the following command to find the top 10 largest files:

sudo find /path/to/directory -type f -exec du -h {} + | sort -rh | head -n 10

Explanation of the Command:

  • sudo: Runs the command with superuser privileges, which might be necessary for accessing certain files or directories.
  • find /path/to/directory -type f: Searches for files (-type f) in the specified directory and its subdirectories. Replace /path/to/directory with the target directory, or use / to search the entire file system.
  • -exec du -h {} +: Executes the du (disk usage) command for each file found, with -h providing human-readable sizes (e.g., MB, GB).
  • sort -rh: Sorts the files in reverse order by size, with the largest files listed first.
  • head -n 10: Displays the top 10 files from the sorted list.

Step 4: Review the Results

After executing the command, you'll see a list of the top 10 largest files, along with their sizes. For example:

1.2G    /var/log/largefile.log
900M    /home/user/bigdata.dat
850M    /opt/application/hugefile.bin
...

Alternative Method: Using du Command Directly

If you're interested in both files and directories, you can use the du command:

sudo du -ah /path/to/directory | sort -rh | head -n 10

This command lists the largest files and directories in the specified path.

Conclusion

By following this tutorial, you should be able to efficiently identify the largest files on your Ubuntu server. This information can help you manage disk space and make informed decisions about file storage and deletion.

For further optimization, consider creating a script that automates this process and sends reports to your email or logs the output for regular monitoring.

Continue Reading

Discover more amazing content handpicked just for you

Tutorial
bash

How to Reset the MySQL Root Password Using DROP USER

Now, you can create a new root user and assign a new password to it.

   CREATE USER 'root'@'localhost' IDENTIFIED BY 'your_new_password';

Oct 03, 2024
Read More
Tutorial

Getting Started with FFmpeg

  sudo apt update
  sudo apt install ffmpeg
  • CentOS/RHEL:

Aug 21, 2024
Read More
Tutorial

How to Install an AppImage on Linux

Installing and running an AppImage on Linux is a straightforward process that involves downloading the file, making it executable, and running it. AppImages provide a convenient way to use software on various Linux distributions without worrying about dependencies or package conflicts.

This tutorial has walked you through the basic steps to get an AppImage up and running. With this knowledge, you can easily try out new applications or run portable versions of your favorite software on any Linux distribution.

Aug 21, 2024
Read More
Code
bash

How to view free space on a Linux server

No preview available for this content.

Aug 11, 2024
Read More
Code
php bash

Laravel Artisan Commands Cheatsheet

  php artisan make:job JobName
  • Create a New Command

Aug 03, 2024
Read More
Code
javascript

Simple WebSocket Server using 'ws' library

No preview available for this content.

Jan 26, 2024
Read More
Code
python

Batch File Renaming Using os Module

import os

# Define the directory containing the files to rename
directory_path = 'rename'

try:
    # Check if the directory exists
    if not os.path.exists(directory_path):
        raise FileNotFoundError(f"The directory '{directory_path}' does not exist.")

    # List all files in the directory
    files_list = os.listdir(directory_path)
    if not files_list:
        print(f"The directory '{directory_path}' is empty.")
    else:
        print(f"Renaming files in '{directory_path}'...")

        # Iterate through files and rename them
        for index, file in enumerate(files_list):
            # Get the full path of the current file
            old_file_path = os.path.join(directory_path, file)

            # Skip directories, only process files
            if os.path.isfile(old_file_path):
                # Split the file name and extension
                file_name, file_extension = os.path.splitext(file)

                # Generate a new file name
                # Option 1: Add a new extension
                new_file_name = os.path.join(directory_path, f"{file_name}.newext")

                # Option 2: Rename with an index and new extension
                # new_file_name = os.path.join(directory_path, f"{index}.newext")

                # Rename the file
                os.rename(old_file_path, new_file_name)
                print(f"Renamed: {file} -> {os.path.basename(new_file_name)}")
            else:
                print(f"Skipping directory: {file}")

except FileNotFoundError as e:
    print(e)
except PermissionError:
    print(f"Permission denied: Unable to access or modify files in '{directory_path}'.")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

Jan 26, 2024
Read More
Code
python

List Files in a Directory Using os Module

No preview available for this content.

Jan 26, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!