i have a list of directories in the current directory named with there permission codes (exemple : 552, 700, 777). I want to get the code permission from the name of directory and apply it to the directory and all the files it contains.
I tried with the xargs command :
find . -name "[0-9][0-9][0-9]" -type d | xargs chmod -R [0-9][0-9][0-9]
the problem with this command it takes the first directory and its change the permission code of all directories.
├── 555
│ └── logs
│ ├── 01.log
│ ├── 02.log
│ ├── 03.log
│ ├── 04.log
│ ├── 05.log
│ ├── 06.log
│ └── 07.log
├── 700
│ └── data
│ └── data1.data
what I want : I have the 555 directory so I want to change all sub files and directory with permission code 555 and for the second directory I want to change all the subfiles and directory with the permission code 700
what my command do: it change all other files and subdirectories with the permission code of the first file 500
CodePudding user response:
Try
find . -name "[0-9][0-9][0-9]" -type d | sed 's#\./\(.*\)#\1#' | xargs -I{} chmod -R {} {}
- the
find
is the same as yours. - the
sed
is added to remove the./
from the directory name. Find returns./700
,./555
, ... xargs
with-I
uses{}
to reuse what it received into the command. So it says "chmod -R
DIRNAME DIRNAME". Sochmod -R 700 700
and so on.- In your attempt,
xargs chmod -R [0-9][0-9][0-9]
, there is nothing to link the [0-9] in thefind
to the [0-9] inxargs
.
CodePudding user response:
Without xargs
find . -type d -regextype sed -regex ".*/[0-9]\{3\}$"| awk -F"/" '{print "chmod -R " $NF,$0}'|sh