X
Popular Searches

How to Rename Screenshots in Linux

Bash Shell

Many desktop Linux systems save screenshots with names like Screenshot from 2020-11-29 18-57-51.png. Often, what you really needed was to rename the files to something more obvious like webinar1.png, webinar2.png, and so on. Fortunately, renaming a bunch of files is really easy to do on the Linux command line.

Simple Arithmetic in Bash

The Bash shell is very versatile, and provides different ways to evaluate values and expand variables. One neat evaluation is arithmetic evaluation. To perform this evaluation, wrap your arithmetic statement with $(( and )).

The evaluation can also include variable expansion, like $sum to resolve into a value. But for convenience, any Bash variables listed between $(( and )) are expanded automatically. For example, to increment a variable count by 1, you could type:

count=$(( count + 1 ))

This is the same as typing:

count=$(( $count + 1 ))

Arithmetic expansion supports the same operators found in other programming languages, including + and - for addition and subtraction, * and / for multiplication and division, and % for remainder. You can also use ++ and -- to increment and decrement a value in a variable. Check the man page for Bash, and scroll down to ARITHMETIC EVALUATION, for the full list of supported operators and their precedence.

Renaming Screenshots on One Line

To rename all my screenshots, I needed to write this one-line Bash command:

n=1; for f in Screenshot*.png; do mv -v "$f" webinar$n.png; n=$(( n + 1 )); done

But what does this do?

Advertisement

The first part of the command, n=1, initializes the variable n to 1.

Then I use a for loop to operate on all the files that start with Screenshot and end with the .png extension. These are usually all the screenshots I captured during my last webinar. If I needed to be more precise, I might include the date in that file specification, such as Screenshot\ from\ 2020-11-29*.png. The backslashes are literal escapes to preserve the spaces in the file name.

Each iteration of the for loop stores a file name in the f variable. So the mv command mv -v "$f" webinar$n.png renames each file to my preferred file names like webinar1.png , webinar2.png , and so on. I need quotes around the $f variable expansion so the spaces in Screenshot from YYYY-MM-DD hh-mm-ss.png don’t cause problems in my mv command. If you get an error like mv: target 'webinar1.png' is not a directory, you probably didn’t put quotes around the $f.

Finally, I increment the value in the n variable so it’s ready for the next iteration in the loop. The arithmetic expansion n=$(( n + 1 )) increments the n variable by 1.

Jim Hall Jim Hall
Jim Hall is an innovative, high-achieving Senior IT Leader with over 25 years' experience in Linux, open source, and IT Leadership. He has contributed to dozens of open source projects as developer or maintainer, including FreeDOS and GNOME. Jim is also CEO at Hallmentum, where he empowers IT Leaders to drive meaningful change through hands-on training, workshops, and coaching. Read Full Bio »

The above article may contain affiliate links, which help support CloudSavvy IT.