I have a set of folders named
-> section 1, section 2, section 3, ... , section 12
How do I change them to read
-> video 1, video 2 ..., video 12
on a linux platform?
I tried:
mv section* video*
But no love
I have a set of folders named
-> section 1, section 2, section 3, ... , section 12
How do I change them to read
-> video 1, video 2 ..., video 12
on a linux platform?
I tried:
mv section* video*
But no love
We can use the ${target/search/replace}
parameter expansion to replace the first occurrence of a pattern within a given string.
Combine that with find -exec
to rename the folders.
find . -type d -iname 'section*' -maxdepth 1 -exec bash -c 'mv "$0" "${0/section/video}"' {} \;
find .
: Find in the current directory-type d
: Only search for folders (use -type f
for files)-iname 'section*'
: Search pattern-maxdepth 1
: Restrict find from going deep-exec bash -c 'mv "$0" "${0/section/video}"' {} \;
-exec
run the following command for each path found{}
(path found by find
) as argument so we can use $0
mv
"$0" "${0/section/video}"
section
with video
Example:
$ cd /tmp/test
$
$
$ rm -rf *
$ mkdir section\ 1 section\ 2 section\ 35
$
$
$ find .
.
./section 1
./section 35
./section 2
$
$ find . -type d -iname 'section*' `-maxdepth 1` -exec bash -c 'mv "$0" "${0/section/video}"' {} \;
$
$ find .
.
./video 2
./video 1
./video 35
$
$
There is a very nice shell script which can perform batch renaming: vimv.
Warning: before use, please read carefully the README, especially the "Gotchas" section at the end.
It takes a list of files as parameters (or defaults to the files of the current directory) and opens your favourite editor with a temporary file where each line is one of the file paths you gave. Then you just have to modify any path in order to move or rename it.
The advantage is that you can perform very complex renamings, depending on the editor you are using.
In your case, you could do the following:
$ ls
'section 1' 'section 10' 'section 11' 'section 12' 'section 2' 'section 3' 'section 4' 'section 5' 'section 6' 'section 7' 'section 8' 'section 9'
$ vimv section*
[Inside Vim editor...] :%s/section/video
[Inside Vim editor...] :wq
12 files renamed.
$ ls
'video 1' 'video 10' 'video 11' 'video 12' 'video 2' 'video 3' 'video 4' 'video 5' 'video 6' 'video 7' 'video 8' 'video 9'