Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to run this command in background because the process could take a long time. How can I send it to a background process?

find /tmp/ -type f -mtime +3 | xargs rm -Rf

This does not work:

find /tmp/ -type f -mtime +3 | xargs rm -Rf &

How can I do this instead?

share|improve this question
    
Wait, your second example should work (except for the spaces problem slhck mentions). –  derobert Jul 24 '13 at 19:43
add comment

2 Answers

up vote 7 down vote accepted

If you want to run both in background, put them in a subshell:

(find /tmp/ -type f -mtime +3 | xargs rm -Rf) &

But, please, don't do this. Piping find output into xargs is unsafe unless you use the following options, which are supported in GNU and BSD find and xargs:

find … -print0 | xargs -0 …

If find returned files with spaces in their name you could – without even knowing – irreversibly delete the wrong folders. Carefully read the find manual and the section about deleting files for more info.

The safest way, in your case, would be:

find /tmp/ -type f -mtime +3 -delete &
share|improve this answer
add comment

Depending on the hardware that the filesystem is sitting on it may be faster to delete in parallel:

( find . | parallel -X rm ) &

In contrast to xargs GNU Parallel deals correctly with space, " and ' in file names. Only if you have file names containing newline you will need -print0/-0.

10 seconds installation:

wget -O - pi.dk/3 | bash

Watch the intro video for a quick introduction: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.