I have a folder containing video lectures on some particular topic. It is structured like this:
.
├── 1_fol
│ ├── 1_file.mp4
│ ├── 2_file.mp4
│ └── 3_file.mp4
└── 2_fol
├── 10_file.mp4
├── 4_file.mp4
├── 5_file.mp4
├── 6_file.mp4
├── 7_file.mp4
├── 8_file.mp4
└── 9_file.mp4
I want to change this structure into
.
├── 001_fol
│ ├── 001_file.mp4
│ ├── 002_file.mp4
│ └── 003_file.mp4
└── 002_fol
├── 004_file.mp4
├── 005_file.mp4
├── 006_file.mp4
├── 007_file.mp4
├── 008_file.mp4
├── 009_file.mp4
└── 010_file.mp4
This helps, because you can then use find . -regextype sed -regex ".*/.*\.\(mp3\|mp4\)" -print0 | sort -z | xargs -r0 vlc to open the whole playlist. I came up with a script to pad 0's, but it's quite lengthy and slow:
find . -depth -exec rename -v 's/(.*)\/([0-9]$)/$1\/00$2/;
s/(.*)\/([0-9]{2}$)/$1\/0$2/;
s/(.*)\/([0-9][^0-9][^\/]*$)/$1\/00$2/;
s/(.*)\/([0-9]{2}[^0-9][^\/]*$)/$1\/0$2/' '{}' ';'
Can this be optimized further?
Edit
Actually, the execution became quite fast after the ';' was changed to '+'. But the set of regex(es) still looks quite ugly.
renameis a perl tool and allows you to use any perl expressions. The following perl expression searches for the last component of a path (for instancecina/b/c) and pads its leading number (if existing) to three digits.Example:
1_fol/23_file.mp4becomes1_fol/023_file.mp41_folbecomes001_fol1_2/3_4.mp4becomes1_2/003_4.mp41_2becomes001_2Use above perl expression in your
findcommand forbash✱ ...... or the extended globstar features in
zsh. On my system globs were faster thanfind.**/*recursively lists all files and directories and(On)reverses the order similar to-depth.✱ For
bashyou still could use**/*withshopt -s globstarbut reversing the order of the matched files is not so easy, sofindis simpler and probably faster.