Skip to content

Instantly share code, notes, and snippets.

@janxkoci
Last active November 15, 2022 10:57
Show Gist options
  • Save janxkoci/29951cfc47b7ed4ad46b981324b3e118 to your computer and use it in GitHub Desktop.
Save janxkoci/29951cfc47b7ed4ad46b981324b3e118 to your computer and use it in GitHub Desktop.
See size of your top 10 largest files and folders.
#!/bin/bash
## Takes path as argument ($1) or uses current path instead (.) if no argument given.
path="${1:-.}"
## Gets size for items in path and reports the top 10 files and folders.
du -sh ${path}/* | sort -rh | head -n ${2:-10}
@janxkoci
Copy link
Author

This simple script shows sizes of top 10 largest files and folders for a given path, or if no path is given for the current path.

The magic is in the ${1:-.} piece. It is the traditional positional argument $1, but with a default value . in case the argument is omitted. I've stolen this trick from this awesome awk tutorial. (It's awesome, because it teaches more than awk 😉 )

@janxkoci
Copy link
Author

janxkoci commented Mar 8, 2022

Update

  • I've removed an underscore from the file name so it's easier to call the script with TAB completion.
  • I've added a second (optional) parameter to display different number of items. Provide any number.
    • Note that if you use this you also need the first parameter, so provide at least a dot . for the current folder, e.g. bigfiles.sh . 20.

@janxkoci
Copy link
Author

janxkoci commented Nov 15, 2022

Evgeny uses the following:

# Use this command in your home dir to locate large files:
find . -type f -size +10G | xargs -I {} ls -sh {}

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