4

I want to rename all files and folder containing underscore in name and replace underscore with hyphen.

Currently I am using following code,

rename '_' '-' */*/*

It was working but now it is showing me "Argument list too long"

Vishal Barot
  • 39
  • 1
  • 4
  • The command probably matches too many files names, so that their name's expansion grows to long, at least longer then the maximum size of a shell's command. – alk Apr 06 '14 at 12:52
  • How can we make to work in a loop? I will be very thankful it someone can help me here! – Vishal Barot Apr 06 '14 at 12:53
  • You might like to take a look at the awk tool. Or use the find command with its option `-exec`. – alk Apr 06 '14 at 13:12

1 Answers1

1

You can try this:

$ tree foo
foo
├── dir_1
│   └── foo_file_2
└── file_1

1 directory, 2 files
$ for ft in d f; do find foo -type $ft -execdir sh -c 'mv "$0" "${0//_/-}"' {} \; ; done 2>/dev/null
$ tree foo
foo
├── dir-1
│   └── foo-file-2
└── file-1

1 directory, 2 files

This renames all directories and then all files (the for loop over d f) because I haven't been able to make it do all renaming in one iteration.

Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175