Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I can (redirect) textual output to /dev/null in order to get rid of it. This is quite handy because this way I can easily enable/disable it in my bash code.

Now I have a script that processes files, and after it is done with them, moves them to another destination. An simple example would look like this:

for f in `ls $source_dir`; do echo $f; mv $f $target_dir; done

Now when I decide that I'm no longer interested in moving the files to the target dir, but to delete the files directement, what is the most efficient way to achieve this (in terms of lines of code to edit)?

I can think of putting the mv command into a variable as well:

for f in `ls $source_dir`; do $process_cmd $f; $finnish_cmd $f $target_dir; done

Then I could later change to something like:

for f in `ls $source_dir`; do $process_cmd $f; $finnish_cmd $f $target_dir; done

But I thought it would be even simpler, if it was possible to specify a /dev/null-like trash target directory that is automatically cleaned up eventually (that is sort of rm semantics with the mv command):

target_dir=/dev/null # <--- is there some sort of trash folder for such tasks?
for f in `ls $source_dir`; do $process_cmd $f; $finnish_cmd $f $target_dir; done

Is there a definitive way to go / a general best practice for a case like this?

share|improve this question
I genuinely don't understand why you are making things so complicated. Use rm. Directly. – Chris Down Jul 25 '13 at 12:01
And don't ever parse the output of ls with anything but your eyes. – msw Jul 25 '13 at 12:12
Move it to ramfs drive, it will disappear or reboot. – user84461 Sep 17 '14 at 12:21
up vote 3 down vote accepted

There is no such place for a fairly straightforward reason: there isn't any use for one. There is no situation I can think of where you can use mv globally but not rm. So while /dev/null exists because it meets a need, /dir/garbage does not because it would not.

Of course, you could say there is a need in this situation, to do with the issue: "what is the most efficient way to achieve this (in terms of lines of code to edit)?" Presuming the mechanism determining which files should be moved vs. deleted is whether target_dir is defined or not:

if [ -n "$target_dir" ]; then
    # move file
    # delete file

Pretty simple, and you don't have to re-edit anything (I presume by "edit" you didn't mean something about maintenance or reducing the number of lines, because doing something in a an awkward, hack, or incorrect manner in the name of "simpler maintenance" -- or worse yet, pseudo-cleverness -- is oxymoronic).

That said, you could fairly easily implement your own garbage directory by running a cron job like this once an hour:

for file in $garbage_dir/*; do
    rm -f -R $file;
share|improve this answer

I think you are simply looking for the /tmp directory. In most cases, anything you place there will be removed next time you reboot. The details depend on your specific OS and are governed by the value of the $TMPTIME variable. For many distributions, the default is clearing /tmp every boot because $TMPTIME is set to 0. In most (if not all) distributions, this is set in the file /etc/default/rcS.

So, just do something like this (note that I am using shell globbing and not parsing ls):

for f in $source_dir/*; do $process_cmd "$f"; $finish_cmd "$f" $target_dir; done
share|improve this answer
+1 for the globbing/quoting. But not every system deletes /tmp on reboot, or reboots at all for that matter. If you want something to be deleted directement (as the question states), rm it will have to be. – frostschutz Jul 25 '13 at 12:46
@frostschutz I added some more details on how/when the /tmp is cleared, but the OP wanted to "specify a /dev/null-like trash target directory that is automatically cleaned up eventually". That is exactly what /tmp is for. – terdon Jul 25 '13 at 13:00
Using /tmp as a dustbin sounds like a completely HORRENDOUS idea to me. It obviously is not intended for this purpose and you are putting a gun to your foot using it for such. – goldilocks Jul 25 '13 at 13:37
That is not at all what it is for. It is for storage of temporary files; tmp files are essentially a programming related conceit and they do have a purpose. They are not "things you don't care about that can be deleted without warning"; they are things whose use value will not continue beyond the runtime of the process which created them (hence, temporary), and they certainly should never ever be deleted "without warning". – goldilocks Jul 25 '13 at 13:59
The point is, it is not for garbage (none of the files you'll find there normally are garbage), and while this may work for some people in some situations, it is not a good solution: it's prone to overfilling /tmp (which may be a separate, smaller partition, or even in memory on some systems) and rendering it useless for its real purpose, and it is also a security concern (consider: "I'm done with these bank statements, I will just leave them on a park bench, because someone will clean them up eventually"). – goldilocks Jul 25 '13 at 14:37

Your Answer


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.