Tutorial :Removing only my files in Unix [closed]



Question:

I need to rm files from a unix directory that only belong to my id. I tried building this command, but to no avail:

ls -la | grep 'myid' | awk ' { print $9 } ' | rm  

My result: Usage: rm [-firRe] [--] File...


Solution:1

You were really close. Try:

rm `ls -la | grep 'myid' | awk ' { print $9 } '`  

Note that those are backticks, not single quotes surrounding the first three segments from your original pipeline. Also for me the filename column was $8, but if $9 is the right column for you, then that should do it.


Solution:2

find . -user myuser -print0 |xargs -0 rm  

Put your own userid (or maybe user number) in for "myuser".


Solution:3

rm doesn't read from stdin.

find -user $(whoami) -delete

Please always test without the delete first.


Solution:4

Try with find where you can search for files belonging to a user and then delete them:

find . -user *username* -delete   

More info: man find


Solution:5

rm does not accept a list of files to delete on the stdin (which is what you are doing by passing it through the pipe.

Try this

find . -type f -user username -exec rm -f {} \;  


Solution:6

You could use find:

find . -maxdepth 1 -type f -user myid -print0 | xargs -0 rm -f   

Drop the -maxdepth 1 if you want it to handle subdirectories as well.


Solution:7

Delete files of user_name from folder /tmp (you can replace this with your folder) older than 60 days - you ca use any date here but make sure you keep evidence in a deleted.txt file in user_name home folder:

find /tmp -user user_name -mtime +60 -exec rm -rfv {} \; >> /home/user_name/deleted.txt  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »