LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 08-12-2005, 09:47 AM   #1
Boffy
Member
 
Registered: Jul 2003
Location: Durham, UK
Distribution: Ubuntu 8.04
Posts: 419

Rep: Reputation: 30
Simple Renaming Script


Can anyone advise me of a simple bash or perl script that would go through a directory and remove each " ' " from any filenames that contain it. I am having difficulty getting another script to work properly because it goes mad if a filename contains a " ' ".

Thanks

Boffy
 
Old 08-12-2005, 10:00 AM   #2
orgcandman
Member
 
Registered: May 2002
Location: dracut MA
Distribution: Ubuntu; PNE-LE; LFS (no book)
Posts: 594

Rep: Reputation: 102Reputation: 102
In your original script, you have to escape the ' by doing \'

I'd say it's probably good practice to not have your script rely on the user making name changes before your script can run. They should be able to use whatever filename they want.

Thanks,
Aaron

Last edited by orgcandman; 08-12-2005 at 10:01 AM.
 
Old 08-12-2005, 10:04 AM   #3
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
Code:
find directory/ -type f -name "*'*" -exec bash -c 'new="$(echo "$0" | tr -d "$1")"; mv "$0" "$new"' "{}" "'" \;
This should work, but you may want to test this on a copy of your directory.

Note that if you have for example both "hello.txt" and "hel'lo.txt" in the same dir, the command will want to rename the second to "hello.txt" and conflict with the already existing file.

Yves.
 
Old 08-12-2005, 10:04 AM   #4
Boffy
Member
 
Registered: Jul 2003
Location: Durham, UK
Distribution: Ubuntu 8.04
Posts: 419

Original Poster
Rep: Reputation: 30
I agree fully that's why I am trying to avoid the problem. At present the filename is loaded into the script automatically from a directory scan. How do I instert a "/" at the appropriate place?
 
Old 08-12-2005, 10:06 AM   #5
Boffy
Member
 
Registered: Jul 2003
Location: Durham, UK
Distribution: Ubuntu 8.04
Posts: 419

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by theYinYeti
Code:
find directory/ -type f -name "*'*" -exec bash -c 'new="$(echo "$0" | tr -d "$1")"; mv "$0" "$new"' "{}" "'" \;
This should work, but you may want to test this on a copy of your directory.

Note that if you have for example both "hello.txt" and "hel'lo.txt" in the same dir, the command will want to rename the second to "hello.txt" and conflict with the already existing file.

Yves.
Thats perfect thanks. I don't think that scenario will occur often if ever. Thanks a lot.
 
Old 08-12-2005, 10:07 AM   #6
sm1else
Member
 
Registered: Oct 2004
Distribution: At home: Ubuntu, Debian Sarge
Posts: 56

Rep: Reputation: 15
I would use 'mmv' to do this:
Code:
mmv "*'*" "#1#2"
Though the above command would need to be run several times (until it says 'No match') and it can be a pain to install if your distro doesnt provide it

This might work with bash (its not recursive though):
Code:
for file in *\'*; do
  newfile=`echo $file | sed -e "s/'//g"`
  echo $newfile
  mv "$file" "$newfile"
done
In your other script (I am assuming that its a shell script too) you could try
Code:
escaped=`echo "$file" | sed -e "s/'/\\\\'/g"`
note that the four backslashes in that command is due to multiple levels of escaping, sed wants \\ in its input but the double quotes needs escaping too so it becomes \\\\

Last edited by sm1else; 08-12-2005 at 10:15 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
shell renaming script paskari Programming 3 09-24-2005 03:15 PM
file renaming with shell script or ? XJNick Linux - General 5 07-29-2005 03:43 PM
directory renaming shell script? Apostasy Linux - General 15 02-22-2005 07:50 AM
renaming files script. xushi Programming 4 10-10-2004 09:06 AM
Bash script renaming a variable zael Programming 3 09-30-2003 05:37 AM


All times are GMT -5. The time now is 10:21 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration