how to find all directories in /home that are not owned by root and change their permissions to ensure they have 711 permission? - linux

I try to find all directories in /home that are not owned by root and change their permissions to ensure they have 711 permission in the same command.
find \home type -d -not -user root -ls | chmod 711 {} \
But the command I used doesn't work.

The following should work:
find /home -type d -not -user root -exec chmod 711 {} +
The -exec action allows you to run a separate executable (in this case chmod) and supply the found names to it. The + at the end allows find to run chmod with multiple names at once.
The above includes fixes for a few typos: \home should be /home, type -d should be -type d.

The first instruction has wrong syntax. You could try:
find /home -type d -not -user root -ls
Also, you shouldn't use "ls" if you care about performance. Instead I suggest using the -exec switch.
Good luck

Related

What is the command to remove files in Linux from a particular directory which are owned by a particular user?

Let's say we have a directory path /home/username. How can we delete all the files only from this path which are owned/created by the user dev-user? When I am trying
find . -user c70945a -exec rm /home/dev-user/* {} \;
but it's giving an error and it's removing files from other directories as well.
find /home/username -maxdepth 1 -type f -user "dev-user" -delete
Use the user flag to specify files owner by a specific user and use -delete to remove the files.
Set maxdepth 1 to search for files within /home/username only and not child directories.
Use this find command:
find /home/dev-user -user 'dev-user' -type f -exec rm {} +
+ at the end of -exec means that {} is expanded to the list of all matching files.

Display the Files and Folder details under a parent directory in Linux

I have to display all files and folders details under a parent directory.
I am using the command is 'find'. For example,
find /usr/local
/usr/local/bin
It's display only the file name. I have to display file name with details about files like below. Means I have to add below information in the above result set.
-rw-rw-- 1 hduser hduser 213 jan 22 11:51
How to do it?
Thanks in advance.
There's the convenient action -ls:
find /usr/local -ls
If you need some other than the default -ls output format, the action -printf is appropriate; with that you can freely define the format, e. g.:
find /usr/local -printf "%i,%k,%M,%n,%u,%g,%s,%t,%p\n"
Cf. man find: Print File Information.
you can use below command to list it nicely in a order and block wise:-
find . -type d |xargs ls -ltr
For your case:-
find /usr/local -type d |xargs ls -ltr
Try sudo find /usr/local -name "filename" -depth -exec ls -ll {} \;

How to search (using find command) for directories and copy all the files and directory itself to another directory in linux?

How to search (using find command) for directories and copy all the files and directory itself to another directory in linux?
Here is what I have so far:
find -type d -name "*.ABC" -exec {} /Desktop/NewFile \;
I get this as output:
find: './GAE/.ABC: PERMISSION DENIED
Please Help, Thanks!
Your error here above has nothing to do with file read permission. You're trying to execute the directories you find! Avoid running commands as root or sudo unless: (1) you really need it and (2) you really know what you're doing. Quite often people asking for root or sudo privileges are exactly the ones should not have it.
That said... there are several ways to copy a directory tree under *nix. This is just one possible approach:
$ find <start> -type d -name \*.ABC -exec cp -av {} <target> \;
Where:
<start> is a directory name. It's used to tell find where to start its search (for example /usr/local or $HOME)
<target> is another directory name to define the final destination of your copied directories
UPDATE
In case you want to search for multiple paths...
$ find <start> -type d \( -name \*.ABC -o -name \*.DEF \) -exec cp -av {} <target> \;
This should work:
find ./source_dir -name \*.png -print0 | xargs -0 cp -t path/to/destination
For more info, you can look up here.

Bash - Recursively change ownership of only the directories which belong to a certain user/group

I have a directory (we will call /files) with ~1300 subdirectories, each of which contains further subdirectories and files.
90% of the top level directories in /files belong to apache:apache and the rest belong to root:root. I need everything to belong to apache:apache.
I think if I do a recursive chown on the whole lot it will be quite extreme, so I was wondering if there's a more efficient way to recursively change ownership of just the root:root directories to apache:apache.
Bonus if chmod can be done on these directories in the same way.
Your recursive chown would have probably been done already, but you could use this instead:
find . -type d \( ! -user apache -o ! -group apache \) -print0 | xargs -0 chown apache:apache
To change directories that have the wrong permission:
find . -type d ! -perm 755 -print0 | xargs -0 chmod 755
Using linux's find command is going to help there:
find /files -user root -group root -type d \
-exec chmod something {} \; -exec chown apache.apache {} \;
for more details on WHY that works there is http://www.explainshell.com/explain?cmd=find+%2Ffiles+-user+root+-group+root+-type+d+-exec+foo+\%3B

changing permissions of files in a directory recursively

I am trying to change the permissions of a files present in a directory and subdirectories using the below command and running into below error..can anyone help?
user#machine:/local/mnt/workspace$ find . -type f -exec chmod 644 {} \;
chmod: changing permissions of `./halimpl/ncihal/adaptation/NonVolatileStore.cpp': Operation not permitted
you can run the following command:
#chown -R directory_path
But it will change the permissions of directories also.
For only files, you can run.
#find directory_path -type f -exec chmod 644 {} \;
It also looks like you dont have enough permissions. try
#sudo find directory_path -type f -exec chmod 644 {} \;
or run the command as root user.
It looks to me like you don't have permission to change NonVolatileStore.cpp.
Are you aware of chmod's -R switch that recursively changes permissions?
if you have the root privilege, try:
sudo find . -type f -exec chmod 644 {} \;
It could be that you simply don't own that file. Run an ls -l on it to see full permissions and who the owner is.
It could also be the filesystem is read only.

Resources