How to Copy Multiple Files from a Different Directory Using Cp

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} .

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

Copy multiple file from multiple directories with new filename

Note that above $file is set only by the for file in ... ; do ... ;done loop, i.e. in your xargs cmdline you were just using the last leftover value from the loop.

Some things to consider:

  • need to process each file separately => use xargs -l1 (process each 1 line).

  • need to separate DIR/FILENAME as the needed command is something like 'cp $DIR/$FILENAME $DIR/prefix-01-$FILENAME' (and prefix-02 also), use find ... -printf "%h %f\n" for this

  • for each line, need to do couple things (prefix-01,02) => use a scriptlet via sh -c '<scriptlet>'

  • better skip prefix-0?-*.jpg files from find, to be able to re-run it without "accumulating" copies

A possible implementation would be:

find . -type f \( -iname "*.jpg" ! -iname "special-*.jpg"  ! -name "prefix-0?-*.jpg" \) -printf "%h %f\n" | \
xargs -l1 sh -c 'cp -v "$1/$2" "$1/prefix-01-$2"; cp -v "$1/$2" "$1/prefix-02-$2"' --

As xargs runs sh -c '<scriptlet>' -- DIR FILE for each line, the scriptlet will properly evaluate $1 and $2 respectively.

--jjo

PS: directory separator in Unix-like systems is / :)

[Update: fixed to use %f instead of %P, as per comments below]

How to copy a file to multiple directories using the gnu cp command

No, cp can copy multiple sources but will only copy to a single destination. You need to arrange to invoke cp multiple times - once per destination - for what you want to do; using, as you say, a loop or some other tool.

Linux cp command implementation to copy multiple files to a Directory

You would loop through all the argument values (from av[1] to av[ac - 2]) and copy it to the destination argument, which would be av[ac - 1].

In your case, you would pass av[i] and av[ac - 1] to the copyFiles function, where i would be your loop index.

How to copy multiple files from a different directory using cp, variable and brackets?

This answer is limited to the bash.

Prepend an echo to see what your cp command turns into:

echo cp $HOME/tools/{$FILES_TOOLS} $TOP_DIR/removeme

You have to insert an eval inside a sub-shell to make it work:

cp $( eval echo $HOME/tools/{$FILES_TOOLS} ) $TOP_DIR/removeme

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

How to copy a file with the same name from multiple directories into new directories using cp linux

If you have cp from GNU coreutils, which is highly probable since the question is tagged with linux, then:

cd path1 && cp --parents folder*/file.zip ../path2


Related Topics



Leave a reply



Submit