copy two files at a time
Solution 1
Assuming you want to cp
files into a directory, you can use the usual syntax for cp
:
cp mno.txt xyz.txt destination_directory
Or use brace expansion for brevity:
cp {mno,xyz}.txt destination_directory
For the sake of clarity, it is better to use the -t
(--target-directory
) option of cp
, this is GNU-ism:
cp -t destination_directory {mno,xyz}.txt
Just to note, if you want to cp
the contents of multiple files with one go of cp
, you can't. cp
deals with one file at a time when copying contents of one file to another.
Solution 2
if you want to copy them at the same location (not to a new directory) to make backups, (for example), you can use a very small for
loop to copy them with new names (here adding a .bak
extension)
for f in {mno,xyz}.txt; do cp -- "$f" "$f".bak; done
{
brace expansion}
is the most succinct way to specify the particular files in your example, but you can use any suitable shell wildcards/globbing, or list out the files if necessary: for f in foo bar baz;
Solution 3
Use cp -t destination_dir/ file1 file2
syntax.
Example:
bash-4.3$ ls dir1
file1 file2 file3
bash-4.3$ ls dir2/
bash-4.3$ cp -t dir2/ dir1/file1 dir1/file2
bash-4.3$ ls dir2
file1 file2
Addition to original answer.
The uses who like to play with python , may be interested in the following script, which allows copying arbitrary number of files specified on command line, with last argument being the destination.
Demo:
bash-4.3$ ls dir1
file1 file2 file3
bash-4.3$ ls dir2
bash-4.3$ ./copyfiles.py dir1/file1 dir1/file2 dir2
bash-4.3$ ls dir2
file1 file2
Script itself:
#!/usr/bin/env python3
from shutil import copyfile
from os import path
from sys import argv
new_dir = path.realpath(argv[-1])
for f in argv[1:-1]:
base = path.basename(f)
orig_file = path.realpath(f)
new_file = path.join(new_dir,base)
copyfile(orig_file,new_file)
Solution 4
You can do like this:
cp {mno,xyz}.txt /path/to/destination
Or if you need all .txt files:
cp {*}.txt /path/to/destination
Related videos on Youtube
Avani badheka
Updated on September 18, 2022Comments
-
Avani badheka almost 2 years
What to do if I want to copy two file at a time using command ? let's say I have one folder named
ABC
and files aremno.txt xyz.txt abcd.txt qwe.txt and so on (100 no. of files)
Now I want to cp
mno.txt
andxyz.txt
at a time . How can I do this ? -
Sergiy Kolodyazhnyy over 7 yearsDisregard the previous comment. I understand now you're referring to basically simultaneous copying of files. That indeed cannot be done, at least not with
cp
. My script also does each file one by one. Simultaneous copying would require a very sophisticated algorithm, at least on the level of TCP algorithm -
Carsten S over 7 yearsWhat does the script do that
cp
doesn't? -
Piskvor left the building over 7 years@Serg: Very sophisticated? As in
for fname in {mno,xyz}.txt ; do cp "${fname}" target_directory & done
? The&
launches command in background, which essentially means "run them all at once". Note that since you're copying to the same destination (and over the same channel), the speed of simultaneous copy would probably be the same as serial copy, minus overhead. TL;DR: It's easy - but pointless. -
Sergiy Kolodyazhnyy over 7 years@CarstenS it does exactly the same thing :) that's the whole point - provide alternative approach, different perspective on the same task.
-
Sergiy Kolodyazhnyy over 7 years@Piskvor well, that's essentially creating multiple processes in background. Good approach, no doubt ! But what i am referring to is processing multiple files from the same one. That's also what heemayl alluded to - cp iterates over command line arguments, which is an obvious solution. OP hasn't clearly stated if that's what they want.
-
heemayl over 7 years@Piskvor The thing you are doing clumsily in a loop
cp
is doing natively. I am not sure what you are trying to achieve here. Also, sending command in background is not necessarily simultaneous to multi-threading, also there is looping overhead involved. -
Sandeep over 7 yearsFor the sake of clarify, one can also simply add a
/
todestination_directory
, and there is no confusion. -
Sergiy Kolodyazhnyy over 7 yearsSomebody downvoted my answer.By definition, downvote means that answer isn't useful. I'd like to point out, that my answer provides a proper solution of using
cp -t DEST FILE1 FILE2 . . .
and extra material as well. Just because you don't like alternative solutions or extra material added to answers, doesn't make my answer not useful :) -
Piskvor left the building over 7 years@heemayl: I'm aware that cp can do that sequentially; was reacting to Serg's comment that it can't be done in parallel - note my observation that it's essentially pointless, as the bottleneck would be in the FS, not in "how quickly can you throw requests at the FS."