Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. 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.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

Stay in the loop…

Follow the Tweets.

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

Subscribe to the feeds.

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:

Hide

News

2012-05-20 - test
test
2012-05-20 - test
test
2012-05-20 - test
test
2012-05-20 - Test tweets
YU not working?
Hide

Tags

Hide

Functions

Renames all files in the current directory such that the new file contains no space characters.

Terminal - Renames all files in the current directory such that the new file contains no space characters.
rename 's/ /_/g' *
2011-07-04 06:21:27
User: twjolson
Functions: rename
-2
Renames all files in the current directory such that the new file contains no space characters.

Alternatives

There are 3 alternatives - vote for the best!

Terminal - Alternatives
rename 'y/ /_/' *
2009-06-27 22:20:47
User: signal9
Functions: rename
Tags: file rename
33

This command will replace all the spaces in all the filenames of the current directory with underscores. There are other commands that do this here, but this one is the easiest and shortest.

for file in *; do mv -v "$file" "$(sed 's/ //g' <(echo $file))"; done
2011-07-10 21:08:31
User: laebshade
Functions: file mv
1

This is a better version, as it does no command piping, uses for instead of while loops, which allows for a list of files in the current working directory to be natively processed. It also uses the -v/verbose option with mv to let you know what the command is doing.

While the command does exactly the same in a better way, I would modify the sed option to replace spaces with underscores instead, or dashes.

Please note that you'll receive errors with this command as it tries to rename files that don't even have spaces.

This is an alternative to: http://www.commandlinefu.com/commands/view/8761/renames-all-files-in-the-current-directory-such-that-the-new-file-contains-no-space-characters.

find ./ $1 -name "* *" | while read a ; do mv "${a}" "${a//\ /_}" ; done
ls -1 | while read file; do new_file=$(echo $file | sed s/\ //g); mv "$file" "$new_file"; done
2011-07-03 04:02:48
User: ddc
Functions: echo ls mv read sed
-2

File names with spaces may cause problems, this command could help to avoid that.

ls -1 | while read file; do new_file=$(echo $file | sed s/\ //g); mv "$file" "$new_file"; done
2011-07-03 04:02:48
User: ddc
Functions: echo ls mv read sed
-2

File names with spaces may cause problems, this command could help to avoid that.

Know a better way?

If you can do better, submit your command here.

What others think

This depends on the Perl 'prename' command to be installed -- popular on Debian-based distributions, where /usr/bin/rename is a link to this file. The more common POSIX version of 'rename' doesn't do regular expressions, only one-time substring matches. 'rename oldstring newstring list_of_files' I.E.:

rename .htm .html *.htm
Comment by Mozai 46 weeks and 4 days ago

Your point of view

You must be signed in to comment.

Related sites and podcasts