LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   script to change multiple filenames in a directory (https://www.linuxquestions.org/questions/linux-newbie-8/script-to-change-multiple-filenames-in-a-directory-305324/)

jeffreybluml 03-23-2005 09:36 PM

script to change multiple filenames in a directory
 
I imagine this would be simple, hoping somebody can help me out.

I just need a script that, when run in a directory, will take all of the image files in there and rename numerically. For instance, let's say I've got a directory with 10 .jpg images in it, all named randomly, and mostly with words as opposed to numbers (depends on whether the wife or I took the pics off the camera). All have the same file extension though, I imagine that'll help.

So, when run, I'd just like it to rename the images (in which order I guess I really don't care) starting with 1.jpg, 2.jpg, 3.jpg, etc...

Any help?

Thanks in advance...

mjrich 03-23-2005 09:50 PM

There are hundreds of threads covering this on LQ and elsewhere...

Best to do a search, and have a quick read of http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html and http://www.tldp.org/LDP/abs/html. For starters though, something like this will do the trick...
Code:

for f in *.jpg; do
    echo Renaming $f to $g.jpg...
    mv $f $g.jpg
done

I'll leave it up to you to figure out the the last bit (incrementing g).

Cheers,

mj

xowl 03-23-2005 09:55 PM

U could try this:


cd MyDirectory
j=1; for i in $(ls --color=none); do mv $i $j.jpg; j=$(($j+1)); done


Tell me if it worked

visaris 03-23-2005 09:56 PM

Code:

prompt # let "j = 1"; for file in * ; do cp $file $j.jpg; let "j += 1"; done
That worked for me in bash. Give it a try (on a copy to be safe).

jeffreybluml 03-24-2005 05:50 AM

Thanks folks! They all work wonderfully...

peter88 11-19-2006 03:25 PM

script to change multiple filenames in a directory
 
Thx for above script, works :) I want to achieve a slight variation on the theme, any pointers appreciated:

where its standard to be able to set an intger counter to count asin "j += 1", what I wish to achieve is to be able to increment ascii or unicode values. So for example lets say you had digital pic DSC001.jpg, DSC002.jpg .... DSCnnn.jpg, Now lets say I wanted to create a script and be able to pass it an argument as the $1 parameter such that the file(s) would be renamed to A.jpg, B.jpg...Z.jpg, OR aa.jpg, bb.jpg...zz.jpg, or aa.jpg, ab.jpb...az.jpg., how would I achieve this? Is there a function in bash or other Linux shells which enables me to do this?

A second related (or perhaps not :) ) question is as follows: When you have a file in a directory which begins with a hyphen ( - ), the mv cmd fails. Normally putting quotes around the filename does the trick, however lets say you have 5 files in a dir and one is called --abc def ghi.jpg , then of course you need to use "" around it to handle the spaces, but the wildcard does not work properly.

So :
$ cd /tmp
$ mv *.* /usr/bin/ will not work properly (* or *.*) and the --abc def ghi.jpg file will not be moved. How can I do this so that files which begin with a - and also contain spaces are moved?

TIA

xowl 11-27-2006 03:23 PM

Hi.

Actually, I don't know a "decent" method to do what you want to do just by using bash. It's possible to do it by using another scripting language joined with bash.

The only "pure bash" approach that I found out was something like

Code:

chars="a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9"
files=$(ls --color=none )

for r in $files; do
  echo $r
done | {
for i in $chars; do
  for j in $chars; do
    for k in $chars; do
      NewFileName=$i$j$k.jpg
      OldFileName=$(line)
      [ "$OldFileName" != "" ] &&  echo -n moving $OldFileName into $NewFileName
      [ "$OldFileName" != "" ] &&  mv $OldFileName $NewFileName
      [ "$OldFileName" != "" ] &&  echo ". Done"
    done
  done
done
}

Remember to set an appropiate comodin in the $files variable declaration.

Tell me if this works.


Camilo

xowl 12-03-2006 01:13 AM

The solution to the second problem is pretty easy:

to move the file "-camilo" to the file "asd" use:

Code:

mv ./-camilo asd
and voila!!

good louck

Camilo

peter88 12-06-2006 01:46 AM

xowl, thanks very much for your response, I will give it a go :)


All times are GMT -5. The time now is 05:55 PM.