Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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:

source_dir=some/source/path
target_dir=some/target/path
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:

process_cmd=echo
finnish_cmd=mv
source_dir=some/source/path
target_dir=some/target/path
for f in `ls $source_dir`; do $process_cmd $f; $finnish_cmd $f $target_dir; done

Then I could later change to something like:

process_cmd=echo
finnish_cmd=rm
source_dir=some/source/path
target_dir=
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):

process_cmd=echo
finnish_cmd=mv
source_dir=some/source/path
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
9  
I genuinely don't understand why you are making things so complicated. Use rm. Directly. –  Chris Down Jul 25 '13 at 12:01
8  
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. –  domein Sep 17 at 12:21

2 Answers 2

up vote 2 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
else
    # delete file
fi

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:

garbage_dir=/usr/local/garbage
for file in $garbage_dir/*; do
    rm -f -R $file;
done
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):

process_cmd=echo
finish_cmd=mv
source_dir=some/source/path
target_dir=/tmp
for f in $source_dir/*; do $process_cmd "$f"; $finish_cmd "$f" $target_dir; done
share|improve this answer
2  
+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
3  
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
2  
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
1  
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

 
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.