tags:

views:

237

answers:

7

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...

+5  A: 
find . -user myuser -print0 |xargs -0 rm

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

Paul Tomblin
A: 

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

man find

so: find . -user uname -delete

A: 

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 {} \;
Stefano Borini
+3  A: 

rm doesn't read from stdin.

find -user $(whoami) -delete

Please always test without the delete first.

Matthew Flaschen
Note, add -type f if you only want to delete files.
Matthew Flaschen
Also possibly a -maxdepth 1 to only work in the current directory.
Sii
This is the most concise, but note that you have to put the directory in the command before the -user option, e.g. 'find . -user $(whoami) -delete'
Jay
You don't in GNU find. You may be right that the standard (http://www.opengroup.org/onlinepubs/009695399/utilities/find.html) requires it, though.
Matthew Flaschen
ah, good to know. In that case nevermind :-)
Jay
A: 

You're missing a xargs before the rm.

Sii
A: 

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.

CoverosGene
A: 

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.

stinkymatt
Thank you very much for the input. Worked great. It is less typing than the other answers, but I do appreciate those as well.