LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 02-07-2003, 12:50 PM   #1
buzzbuzz
LQ Newbie
 
Registered: Jan 2003
Posts: 23

Rep: Reputation: 15
Rename files with spaces


I was recently put in charge of archiving alot of files onto my linux system, however the majority of the files have spaces in their names. This is causing my perl scripts to give me back tons of warning messages in regards to pattern matching errors.

I'd like to replace files with spaces in them with a '_'

Example: abbies file.txt --> abbies_file.txt

I'm hoping this is the cause of my perl scripts gagging.

However, I don't know how to do this...been looking around but I'm alittle nervous about wiping everything on accident. Any help?

Thanks!
 
Old 02-07-2003, 01:01 PM   #2
mdtex
LQ Newbie
 
Registered: Feb 2003
Posts: 4

Rep: Reputation: 0
Use quotes " for example:

mv "abbies file.txt" abbies_file.txt
 
Old 02-07-2003, 01:03 PM   #3
buzzbuzz
LQ Newbie
 
Registered: Jan 2003
Posts: 23

Original Poster
Rep: Reputation: 15
I would love to but I have about 2000 files that I have to do this with ... that would take way too long! Thanks though!
 
Old 02-07-2003, 01:19 PM   #4
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Look for a recursive option.
 
Old 02-07-2003, 09:46 PM   #5
nxny
Member
 
Registered: May 2002
Location: AK - The last frontier.
Distribution: Red Hat 8.0, Slackware 8.1, Knoppix 3.7, Lunar 1.3, Sorcerer
Posts: 771

Rep: Reputation: 30
Code:
rename ' ' '_' *
Run it howevermany times as there are spaces in a single file name. If the maximum number of spaces you have in a filename is 5, run it 5 times.
 
Old 02-08-2003, 12:02 AM   #6
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally posted by buzzbuzz
I would love to but I have about 2000 files that I have to do this with ... that would take way too long! Thanks though!
Assuming that all the files are in one subdirectory, try:

---

#!/bin/bash

for FILE in `echo *.txt`
do
NEWFILE=`echo ${FILE} | sed 's/ /_/g'`
mv "${FILE}" ${NEWFILE}
done

---

(In case you have trouble reading it, the characters that surround the command that defines NEWFILE are backticks and the sed command is in single quotes.)

Hope this helps...

Rick
 
Old 02-08-2003, 12:13 AM   #7
nxny
Member
 
Registered: May 2002
Location: AK - The last frontier.
Distribution: Red Hat 8.0, Slackware 8.1, Knoppix 3.7, Lunar 1.3, Sorcerer
Posts: 771

Rep: Reputation: 30
Quote:
Originally posted by rnturn

for FILE in `echo *.txt`

Rick
That wont work.

the 'for FILE in word' construct would break the filename apart because the shell interprets the space character as a delimiter.
 
Old 02-08-2003, 12:25 AM   #8
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally posted by nxny
That wont work.

the 'for FILE in word' construct would break the filename apart because the shell interprets the space character as a delimiter.
You know... after I hit submit I was thinking that I should have made that loop begin with:

ls -1 *.txt | while read FILE

The rest of the script would be unchanged.

Nice catch. Thanks.

Rick
 
Old 02-08-2003, 06:49 PM   #9
buzzbuzz
LQ Newbie
 
Registered: Jan 2003
Posts: 23

Original Poster
Rep: Reputation: 15
Sweet thanks! I'll give that a try first thing on monday! Can't believe the spaces in all these file names...windows users....

Thanks Again!! I'll let you know how I turn out!
 
Old 02-10-2003, 07:13 AM   #10
buzzbuzz
LQ Newbie
 
Registered: Jan 2003
Posts: 23

Original Poster
Rep: Reputation: 15
Thanks for the help! Here's the script I ended using:

#!/bin/sh

find . -name '* *' | sort | while read FILE
do
NEWFILE=`echo ${FILE} | sed 's/ /_/g;`
mv "${FILE}" ${NEWFILE}
done

Seems to work great!

Thanks again!
 
Old 03-09-2003, 02:51 AM   #11
mcredz
LQ Newbie
 
Registered: Mar 2003
Posts: 5

Rep: Reputation: 0
NEWFILE=`echo ${FILE} | sed 's/ /_/g;`
^

I had to insert a (') single quote between the ( semicolon and (`) back-tick to make this work on my redhat 7.3 system. I am still getting errors for a few files, but I have not investigated these yet. Probably has something to do with (') single quotes or parenthesis in the file names.
 
Old 03-09-2003, 09:51 AM   #12
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally posted by mcredz
NEWFILE=`echo ${FILE} | sed 's/ /_/g;`
^
I had to insert a (') single quote between the ( semicolon and (`) back-tick to make this work on my redhat 7.3 system.
Yep, that line as posted has a syntax error in it.
Quote:
Probably has something to do with (') single quotes or parenthesis in the file names.
Heh, heh. You're renaming mp3 files aren't you. (Ran into this myself after downloading a bunch of mp3s from the NEARfest web site. There were only a dozen or so I did it by hand rather than fool around figuring out how to replace spaces as well as all the parentheses.)
 
Old 03-09-2003, 10:34 AM   #13
mcredz
LQ Newbie
 
Registered: Mar 2003
Posts: 5

Rep: Reputation: 0
*grin* yes, some of the files in questions were mp3/music files. I really want to force samba to rename these files as they are attempted to be written, but for now I am using above script in a cron job. I ended up doing the others by hand as well after an hour of not quite getting it last night after posting my reply!

thanks for the help though, I'm coming from the winworld, and my shell scripting (and use of certain utils like sed/awk) aren't quite up to par with my dos batch and vb scripting!

-Dave
 
Old 04-17-2003, 10:01 AM   #14
hockeybrad
LQ Newbie
 
Registered: Apr 2003
Posts: 1

Rep: Reputation: 0
Question what about directories?

This script works if all files are in the same directory. What if you have directories and files that are deeper?

ex: /this has spaces/this one too/file with spaces.doc

If you run this script above, it will change "this has spaces", and then when it gets to "this one too" the path will have changed to "this_has_spaces/this one too" and it cannot access that directory or file.

Any ideas?
 
Old 04-17-2003, 12:36 PM   #15
Shinobi
Member
 
Registered: Mar 2003
Posts: 46

Rep: Reputation: 15

This is tough, so far I'm looking at having to count the /'s to determine how deep the directory structure is and then minus a count of one to determine the end directory. I may be overshooting but i'll post an answer unless someone gets it before I do.

Ain't scripting fun? ;P

Last edited by Shinobi; 04-17-2003 at 01:13 PM.
 
  


Reply



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
rename files allelopath Linux - General 5 07-05-2005 03:00 AM
files that have spaces in their names hesher Linux - Software 5 01-14-2005 01:43 PM
Can not rename files. Maximus2000 Linux - General 0 04-22-2004 01:36 PM
Files with spaces in their names downinthemine Linux - General 3 12-12-2003 01:48 PM
How to open/rename files like this... Mega Man X Linux - General 4 05-08-2003 11:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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