How to change several directories' names?

Alireza Balayi

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?

Eliah Kagan

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to reformat the directories names?

From Dev

How to ignore several directories in Git

From Dev

How to create multiple directories based on file names and change the file names in linux?

From Dev

Change files names in parent and child directories

From Dev

How to move several text files to several listed directories?

From Dev

how to change chmod of directories in path

From Dev

How to change the shortcut directories in Files?

From Dev

How to loop over directories with dots and spaces in their names?

From Dev

How to create multiple files/directories with randomized names?

From Dev

How to list directories names but not their contents, in *N*X?

From Dev

Simple Bash Script: Change names of files to mimic directories

From Dev

Windows 10 Batch file to change directory names and move directories

From Dev

How to store several variable names in Stata?

From Dev

How to open several Terminal Tabs in different folders (working directories)?

From Dev

Batch Scripting - How to delete a subfolder (files/folders) over several directories

From Dev

How to open several Terminal Tabs in different folders (working directories)?

From Dev

How can I recursively grep through several directories at once?

From Dev

How to move several files in different directories to one unique directory?

From Dev

.vimrc backupdir several directories

From Dev

Babel ignore several directories

From Dev

Select several directories with tkinter

From Dev

.vimrc backupdir several directories

From Dev

How Do I Change The Gradle Output Directories?

From Dev

How do I change working directories with KDialog?

From Dev

How Do I Change The Gradle Output Directories?

From Dev

How to change URL names in Umbraco

From Dev

How to change the alfresco permission names

From Dev

How do I shim executables with the same names in different directories?

From Dev

How can I create a table with names of directories and subdirectories in R?

Related Related

  1. 1

    How to reformat the directories names?

  2. 2

    How to ignore several directories in Git

  3. 3

    How to create multiple directories based on file names and change the file names in linux?

  4. 4

    Change files names in parent and child directories

  5. 5

    How to move several text files to several listed directories?

  6. 6

    how to change chmod of directories in path

  7. 7

    How to change the shortcut directories in Files?

  8. 8

    How to loop over directories with dots and spaces in their names?

  9. 9

    How to create multiple files/directories with randomized names?

  10. 10

    How to list directories names but not their contents, in *N*X?

  11. 11

    Simple Bash Script: Change names of files to mimic directories

  12. 12

    Windows 10 Batch file to change directory names and move directories

  13. 13

    How to store several variable names in Stata?

  14. 14

    How to open several Terminal Tabs in different folders (working directories)?

  15. 15

    Batch Scripting - How to delete a subfolder (files/folders) over several directories

  16. 16

    How to open several Terminal Tabs in different folders (working directories)?

  17. 17

    How can I recursively grep through several directories at once?

  18. 18

    How to move several files in different directories to one unique directory?

  19. 19

    .vimrc backupdir several directories

  20. 20

    Babel ignore several directories

  21. 21

    Select several directories with tkinter

  22. 22

    .vimrc backupdir several directories

  23. 23

    How Do I Change The Gradle Output Directories?

  24. 24

    How do I change working directories with KDialog?

  25. 25

    How Do I Change The Gradle Output Directories?

  26. 26

    How to change URL names in Umbraco

  27. 27

    How to change the alfresco permission names

  28. 28

    How do I shim executables with the same names in different directories?

  29. 29

    How can I create a table with names of directories and subdirectories in R?

HotTag

Archive