[SOLVED] Simple bash question - replacing single quotes in filenames with another character
Linux - NewbieThis 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.
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.
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. "_").
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 "*['\"]*")
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.
@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??
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.
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:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.