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...
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
Matlab Mex Socket Wrapper Library
Bash Sort - How to Sort Using Timestamp
How to Connect a Shell to a Pseudo Tty
Crontab Is Not Working on Amazon Ec2 Server
Cuda Compiler Not Working with Gcc 4.5 +
Bash Echo with an $ Character Outside the String
Netfilter-Like Kernel Module to Get Source and Destination Address
Mercurial Hg No Changes Found - Can't Hg Push Out
Linux, Where Are the Return Codes Stored of System Daemons and Other Processes
Stty Serial Port Settings for Parity Not Persistent
Extending a Script to Loop Over Multiple Files and Generate Output Names
Perl Fails to Set Locale Even Though It Is Installed
Merge Files with Bash by Primary Key
Linux 64 Command Line Parameters in Assembly