I have some directories whose names I want to change to {00..16}
.
$ ls
'FVSFT1092A' 'FVSFT1092I'
'FVSFT1092B' 'FVSFT1092J'
'FVSFT1092C' 'FVSFT1092K'
'FVSFT1092D' 'FVSFT1092L'
'FVSFT1092E' 'FVSFT1092M'
'FVSFT1092F' 'FVSFT1092N'
'FVSFT1092G' 'FVSFT1092O'
'FVSFT1092H' 'FVSFT1092P'
Is there any command for this case?
There may be better ways, but this is how I would do it:
file-rename -n 's/.*/sprintf "%02d", ++our $i/e' FVSFT1092?
If your shell tells you that the file-rename
command does not exist, install the rename
package.
If you wanted to replace just the last characters, rather than everything in the original names, you could use this instead:
file-rename -n 's/.$/sprintf "%02d", ++our $i/e' FVSFT1092?
As written, those commands don't rename any files. When it's time to rename them, remove the -n
. Before removing -n
, you should inspect the output. It should show what each file is renamed to. You should also make sure that only the files you actually want to rename, and not other files, are listed. For the first command shown above, it should show this:
rename(FVSFT1092A, 01)
rename(FVSFT1092B, 02)
rename(FVSFT1092C, 03)
rename(FVSFT1092D, 04)
rename(FVSFT1092E, 05)
rename(FVSFT1092F, 06)
rename(FVSFT1092G, 07)
rename(FVSFT1092H, 08)
rename(FVSFT1092I, 09)
rename(FVSFT1092J, 10)
rename(FVSFT1092K, 11)
rename(FVSFT1092L, 12)
rename(FVSFT1092M, 13)
rename(FVSFT1092N, 14)
rename(FVSFT1092O, 15)
rename(FVSFT1092P, 16)
Running it without -n
actually renames the files. You should pretty much always run file-rename
(and the related commands prename
and rename
) with -n
first to do a dry run. But in this case it is even more important than usual, because in this case the behavior of the command may behave differently for different locale settings or if you have other similarly named files that you don't want to rename.
The way that command works is that the shell expands FVSFT1092?
into a list of all files in the current directory whose names are FVSFT1092
followed by any one additional character. This list is passed to the file-rename
command, which modifies the names according (in the first command shown above) to the Perl expression:
s/.*/sprintf "%02d", ++our $i/e
That renames any filename passed to it to the lowest number not yet used, starting with 1
, and formats it as two digits even if it's less than 10. Therefore it depends on the order in which the filenames are supplied, which in turn depends on what files actually exist in the current directory and what order the shell expands them in.
This should work. In strange corner cases, it won't do what you want, and it's easy to check for that by running the command with -n
first (as shown above), which you should do anyway to catch any mistakes.
For general information on how the syntax accepted by the file-rename
command works, I recommend Bulk renaming files in Ubuntu; the briefest of introductions to the rename command (by Oli).
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments