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.
...how do I?
I have a dir structure with .html, .php etc.
I can use:
chmod -R 644 *.html
chmod -R 755 *.php
ok, but can't find a way to change all dirs to e.g. 755
tried getting a list of dirs and pipe to chmod:
sudo ls -R | grep :$ | sed s/:// | chmod 755
(am running ubuntu)
just comes back chmod: missing operand after `755'
Piping a list of directories, that literally are strings separated by newlines, does not tell to chmod what to do. You can use xargs to build and execute commands from the standard input, as in
Code:
sudo ls -R | grep :$ | sed s/:// | xargs chmod 755
A better way to do can be
Code:
sudo find ./test_dir -type d -exec chmod 755 {} \;
this looks for directories under test_dir and execute the specified command on every item found. See man xargs and man find for details.
You have directory names with extensions like .html?? seems unusual, but I'm not sure how it is relevant.
chmod -R will change everything (directories and files)--I think you are saying you want to change directories only.
I would suggest a loop starting with:
for f in `ls -R` do
...
...
done
what this does is make a list of values in the variable "f", you can then test each one using--eg--the "file" command. If the test shows it is a directory, then:
chmod 755 $f
Note: In your other code, you were grepping using ":$" I assume you meant this to mean ":" at the end of the line. Trouble is, the ":" is not part of the actual directory name---I suspect it is just the character that your terminal uses to designate a directory.
Distribution: Ubuntu 16.04.7 LTS, Kali, MX Linux with i3WM
Posts: 299
Rep:
Code:
for i in
`ls -al | grep '^d' | awk ' NR > 2 ' | awk '{print $9}'` ;
do chmod 7XX $i ;
done
There must be a way of combining the two awk commands ?
i.e print only the 9th field of all but the first 2 lines ?
It would make the above script look a bit shorter.
I've always done it in two steps. The -print0 argument to find and the -0 argument to xargs mean that the list of files is de-limited with the NULL character - this can save you problems if there are file names with spaces in:
Code:
find /path/to/files -type d -print0 |xargs -0 chmod 755
find /path/to/files -type f -print0 |xargs -0 chmod 644
pitfall: besure to back-escape that semicolon to protect it from your shell. The semi needs to go as an arg to find, not a command sep eaten by your shell
Last edited by cconstantine; 10-31-2007 at 01:02 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.