LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-16-2008, 03:52 PM   #1
babag
Member
 
Registered: Aug 2003
Posts: 419

Rep: Reputation: 31
bash - replace all spaces in file, folder names


how would i do this?

i have a folder i just tried ro copy and it wouldn't
do it. i'm suspecting it's because of spaces in the
file and folder names.

i need a simple command to recursively replace all
of the " " with "_" in this folder's files and
subfolders.

thanks,
BabaG
 
Old 04-16-2008, 04:07 PM   #2
eco
Member
 
Registered: May 2006
Location: BE
Distribution: Debian/Gentoo
Posts: 412

Rep: Reputation: 48
Try using sed

$ echo "Hello World"|sed -e 's/\ /_/g'


This way, you might not even have to rename your folders... just do the following


The following fails:
mkdir "test me"
$ for i in `ls -d test\ me/`; do echo $i; done
test
me/

So try this:
$ for i in `ls -d test\ me/|sed -e 's/\ /_/g'`; do echo $i|sed -e 's/_/\ /g'; done
test me/


Hope this helps a bit
-Ed

Last edited by eco; 04-16-2008 at 04:11 PM.
 
Old 04-16-2008, 09:04 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
try , as far as possible , not to use ls in combination with for loop, unless really no choice.
 
Old 04-17-2008, 12:01 AM   #4
eco
Member
 
Registered: May 2006
Location: BE
Distribution: Debian/Gentoo
Posts: 412

Rep: Reputation: 48
Hi ghostdog74,

I've always done it and never (that I know of) had any problems.

Could you let me know why it's bad practice? Keen to learn a new thing today.

Thanks
-Ed
 
Old 04-17-2008, 02:05 AM   #5
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
please tell us the command you used to copy.

and i'm also kind of confused. you said you're doing ro copy.. can the source files be modified or are you trying to copy the file with namespaces replaced with _ in the target?..

about ls.. probably it is not good because it sometimes print the files with colors.
but perhaps using the proper options can prevent the unexpected errors.
 
Old 04-17-2008, 02:07 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by eco View Post
Hi ghostdog74,

I've always done it and never (that I know of) had any problems.

Could you let me know why it's bad practice? Keen to learn a new thing today.

Thanks
-Ed
Code:
# ls -ltr
total 0
-rw-r--r-- 1 root root 0 Apr 17 12:08 file with spaces
# for files in `ls`
> do
> echo $files
> done
file
with
spaces
# for files in `ls`; do mv "$files" "otherfile"; done
mv: cannot stat `file': No such file or directory
mv: cannot stat `with': No such file or directory
mv: cannot stat `spaces': No such file or directory
#  
# for files in *; do mv "$files" "otherfile"; done
# ls -ltr
total 0
-rw-r--r-- 1 root root 0 Apr 17 12:09 otherfile
 
Old 04-17-2008, 02:49 AM   #7
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
you can do IFS=$'\n' before to prevent separation within spaces. also add -1 to the parameter of ls.
 
Old 04-17-2008, 03:06 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
What about to let the wildcard do the job? If you have a directory called "my directory with spaces" you can do simply
Code:
cp -pR my* /path/to/destination/dir
if you don't have other files or directories beginning with "my".
 
Old 04-17-2008, 03:10 AM   #9
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
of course.. i think it's better to to use wildcards when it's possible most of the time. you can also use it within directories like:

cp -pR dir/* /path/dest

to know how things work, do

echo cp -pR dir/* /path/dest
 
Old 04-17-2008, 05:44 AM   #10
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Code:
find -depth -name "* *" | while read file; do
  rename "$( basename "$file" )" "$( basename "$file" | sed "s/ /_/g" )" "$file"
done
This will replace all spaces in all file names recursively, so be careful what directory you call it from!
ta0kira

Last edited by ta0kira; 04-17-2008 at 05:47 AM.
 
Old 04-17-2008, 06:26 AM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by konsolebox View Post
you can do IFS=$'\n' before to prevent separation within spaces. also add -1 to the parameter of ls.
you mean like this?
Code:
# IFS=$'\n'
# for files in `ls -1`;do echo "$i";done
have you tried? I am curious about your results
 
Old 04-17-2008, 06:28 AM   #12
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by ta0kira View Post
Code:
find -depth -name "* *" | while read file; do
  rename "$( basename "$file" )" "$( basename "$file" | sed "s/ /_/g" )" "$file"
done
This will replace all spaces in all file names recursively, so be careful what directory you call it from!
ta0kira
is rename a perl script? does it have the ability to do the sed portion, so that sed can be removed. all done using rename.
 
Old 04-17-2008, 08:04 AM   #13
eco
Member
 
Registered: May 2006
Location: BE
Distribution: Debian/Gentoo
Posts: 412

Rep: Reputation: 48
Quote:
Originally Posted by konsolebox View Post
you can do IFS=$'\n' before to prevent separation within spaces. also add -1 to the parameter of ls.
Works great... very smooth and so much better than my sed!
 
Old 04-17-2008, 09:18 AM   #14
ararus
Member
 
Registered: Mar 2008
Location: UK
Distribution: Slackware
Posts: 56

Rep: Reputation: 15
Quote:
Originally Posted by ta0kira View Post
Code:
find -depth -name "* *" | while read file; do
  rename "$( basename "$file" )" "$( basename "$file" | sed "s/ /_/g" )" "$file"
done
This will replace all spaces in all file names recursively, so be careful what directory you call it from!
ta0kira
This is completely redundant, rename translates patterns itself.

Code:
find -type d -exec rename ' ' '_' {} \;
find -type f | xargs rename ' ' '_'
You rename directories first for obvious reasons (you can't rename multiple directory components at the same time, which is also why we use -exec). Then rename the files, using xargs to pass mulitple files at once. Rename does the space to _ translation, no need for sed.

Quote:
is rename a perl script?
Nope.

Code:
# file $(which rename)
/usr/bin/rename: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), stripped
edit:
actually, you might want to nul terminated the filenames:

Code:
find ... -print0 | xargs -0 ...

Last edited by ararus; 04-17-2008 at 09:23 AM.
 
Old 04-17-2008, 09:26 AM   #15
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Quote:
is rename a perl script?
Nope.
Actually it is a perl script on Debian-based systems, an ELF executable on the others.
 
  


Reply

Tags
sed


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
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 folder/file names ~ termial Garoth Linux - Software 1 11-12-2004 01:56 PM
Spaces in file names JohnKFT Slackware 3 11-09-2004 03:44 PM
Bash crashes ? File names with () and spaces Danodare Slackware 1 02-27-2004 02:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:58 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
Open Source Consulting | Domain Registration