LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 07-20-2004, 09:37 PM   #1
jsjohnst
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.

Code:
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 09:42 PM.
 
Old 07-20-2004, 10:17 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,000
Blog Entries: 11

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

Last edited by Tinkster; 07-20-2004 at 10:18 PM.
 
Old 07-20-2004, 10:39 PM   #3
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: Ubuntu, FreeBSD, NetBSD
Posts: 1,449

Rep: Reputation: 47
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.
 
  


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 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 01:04 PM
how to handle file names with spaces in them bahadur Programming 3 03-23-2005 06:15 AM
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
File/Direcory Names with Spaces ar1 Linux - General 3 01-15-2004 11:41 AM


All times are GMT -5. The time now is 07:57 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration