I am writing an sh file I have this command

cp test/*.txt NewDirectory/

from my understanding it will go one by one in the test folder


but won't copy


because they are not there

I need it to print out or in a log file the files it couldn't find or couldn't copy to the new folder. How do I do that ?

  • It's quite unclear what you are asking for, so update your question and comment on already proposed answers. Commented Mar 8, 2016 at 7:04

2 Answers 2


Your understanding is incorrect. If there are only 1,2,5,6 there will be no error message in case those files are copied successfully. IF your need is to parse N number or txt files in folder and see which does not exist you can do something like: for x in {1..10}; do cp $x.txt NewDirectory/;done .Further you can append > output.log to redirect the command output to file.


with cp test/*.txt NewDirectory/ 2>./output.log

but the cp with * command will have to copy the files that exist and so do not show display error if files do not exist

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .