Computer Hope

Software => BSD, Linux, and Unix => Topic started by: Blisk on July 18, 2019, 04:25:23 AM

Title: linux changing ownership
Post by: Blisk on July 18, 2019, 04:25:23 AM
I have about 300 folders and I need to change user for that all folders except hidden folders and files.
so how can I
sudo chown -R apache:apache /myfolder
or
sudo chown -R apache:apache /myfolder/*

but not hidden folders and files under that

I also need to chmod
sudo chmod -R 755 /myfolder

but not hidden files or folders
Title: Re: linux changing ownership
Post by: nil on July 19, 2019, 06:29:16 AM
You can use the find command with the -exec option to locate files and folders that meet a certain set of conditions, then execute a certain command on them.

https://www.computerhope.com/unix/ufind.htm

For example the following command changes the ownership of all files, but not folders, under the directory /path/to/folder/

Code: [Select]
sudo find /path/to/folder/ -type f -exec chown apache:apache {} \;
-type f
specifies files only, and excludes directories

-exec
specifies a command to run

{}
The curly brackets are where the found file is inserted in the command

\;
You have to end the command in a semicolon, to indicate the end of the command. The semicolon is a special operator in bash, so you prefix it with a backslash to protect it from the shell. more info here https://stackoverflow.com/questions/20913198/why-are-the-backslash-and-semicolon-required-with-the-find-commands-exec-optio/20913251

For hidden files ("dot files" whose name starts with a period) you can probably use the -not operator in combination with the -path or -name options to tell find "skip all files and folders that start with a dot." More info https://askubuntu.com/questions/266179/how-to-exclude-ignore-hidden-files-and-directories-in-a-wildcard-embedded-find

Be very careful when running find -exec with sudo, you could destroy a lot of data by using it incorrectly.

I hope this helps.