#------------------------------------------------------------------------ #Problem: delete files which are empty (or below a certain size) #------------------------------------------------------------------------ # First let us create files needed for the exercise $ cat > HelloKitty.txt Hello, my name is Kitty I am the famous Japanese cat As in manga and merchandise! cntrl D #we now create a few empty files $ touch {a..d}.txt $ wc -c < HelloKitty.txt 83 $ ls -rw-r--r-- 1 srk staff 83 Feb 18 10:13 HelloKitty.txt # as can be seen from the wc output # the fifth "field" is the number of bytes $ ls -l -rw-r--r-- 1 srk staff 83 Feb 18 10:18 HelloKitty.txt -rw-r--r-- 1 srk staff 0 Feb 18 10:23 a.txt -rw-r--r-- 1 srk staff 0 Feb 18 10:23 b.txt -rw-r--r-- 1 srk staff 0 Feb 18 10:23 c.txt -rw-r--r-- 1 srk staff 0 Feb 18 10:23 d.txt # identify files with zero length $ ls -l | awk '$5==0{print $NF}' a.txt b.txt c.txt d.txt #usual approach: use xargs $ ls -l *.txt | awk '$5==0{print $NF}' | xargs rm #a variant $ ls -l *.txt | awk '$5==0{print "rm ", $NF}' | sh # a clever approach (fewer characters!) $ rm $(ls -l *.txt | awk '$5==0{print $NF}') #unix geeks: $() gets rid of \n, convenient here but annoying elsewhere #find is a powerful tool #i usually first print the files and only then delete $ find . -size 0 -exec ls {} \; #to ensure I know what I am doing $ find . -size 0 -exec rm {} \; #now execute