You are not logged in.
Pages: 1
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
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
Offline
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)
//github/
Offline
If you use vim, renamer script can be very useful:
Offline
A little more indepth bash script:
Offline
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
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
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
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)
//github/
Offline
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
Pages: 1