You are not logged in.

#1 2009-03-05 22:23:43

build
Member
Registered: 2009-03-05
Posts: 15

File renaming hardships

Hi there,

I have been trying to rename some files and it became a mess. I undone it, and now i could use some help please smile I am tired of trying things myself. And i am certainly not a hero when it comes to anything with programs.

I have a directory with 50 subdirectories, named like

"005 - lateral cosmonaut transportation vehicle"
"006 - trianguloid vacuum phaser"
etc.

In the directories are files which I want to rename in reversed order (last date stamped file will be renamed first). Files are currently  typical digicam naming (e.g. P0000000.JPG). I would like to have the three numbers of the directory prefixed in the new filename. Then I want the new filename to have a "listnumber" Ill explain, thatll be more wise:

This is the current situation of a directory:

$ ls -x -1 --reverse
P1010857.JPG
P1010856.JPG
P1010564.JPG
P1010563.JPG
P1010562.JPG
P1010561.JPG
P1010560.JPG
P1010559.JPG
P1010558.JPG

That is reversed. Directory is lets say 041... Then i want the first file P1010857.JPG renamed to 041 - 001.jpg the second to 041 - 002.jpg.. etc etc.
For all 50 directory's contents
Help is appreciated!

Thanks

Offline

#2 2009-03-05 22:55:07

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

Re: File renaming hardships

hmm, i like this project.  here's what i'm thinking just off the top of my head:

#!/bin/bash

find ./ -maxdepth 1 -name '*' | while read dir; do
  count=1
  ls -x -1 --reverse $dir | while read file; do
    newname="${dir} - ${count}.jpg"
    mv ${dir}/${file} ${dir}/${newname}
    count=$(( count + 1 ))
  done
done

exit 0

someone who knows awk has to come along and get count to print as 001, 002, 003 etc. b/c i can't figure that part out.

Last edited by brisbin33 (2009-03-05 22:56:13)

Offline

#3 2009-03-05 22:57:00

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: File renaming hardships

IFS="
"
for dir in *; do ! [ -d "$dir" ] && continue; (i=1; cd "$dir"; for file in $(\ls -1 --reverse); do echo $file; mv --backup=t -v "$file" "$dir - $i.jpg"; i=$(($i + 1)); done; rename "- " "- 00" *-\ ?.jpg; rename "- " "- 0" *-\ ??.jpg) ; done

Well, that renaming is a bit of a hack. If you have +1000 pictures, then you will have to use
rename "- " "- 000" *-\ ?.jpg; rename "- " "- 00" *-\ ??.jpg; rename "- " "- 0" *-\ ???.jpg

Offline

#4 2009-03-06 02:38:10

dsr
Member
Registered: 2008-05-31
Posts: 187

Re: File renaming hardships

For a simple-to-follow but not very efficient solution, you could implement the following pseudocode in your favorite language.

for each directory
    list all the files that end in .JPG
    sort the list in reverse order
    for each file in the list
        rename the file to "<directory> - <list index>.jpg"

Offline

#5 2009-03-06 05:11:46

Killa B
Member
From: United States
Registered: 2008-10-28
Posts: 42
Website

Re: File renaming hardships

I think mine is the first to do exactly what you asked for:

#!/bin/bash

dirname="`pwd | sed s,.*/,,`"

numplaces=`expr length $(ls | wc -l)`

i=0
ls -x -1 --reverse | while read fname
do
    i=$[i+1]

    iplaces=`expr length $i`
    fileext="`echo $fname | awk -F . '{print $NF}'`"

    newfname="$dirname - "
    echo $newfname
    echo $[numplaces-iplaces]

    j=0
    while [ $j -lt $[numplaces-iplaces] ]
    do
        j=$[j+1]
        newfname="${newfname}0"
        echo $newfname
    done
    newfname="$newfname$i.$fileext"
    echo $newfname

    mv "$fname" "$newfname"
done

Edit: Damn, I just realized you wanted this to be done for each directory. I'm too tired, though. It shouldn't be hard to add that bit yourself.

Last edited by Killa B (2009-03-06 05:13:46)

Offline

#6 2009-03-06 11:12:56

build
Member
Registered: 2009-03-05
Posts: 15

Re: File renaming hardships

Thanks for the replies guys! However, none, of the scripts did the job. Ill describe what they did. I hope it wont sound as criticism bc i appreciate your efforts.

Executing Brisbin's script was followed by

mv: target '1.jpg' is not a directory
mv: target '1.jpg' is not a directory
...
ls: kan geen toegang krijgen tot ./010: Bestand of map bestaat niet
ls: kan geen toegang krijgen tot -: Bestand of map bestaat niet
ls: kan geen toegang krijgen tot nuclear: Bestand of map bestaat niet
ls: kan geen toegang krijgen tot assault: Bestand of map bestaat niet
ls: kan geen toegang krijgen tot hoover: Bestand of map bestaat niet

The ls:part says it cant access the directories, because file or directory doesnt exist. Each word or section of the filename or directoryname gets treated as if it were a file/dir.

btw: This happens also with plain ls (. e.g.: "for i in ls; do mv ...") Then stuff like permission, owner, group, date etc is included in the long print of errors. If anyone knows moving a filename can be treated as a whole, instead of the parts, then please tell. Although this is a bit off-topic, I never could have found an answer to this.


Procyon, your script does well. However, it does rename a file with the directory name in it. And something with zeroes when a dir has more than 10 files.

005 - 00lateral cosmonaut transportation vehicle - 8.jpg
005 - 00lateral cosmonaut transportation vehicle - 9.jpg
005 - 0lateral cosmonaut transportation vehicle - 10.jpg
005 - 0lateral cosmonaut transportation vehicle - 11.jpg

Some directories are just called 009 or 010. The content of those is renamed fine.


Killa B, your script, upon execution in a 0[0-9][0-9]* directory, does something strange. I cant get what it does. I tried it in different dirs with all other outcomes.

Offline

#7 2009-03-06 11:45:23

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: File renaming hardships

@build: I see, I missed that.

IFS="
"
for dir in *; do
! [ -d "$dir" ] && continue
(i=1; cd "$dir"
for file in $(\ls -1 --reverse *.jpg); do
mv --backup=t -v "$file" "${dir%% *} - $i.jpg"
i=$(($i + 1)); done
rename "- " "- 00" *-\ ?.jpg; rename "- " "- 0" *-\ ??.jpg)
done

I added a *.jpg check for files, and variable substitution ${dir%% *}

Offline

#8 2009-03-06 14:55:46

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

Re: File renaming hardships

effing spaces... here's my new hack... works all except for 1.jpg instead of 001.jpg again; awk should do that but i don't know it:

EDIT: ok, got the awk command:

#!/bin/bash

find ./ -maxdepth 1 -name '*' | grep -v \./$ | cut -c 3- | while read dir; do
  count=1
  ls -x -1 --reverse "$dir" | while read file; do
    name=$(echo $count | awk '{printf "%03d", $1}')
    newname=$dir\ -\ $name.jpg
    mv "$dir/$file" "$dir/$newname"
    count=$(( count + 1 ))
  done
done

exit 0

and the result:

┌─[ 10:17 ][ ~/Temp/test ]
└─> lt
.
|-- 041
|   |-- 1234.jp
|   `-- 23456.jpg
`-- 051
    |-- 1234.jp
    `-- 23456.jpg

2 directories, 4 files
┌─[ 10:17 ][ ~/Temp/test ]
└─> photo_renamer
┌─[ 10:17 ][ ~/Temp/test ]
└─> lt
.
|-- 041
|   |-- 041 - 001.jpg
|   `-- 041 - 002.jpg
`-- 051
    |-- 051 - 001.jpg
    `-- 051 - 002.jpg

2 directories, 4 files

this should work with directories up to 999 files.  change the awk command to increase this.

edit2: thanks toad, that's a good reference; google actually found me this which is just the printf syntax, which i knew awk accepts.

Last edited by brisbin33 (2009-03-06 15:22:06)

Offline

#9 2009-03-06 15:11:29

toad
Member
From: if only I knew
Registered: 2008-12-22
Posts: 1,775
Website

Re: File renaming hardships

You want awk? Here is the only resource I have been able to make sense of so far...

http://www.vectorsite.net/tsawk_1.html#m1


never trust a toad...
::Grateful ArchDonor::
::Grateful Wikipedia Donor::

Offline

#10 2009-03-06 22:16:46

Killa B
Member
From: United States
Registered: 2008-10-28
Posts: 42
Website

Re: File renaming hardships

build wrote:

Killa B, your script, upon execution in a 0[0-9][0-9]* directory, does something strange. I cant get what it does. I tried it in different dirs with all other outcomes.

That's weird, it works perfectly for me.

Before running the script:

0112091422.jpg
1216529417156.png
1231462747325.jpg
1231725599150.jpg
20080621.gif
98530944649702a5de6414.gif
9_19.png
Active_Time_Battle.png
Blue_Screen_of_AAAAA.jpg
IMAGE_474.jpg
IMAGE_476.jpg
Mort_du_fossoyeur.jpg
Nosrsly.jpg
Rikers_Island.jpg
addictive.png
aegisiconbymiusaionjigizw2.gif
akira-sama.png
akira_avatar_57x80.png
aku.jpg
archlinux-vert-dark-grad2-splash-trans.png
archlinux-vert-dark-grad2-splash.png
claphandss.gif
combobreakergi9.jpg
disgaea_fallenflonne.jpg
fallenangelflonne.c.png
fallenangelflonne.xcf
fallenangelflonne_100x100.new.png
fallenangelflonne_100x100.png
ft.png
grilled_cheese_butterfly.jpg
j0ljtl.jpg
killa b.png
killa b.svg
killab_font_shadow.png
mothergift.png
neowin_weather_shop.jpg
panel.png
panel.xcf
panel2.png
panel3.png
panel3.xpm
prinny_controls.png
rasta.png
rhdn.bak.xcf
rhdn.xcf
rhdn_web2.0.png
rhdn_web2.0.xcf
snowbee.xcf
temp.sh~
this-is-jenga.gif
userbar-papoosefan.jpg
v.jpg
y_tvgame-1.png

After running the script:

temp orarios - 01.png
temp orarios - 02.jpg
temp orarios - 03.jpg
temp orarios - 04.gif
temp orarios - 05.sh~
temp orarios - 06.xcf
temp orarios - 07.xcf
temp orarios - 08.png
temp orarios - 09.xcf
temp orarios - 10.xcf
temp orarios - 11.png
temp orarios - 12.png
temp orarios - 13.xpm
temp orarios - 14.png
temp orarios - 15.png
temp orarios - 16.xcf
temp orarios - 17.png
temp orarios - 18.jpg
temp orarios - 19.png
temp orarios - 20.png
temp orarios - 21.svg
temp orarios - 22.png
temp orarios - 23.jpg
temp orarios - 24.jpg
temp orarios - 25.png
temp orarios - 26.png
temp orarios - 27.png
temp orarios - 28.xcf
temp orarios - 29.png
temp orarios - 30.jpg
temp orarios - 31.jpg
temp orarios - 32.gif
temp orarios - 33.png
temp orarios - 34.png
temp orarios - 35.jpg
temp orarios - 36.png
temp orarios - 37.png
temp orarios - 38.gif
temp orarios - 39.png
temp orarios - 40.jpg
temp orarios - 41.jpg
temp orarios - 42.jpg
temp orarios - 43.jpg
temp orarios - 44.jpg
temp orarios - 45.jpg
temp orarios - 46.png
temp orarios - 47.png
temp orarios - 48.gif
temp orarios - 49.gif
temp orarios - 50.jpg
temp orarios - 51.jpg
temp orarios - 52.png
temp orarios - 53.jpg

Offline

Board footer

Powered by FluxBB