Delete file if it is owned by specific user - linux

There is a file that is sometimes not owned by root
I want my perl script in linux to basically check if a file is owned by root if it is delete it.
Currently what I have unlink("$File_Path/File_Name");
but this just deletes the file I want it to check if it's owned by root first then delete otherwise ignore.
can you please guide me how I can achieve this I am out of ideas?

The documentation for stat shows that the fifth element in the returned list is "numeric user ID of file's owner". The superuser account on *nix must have uid of 0, so
if ( (stat $fqn)[4] == 0 ) {
unlink $fqn or die "Error with unlink($fqn): $!";
}

If you're doing this to a bunch of files in a folder somewhere, you might be better off by just one of these:
find /folder/somewhere/ -type f -user root -exec rm {} \;
find /folder/somewhere/ -type f -user root -exec rm -i {} \; #interactive y/n each file
find /folder/somewhere/ -type f -user root -print0 | xargs -r0 rm
You might also need sudo in front of find. Careful though, this is a kind of command that can do a lot of harm...

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.

List all user directories and look for specific file

I'm working on a script which will check for a specific file in ~/ of all users with home directory.
I tried ls /home and cd into users into their home directories but it gives too many arguments error.
username=$(ls /home)
cd /home/$username
cat file.json
I except the output of json file but it doesn't gives output of json file even user have a json file.
Edit:
Now I need to extract username of users with file file.json I have tried to do this with grep but it didn't worked.
files=$(find /home -name tilde.json -print)
echo "$files" >> jsons.txt
cat jsons.txt | grep /*/
This will find and list all files called file.json under the /home directory:
find /home -name file.json -print
You may want to redirect errors to /dev/null in the event you don't have access to all users' home dirs.
If you want to print out the contents of all these files, try:
find /home -name file.json -print -exec cat {} \;
To limit the search to only the directories under /home (i.e. not /home itself, and no sub directories in the user home), use:
find /home -mindepth 2 -maxdepth 2 -type f -name file.json -print -exec cat {} \;
I also added the -type flag there to limit the search to files and exclude any dirs that may happen to share the name.
This'll do:
cat /home/*/file.json
It'll print
cat: '/home/*/file.json': No such file or directory
on standard error if it can't find any.
What about:
cd /home
find . -name file.json -maxdepth 1 -exec cat {} \;
Suppose /home contains user1 and user2.
Then your cd command is invoked as
cd /home/user1 user2
That's not what you wanted, and isn't valid syntax for cd, which accepts only a single argument. You probably wanted a for loop instead.
If you can't predict such expansions, set -x enables tracing, which may provide insight into what commands are actually run, and would show your problem here. set +x to turn it off again.
Finally, note that not all users' home directories are necessarily in home. You might want to use getent or similar to find all user home directories.

cannot delete directory using shell command

I wanted to delete all directories with name in same pattern RC_200, here is what I did:
find -name "RC_200" -type d -delete
But it's complaining about this:
find: cannot delete '.RC200': Directory not empty
You should try:
find . -name RC200 -type d -exec rm -r {} \;
You can see here, what the command does
More, you can try what #anubhava recommended in comment (Note the + at the end); this one is equivalent to a xargs solution:
find . -name RC200 -type d -print0|xargs -0 rm -r
xargs executes the command passed as parameter, with the arguments passed to stdin. This is using rm -r to delete the directory and all its children
May be you should run administrator privilege.
if your os Ubuntu add "sudo" before your Command or what ever your os look at administrator key.
OR
remove file protection if it has.

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.

How can I get the owner of every file in a directory in Linux?

I need to check if root is the owner of every file in a particular directory. I can do
stat --format=%u /directory/name/here
to get the owner of the directory itself, but not the files in it.
My other idea was to do
ls -lL | grep "^-\|^d" | cut -d ' ' -f 2
but that doesn't work if the last byte in the permissions is a space and not a '.'.
This is also CentOS if that matters.
you can use find:
find /tmp -type f -printf '%u\n' | sort -u
lightdm
root
tiago
If you need UID in numeric form, like using stat:
find /tmp -type f -printf '%U\n' | sort -u
0
1000
104
You're asking two different questions.
I need to check if root is the owner of every file in a particular directory
To find any files that are not owned by root, you can do:
find /yourdir ! -user root
If it returns any filenames at all, then root is not the owner of every file in the particular directory.
How can I get the owner of every file in a directory in Linux?
To print every file in the directory with username:
find /yourdir -printf '%u %p\n'
And if the final step would be to chown the files not owned by root, you can simply do chown -R root /yourdir, since there's no harm in chowning root's files to root.
Try
find /your/dir/ -type f -exec stat --format='%u %n' '{}' \;
I added %n to display the file name.
Read find(1) for more info about find .... You may want -max_depth 1 to avoid going deeply in /your/dir/
for F in /directory/*; do stat --format='%u' "$F"; done
And optionally add dotglob option to match files beginning with . as well:
shopt -s dotglob
for F in /directory/*; do stat --format='%u' "$F"; done
* --format is equivalent to -c.

Resources