Shell cp: cannot stat no such file or directory

Issue

I was trying to use cp to copy files from one directory to another by globing

for files in index/*
do
  file=$(echo $files|cut -d'/' -f2)
  cp -r "$files" ".target/file"
done

However, cp will give this warning if the directory is empty. I tried 2>/dev/null to mute this message but it did not work. I wonder how I could fix it.

Solution

What about this: (not tested)

find /index -maxdepth 1 -type f -exec cp {} .target/ \;
  • -maxdepth 1 : only look in this directory
  • -type f : only take the files
  • -exec cp {} .target/ \; : execute a "file copy" action

Answered By – Dominique

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published