The right way to Rename Screenshots in Linux – CloudSavvy IT

Posted on

Bash Shell

Many desktop Linux techniques save screenshots with names like Screenshot from 2020-11-29 18-57-51.png. Typically, what you actually wanted was to rename the recordsdata to one thing extra apparent like webinar1.png, webinar2.png, and so forth. Thankfully, renaming a bunch of recordsdata is very easy to do on the Linux command line.

The Bash shell may be very versatile, and gives other ways to guage values and develop variables. One neat analysis is arithmetic analysis. To carry out this analysis, wrap your arithmetic assertion with $(( and )).

The analysis can even embrace variable enlargement, like $sum to resolve into a worth. However for comfort, any Bash variables listed between $(( and )) are expanded routinely. For instance, to increment a variable rely by 1, you possibly can kind:

rely=$(( rely + 1 ))

This is similar as typing:

rely=$(( $rely + 1 ))

Arithmetic enlargement 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 worth in a variable. Examine the person web page for Bash, and scroll all the way down to ARITHMETIC EVALUATION, for the complete listing of supported operators and their priority.

To rename all my screenshots, I wanted to jot down this one-line Bash command:

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

However what does this do?

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

Then I exploit a for loop to function on all of the recordsdata that begin with Screenshot and finish with the .png extension. These are normally 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, reminiscent of Screenshot from 2020-11-29*.png. The backslashes are literal escapes to protect the areas within the file title.

Every iteration of the for loop shops a file title within the f variable. So the mv command mv -v "$f" webinar$n.png renames every file to my most popular file names like webinar1.png , webinar2.png , and so forth. I would like quotes across the $f variable enlargement so the areas in Screenshot from YYYY-MM-DD hh-mm-ss.png don’t trigger issues in my mv command. If you happen to get an error like mv: goal 'webinar1.png' just isn't 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 subsequent iteration within the loop. The arithmetic enlargement n=$(( n + 1 )) increments the n variable by 1.

Source link

Gravatar Image
I love to share everything with you

Leave a Reply

Your email address will not be published. Required fields are marked *