Search for and remove files safely

locatermxargs

I would like to search for all the nohup.out in my mounted filesystems, and delete them.

  1. There are some directories and files whose filenames contain space, so I think of xargs -0.

  2. To be safe, I would like to interactively delete them, i.e. be asked if I really want to delete them. So I think of rm -i and xargs -p

  3. I also would like to delete each found file seperately, so I think of xargs -n 1.

But the following command doesn't work in the way I hope.

locate  -i nohup.out | xargs -0 -n 1 -p rm -i

It doesn't prompt each file to be removed and ask for my permission.

I wonder why and what command works as I hope?

By the way my xargs version is xargs (GNU findutils) 4.4.2. Can it be the reason?

Best Answer

But the following command doesn't work in the way I hope.

locate -i nohup.out | xargs -0 -n 1 -p rm -i

Of course if you are using xargs -0 then you must match that with locate -0.

It doesn't prompt each file to be removed and ask for my permission. I wonder why

xargs should read input from /dev/tty when prompting you. It works that way so that there is no conflict between the use of xargs's stdin for reading arguments from locate and reading responses for the -p option.

However, you are using xargs to run rm -i. The rm -i command also wants to read your input, which it will read from stdin. Hence it eats up part of xargs's input and things don't work how you expect.

and what command works as I hope?

If you are using Bash, this is one option:

xargs -a <(locate -0 -i nohup.out) -0 -n 1 -p rm -i

However, you can also do everything in find:

find / -depth -name nohup.out -ok rm -i '{}' ';'

Change ';' to '+' to delete more than one file at a time. This requirement and the various ways you can solve this problem are explained in the Texinfo manual for findutils.

Related Question