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.
Hey guys I am having some trouble operating the for loop. I am trying to change permission rights on every file in a folder. I want to change the ownerships from root to another user. I know there are probably much easier ways to do this, but I really won't stop until i get it to work. here is my code:
Code:
hal@Hal-Laptop:~$ for x in $ls /home/hal/comics/Amazing_Spiderman_1-535/; do chown hal $x; done
hal@Hal-Laptop:~$
According to the results I got from the terminal, my for loop worked, but when I checked the files, the ownerships had not changed. So i tried entering something slightly different into the terminal and this is what happened:
Code:
hal@Hal-Laptop:~$ for x in `$ls /home/hal/comics/Amazing_Spiderman_1-535/`; do chown hal $x; done
bash:/home/hal/comics/Amazing_Spiderman_1-535/: is a directory
hal@Hal-Laptop:~$
I am not sure what I am doing wrong so any guidance would be appreciated. Thanks, Hal
It should be "for x in $(ls /home/hal/comics...) ..." or "for x in `ls /home/hal/comics...` ...", because you either use $ or the backticks for command substitution, not both.
Hey Nylex,
Thanks for the reply, your solution corrected my syntax error. Now I have another, but slightly unrelated question. I said before that I was trying to change ownerships on all the files in a directory, but these files were moved from a windows partition. The problem is that these filenames have spaces in them, and the chown command reads the words of the filenames, as files themselves, because of the spaces between them. I was wondering if there is something I can do to fix the filenames, without having to do them one by one. Usually a for loop would be helpful to rename many files, but the mv command, like chown, would not be able to read the filenames as they really are. Thanks for the help, Hal.
Hey pwc101,
The script you gave me worked perfectly and I wanted to thank you for that. I was able to change file permissions the way I wanted to, so everything worked out fine. Although the script did work, I want to understand why it does. Im confused about the second part of the "do" clause:
Code:
"$(echo ${i// /_})"
I am not sure I understand how it operates to alter the filename, on account of spaces and underscores. So it would be great if you or someone could explain the logic to the script. Thanks again, Hal.
Another name for it is Parameter Expansion. There is a section on this in the bash man page too. Several other methods of manipulating parameters are listed.
I understand the concept of command substitution in general, but I don't understand the logic behind the forward slashes. When I was learning how command substitution operated, I only came across backslashes and their function in escaping, but I don't understand the function of forward slashes or the double slash.Thanks, Hal
${parameter//pattern/string}
The pattern is expanded to produce a pattern just as in filename expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string. In the first form, only the first match is replaced. The second form causes all matches of pattern to be replaced with string. (from http://www.linuxtopia.org/online_boo...shref_29.html).
So basically, the double forward-slash causes all occurrences of pattern to be replaced with string. In our case, that's " " with "_", so ${i//this/that} would find all occurrences of "this" within our original variable $i (which is a filename), and substitute them with "that". I think.
edit: ${i/this/that} would only do the first occurrence of "this", not all occurrences.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.