3

I'm trying to rename the files like: Name1_searchstats_metrics_20141230T133000036.log to something like: Name2_searchstats_metrics_20141230T133000036.log

I'm trying: rename -n 's/\Name1_/\Name2_/' *.log but am getting the error:

bash: /usr/bin/rename: Argument list too long

Can someone please help ?

Saurabh Verma
  • 6,328
  • 12
  • 52
  • 84
  • 1
    This is because you have so many files expanded with the `*.log` pattern. Maybe you can use `find` or a `while` loop – fedorqui Jan 02 '15 at 09:13

2 Answers2

6

probably the easiest solution, since you're using bash is to iterate over the list of files with a for loop:

$ for i in *; do rename -n 's/Name1_/Name2_/' $i; done

you can also filter the files if needed by using any wildcard in the command, like *.log.

There are other more convoluted ways to achieve this, especially if you need to do particular string manipulation of the file name, i.e. using awk or find -exec, but hopefully this could help you sort things out in a clear way.

Edited answer as suggested by @glglgl

a more comprehensive and detailed explanation of the above can be found on superuser: https://superuser.com/questions/31464/looping-through-ls-results-in-bash-shell-script

Community
  • 1
  • 1
Mr Peach
  • 1,364
  • 1
  • 12
  • 20
2

If the argument list is too long for a linux command, xargs usually comes to the rescue.

Try this:

ls *.log | xargs rename -n 's/\Name1_/\Name2_/' 
Masked Man
  • 1
  • 7
  • 40
  • 80
  • I don't have access to a UNIX terminal here at work, so not sure if I got the syntax right, but I guess it should work. – Masked Man Jan 02 '15 at 09:16
  • Getting the same error with this command also – Saurabh Verma Jan 02 '15 at 09:21
  • 1
    In your original command (that you posted in the question), try by first feeding it less number of files (say 10 or so). See if that succeeds. – Masked Man Jan 02 '15 at 09:23
  • 1
    @SaurabhVerma How many log files are you passing to the rename command? If it is several million, it is possible that the sublists created by xargs are *also* too big. Disclaimer: I don't know how xargs splits its input into sublists, that is, how big is each sublist, and what is the "threshold" for rename to consider its input list as too big. – Masked Man Jan 02 '15 at 09:31