Examples of how to add text before file names, add text after file names, rename part of file names and rename to incrementing numbers by batch processing on the command line in bash shell.
What is a bash shell?
The GNU Bourne-Again Shell, known as bash, is a command language interpreter, that executes commands from standard input, known as an interactive shell, or from a file, known as a shell script. Shells are commonly used as command line interfaces (CLIs) to Linux and Unix operating systems. bash is based on sh.
The default user shell in Ubuntu is bash, while the default user shell in FreeBSD is sh. However, any shell can be installed and used for interactive commands and scripts.
What is batch processing of files in bash shell script?
Batch processing of files in command lines or shell scripts means executing the same operation for each file in a list. An example of this is a photographer, who have shot a large number of pictures, which he would like to post proces, such as adding his copyright logo to each picture just by executing a single command. This command or shell script would be batch processing.
A popular feature is list expansion, that is used for batch processing. The list of words following in is expanded, generating a list of items. The variable name is set to each element of this list in turn, and list is executed each time.
for name [ [ in [ word ... ] ] ; ] do list ; done
Another popular feature is arithmetic expansion, that evaluates an arithmetic expression (mathematical calculation) and substitutes the result.
$((expression))
How to switch to bash.
If the current interactive shell is not bash, then switch to bash by loading it from the command line.
$ bash foo@bar:~$
The command line prompt can be customized and configured in the .bashrc configuration file in the user home directory.
$ nano .bashrc PS1="foo@bar$ "
How to quickly create a set of test files with touch in bash.
A set of test files can quickly be created with the use of the touch utility and brace sequence expansion. In the following example, 3 files will be created and named as img and followed by a, b and c.
$ touch img{a,b,c}.jpg $ ls *.jpg imga.jpg imgb.jpg imgc.jpg
How to add text to beginning of file names in bash.
In the following example, the text foo- is added to the beginning of the file name.
$ ls *.jpg imga.jpg imgb.jpg imgc.jpg $ for file in *.jpg ; do mv "$file" "foo-$file" ; done $ ls *.jpg foo-imga.jpg foo-imgb.jpg foo-imgc.jpg
How to add text to end of file names in bash.
In the following example, the text -foo is added to the end of the file name, but before the file extension (suffix).
$ ls *.jpg imga.jpg imgb.jpg imgc.jpg $ for file in *.jpg ; do mv "$file" $( basename $file .jpg )-foo.jpg ; done $ ls *.jpg imga-foo.jpg imgb-foo.jpg imgc-foo.jpg
How to rename part of file names in bash.
In the following example, a the img part of the file name is renamed to pic. This kind of pattern substition is also known as the search and replace feature in text editors.
$ ls *.jpg imga.jpg imgb.jpg imgc.jpg $ for file in *.jpg ; do mv $file ${file//img/pic} ; done $ ls *.jpg pica.jpg picb.jpg picc.jpg
How to delete part of file names in bash.
In the following example, the img part of the file name is deleted. This is a kind of pattern substition, similar to above.
$ ls *.jpg imga.jpg imgb.jpg imgc.jpg $ for file in *.jpg ; do mv $file ${file//img} ; done $ ls *.jpg a.jpg b.jpg c.jpg
How to rename files to incrementing numbers in bash.
In the following example, the files are renamed to img and an incrementing number. The number will be zero padded and 3 digits, such as 000 and 001 is. The number will begin at 000.
$ ls *.jpg imga.jpg imgb.jpg imgc.jpg $ ls *.jpg | cat -n | while read number file ; do mv "$file" `printf "img%03d.jpg" $(($number-1))` ; done $ ls *.jpg img000.jpg img001.jpg img002.jpg
How to rename images to date and time in JPG EXIF header data.
In the following example, the EXIF header manipulation tool jhead is used to rename a batch of digital camera images to the JPG EXIF header data date and time. The format is set to ISO 8601 date and time standard format. This is smart for later handling and use, because it makes operating systems sort after time of shooting and thereby list them in chronological order.
$ ls *.JPG IMG0001.JPG IMG0002.JPG IMG0003.JPG $ jhead -n%Y-%m-%d-%H-%M-%S *.jpg $ ls *.jpg 2022-02-22-13-36-12.jpg 2022-02-22-13-37-00.jpg 2022-02-22-13-38-48.jpg
In the followin g example, the title portrait is added to the argument.
$ ls *.JPG IMG0001.JPG IMG0002.JPG IMG0003.JPG $ jhead -n%Y-%m-%d-%H-%M-%S-portrait *.JPG $ ls *.jpg 2022-02-22-13-36-12-portrait.jpg 2022-02-22-13-37-00-portrait.jpg 2022-02-22-13-38-48-portrait.jpg
How to rename Samsung Galaxy screenshots to date.
In the following example, a number of screenshots, taken on a Samsung Galaxy smartphone, are renamed to ISO 8601 date format. This is done by writing a little script, that loads bash and uses the cut utility to build the filenames for batch renaming.
$ ls *.jpg Screenshot_20220222-133700_appa.jpg Screenshot_20220222-133701_appb.jpg Screenshot_20220222-133702_appc.jpg $ nano ~/bin/rename-screenshots #!/usr/local/bin/bash for file in $(ls Screenshot_*.jpg); do year=$(echo $file | cut -c 12-15) month=$(echo $file | cut -c 16-17) day=$(echo $file | cut -c 18-19) hour=$(echo $file | cut -c 21-22) minute=$(echo $file | cut -c 23-24) second=$(echo $file | cut -c 25-26) combined="$year-$month-$day-$hour-$minute-$second" mv "$file" `printf "%s-screenshot.jpg" $combined` done $ rename-screenshots $ ls 2022-02-22-13-37-00-screenshot.jpg 2022-02-22-13-37-01-screenshot.jpg 2022-02-22-13-37-02-screenshot.jpg
More about batch processing.
How to add a transparent copyright notice to a batch of pictures with ImageMagick by myself. bash, csh and sh manuals in FreeBSD Manual Pages. Finding large files and sorting by size FreeBSD, UNIX, Linux and Mac OS X.