1

I'm trying to rename all my folders and its files so that the first letter is a capital.

I've got this script which does that for the current directory:

rename -f 's/./\U$&/' *

This is working great. How can I run this command in every subfolder (**/*)?

Alexander
  • 396
  • 1
  • 9
  • 19

2 Answers2

1

After running a few tests and jumping around the forums, I put together the following:

find . -depth -execdir rename -f 's/./\U$&/' {} +

The following answer to a similar question does a great job explaining the -depth and -execdir options and why they're necessary for this particular scenario. I was struggling to get it to work until I swapped -exec with -execdir!

Find multiple files and rename them in Linux

paul
  • 96
  • 4
  • 1
    Thank you, there were indeed other similar questions but I couldn't get it to work. This one works like a charm. – Alexander Jul 25 '23 at 16:19
  • Glad I could help! – paul Jul 25 '23 at 18:56
  • Shouldn't there be also a `-type f`? With your command, directories would be renamed as well. – user1934428 Jul 26 '23 at 06:20
  • @user1934428 `-type f` can be used to specify files, but OP requested both directories and files: "I'm trying to rename all my folders and its files so that the first letter is a capital." – paul Jul 26 '23 at 14:56
  • Right you are! Final question: What is the `-f` doing with `rename`? I didn't find this option documented in the man-page. – user1934428 Jul 29 '23 at 12:03
  • @user1934428 -f in rename forces the operation despite warnings (specifically `-f/--force or -i/--interactive (proceed or prompt when overwriting) ` when typing `rename -h`), which is useful in this case because rename will stall on identical filenames in different directories without this option – paul Aug 08 '23 at 14:15
0

For recursive solution find command can be used with rename. With find recursive modification or filteration could be used for specific file/folder type.

Here is the solution I've tested in my WSL Ubuntu:

find . -exec rename -d 's/./\U$&/' {} +

This question is kind of duplicate. Similar question is : Use find command with rename

My solution here is derived/changed from above linked question/solution.

Milon Sarker
  • 478
  • 8
  • 25