Display what has been copied by `cp` (using `ksh`)

26,638

Solution 1

Like Lawrence has mentioned, you can use

cp -v

to enable "verbose" mode, which displays the files you copy. Something else that might be useful is

cp -v > foo

which will output the list of files to a file called foo. This is useful if you're going to copy a lot of files and you want to be able to review the list later.

Solution 2

cp -v enables verbose mode which displays what's being copied.

Solution 3

Check if your system has the -v option to cp.

If it doesn't, you can make a loop to show the file names and copy them one by one. This is not completely straightforward if you want to keep track of whether some copies failed.

err=0
for x in ./sourceDir/*; do
  echo "$x -> $destinationPath/${x##*/}"
  cp "$x" "$destinationPath/" || err=1
done
return $err

Alternatively, you might use a tool with many options such as rsync.

rsync -av ./sourceDir/ "$destinationPath/"

Going in the other direction, you might find it enough to see the expansion of the wildcard.

echo "Copying files:" ./sourceDir/*
cp ./sourceDir/* "$destinationPath/"

Or you might print a trace of shell commands:

set -x
cp ./sourceDir/* $destinationPath/
Share:
26,638

Related videos on Youtube

user1058398
Author by

user1058398

Updated on September 18, 2022

Comments

  • user1058398
    user1058398 almost 2 years

    I'd like to get output showing what has been copied by cp. The only problem is how to do it when I cp many files at a time. For instance, cp ./sourceDir/* $destinationPath/.

  • Davidson Chua
    Davidson Chua over 10 years
    +1 for suggesting what to do if the -v option is unavailable.