Name changing script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Name changing script
# 1  
Old 10-26-2016
Name changing script

I have a problem, and I really need help with this. And I imagine that I will have to learn a new level of scripting to complete this. But I have over 5,000 pictures that are misnamed with the extension .jpg. They're not jpg, they're png, and it's extremely important that they're correctly filed. I need a script that will take all .jpg files, keep the names before the extension, and change the extension to .png. I tried something like

Code:
mv *.jpg* .png

but obviously, it didn't work. Does anybody have any information on how I would even go about doing this??

Also, I am using Kali Rolling, with the BASH shell.

Last edited by Huitzilopochtli; 10-26-2016 at 11:05 PM..
# 2  
Old 10-26-2016
With a question like this it ALWAYS helps to know what operating system and shell you're using...

Assuming you're using a shell that provides the basic parameter expansions required by the POSIX standards, you could try something like:
Code:
for file in *.jpg
do	mv "$file" "${file%.jpg}.png"
done

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 10-26-2016
Quote:
Originally Posted by Don Cragun
With a question like this it ALWAYS helps to know what operating system and shell you're using...

Assuming you're using a shell that provides the basic parameter expansions required by the POSIX standards, you could try something like:
Code:
for file in *.jpg
do    mv "$file" "${file%.jpg}.png"
done

Thank you for that, but as a new-ish user, my understanding of your answer is limited. Could you explain to me what exactly is happening right after the do command?? The punctuation is unfamiliar. Learning what it means could help me a lot during the future.

---------- Post updated at 09:14 PM ---------- Previous update was at 09:08 PM ----------

@ Don Cragun

Also, I just tested your script, and it works. Thank you so much for your time. But still, if you could explain to me, or point me to a place where I might learn, what exactly was scripted, it would be a tremendous help to me.
# 4  
Old 10-26-2016
The move command:
Code:
mv old new

moves the file named old to replace (or, if new did not exist, create) the file named new.
The command:
Code:
for file in *.jpg
do	mv "$file" "${file%.jpg}.png"
done

Runs that move command once for every file in the current working directory with a name ending with the string .jpg with the shell variable file set to the name of one of those files. With old being "$file", the name of the old file is the expansion of that variable protected by double quotes to be a single operand even if the old filename contains field separators (such as <space> or <tab>). With new being the parameter expansion ${file%.jpg} which expands the shell variable named file removing the smallest string matched by the filename matching pattern.jpg from the end of the contents of the variable and appends the string .png to the end of that variable expansion, and, again is enclosed in double quotes to protect agains field separators being present in the filename that is being expanded.

If you look at the man page for your shell, you'll find that there are several variable expansion operators in addition to the % operator that was used in the above expansion that can be used to remove the longest trailing pattern match, the shorted leading pattern match, the longest leading pattern match, return the number of characters in the contents of the variable; and with many shell, lots of other operators are also available.

Last edited by rbatte1; 10-27-2016 at 06:15 AM.. Reason: Corrected tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Changing TZ from a script

Hi i have a case statement so when the user selects a differnent timezone the TZ value should change, so when another script generates a html page the when date is displayed the time corresponds to the timezone selected. I can't get the TZ to change when using a script but can change the TZ by... (7 Replies)
Discussion started by: scriptnewbie
7 Replies

2. Solaris

Changing Passwords with a script.

We are real strict when it comes to passwords. Every 60 days the admins have to change passwords on all of the accounts. And there is pretty strict enforcement of the type of passwords chosen. This is a tedious and monotonous job. Ww don't use NIS or LDAP, so this has to be done on each machine. ... (5 Replies)
Discussion started by: brownwrap
5 Replies

3. Shell Programming and Scripting

Help with changing rsync script

#/bin/bash #set -vx DST_SRV=<destination_hostname> MDATE=`date +%Y%m%d%H%M` SRC_CONTENT="/home/prad/sourcecontent/" DST_CONTENT="/tmp/prad/destinationfolder/" DST_LOG="/tmp/prad/STATS" CURRENT_LOG="/home/prad/STATS/rsync-current.log" EMAIL="/home/prad/EMAIL/email.log"... (3 Replies)
Discussion started by: pnara2
3 Replies

4. Solaris

Help in changing the Parent id of a script

HI I want to run a script as new process. for example, I have two script A and B. From A I am calling B, but When I do ps -ef for the script B the parent ID should not be A' ID. Please help me Thanks & Regards Ramu (3 Replies)
Discussion started by: ramukumar
3 Replies

5. Shell Programming and Scripting

script for changing passwords

Hello, We are running aix 5.3. We're looking for a script that can change passwords, taking 2 arguments ( old password, new password ). I am wondering if this can be done with a here document, or some generic scripting method. Or, if I would have to download expect. Alternatively I wonder... (3 Replies)
Discussion started by: fwellers
3 Replies

6. Shell Programming and Scripting

Need help with script changing dates

I am attempting to write a script where the user enters the month and day (two digit format). I am trying to have script will increase 6 more times (totaling 7). I am having issues with the script increasing by one (its either dropping off the lead zero or not increasing for 08 and 09). While... (8 Replies)
Discussion started by: bbraml
8 Replies

7. Shell Programming and Scripting

Changing script into 2 functions.

ed... always same values get printed out... (2 Replies)
Discussion started by: HardyV2
2 Replies

8. Shell Programming and Scripting

Bash script: issue changing directories in script

I am working on a script that checks two arguments at the command line. The first argument is a search pattern, the second can be a file or a directory, if it is a file a second script is called that checks it for the search pattern. If the second argument is a directory, it checks for the search... (5 Replies)
Discussion started by: Breakology
5 Replies

9. UNIX for Dummies Questions & Answers

Changing directory through script

Hi Friends ! I want to change my current directory through a script. I am running the script say, from /home/proj directory. The script is like this : #!/usr/bin/sh cd module/pack/data once i run the script, i am still in /home/proj directory only. My problem is, i dont want to type... (5 Replies)
Discussion started by: mrgubbala
5 Replies

10. UNIX for Advanced & Expert Users

Changing font in script

I'm trying to figure out how to change the font or color of output in the middle of a shell script. I can't find anything that tells me how to do that. Any ideas? (1 Reply)
Discussion started by: MDyer18
1 Replies
Login or Register to Ask a Question