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}'"