Mass Rename of IFS Files
September 30, 2009 Hey, Ted
I find Qshell a powerful tool for manipulating IFS files, but some Qshell features make me long for my MS DOS batch file programming days. A case in point is the ability to rename files in mass. Let me show you what I mean.
I want to rename the files with a .txt extension so that they have a .csv extension instead. I only want to rename .txt files, and I only want to rename the files in a certain directory. In MS DOS, I would have used a rename command with wildcards in both arguments.
ren *.txt *.csv
Try the same thing in Qshell and you get an error.
mv *.txt *.csv mv: 001-0085 Too many arguments specified on command. mv: 001-3017 usage: mv [-f | -i] source_file target_file mv [-f | -i] source_file ... target_dir
Please don’t tell me that I have no choice but to rename the files one by one.
The Unix shells predate MS DOS, Chuck, so Microsoft had the advantage of circumventing some of the Unix shells’ less desirable features. I don’t know if they purposely did so or not.
But to answer your question, you can rename files in bulk. Use the following three-line script.
for file in *.txt; do mv "$file" "$(basename $file txt)csv"; done
Here’s how it works:
The “for” loop runs once for each file that has a .txt extension. The basename utility returns the name of a file without the trailing txt extension. That is, “customer.txt” becomes “customer.”. Yes, the period is not stripped off. Qshell appends “csv” to the name.customer.csv. and passes it to the mv command, which interprets it as the second parameter.
mv customer.txt customer.csv
The mv renames the file.
If file names can have blanks in them, use this command sequence instead.
for file in *.txt; do base=$(basename "$file" txt); mv "$file" "$base"csv; done