0

Say I have the following folder structure

folder/
├─ subfodler_2/     #User1
├─ subfolder_3/     #User2
├─ subfolder_1/     #User1

where I want to chmod all the folders (and files) created by User1 to, say, 777.

I know I cant get the files by

ls -la | grep User1

and I know how to chmod a specific folder, but I don't know how to "use the output" of ls i.e parse it to chmod afterwards.

It does not have to be a one-liner if I can write it as a bash-script it would be fine aswell

1 Answers1

4

You can use find to search for specific files. -user User1 will filter results to only those owned by User1.

Then you can use -exec to run a command on those files

find folder -user User1 -exec chmod 777 {} +

If none of your files are executable already (maybe you're doing this on a library of PDFs), then 777 may not be the cleanest solution. In that case you could do this to make directories world-readable/writable/executable, but files only world-readable/writable.

find folder -user User1 -type d -exec chmod 777 {} +
find folder -user User1 -type f -exec chmod 666 {} +

Your original question asked about parsing ls. You shouldn't do that. Further reading on that matter:

Why *not* parse `ls` (and what to do instead)?

Stewart
  • 13,677
  • Just found out that adding -maxdepth 1 returns only the folders. THanks a lot – CutePoison Feb 04 '22 at 10:00
  • -type f will find regular files. that still leaves out all the other stuff, like device nodes, named sockets, pipes and symlinks. Not that it matters here, you can't change the permissions of a symlink itself, and you probably don't have those others. – ilkkachu Feb 04 '22 at 10:16