1

I use a little script to numbering files. I start it with a thunar custom action. This only works when all files are in the same directory. the new files name are 00001.ext to 00005.ext when I rename 5 files. krename has an option to restart for every folder.

When i have this

/path/to/folder1/file1
/path/to/folder1/file2
/path/to/folder2/file1
/path/to/folder2/file2

my script would create this

/path/to/folder1/00001
/path/to/folder1/00002
/path/to/folder1/00003
/path/to/folder2/00004
               ^
           folder two

What I want is

/path/to/folder1/00001
/path/to/folder1/00002
/path/to/folder1/00003
/path/to/folder1/00004
               ^
           folder 1

is there a command line tool that can rename the files and restart for every new folder?

fmeier
  • 321
  • 1
  • 4
  • 10
  • This is the script i use at the moment

    a=1 for i; do ext=${i##*.} new=$(printf "%05d.$ext" "$a") mv -i -- "$i" "$new" let a=a+1 done

    – fmeier Feb 01 '24 at 16:26
  • 1
    Add this to you original post – Gilles Quénot Feb 01 '24 at 16:27
  • 1
    comments suck for code blocks and anything that needs formatting, it's better to [edit] the information to your post. That's where people will look for it anyway. – ilkkachu Feb 01 '24 at 16:31
  • anyway, looking at the post, I'm not exactly sure what the rule about moving files from one directory to another should be, nor can I tell how the scriptlet you posted in the comment would move them – ilkkachu Feb 01 '24 at 16:34
  • Advice to newcomers: If an answer solves your problem, please accept it by clicking the large check mark (✓) next to it and optionally also up-vote it (up-voting requires at least 15 reputation points). If you found other answers helpful, please up-vote them. Accepting and up-voting helps future readers. See the relevant help-center article – Gilles Quénot Feb 05 '24 at 22:06

2 Answers2

0

Using Perl's rename (usable in any OS):

rename -n 's!folder\d+/.*!sprintf "folder1/%05d", ++$MAIN::c!se' ./folder*/*
$ tree folder*
folder1
├── 00001
├── 00002
├── 00003
└── 00004
folder2

Remove -n switch, aka dry-run when your attempts are satisfactory to rename for real.

To go deeper:

you can capture folder with:

rename -n 's!([^/]+)\d+/.*!sprintf("%s1/%05d", $1, ++$MAIN::c)!se' ./folder*/*

to make it dynamic.

You can add more logic inside if needed, it's Perl code.

0

With zsh:

autoload -Uz zmv
n=0
(cd /path/to && zmv -n 'folder<->/*(#qDn.)' 'folder1/${(l[5][0])$((++n))}')

Note the n glob qualifier so files are sorted numerically and for instance files in folder10 are renamed after those in folder9, not between those in folder1 and folder2 which you'd get with the default lexical ordering.

Remove the -n (dry-run) if happy.