I’ve been working all this time with some data text files but things got really hard and complicated when the number of files exceed 5000. In fact someone has to format these files, validate, rename the extension and move them to a different directory.
Hmm… yeah sounds painful, although is a good reason to practice with shell scripting…
Let’s start with copying the file to a different directory :)
$ for file in *.txt; do cp $file ~/Desktop/newdir/$file; done
Now, checking if a file is empty to remove it! Ever heard of du command? Well if not man du for extra information. Now a simple script to check if the file size is 0 to remove the file, dead simple:
for file in *; do file_size=$(du $file | awk ‘{print $1}’); if [ $file_size == 0 ]; then rm -f $file; fi; done
Finally renaming the extension of all these files: (from .txt to .myformat)
for name in `ls *.txt` ; do newname=`echo $name | sed -e “s/^\(.*\).txt/\1.myformat/g”` ; mv $name $newname ; done