This is a quick reference cheat sheet for Linux commands to manage files on the command line.
Counting Files
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 filename:
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 filename. This one is not recursive.
ls | grep '\.jpg$' | wc -l
Recursively count all the files in a directory of one particular extension. This example counts all .php
files. It will give you the count, but this doesn’t list each filename.
find . -type f -print | grep '\.php$' | wc -l
Listing Filenames
List Files by Extension
List all files of a particular extension in a directory. This one is not recursive. (See below for the recursive one.) This example lists all .jpg
files:
ls | grep '\.jpg$'
Recursively list all files of a specific extension in a directory and all sub-directories. This example lists all .jpg
files, and the sub-directory it is found in, if any:
find . -type f -name "*.jpg"
List Files By Size
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 Directories Sorted by Size
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}'
Compare Lists of Files in 2 Text Files
Get the Difference of 2 Lists
If you have two text files that each contain a list of filenames, this lets you find the filenames that are in one list and not in the other list. This will find and list all files that are in “file_1.txt” but not in “file_2.txt”:
grep -v -f file_2.txt file_1.txt
To see the opposite, that is, the files that are in “file_2.txt” but not in “file_1.txt”, flip the order of the files in the command, like this:
grep -v -f file_1.txt file_2.txt
Get the Union of 2 Lists, That is Files That Appear in Both Lists
If you have two text files that each contain a list of filenames, this lets you find the filenames that appear in both lists. This will find and list all files that are listed in “file_1.txt” and in “file_2.txt”:
comm -12 <(sort file_1.txt) <(sort file_2.txt)
Compare Files in a Directory
Compare the contents of two folders with the diff
command. This lets you see the difference between two directories, meaning the difference of files between two folders. This compares just filenames, and not the file contents:
diff <(cd folder1 && find . | sort) <(cd folder2 && find . | sort)
Search Contents of All Files For Strings Listed in a Text File
Search the content of all files in a directory for each string listed in a text file. In other words, search to find all strings that are listed in a text file, searching the contents (within the file content) of all files of a specified directory, recursively. In the following example command, the search strings are listed in a file called “searchlist.txt” with each search string on a new line. It will search the content of all files in the dir
directory.
find /home/path/to/search/dir -type f -exec grep -if searchlist.txt /dev/null {} +
Copying or Moving Files
Copy all files of a certain extension from one directory to another directory. The following example copies all JPG files (ending with “.jpg”) from directory 1 (dir1
) to directory 2 (dir2
).
find /dir1 \( -name "*.jpg" \) -exec cp {} /dir2 \;
To copy files from a list of filenames, from one directory to another, assuming the list is named “copy_list.txt”, and you want the files moved to “new_dir”:
xargs -a copy_list.txt cp -t /home/path/to/new_dir
For the above command, it is important that the “copy_list.txt” contain a list of filenames that have their full path, for example:
/home/path/to/image-1.jpg /home/path/to/image-2.jpg /home/path/to/image-3.jpg
Deleting Files
To delete all files listed in a text file, assuming your list is saved in a file called “delete_list.txt” with each file on a new line. It should list the full path to the file, not just the filename. This command will delete all files listed in the text file:
xargs rm -r <delete_list.txt
Renaming Files
The following examples bulk rename all files in a folder.
Change the extension in a filename. This example changes the extension of all files 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
The following commands will rename files by removing the last 8 characters, not counting the extension. So this will re-attach the extension to the filename after removing the last 8 characters. You can change 8 to the number of characters you need to remove from the end of the filename.
This will remove the last 8 characters from all PNG files:
rename 's/.{8}\.png$/\.png/' *.png
This will remove the last 8 characters from all JPG files:
rename 's/.{8}\.jpg$/\.jpg/' *.jpg
This will remove the last 8 characters from all JPEG files:
rename 's/.{8}\.jpeg$/\.jpeg/' *.jpeg
Questions and Comments are Welcome