Share your knowledge at the LQ Wiki.
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 11-22-2010, 04:00 AM   #1
LQ Newbie
Registered: Nov 2010
Posts: 3

Rep: Reputation: 0
How to remove spaces from many file names under Cygwin


Im hoping that someone can help me, I need to remove spaces (not replace with underscores) from several thousand files on a system with cygwin.
Can I do this from the shell using rename or mv somehow?

Old 11-22-2010, 04:27 AM   #2
Registered: Oct 2010
Location: Liège, Belgium
Distribution: Debian / Slackware
Posts: 47
Blog Entries: 1

Rep: Reputation: 7

Why don't you try to use "tr"?
I am not under unix today so I cannot test, but something like
ls -1 | tr -d ' '
to delete the space or
ls -1 | tr ' ' '_'
to change them into underscore.
The man page should give you more options and ideas.

Hope this helps ;-)
Old 11-22-2010, 04:46 AM   #3
LQ Newbie
Registered: Nov 2010
Posts: 3

Original Poster
Rep: Reputation: 0

ls -1 | tr -d ' '

Seems to give the directory listing with out spaces in file names, but it hasnt removed (renamed the files) the spaces from the file names.

However I found that this works exactly how I need it to:

find $DIR -type f -name '* *' | while read f; do d="${f%/*}"; f="${f##*/}"; mv "$d/$f" "$d/${f// }"; done
Old 11-22-2010, 05:00 AM   #4
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,544

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
If you have access to the perl copy of rename (as on Ubuntu) you could try:
find $DIR -type f -name '* *' -exec rename 's/ //g' {} \;

Last edited by grail; 11-22-2010 at 05:39 AM.
Old 11-22-2010, 06:01 AM   #5
Registered: Dec 2004
Location: Hyderabad, India
Distribution: Slackware 13, Ubuntu 12.04
Posts: 431

Rep: Reputation: 32
I think this command will server your purpose

$ for i in *; do rename 's/ //g' "$i"; done

Obviously, it has to be executed in the directory where the required files are existing.

Last edited by adityavpratap; 11-22-2010 at 06:16 AM.


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
[SOLVED] Bash file names with spaces alex1986 Programming 5 07-26-2010 09:47 PM
how to handle file names with spaces in them bahadur Programming 14 04-04-2005 12:04 PM
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
Need to remove spaces from all file / dir names ?? Solution here! jsjohnst Linux - General 2 07-20-2004 09:39 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:39 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