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

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.

Related

Remove dirs from parent dir without deleting parent directory (Linux)

I am using the following command to remove all dirs older than 1 minute from the following path:
find /myhome/me/xyz/[0-9][0-9]/[0-9][0-9][0-9][0-9]/my_stats/ -mmin +1 -exec rm -rf {} \;
folder structure :
/home/myhome/me/xyz/<2 digit name>/<4 digit name>/my_stats/
There could be multiple dirs or a single dir inside my_stats. The issue is when there is a single folder inside my_stats, the find command is deleting the my_stats dir as well.
Is there a way to solve this?
Thanks
If I understand your question correctly you are probably looking for this:
find /myhome/me/xyz/[0-9][0-9]/[0-9][0-9][0-9][0-9]/my_stats/ -mmin +1 -maxdepth 1 -mindepth 1 -type d -exec rm -rf {} \;
The -mindepth 1 parameter is what excludes the my_stats directory from the listing, as it is located at depth 0.
The -maxdepth 1 parameter will not show subdirs of subdirs (you are deleting their parents recursively anyway).
The -type d parameter limits the output to directories only, not ordinary files.

How to delete files under subdirectories but not deleting subdirectories themselves in linux

I have the following directory structure:
/archive/file1.csv
/archive/file2.csv
/archive/myfile/my.txt
/archive/yourfile/your.txt
I want to delete all files under /archive but not its subfolders, so after deletion, the directory structure should look like:
/archive/
/archive/myfile/
/archive/yourfile/
I have tried the following two commands, but the files under the subfolders are not deleted (ie. my.txt and your.txt), anyone know why ?
find -L /archive ! -type d -exec rm -rfv {} +
find -L /archive -type f -exec rm -rfv {} +
use find
$ find . ! -type d -delete
make sure you're in the right path.

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.

Linux: how to look for files with a certain extension in hierarchy and execute command whenever one is found?

I have a directory hierarchy, whose names do not follow a pattern. E.g.
parent
bcgegec
hfiwehfiuwe
huiwwuifegeufg
whegwgefyfeg
hfeohfeiofe
chidchuehugfe
dedewdewf
tegtgetg
gtgetgtg
and so on.
Inside some of such directories there is a file with "gr" extension. I need to find each of such files, cd to its dir and execute "gnuplot" command having the .gr file as argument. I tried the following to nest two find commands, but the {} of the inner one does not work as I need. The outer find should iterate for every directory, and the inner find should look for the presence of the .gr file.
find $parentDir -type d -exec sh -c '(cd {} && find . -maxdepth 1 -name *.gr -exec /usr/bin/gnuplot {} \;)' \;
Perhaps this is what you are looking for:
find . -type f -name "*.gr" -execdir /usr/bin/gnuplot {} \;
Read through man find for other useful information.

Is there a way to list out files and delete by creator in unix or linux

I have to list out the file and then i have to delete the file by the creator.
I have used below command to list the files :-
ls -lrt
but i am not sure how i can delete the file by the creator name.
please help me asap.
You can do that with find. First find the right syntax and confirm the matches:
find /path/to -type f -user username -maxdepth 1
If all looks ok, you can go ahead and make it delete the matched files:
find /path/to -type f -user username -maxdepth 1 -delete
Of if your version of find doesn't have -delete then you can do like this:
find /path/to -type f -user username -maxdepth 1 -exec rm {} \;
There are a couple ways to do this
find /path/to/files -user username -exec rm -fr {} \;
This will recursively search everything under /path/to/files for files owned by username
Another option is to use the deluser command and the --remove-all-files switch
deluser --remove-all-files username

Resources