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:
in vifm (my preferred file manager), I add the
following line to the vifmrc:
command! Cp :!flock /proc/$PPID/cmdline cp -a %f %D &
after that, hitting :Cp with the cursor on a file/dir
starts a background copy of the file/dir to the directory in the other
pane (as usual for 2-pane FMs)
most file managers will have some way to accomplish this. Worst
case, wrap that line in a script or a shell alias and run it at the
command line with appropriate arguments (e.g.,
queued-cp source-dir dest-dir, where queued-cp
is basically
#!/bin/bash flock /proc/$PPID/cmdline cp -a "$1" "$2"
keep doing that for each file/directory you want to copy, and they get effectively queued up because of the flock
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