Useful Bash Tricks

Print the path of a temporary file which contains the output of ‘’ls’’:

echo <(ls)

Print all rows contained in ‘‘fileA’’ which are not contained in ‘‘fileB’’ (diff):

comm -23 <(sort fileA) <(sort fileB)

Print all rows contained in ‘‘fileA’’ which are also contained in ‘‘fileB’’ (intersection):

comm -12 <(sort fileA) <(sort fileB)

Assign the output of ‘’ls’’ to variable ‘‘THE_LIST’’:

THE_LIST=$(ls)

Loop over files:

for file in *; do echo $file; done

Loop over lines in file:

cat file.txt | while read line ; do echo $line ; done

Watch the size of a file change (updates every 5 seconds):

watch -n 5 "ls -lh myfile | awk '{print \$5}'"

Archive and compress files using tar but use a file list:

tar -cvfr allfiles.tar.gz -T mylist.txt

Delete a directory with lots of subdirectories and files (fast according to this comment):

mkdir empty_dir
rsync -a --delete empty_dir/ dir_to_delete/

Add line numbers to a file:

nl -ba file.txt

Batch renaming files

Using find and sed piped into a shell:

find . -type f | sed -n 's/\.\/\(.*\)$/mv "error_\1" "\1"/p' | sh

Using Bash parameter expansion:

for i in *.txt ; do mv "$i" "${i/.txt/.sql}" ; done

Change filenames of all JPEG files in this directory (and directories below) to lower case:

for f in `find . -type f -name '*.jpg'`; do mv -v "$f" "`echo $f | tr '[A-Z]' '[a-z]'`"; done