Author Topic: Renaming all the files in a dir.  (Read 3438 times)

0 Members and 1 Guest are viewing this topic.

Offline Super_X

  • I suck.
  • Hero Member
  • *****
  • Posts: 1340
  • I suck!
    • View Profile
Renaming all the files in a dir.
« on: January 13, 2007, 08:52:21 pm »
Hey, I'm needing help renaming all the files in a directory. I have a lot of pictures in a lot f different folders that are named 01.jpg 02.jpg, etc. and I want to move them all into one folder. There in lies the problem. The files all need to be renamed, or over written. I have a lot of files to rename if I do it by hand and I hoped that you may have a better idea for me to try.

I'm running Slack 11, with the bare.i 2.4 kernal.

What I'm trying to do is just like .. mv /file/dir/01.jpg /file/dir/prefix_01.jpg but with everythig in the file. Would this work:
mv /file/dir/*.jpg /file/dir/prefix_*.jpg ?

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: Renaming all the files in a dir.
« Reply #1 on: January 13, 2007, 09:11:56 pm »
A generic re-name program is "rename".  I don't know how to use it, but Newby does, or you can check the manpage.

Alternatively, in this situation, you can do this:
for i in *.jpg; do mv "$i" "prefix_$i"; done

Replace "*.jpg" with any pattern matching your files.  Even "*" works. 

Offline Super_X

  • I suck.
  • Hero Member
  • *****
  • Posts: 1340
  • I suck!
    • View Profile
Re: Renaming all the files in a dir.
« Reply #2 on: January 13, 2007, 10:41:03 pm »
I tried that, and it says: mv: cannot stat `.JPG': No such file or directory


lamer@kompooter:~/torrents/AMETHYST$ ls
01.JPG  07.JPG  13.JPG  19.JPG  25.JPG  31.JPG  37.JPG  43.JPG  49.JPG
02.JPG  08.JPG  14.JPG  20.JPG  26.JPG  32.JPG  38.JPG  44.JPG  50.JPG
03.JPG  09.JPG  15.JPG  21.JPG  27.JPG  33.JPG  39.JPG  45.JPG  51.JPG
04.JPG  10.JPG  16.JPG  22.JPG  28.JPG  34.JPG  40.JPG  46.JPG  52.JPG
05.JPG  11.JPG  17.JPG  23.JPG  29.JPG  35.JPG  41.JPG  47.JPG  THUMBS.DB
06.JPG  12.JPG  18.JPG  24.JPG  30.JPG  36.JPG  42.JPG  48.JPG`

That's one of the dirs, and like, I want it to be "ameth_[picture#].jpg" There's not enough constant besides the .JPG to do the $ with.
« Last Edit: January 13, 2007, 10:43:33 pm by Super_X »

Offline Newby

  • Moderator
  • Hero Member
  • *****
  • Posts: 10877
  • Thrash!
    • View Profile
Re: Renaming all the files in a dir.
« Reply #3 on: January 13, 2007, 10:58:13 pm »
Quote
$ cd ~/torrents/AMETHYST
$ for i in *.JPG; do mv "$I" "ameth_$I"; done
- Newby
http://www.x86labs.org

Quote
[17:32:45] * xar sets mode: -oooooooooo algorithm ban chris cipher newby stdio TehUser tnarongi|away vursed warz
[17:32:54] * xar sets mode: +o newby
[17:32:58] <xar> new rule
[17:33:02] <xar> me and newby rule all

I'd bet that you're currently bloated like a water ballon on a hot summer's day.

That analogy doesn't even make sense.  Why would a water balloon be especially bloated on a hot summer's day? For your sake, I hope there wasn't too much logic testing on your LSAT. 

Offline Super_X

  • I suck.
  • Hero Member
  • *****
  • Posts: 1340
  • I suck!
    • View Profile
Re: Renaming all the files in a dir.
« Reply #4 on: January 13, 2007, 11:42:50 pm »
lamer@kompooter:~/torrents/AMETHYST$ for I in *.JPG; do mv "$I" "ameth_$I"; done
mv: cannot stat `': No such file or directory
lamer@kompooter:~/torrents/AMETHYST$

Edit: It worked once I navigated to the directory with the images in them.. :")
« Last Edit: January 13, 2007, 11:52:33 pm by Super_X »