Solved

need to rename many files on linux server

Posted on 2014-02-18
7
210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
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.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

737 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