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

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

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

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

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

tar -cvfr allfiles.tar.gz -T mylist.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