LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 11-22-2010, 05:00 AM   #1
gwc01
LQ Newbie
 
Registered: Nov 2010
Posts: 3

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


Hi,

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?

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

Rep: Reputation: 7
Hello

Why don't you try to use "tr"?
I am not under unix today so I cannot test, but something like
Code:
ls -1 | tr -d ' '
to delete the space or
Code:
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, 05:46 AM   #3
gwc01
LQ Newbie
 
Registered: Nov 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Hi,

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, 06:00 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,246

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

Last edited by grail; 11-22-2010 at 06:39 AM.
 
Old 11-22-2010, 07:01 AM   #5
adityavpratap
Member
 
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 07:16 AM.
 
  


Reply


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 10:47 PM
how to handle file names with spaces in them bahadur Programming 14 04-04-2005 01:04 PM
du or wc and file names with spaces bramadams Slackware 2 01-27-2005 12:43 PM
Spaces in file names JohnKFT Slackware 3 11-09-2004 04:44 PM
Need to remove spaces from all file / dir names ?? Solution here! jsjohnst Linux - General 2 07-20-2004 10:39 PM


All times are GMT -5. The time now is 04:32 AM.

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