Skip to content

Instantly share code, notes, and snippets.

@vijayhardaha
Created September 24, 2023 18:56
Show Gist options
  • Save vijayhardaha/11c3f9e9fff7e90354093643845e6294 to your computer and use it in GitHub Desktop.
Save vijayhardaha/11c3f9e9fff7e90354093643845e6294 to your computer and use it in GitHub Desktop.
Find Large Size Files in the Command Line

Find Large Size Files in the Command Line

When managing your files and directories in the command line, it's important to locate and manage large files that may be consuming significant storage space. This custom shell function, find-large-files(), enables you to search for files larger than a specified size.

How to Use the Function in Your Shell Configuration Files

You can add the find-large-files() function to your shell configuration files (.bashrc, .bash_profile, or .zshrc) to make it available each time you start a new terminal session. Here's how to add and utilize the function in your shell configuration:

  1. Open Visual Studio Code or your preferred code editor.

  2. For Bash (.bashrc or .bash_profile) and Zsh (.zshrc):

    a. Bash Configuration: If you're using Bash, open your Bash configuration file. Depending on your system, it might be either ~/.bashrc or ~/.bash_profile.

    b. Zsh Configuration: If you're using Zsh, open your Zsh configuration file, typically ~/.zshrc.

  3. Copy and Paste: Copy and paste the following shell function at the end of your configuration file:

    # Function to find files larger than a specified size
    find-large-files() {
        local size="${1:-10M}"  # Default size is 10M if not provided
        find . -type f -size +"$size" -exec du -h {} + | sort -rh
    }
  4. Save the Changes: Save the changes to your configuration file.

  5. Reload the Configuration:

    • For Bash: In your terminal, either restart your terminal or run:

      source ~/.bashrc
    • For Zsh: In your terminal, either restart your terminal or run:

      source ~/.zshrc
  6. Usage: You can now use the find-large-files function in your Bash or Zsh shell. For example, to find files larger than 100 megabytes:

    find-large-files 100M

Conclusion

By incorporating the find-large-files function into your shell configuration, you can effortlessly locate large files within your command-line environment. This custom function proves to be a valuable tool for managing disk space and organizing your files and directories more effectively.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment