Solved

need to rename many files on linux server

Posted on 2014-02-18
7
209 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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 29

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is the error message I got (CODE) Error caused by incompatible libmp3lame 3.98-2 with ffmpeg I've googled this error message and found out sometimes it attaches this note "can be treated with downgrade libmp3lame to version 3.97 or 3.98" …
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
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.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

820 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