Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 07-14-2006, 03:25 AM   #1
Registered: Mar 2005
Location: Washington DC area
Posts: 213

Rep: Reputation: 30
please help me remove the spaces in filenames in a directory!

Hey everyone

I recently copied a folder over from my Winbl0wz partition. I noticed that many of the files have spaces in their file names. Do you all know of any scripts that i could use to remove all the spaces in the names of files contained in a directory? All help is GREATLY appreciated in advance!
Old 07-14-2006, 04:51 AM   #2
Registered: May 2001
Posts: 29,362
Blog Entries: 55

Rep: Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548
Not onehundred percent trouble free so YMMV(VM):
find /home/asilentmurmur/windowsdir -type f | while read f; do mv "${f}" "${f// /_}"; done
BTW, try searching LQ, we definately have similar solutions, probably in the Programming forum.
Old 07-14-2006, 05:03 AM   #3
Senior Member
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
how about
find /home/asilentmurmur/windowsdir -type f -execdir rename '" "' '""' '{}' \;
Old 07-14-2006, 07:17 PM   #4
Registered: Oct 2004
Posts: 215

Rep: Reputation: 30
If you have python installed try this:
# the following script will traverse through all of the files
# within the directory where it is placed and replace each
# space within the filename with an underscore

import os, sys
if len(sys.argv) == 1:
    filenames = os.listdir(os.curdir)
    filenames = sys.argv[1:]
for filename in filenames:
    if ' ' in filename:
        newfilename = filename.replace(' ', '_')
        print "Renaming", filename, "to", newfilename, "..."
        os.rename(filename, newfilename)
place the code in a text file (with a .py extension), make it executable, place it in the directory whose files you wish to change, and either run it from the command line (python or just click on it.

If you want to do it in bash, try this:
 for i in *; do mv "$i" "$(echo $i | tr ' ' '_')"; done
or this:
for i in * *; { mv ''$i'' $(echo $i | sed 's/ /_/g'); }
or this (this one is recursive):
# Recursive replacement of spaces with "_" in files/dirs
[ $# -ne "1" ] && echo "Usage: $0 <dir-path>" && exit 1
find $1 -name "* *" | sed 's/^.*$/mv \"&\"/g' > t1$$
find $1 -name "* *"|sed -e 's/ /_/g' -e 's/^.*$/\"&\"/g' >t2$$
paste t1$$ t2$$ > t3$$; sh t3$$; #rm -f t?$$
#[t?$$ files will help you in an accidental action]
just cd to the directory and type one of the above. It should work, but I think the python code is a little faster.

Last edited by bushidozen; 07-14-2006 at 07:20 PM.


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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
script to remove spaces from multiple filenames jeffreybluml Linux - Newbie 36 07-31-2013 02:10 AM
spaces in filenames dotancohen Programming 12 02-17-2006 02:42 PM
ls and filenames with spaces rose_bud4201 Programming 10 07-01-2005 08:28 AM
replacing all spaces in filenames of a directory merlin23 Linux - Newbie 1 01-11-2005 07:52 AM
spaces in filenames ebone Linux - General 2 11-12-2001 11:56 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:28 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration