• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

need to rename many files on linux server

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
befidled
Asked:
befidled
1 Solution
 
SandyCommented:
use cut command to get rid of it.
0
 
woolmilkporcCommented:
cd Images_directory
for file in $(ls *jpg); do  mv $file ${file%jpg}.jpg; done
0
 
SandyCommented:
excellent woolmilkporc :D
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
SandyCommented:
in case any file begins with jpg then use "#" instead of "%"

TY/SA
0
 
woolmilkporcCommented:
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
 
serialbandCommented:
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
 
befidledAuthor Commented:
worked great! Thank you.
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now