Solved

need to rename many files on linux server

Posted on 2014-02-18
7
206 Views
Last Modified: 2014-02-27
I have a website where the previous developer uploaded hundreds of jpg images but included jpg in the filename but not as a suffix. So for instance the images are named like this:

Images directory:
imageonejpg
imagetwojpg
imagethreejpg
etc.

From the command line can I rename all the images in a directory so that they will look like the following?

Images directory:
imageone.jpg
imagetwo.jpg
imagethree.jpg
etc.
0
Comment
Question by:befidled
7 Comments
 
LVL 13

Expert Comment

by:Sandy
ID: 39867125
use cut command to get rid of it.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39867136
cd Images_directory
for file in $(ls *jpg); do  mv $file ${file%jpg}.jpg; done
0
 
LVL 13

Expert Comment

by:Sandy
ID: 39867148
excellent woolmilkporc :D
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 13

Expert Comment

by:Sandy
ID: 39867189
in case any file begins with jpg then use "#" instead of "%"

TY/SA
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 39867204
If there are already some files with correct suffixes ".jpg" you can skip them with this:

cd Images_directory
for file in $(ls *jpg | grep -v "\.jpg$"); do mv $file ${file%jpg}.jpg; done
0
 
LVL 27

Expert Comment

by:serialband
ID: 39867721
Depending on your distribution, there may be a rename command now in /usr/bin/rename that uses perl regular expressions that allows you to rename multiple files.
0
 
LVL 1

Author Closing Comment

by:befidled
ID: 39893920
worked great! Thank you.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now