You are not logged in.

#1 2012-12-30 02:25:47

shuuichi_nitori
Member
Registered: 2012-12-30
Posts: 37

[SOLVED] massive rename help

hi this is my very first message asking for help.
I want to rename a lot of files via terminal, each 10 files are in different folders, which are arranged in this way:
.../my_files
    |---/folder_01
    |---/folder_02
    |---/folder_03

and the name of this is something like:
[name] of the file.png (with the spaces)

I want to rename the files in ascending numbers like:
Folder 01
0001.png
0002.png
........

Folder 05
0110.png

can you help me to figure it out?
(I use the terminal everyday, is just that I haven't faced this situation before.)

PD: to get the answer go right to the end of this page

Last edited by shuuichi_nitori (2013-01-09 18:07:08)


Archlinux + AwesomeWM

Offline

#2 2012-12-30 02:44:27

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: [SOLVED] massive rename help

If you don't care about the ordering of the subdirectories and the subdirs don't themselves have subdirs, then this should work:

find /path/to/my/files -type f -print0 | while IFS= read -rd '' file; do
  ext=${file##*.} dir=${file%/*}
  printf -v dest '%s/%04d.%s' "$dir" $(( ++i )) "$ext"
  mv -v "$file" "$dest"
done

I suppose if you do need to care about ordering, then raw bash is better suited to doing this:

for d in /path/to/my/files/*/; do
  pushd "$d" >/dev/null
  for f in *; do
    [[ -d $f ]] && continue
    printf -v dest '%04d.%s' $(( ++i )) "${f##*.}"
    mv -v -- "$f" "$dest"
  done 
  popd >/dev/null
done

Lightly tested, no warranty.

Last edited by falconindy (2012-12-30 14:20:15)

Offline

#3 2012-12-30 12:49:24

msthev
Member
Registered: 2012-04-05
Posts: 177

Re: [SOLVED] massive rename help

If I understand your problem correctly, then you can use qmv to do your job. Considering the following directory structure:

$ ls -1R my_files/
my_files/:
folder_01
folder_02

my_files/folder_01:
a file.png
another file.png
whatever.png

my_files/folder_02:
some name.png
some other name.png

use

qmv -R my_files/

or

qmv -f do -R my_files/

and change file names. Of course you don't have to do it by hand -- you can just generate new names automagically and then paste them to the editor launched by qmv.
Then do the same with directory names and you're done. Example result:

$ ls -1R my_files/
my_files/:
Folder 01
Folder 02

my_files/Folder 01:
0001.png
0002.png
0003.png

my_files/Folder 02:
0011.png
0012.png

Last edited by msthev (2012-12-30 12:51:51)

Offline

#4 2012-12-31 15:28:12

pilotkeller
Member
From: Canada
Registered: 2011-12-22
Posts: 513
Website

Re: [SOLVED] massive rename help

I did my best to make this as short as possible.

counter=0;for file in */*;do echo "mv $file ${file/\/*.//`printf "%04d" $counter`.}";((counter++));done

Or if you prefer neater code:

counter=0
for file in */*
    do echo "mv $file ${file/\/*.//`printf "%04d" $counter`.}"
    ((counter++))
done

Just remove the echo quote and the last quote to run what it produces. This way you can be sure it fits your needs.

Edit: Just noted that in will not backslash escape spaces in the output it gives from the echo. Don't worry. It will properly handle files with spaces, I tested for that.

Last edited by pilotkeller (2012-12-31 15:35:51)

Offline

#5 2013-01-09 18:05:43

shuuichi_nitori
Member
Registered: 2012-12-30
Posts: 37

Re: [SOLVED] massive rename help

I finnaly find the right way to do it

find . \( -name '*.jpg' -o -name '*.png' \) -print | sort | (i=0; while read f; do 
    let i+=1; mv "$f" "${f%/*}/$(printf %04d "$i").${f##*.}"; 
done)

Archlinux + AwesomeWM

Offline

Board footer

Powered by FluxBB