You are not logged in.

#1 2009-07-22 17:36:04

playdafunkimuzic
Member
Registered: 2008-10-25
Posts: 220

Command Line Batch Rename Files

Hi,

How can one batch rename files from the command line. E.g. if there's a bunch of files called a1.jpg, a2.jpg, a3.jpg, a4.jpg, a5.jpg etc...rename all the a's to b's or something of the sort. Thanks!

Offline

#2 2009-07-22 18:14:16

devnet
Member
Registered: 2005-07-15
Posts: 8
Website

Re: Command Line Batch Rename Files

If you are using KDE, try krename.

If you are using XFCE, thunar has a bulk rename plugin.

Another option is to install gprename which should be available in the pacman community repo.

I'm not sure about Gnome, cuz I don't use it smile

Offline

#3 2009-07-22 18:22:24

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: Command Line Batch Rename Files

in a simple situation like this:

rename a b *.jpg

in a more complicated situation:

for file in $(find ./ -name '*.jpg'); do
  new="$(echo "$file" | sed 's/a/b/g')"
  mv -iv "$file" "$new"
done

the second way is recursive and extremely powerful with adjustments to the find and sed options; the first is for quick and simple just-some-files-in-this-directory renaming.

Last edited by brisbin33 (2009-07-22 18:54:49)

Offline

#4 2009-07-23 14:43:40

Hrwa
Member
From: Croatia, Zagreb
Registered: 2008-10-09
Posts: 27

Re: Command Line Batch Rename Files

If you use vim, renamer script can be very useful:

http://www.vim.org/scripts/script.php?script_id=1721

Offline

#5 2009-07-23 15:16:32

windtalker
Member
Registered: 2008-03-17
Posts: 220

Re: Command Line Batch Rename Files

A little more indepth bash script:

http://www.moxleystratton.com/articles/ … files-bash

Offline

#6 2009-07-23 19:40:26

quarkup
Member
From: Portugal
Registered: 2008-09-07
Posts: 497
Website

Re: Command Line Batch Rename Files

there is a tool for that in AUR (file-rename-utils)


If people do not believe that mathematics is simple, it is only because they do not realize how complicated life is.
Simplicity is the ultimate sophistication.

Offline

#7 2009-07-23 20:24:36

timttmy
Member
From: UK
Registered: 2008-12-01
Posts: 53

Re: Command Line Batch Rename Files

I've used Batren in the past for renumbering photos. I found it on a Linux Format cover disk, many moons ago.
http://batren.sourceforge.net/cgi-bin/blis.cgi


This Is My Truth, Tell Me Yours

Offline

#8 2010-04-23 17:41:04

Ogion
Member
From: Germany
Registered: 2007-12-11
Posts: 367

Re: Command Line Batch Rename Files

One addendum to brisbins post. If you do the loop with command substituted find/ls, spaces in filenames will break the loop (well actually it will silently fail on you because it splits on whitespace too, thus thinking the parts of the filename with spaces are differentt filenames). If you want to use a loop with command substitution and filenames with spaces set the variable IFS to just tab and newline (in zsh you do this by IFS=$'\t\n'  i think in bash it works like that too, but i'm not sure)

Ogion

Edit: a very nice article i once found and read (it's long though..) about this topic..

Last edited by Ogion (2010-04-23 17:43:39)


(my-dotfiles)
"People willing to trade their freedom for temporary security deserve neither and will lose both." - Benjamin Franklin
"Enlightenment is man's leaving his self-caused immaturity." - Immanuel Kant

Offline

#9 2010-04-23 18:11:43

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: Command Line Batch Rename Files

Ogion wrote:

One addendum to brisbins post. If you do the loop with command substituted find/ls, spaces in filenames will break the loop (well actually it will silently fail on you because it splits on whitespace too, thus thinking the parts of the filename with spaces are differentt filenames). If you want to use a loop with command substitution and filenames with spaces set the variable IFS to just tab and newline (in zsh you do this by IFS=$'\t\n'  i think in bash it works like that too, but i'm not sure)

one pretty clean (bash) approach:

while IFS=$'\n' read -r file; do

  # what you want with "$file"

done < <(find ./ -depth -options)

filenames w/o newlines is an assumption i'm willing to make.

-depth is important; find will work bottom up so you can change parent directories without choking on subsequent changes to their children.  man find for other options to both increase power and prevent unintended consequences.

edit: content removed for clarity -- i talk to much.

Last edited by brisbin33 (2010-04-23 19:03:28)

Offline

#10 2010-04-23 19:13:53

firecat53
Member
From: Lake Stevens, WA, USA
Registered: 2007-05-14
Posts: 1,542
Website

Re: Command Line Batch Rename Files

Try 'prename' from the aur...

then if you have files test1.txt test2.txt test3.txt, you can renmae by:

prename 's/test/joe/' test*txt

to get joe1.txt joe2.txt joe3.txt

Scott

Offline

Board footer

Powered by FluxBB