-2

I want to rename all the folders contained in a specific folder that starts with "Test" replacing by "Product". Example: In my main folder I have the following folders:

Test1
Test2
Test3
....

that I want to rename them into:

Product1
Product2
Product3
...
salconte
  • 213
  • 2
  • 5
  • Does this answer your question? [Rename files and directories recursively under ubuntu /bash](https://stackoverflow.com/questions/15012631/rename-files-and-directories-recursively-under-ubuntu-bash) – TheProgrammer Sep 02 '20 at 16:45
  • https://stackoverflow.com/questions/15012631/rename-files-and-directories-recursively-under-ubuntu-bash – TheProgrammer Sep 02 '20 at 16:46

1 Answers1

0

As far as I understand correctly, you don't want to rename files "recursively", but you want to rename multiple files.

Maybe it is not the best possibility to do this, but I would do it like this:

for a in Test*; do mv $a `echo $a | sed -e s/Test/Products/`; done

Explanation:

for a in Test*; do <some command> ; done

This will perform some command for each file matching the pattern Test*. $a is the placeholder for the file name.

echo $a | sed -e s/Test/Products/

This command prints the file name while Test is replaced by Products. If the file name ($a) is Test123, the command will print Products123.

mv $a `<some command>`

This command renames the file $a. The new file name is the text that is printed by the command <some command>.

If you really want to perform the renaming recursively (in all sub-directories), you might use the find command:

for a in `find . | grep Test`; do ...

EDIT

I want to rename the folders and not the files

A folder is some kind of "file" in Linux; what you call "file" is called "regular file" in Linux. And the commands above do not distinguish between folders and "regular files".

So if your parent folder does not contain any "regular file" (or symbolic link or pipe ...) matching the name pattern Test*, you can use the commands above.

If your parent folder contains both sub-folders and other types of files (for example "regular files" or symbolic links) that match the name pattern Test* and you only want to rename the sub-folders, you may use the find command to select all folders in the current parent folder:

for a in `find -maxdepth 1 -type d | grep /Test`; do ...

Edit 2

I didn't know the rename command mentioned in the answer linked in one of the comments to your question. And the rename tool seems not to be available in every Linux distribution.

Using rename you might do it the following way:

rename Test Product `find -maxdepth 1 -type d`

This will search for all sub-directories in the current directory and replace Test by Product in the name. The sub-directory named MyTest5 becomes MyProduct5.

However, a regular file named Test6 will not be renamed.

If you only want to rename directories whose name starts with Test, you may do it the following way:

rename Test Product `find -maxdepth 1 -type d | grep /Test`

Test5 will become Product5 but MyTest5 will not be renamed.

Unfortunately, I haven't used the rename tool myself, so I'm not sure if this really works.

Martin Rosenau
  • 17,897
  • 3
  • 19
  • 38