[Unix] Process all files in a directory

[How To] Process all the files in a Unix directory

Unix is powerful beyond imagination. You can write simple shell scripts which can automate taks that can take hours and are monotous.

One such scenario is to process all files in a directory. The following tutorial explains how to traverse each and every file in a directory to perform a specific task. The task may include, but are not limited to the following:
- Renaming the file to add timestamp to its name.
- Changing the file extension.
- Conactenating all the files (although there are simpler ways to do this, this tutorial just explores the process.)
- Convert all the xls files to csv files using xls2csv command.
 and many more...
 
For our example, we will try to understand how-to change the extension of all ".txt" the files in a given directory to ".sql"

We write the below shell script for this purpose. Write the below code in a file called "script" using a vi editor.
cd ../
for fname in *.txt
do
mv "$fname" $fname.sql
done


Save the file and execute it with ./script command. The first line of the above script is very important, cd ../ goes back one level in directory structure (where the files to be renamed are stored). The lines that follow it rename all the .txt files to .sql files.

It is important to understand that we cannot keep this script and the files to be rename in the same folder. Otherwise, the script will run for all the files and rename the script itself as well :p !!