$ # before
$ ls -1
big world keeps turning
bye bye
fred
hello world
$ ls -1 | grep " " | awk '{printf("mv \"%s\" ",$0); gsub(/ /,"_",$0); printf("%s\n",$0)}' | sh # rename filenames: spaces to "_"
$ # after
$ ls -1
big_world_keeps_turning
bye_bye
fred
hello_world
Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?
You must be signed in to comment.
commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.
Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.
» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10
Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):
Subscribe to the feed for:
ls|while read f;do mv "$f" "${f// /_}";donerename 'y/ /_/' *# beforels -1big world keeps turning bye bye fred hello worldls|while read f;do mv "$f" "${f// /_}";donemv: `fred' and `fred' are the same file# afterls -1big_world_keeps_turning bye_bye fred hello_world I think we need a ' | grep " " ' in there.ls "* *"|while read f;do mv "$f" "${f// /_}";donels -1big world keeps turning bye bye felicity hello two spaces embeddedls "* *"ls: cannot access * *: No such file or directory This works (for me):ls | grep " " | while read f;do mv "$f" "${f// /_}";done# afterls -1big_world_keeps_turning bye_bye felicity hello two__spaces__embeddedls|awk '/ /{c=$1; for (i=2;i<=NF;i++) c=c "_" $i; system("mv \"" $0 "\" \"" c "\"")}'