Monday, April 7, 2014

Shell Script Programming Challenge

I'm just trying give myself a test on problem solving skill using Bash programming.

Given a scenario where the file, say file_A, locate in directory dir_A is required to rename into this format pattern <file>_yyyymmdd.txt. I’m require to write a bash script to accomplish this task. In addition to this, dir_A sometimes may contain multiple files, the script must be able to handle such situation.

Here is my work:
...
...

for XFILE in `ls ${DIR_A} | grep -v "\."`
do
   mv ${XFILE} ${XFILE}_`date +%Y%m%d`.txt
done
Surprise me! It just take 4 lines of code.

No comments: