Many desktop Linux methods save screenshots with names like Screenshot from 2020-11-29 18-57-51.png
. Usually, what you actually wanted was to rename the recordsdata to one thing extra apparent like webinar1.png
, webinar2.png
, and so forth. Happily, renaming a bunch of recordsdata is very easy to do on the Linux command line.
Easy Arithmetic in Bash
The Bash shell may be very versatile, and supplies alternative ways to judge values and broaden variables. One neat analysis is arithmetic analysis. To carry out this analysis, wrap your arithmetic assertion with $((
and ))
.
The analysis may embrace variable growth, like $sum
to resolve into a price. However for comfort, any Bash variables listed between $((
and ))
are expanded robotically. For instance, to increment a variable depend by 1, you would sort:
depend=$(( depend + 1 ))
This is identical as typing:
depend=$(( $depend + 1 ))
Arithmetic growth helps the identical operators present in different programming languages, together with +
and -
for addition and subtraction, *
and /
for multiplication and division, and %
for the rest. You can even use ++
and --
to increment and decrement a price in a variable. Verify the person web page for Bash, and scroll right down to ARITHMETIC EVALUATION, for the total checklist of supported operators and their priority.
Renaming Screenshots on One Line
To rename all my screenshots, I wanted to put in writing this one-line Bash command:
n=1; for f in Screenshot*.png; do mv -v "$f" webinar$n.png; n=$(( n + 1 )); achieved
However what does this do?
The primary a part of the command, n=1
, initializes the variable n
to 1.
Then I take advantage of a for
loop to function on all of the recordsdata that begin with Screenshot
and finish with the .png
extension. These are often all of the screenshots I captured throughout my final webinar. If I wanted to be extra exact, I would embrace the date in that file specification, comparable to Screenshot from 2020-11-29*.png
. The backslashes are literal escapes to protect the areas within the file identify.
Every iteration of the for loop shops a file identify within the f variable. So the mv
command mv -v "$f" webinar$n.png
renames every file to my most well-liked file names like webinar1.png
, webinar2.png
, and so forth. I would like quotes across the $f
variable growth so the areas in Screenshot from YYYY-MM-DD hh-mm-ss.png
don’t trigger issues in my mv
command. When you get an error like mv: goal 'webinar1.png' is just not a listing
, you most likely didn’t put quotes across the $f
.
Lastly, I increment the worth within the n
variable so it’s prepared for the following iteration within the loop. The arithmetic growth n=$(( n + 1 ))
increments the n
variable by 1.