Renaming and moving files in bulk
We frequently need to move and perhaps rename a set of files. System housekeeping often requires moving files with a common prefix or file type to a new folder. Images downloaded from a camera may need to be renamed and sorted. Music, video, and e-mail files all need to be reorganized eventually.
There are custom applications for many of these operations, but we can write our own custom scripts to do it our way.
Let's see how to write scripts to perform these kinds of operation.
Getting ready
The rename
command changes filenames using Perl regular expressions. By combining the find
, rename
, and mv
commands, we can perform a lot of things.
How to do it...
The following script uses find to locate PNG and JPEG files, then uses the ##
operator and mv
to rename them as image-1.EXT
, image-2.EXT
, and so on. This changes the file's name, but not its extension:
#!/bin/bash #Filename: rename.sh #Desc: Rename jpg and png files count=1; for img in `find . -iname '*.png...