Is there a way to copy files from folder a to folder b but have the process pause for a second between each file?
I want to copy a whole lot of files to a watch folder but I know from experience that if I copy all of them at once (either through terminal cp or GUI) it will make my rtorrent crash…
If i could copy the files with a second interval between them, that would give rtorrent the time to handle things. If I could do that from one command, that would save me from doing it one by one manually.
Ubuntu – Copy files from one folder to another but have an interval between each file
command linecopyscripts
Best Answer
Single command solutions
Among many options, including loops that make use of
cp
andsleep
, we could opt forfind
with two-exec
flags to run those exact commands.Here the options are:
~/dir_1
, the origin directory, through whichfind
works.-type f
get only files-maxdepth 1
descend down 1 level maximum in the directory tree; prevents descending into subdirectories-exec cp {} ~/dir_2 \;
copy the currently processed file designated by{}
parameter to~/dir_2
. The\;
designates forcp
to process single file at a time instead of tacking as many files as possible into{}
. The backslash in\;
is necessary to prevent shell from misinterpreting that as command terminator.exec sleep 1 \;
executessleep 1
per each current file.If you have a list of files in a file, you can do:
Options here are:
-I %
designates the symbol which refers to file within the command--arg-file=input.txt
read arguments to command from specific filesh -c 'cp % ~/dir_2/ ;sleep 1'
execute/bin/sh
with provided commands by-c '...'
flag;%
will be substituted with filename.Loop solutions
You could use a for loop for instance
For clarity, this is formatted on multiple lines, but this works as a single line just as well. You could also turn this into a function:
And use it as
Alternatively, if you want to copy only certain files, not everyting that
*
glob captures in the directory, you could always make use of pathname expansion, something likefile{1..25}
, or have an input text file, something like thisinput.txt
:And from there make use of
while IFS= read -r line
structure to read the file: