Remove spaces in file names recursively using Linux Commands

26 11 2008

Cool stuff for removing space in your file and folders names.This command will remove a whitespace and change it to underscore character “_” . Open your terminal.  Changes path to your working folders. Caution, this command will affect all of your files and folders recursively. Then do this command :

a. Remove spaces in current folders:
user@machine~>rename -v 's/\ /\_/g' *

b. Remove spaces in all files and one sub folder:
user@machine~>rename -v 's/\ /\_/g' */*

*        -> in current folders only (ex : folder1)
*/*        -> in current and sub folders (ex: folder1/folder2)
*/*/*    -> in current, sub folders, sub folders (ex: folder1/folder2/folder3)

Just simply changes */* with your folder depth needs.




5 responses

26 11 2008

Maksud dari ini apa bos,. >> -v ‘s/\ /\_/g’ *
-v = ?
‘s/\/\_/g’= ?

23 06 2010

Why are you so lazy to type “man” in your console mamen… ^_!

v, --verbose
Verbose: print names of files successfully renamed.

in rename commands the matching and rename pattern are separated by forward slash ( / ) sign.

's/\ /\_/g', just simple perl expression, it can said something likes :
"found just one (s) space character (/\ ) and ( / ) replace it with underscore (\_)
/g modifiers means -> rename every space in the files or folder names into underscore.

22 06 2010

This is an incredibly useful post, and I thank you for taking the time. I’ve been using rename to remove whitespace for quite some time, but in a script I’m writing now I’d like to be able to do it recursively. I do not want to have to manually specify the depth of the directory trees, because I don’t want to expect the user to know it!

Any ideas?

23 06 2010

Hello Connor,
Just walking into whole directory, at the same time run the rename commands. Like this:

#change MYPATH variable to your own path
find -type d | while read directory
echo "diving into : ${directory#"./"}"
cd $MYPATH."/${directory#"./"}"
rename -v 's/\ /\_/g' *

22 06 2010

Sorry for the comment spam, but the relevant explanation which I should have included in the comment above is available in the Perl documentation:

finddepth() works just like find() except that it invokes the &wanted function for a directory after invoking it for the directory’s contents. It does a postorder traversal instead of a preorder traversal, working from the bottom of the directory tree up where find() works from the top of the tree down.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:


You are commenting using your account. Logout /  Ubah )

Foto Google+

You are commenting using your Google+ account. Logout /  Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout /  Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout /  Ubah )


Connecting to %s

%d blogger menyukai ini: