Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then 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.
I was recently put in charge of archiving alot of files onto my linux system, however the majority of the files have spaces in their names. This is causing my perl scripts to give me back tons of warning messages in regards to pattern matching errors.
I'd like to replace files with spaces in them with a '_'
Example: abbies file.txt --> abbies_file.txt
I'm hoping this is the cause of my perl scripts gagging.
However, I don't know how to do this...been looking around but I'm alittle nervous about wiping everything on accident. Any help?
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803
Rep:
Quote:
Originally posted by buzzbuzz I would love to but I have about 2000 files that I have to do this with ... that would take way too long! Thanks though!
Assuming that all the files are in one subdirectory, try:
---
#!/bin/bash
for FILE in `echo *.txt`
do
NEWFILE=`echo ${FILE} | sed 's/ /_/g'`
mv "${FILE}" ${NEWFILE}
done
---
(In case you have trouble reading it, the characters that surround the command that defines NEWFILE are backticks and the sed command is in single quotes.)
I had to insert a (') single quote between the ( semicolon and (`) back-tick to make this work on my redhat 7.3 system. I am still getting errors for a few files, but I have not investigated these yet. Probably has something to do with (') single quotes or parenthesis in the file names.
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803
Rep:
Quote:
Originally posted by mcredz NEWFILE=`echo ${FILE} | sed 's/ /_/g;`
^
I had to insert a (') single quote between the ( semicolon and (`) back-tick to make this work on my redhat 7.3 system.
Yep, that line as posted has a syntax error in it.
Quote:
Probably has something to do with (') single quotes or parenthesis in the file names.
Heh, heh. You're renaming mp3 files aren't you. (Ran into this myself after downloading a bunch of mp3s from the NEARfest web site. There were only a dozen or so I did it by hand rather than fool around figuring out how to replace spaces as well as all the parentheses.)
*grin* yes, some of the files in questions were mp3/music files. I really want to force samba to rename these files as they are attempted to be written, but for now I am using above script in a cron job. I ended up doing the others by hand as well after an hour of not quite getting it last night after posting my reply!
thanks for the help though, I'm coming from the winworld, and my shell scripting (and use of certain utils like sed/awk) aren't quite up to par with my dos batch and vb scripting!
This script works if all files are in the same directory. What if you have directories and files that are deeper?
ex: /this has spaces/this one too/file with spaces.doc
If you run this script above, it will change "this has spaces", and then when it gets to "this one too" the path will have changed to "this_has_spaces/this one too" and it cannot access that directory or file.
This is tough, so far I'm looking at having to count the /'s to determine how deep the directory structure is and then minus a count of one to determine the end directory. I may be overshooting but i'll post an answer unless someone gets it before I do.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.