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.
and i'm also kind of confused. you said you're doing ro copy.. can the source files be modified or are you trying to copy the file with namespaces replaced with _ in the target?..
about ls.. probably it is not good because it sometimes print the files with colors.
but perhaps using the proper options can prevent the unexpected errors.
I've always done it and never (that I know of) had any problems.
Could you let me know why it's bad practice? Keen to learn a new thing today.
Thanks
-Ed
Code:
# ls -ltr
total 0
-rw-r--r-- 1 root root 0 Apr 17 12:08 file with spaces
# for files in `ls`
> do
> echo $files
> done
file
with
spaces
# for files in `ls`; do mv "$files" "otherfile"; done
mv: cannot stat `file': No such file or directory
mv: cannot stat `with': No such file or directory
mv: cannot stat `spaces': No such file or directory
#
# for files in *; do mv "$files" "otherfile"; done
# ls -ltr
total 0
-rw-r--r-- 1 root root 0 Apr 17 12:09 otherfile
find -depth -name "* *" | while read file; do
rename "$( basename "$file" )" "$( basename "$file" | sed "s/ /_/g" )" "$file"
done
This will replace all spaces in all file names recursively, so be careful what directory you call it from!
ta0kira
This is completely redundant, rename translates patterns itself.
Code:
find -type d -exec rename ' ' '_' {} \;
find -type f | xargs rename ' ' '_'
You rename directories first for obvious reasons (you can't rename multiple directory components at the same time, which is also why we use -exec). Then rename the files, using xargs to pass mulitple files at once. Rename does the space to _ translation, no need for sed.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.