Renaming Multiple Files
- 0
- Add a Comment
Renaming Multiple Files
Linux is chock full of useful tools for file manipulation. These are core programs, available in nearly all Linux distributions. Editing and renaming files are core functions, after all, in any operating system. These tools are very valuable for getting the most work done with the fewest keystrokes - read “they save time.”
I had just such a situation last week. I’m working on a site redevlopment project, moving as much as possible to php in order to ease site maintenance and user interactivity down the road. I mirrored the site to my test machine at home using ncftpget and started in with the editing process. However, the top-level files were named with an .shtml extension, intended for secure directories. I wanted these files to use a .phtml extension, compatible with php. In the top level directory were at least two dozen files with the .shtml extension. I could have gone through and renamed each file individually, but with the tools available in Linux, the task of renaming these files en masse took less than three minutes, scripwriting included. Here are the commands for renaming multiple files, and an explanation of how they worked.
#!/bin/sh
for a in *.shtml; do
t=`echo $a | sed ’s/.shtml$/.phtml/’`
mv $a $t
done
In particular, this script uses sed, the stream editor for Linux. This program manipulates text read from a stream rather than from a file. This script first finds all the files with the .shtml extension, assigning the document name to the variable a:
for a in *.shtml;
It then starts a loop using the ‘do’ command. Within this loop, the t variable is assigned the output of the command:
t=`echo $a | sed ’s/.shtml$/.phtml/’`
This command does several things. First, it echoes the current value of the $a variable - the file name sans extenstion. That output is piped [|] to sed. sed then replaces [s] the .shtml extension with the .phtml extension. As long as there are documents in the directory with an .shtml extension, the script stays “in the loop,” assigning each new document it finds to the $a variable. That gets piped to sed and renamed using the command
mv $a $t
You’ll remember that mv is the Linux command both to move and to rename a file. In this case, the command takes the old name [$a] and mvs the file to the new name [$t]. Once that’s done, the program looks for another .shtml document to start the process anew. When no .shtml documents remain in the directory, the program exits with [done].
To run this program, I created a shell script (as you’ve already seen by the #!/bin/sh command at the top of the program). I executed the program from within the directory containing the documents I wanted to modify and, before I could think twice, the documents were renamed from .shtml to .phtml.
A couple dozen files isn’t a lot, in the end. This script would work equally well with a directory containing thousands of documents.
That’s the robust and utilitarian nature of Linux - efficiency and speed with minimal keystrokes.
