Tuesday, May 6, 2008

Transferring Files using Shell Script

I have a directory where all my test codes are in there. I have test codes for C, shell scripts, perl and etc inside this directory. I realized that I could organize it further if I'll create a directory for c source codes, shell script source codes and etc. If I'll do the task manually, it will be a repetitive task. So I wrote a shell script to do the task for me.

#!/bin/sh
# Filename: organize.sh
# Description: A simple program that will transfer
# selected files to a certain directory
# Author: Obispo

# create a directory for c files
mkdir cfiles

# create a directory for sh files
mkdir shfiles

# transfer all c source codes to cfiles directory
for f in `ls *.c`
do
mv ${f} ./cfiles
done

# transfer all shell scripts to shfiles directory
for f in `ls *.sh`
do
mv ${f} ./shfiles
done

exit 0

Okay, here's how I came up with the program. If I'll execute the command 'ls *.c' at the console, it will display all the files that ends with '.c' right? So what I did was to get these files that ends with '.c' one by one and place it in a variable called 'f' then move it to my cfiles directory. The Unix command to move a file to another directory is 'mv'.


No comments: