Linux Commands to Copy One File to Many Files

Linux commands to copy one file to many files

Does

cp file1 file2 ; cp file1 file3

count as a "one-line command/script"? How about

for file in file2 file3 ; do cp file1 "$file" ; done

?

Or, for a slightly looser sense of "copy":

tee <file1 file2 file3 >/dev/null

How to append contents of multiple files into one file

You need the cat (short for concatenate) command, with shell redirection (>) into your output file

cat 1.txt 2.txt 3.txt > 0.txt

linux commands to copy one file to many folders?

Not a single command, but you can easily do something like this (assuming your shell is bash):

for d in folder1 folder2 folderN ; do cp file $d/ ; done

Linux moving or copying multiple files with a shell

What about cp *.txt /dest/dir/?

And for adding .backup you could also do a loop that could look like this:

for i in *.txt
do
cp "$i" "/dest/dir/$i.backup"
done

Copy multiple files from one directory to another from Linux shell

I guess you are looking for brace expansion:

cp /home/ankur/folder/{file1,file2} /home/ankur/dest

take a look here, it would be helpful for you if you want to handle multiple files once :

http://www.tldp.org/LDP/abs/html/globbingref.html

tab completion with zsh...

Sample Image

How to copy multiple files from a different directory using cp?


cp ../dir5/dir4/dir3/dir2/file[1234] .

or (in Bash)

cp ../dir5/dir4/dir3/dir2/file{1..4} .

If the file names are non-contiguous, you can use

cp ../dir5/dir4/dir3/dir2/{march,april,may} .

Batch copy and rename multiple files in the same directory

There are going to be a lot of ways to slice-n-dice this one ...

One idea using a for loop, printf + brace expansion, and xargs:

for f in 01*.sh
do
printf "%s\n" {02..05} | xargs -r -I PFX cp ${f} PFX${f:2}
done

The same thing but saving the printf in a variable up front:

printf -v prefixes "%s\n" {02..05}

for f in 01*.sh
do
<<< "${prefixes}" xargs -r -I PFX cp ${f} PFX${f:2}
done

Another idea using a pair of for loops:

for f in 01*.sh
do
for i in {02..05}
do
cp "${f}" "${i}${f:2}"
done
done

Starting with:

$ ls -1 0*.sh
01a_AAA_qwe.sh
01b_AAA_asd.sh
01c_AAA_zxc.sh
01d_AAA_rty.sh

All of the proposed code snippets leave us with:

$ ls -1 0*.sh
01a_AAA_qwe.sh
01b_AAA_asd.sh
01c_AAA_zxc.sh
01d_AAA_rty.sh

02a_AAA_qwe.sh
02b_AAA_asd.sh
02c_AAA_zxc.sh
02d_AAA_rty.sh

03a_AAA_qwe.sh
03b_AAA_asd.sh
03c_AAA_zxc.sh
03d_AAA_rty.sh

04a_AAA_qwe.sh
04b_AAA_asd.sh
04c_AAA_zxc.sh
04d_AAA_rty.sh

05a_AAA_qwe.sh
05b_AAA_asd.sh
05c_AAA_zxc.sh
05d_AAA_rty.sh

NOTE: blank lines added for readability



Related Topics



Leave a reply



Submit