home | tags


queuing up a series of commands

tags: #cli #expert #neat-trick #linux

Sometimes I have a situation where I need to copy an arbitrary set of directories to an external disk or perhaps over a network, but I can’t start all the jobs at once because that would cause tremendous disk or network contention, and in the end it would be much slower than doing it sequentially.

At the same time, the source directories to be copied are not known in advance, otherwise I would just put all the commands in a script and run it.

Typically, I pick the directories to copy by browsing the relevant set of directories in my file manager, and I would like to start the copy as soon as I find the first candidate, and queue up the others after it as I come across a file or a directory that needs to be copied.

The simple trick here is to use the flock command, from the util-linux package. Here’s how I use it:

Note: you can pick some other file for the lock; I just picked one that is unique to each instance of vifm. If you want one unique to a user, just flock ~/.bashrc or something; it’ll work fine