I am interested in modifying a bash script file whose purpose is to copy large amount of files to destination path. What i'm trying to achieve is count the number of files as they are being copied. How can i achieve the objective state above ?


I assume that your file names do not contain a newline.

cp -v file* targetdir | cat -n


     1 `file_123.png' -> `targetdir/file_123.png'
     2 `file_207.txt' -> `targetdir/file_207.txt'
     3 `file_44.png' -> `targetdir/file_44.png'

or only numbers (GNU sed):

cp -v file* targetdir | sed -n '/.*/='
  • does not work if you want to copy a whole directory to a target directory. – Sam Gomari Feb 23 '16 at 19:43
  • Cyrus - assuming gnu cp, it doesn't matter if there are newlines in file names as they're quoted so the wc is always right. – don_crissti Feb 23 '16 at 19:47
  • the objective is to copy the content of a directory into a target directory recursively, such that as the content of the directory "files" is being copied , the output of the command is written to a variable. – Sam Gomari Feb 23 '16 at 19:53
  • @SamGomari: cp -Rv sourcedir/* targetdir | cat -n should do the job. – Cyrus Feb 23 '16 at 20:02
  • 1
    @Cyrus I did the -av , and it worked . Thanks – Sam Gomari Feb 23 '16 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.