Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 07-20-2004, 08:37 PM   #1
LQ Newbie
Registered: Oct 2003
Location: Kansas
Distribution: Mandrake 10 Official
Posts: 11

Rep: Reputation: 0
Need to remove spaces from all file / dir names ?? Solution here!

Have you ever been faces with the problem of having a directory full of files and folders with spaces in them (take your MP3 collection for example)? Well here is an easy way to remove all those spaces and replace them with underscores. The only requirement is that you not have any files / directories with + signs in the name.

for i in `find . | tr "[:blank:]" "+" | grep "+"`; do echo "Changing Directory: " `echo $i | sed -r 's/\+/\\ /g'` " to " `echo $i | tr "+" "_"`;  mv "`echo $i | sed -r 's/\+/\\ /g'`" `echo $i | tr "+" "_"`; done
The above code will change all files in the current directory and all below it. If you get any errors, simply run the command again. Keep running it until the errors are gone and you should be set.

Last edited by jsjohnst; 07-20-2004 at 08:42 PM.
Old 07-20-2004, 09:17 PM   #2
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
j=`find $1 -printf "%d\n" | sort -u | tail -n 1`
echo "Max dir depth:" $j
for (( i=0; i<=j ; i++ ))
  for name in `find -mindepth $i -maxdepth $i -iname "* *" -printf "%p\n"`
    newname=`echo "$name" | tr " " "_"`
    echo "$name" "$newname"
    mv "$name" "$newname"
Not a one-liner, but hey ;)
No need to re-run because of errors ...

Last edited by Tinkster; 07-20-2004 at 09:18 PM.
Old 07-20-2004, 09:39 PM   #3
Senior Member
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Debian, FreeBSD
Posts: 1,450

Rep: Reputation: 48
Ooohhh, I am saving this. I can't count the number of times a script has failed because of spaces that I could not find a way to get the shell to ignore.

Thanks guys.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to handle file names with spaces in them bahadur Programming 14 04-04-2005 12:04 PM
how to handle file names with spaces in them bahadur Programming 3 03-23-2005 05:15 AM
du or wc and file names with spaces bramadams Slackware 2 01-27-2005 11:43 AM
Spaces in file names JohnKFT Slackware 3 11-09-2004 03:44 PM
File/Direcory Names with Spaces ar1 Linux - General 3 01-15-2004 10:41 AM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:39 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration