Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
01-26-2011, 04:46 PM
|
#1
|
LQ Newbie
Registered: Jan 2011
Posts: 2
Rep:
|
Simple bash question - replacing single quotes in filenames with another character
Hopefully easy !
Can anyone offer a code snippet to recursively go through directories and replace any single or double quotes quotes found in a filename with another character (e.g. "_").
My directory structure looks like :-
Parent
|--------DIR1
| |-----File1
| |-----File2
|
|--------DIR2
| |-----File3
| |-----File4
etc
If any of the filenames contain a single quote or double quote, then replace it with an underscore.
Thanks in advance !
|
|
|
01-26-2011, 05:15 PM
|
#2
|
Senior Member
Registered: Aug 2009
Posts: 3,790
|
Give this a go:
Code:
#!/bin/bash
if [[ $# -ne 1 ]]
then
echo "Usage: $0 <dirname>"
exit 1
fi
for file in $(find $1 -type f)
do
echo "Testing $file"
if [[ $(echo "$(basename $file)" | grep -q "\'") -eq 0 ]]
then
dirname=$(dirname $file)
newfile=$(echo $(basename $file) | tr "'" "_")
mv $file $dirname/$newfile
echo "Changed $file to $newfile"
fi
done
Sorry, just noticed you wanted double quotes as well, are you ok to modify the script above ?
Last edited by kbp; 01-26-2011 at 05:20 PM.
|
|
|
01-26-2011, 06:59 PM
|
#3
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,030
|
As we already have unusual characters like quotes in file names, if i was a betting man I would say there are spaces as well which will word split if
using a for loop. So perhaps try something like:
Code:
#!/bin/bash
while read -r F_PATH
do
FILE=${F_PATH##*/}
mv $F_PATH ${F_PATH%/*}/${FILE//[\'\"]/_}
done< <(find $1 -type f -iname "*['\"]*")
|
|
|
01-26-2011, 08:56 PM
|
#4
|
Senior Member
Registered: Jan 2010
Posts: 2,020
|
Hi,
here is a single find that will do it
Code:
find "/path/to/dir/" -type f -iname "*['\"]*" -exec /bin/bash -c 'echo mv -n "${1}" "${1//["'\''"\"]/_}" ' {} {} \;
If the results look alright to you then remove the 'echo' to make the changes actually happen.
The '-n' option after the 'mv' command is there to make sure that no file is overwritten in case you have files like
file'with'quotes
file"with"quotes
In this case handling the renaming process will be a bit more complex. But at this point I assume that the target-names will still be unique. Post again if this assumption is not valid.
|
|
|
01-26-2011, 11:31 PM
|
#5
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,030
|
@crts - I thought of something like yours but then thought, if the filenames are buggered maybe the directories are too, which would then cause something like yours to make the
change to the entire path. hmmm .. just thinking out loud but I guess you could get past that by using execdir??
|
|
|
01-27-2011, 04:30 AM
|
#6
|
Senior Member
Registered: Jan 2010
Posts: 2,020
|
Hi grail,
you are right. And I was considering to use -execdir at first for the same reason. But then I thought that if the OP wants to change the filenames, then maybe he also wants to change the directory names. So I went with -exec.
The main problem I see with normal -exec is in this case:
dir'with'quote/file1
dir"with"quote/file2
Afterwards file1 and file2 will be in the same directory which might be what the OP wants - or maybe not. I think some clarification from the OP is required to clear things up.
PS: If it turns out that several files will have the same name and path after renaming then I think a simple 'find' will indeed not cut it.
|
|
|
01-28-2011, 04:55 AM
|
#7
|
LQ Newbie
Registered: Jan 2011
Posts: 2
Original Poster
Rep:
|
Thanks all - much appreciated. The directory names are fine, it was just the filenames, so the one-liner is exactly what I need.
|
|
|
01-28-2011, 06:37 AM
|
#8
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,030
|
Quote:
Originally Posted by crts
Hi grail,
you are right. And I was considering to use -execdir at first for the same reason. But then I thought that if the OP wants to change the filenames, then maybe he also wants to change the directory names. So I went with -exec.
The main problem I see with normal -exec is in this case:
dir'with'quote/file1
dir"with"quote/file2
Afterwards file1 and file2 will be in the same directory which might be what the OP wants - or maybe not. I think some clarification from the OP is required to clear things up.
PS: If it turns out that several files will have the same name and path after renaming then I think a simple 'find' will indeed not cut it.
|
Actually I think you will also run into an issue should the directory names be buggered (I realise the OP has now said they are fine) as the following would fail:
Code:
path=/var/path/this"has'quotes/file's.txt
mv $path ${path//["'\''"\"]/_}
This will crap out as it will try to look for a path like:
Code:
/var/path/this_has_quotes/file_s.txt
Which of course does not exist
|
|
|
08-10-2012, 11:17 PM
|
#9
|
LQ Newbie
Registered: Dec 2006
Location: Jakarta, Indonesia
Distribution: Fedora 6
Posts: 2
Rep:
|
Thank you
I realize this is an old post, but wanted to pass my thanks on to all. I got a bunch of files which some moron had given filenames with the author's name, including a single quote. Frustrating to no end, until I came across this. I did try some regexp commands, but I'm not clever enough, I guess, to handle this rather special case. Very valuable.
|
|
|
All times are GMT -5. The time now is 10:43 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|