ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
I have a lot of files on a Fedora 9 box that contains '+' in the middle of the file name and I would like to remove them. I believe this should be doable using sed. Here's what I've got so far:
Code:
ls -d *+* | sed 's/+//g'
This however returns the correct results, but if I do another ls, nothing was really changed.
Using sed that way, you don't really rename the files, but you simply parse the output of the ls command. To change file names, you can try the rename command (from inside the directory you have the files with +):
SED processes whatever you give it, and displays it on "STDOUT"---by default, your terminal window. It does not change filenames---that is done with the "mv" command.
why "ls -d" ?
I think you need something like this:
for filename in *; do newname= $(sed 's/+//g' $filename); mv $filename $newname; done
To drill down in the directory tree, use "$(ls -R) instead of "*"
The problem is that the space causes mv to see everything after it as a separate file (or directory) name (in both parameters). If you put double-quotes around the parameters, it will "protect" the space, and you may want to alter the sed command to take account of the multiple +'s and space:
Code:
mv "$file" "$(echo $file | sed 's/[+ ]*//g')"
You could also slap the person sending you these files with a wet fish, until they start using sensible file names .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.