LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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, 08: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, 09:00 AM   #2
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
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 09:01 AM.
 
Old 08-12-2005, 09:04 AM   #3
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
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, 09: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, 09: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, 09: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 09:15 AM.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell renaming script paskari Programming 3 09-24-2005 02:15 PM
file renaming with shell script or ? XJNick Linux - General 5 07-29-2005 02:43 PM
directory renaming shell script? Apostasy Linux - General 15 02-22-2005 06:50 AM
renaming files script. xushi Programming 4 10-10-2004 08:06 AM
Bash script renaming a variable zael Programming 3 09-30-2003 04:37 AM

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

All times are GMT -5. The time now is 12:08 PM.

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