You are not logged in.
Pages: 1
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
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
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
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
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
Pages: 1