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...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.