Web & Software Developer

Linux Terminal Commands For Managing Files in Bulk – Cheat Sheet

This is a quick reference cheat sheet for Linux commands to manage files on the command line.

Count the files (including directories) in a directory. This gives you the total number of files.

ls | wc -l 

Count the files in a directory, per each type of extension. This will tell you how many of each type of file there is in a directory. It’s a quick summary and doesn’t list each single file.

find -type f | sed -e 's/.*\.//' | sort | uniq -c 

Count all the files in a directory of one particular extension. This example counts all .jpg files. It will give you the count, but this doesn’t list each file. (The next command lists each file.)

ls | grep '\.jpg$' | wc -l 

List all files of a particular extension in a directory. This example lists all .jpg files.

ls | grep '\.jpg$' 

Compare the contents of two folders with the diff command. This lets you see the difference between 2 folders. This compares just filenames, and not the file contents:

diff <(cd folder1 && find . | sort) <(cd folder2 && find . | sort) 

Find all files in a directory with a specific file size. This example lists specific files that are 50000k or larger:

find -type f -size +50000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }' 

List all folders in a directory sorted by MegaBytes. This will give the size, in MB, of all folders:

du --max-depth=1 | sort -n | awk 'BEGIN {OFMT = "%.0f"} {print $1/1024,"MB", $2}' 

Bulk rename all files in a folder. This example changes the extension of all files in the directory from .htm to .html

rename 's/\.htm$/\.html/' *.htm 

To do a test run for the line above, add -n after rename. This will not actually change anything. It will show the proposed changes:

rename -n 's/\.htm$/\.html/' *.htm 

More examples of bulk renaming here.


Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this: