قالب وردپرس درنا توس
Home / Tips and Tricks / How to Rename Screenshots in Linux – CloudSavvy IT

How to Rename Screenshots in Linux – CloudSavvy IT

Bash Shell

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

The Bash shell is very versatile and offers several ways to evaluate values ​​and extend variables. A nice evaluation arithmetic evaluation. To perform this evaluation, wrap your arithmetic statement $(( and )).

The evaluation may also include variable expansion, such as $sum to resolve into a value. But for the sake of convenience, all Bash variables are in between $(( and )) are automatically expanded. For example, if you want to increase the number of variables by 1

, you can type:

count=$(( count + 1 ))

This is the same as typing:

count=$(( $count + 1 ))

Arithmetic extension supports the same operators as in other programming languages, including + and - for addition and subtraction, * and / for multiplication and division, and % for the rest. You can also use ++ and -- to increase and decrease 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 priority.

To rename all my screenshots I had 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?

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

Then I use one for loop to all files starting with Screenshot and end with the .png extension. These are usually all the screenshots I took during my last webinar. If I had to be more precise I could include the date in that file specification, like Screenshot from 2020-11-29*.png. The backslashes are literal escapes to preserve spaces in the file name.

Each iteration of the for loop stores a filename in the f variable. So the mv order mv -v "$f" webinar$n.png renames any file to my favorite filenames such as webinar1.png , webinar2.png , and so on. I need quotes around the $f variable extension so that the spaces in Screenshot from YYYY-MM-DD hh-mm-ss.png Don’t cause any trouble in my mv order. If you get an error such as mv: target 'webinar1.png' is not a directory, you probably don’t have quotes around the $f.

Finally, I increase the value in the n variable so that it is ready for the next iteration in the loop. The arithmetic extension n=$(( n + 1 )) increases the n variable with 1.

Source link