linux bash mass rename tool Centos/Debian/Ubuntu

Say you have a bunch of files like this:

file-01.jpg

file-02.jpg

...............

What if you want to mass rename them?

the rename tool can work just like sed

For Debian based distros:

Note "file" is what we search for and "newname" is what we replace it with, so substitute according to your requirements.

rename s/file/newname/ name-file-*.jpg

For RHEL Centos:

rename file newname file-*
 

Alternative way to batch rename files in Linux with bash using ls, sed and mv

for file in `ls -1`; do
newname=`echo $file|sed s/originalname/newname/g`
mv $file $newname
done

Just change "originalname" and "newname" in sed to match what you need.

The above doesn't work on spaces you'll need to use this code instead:

Based on this solution for not treating spaces as a new line or new variable in bash

For example the below code will replace all spaces with _

IFS=$(echo -en "\n\b")

for file in `ls -1`; do newname=`echo $file|sed s/" "/"_"/g`;  mv $file $newname;  done

 

Another example

Say if you have some filenames that have illegal samba characters and want to remove them.

In this example it is times with colons eg "14:30:32"

Sed searches for ":" and replaces with "" (nothing) but we could replace with _ - or whatever else.

for file in `ls -1`; do
newname=`echo $file|sed s/://g`
mv $file $newname
echo $newname
done
 


Latest Articles

  • AMD Set Fan Speed and Other Powerplay Memory/CPU Timings with a Linux script
  • Ethereum Mining Claymore Nanopool Error
  • genisoimage errors with long filenames and deep directory structures
  • Linux Kernel USB Export Errors
  • How to download gajim 0.16.9 XMPP/Jabber client so you can use OMEMO encryption
  • HP DL385 G7 Linux BIOS Update Flash
  • hwloc-nox set CPU affinity in Linux
  • Firefox An error occurred during a connection to some-ip-or-domain. SSL peer reports incorrect Message Authentication Code. Error code: SSL_ERROR_BAD_MAC_ALERT Solution
  • Proxmox understanding the directory structure and why an NFS datastore appears to be missing files/isos
  • pandoc convert markdown to html
  • Proxmox error uploading an iso solution
  • Cannot install moodle
  • MySQL change for Antelope format to Barracuda error solution
  • vmkping -I vmk1 10.0.2.69 PING 10.0.2.69 (10.0.2.69): 56 data bytes sendto() failed (Host is down)
  • gvfs mount in /run/user cannot be accessed or displayed wrong permissions
  • VMWare vSphere 6.7 Errors Solution 503 Service Unavailable (Failed to connect to endpoint:
  • How To Enable Nested KVM so guests can virtualize with hardware extensions
  • vi error solution E166: Can't open linked file for writing
  • Supermicro IPMI / KVM / BMC Remote Console Screen Resizing Issue - Window Cut Off Solution
  • Linux bash shell doesn't show username, hostname or current path fix solution