Getting an error cp: cannot stat when trying to copy files from one folder to another

BashUnixCommand LineCpMv

Bash Problem Overview


I have this directory called "mock", which contains 3 directories. I am trying to copy all the items from "mock" directory into the "projweek" directory using the following command:

cp  /mock/* ~/projweek

But I get this error:

> cp: cannot stat ‘mock/*’: No such file or directory

Any ideas as to why that is?

Bash Solutions


Solution 1 - Bash

If your source directory is set in quotes, then make sure that the * is outside the quotes, i.e.

cp "source/"* dest

or

cp "source"/* dest

Solution 2 - Bash

It's an odd thing about the unix system that glob expansion (aka use of the "*") is done by the shell, and not by the program you are calling, and furthermore, if the glob doesn't match anything, instead of expanding to nothing, it expands to itself and passes that to the program. So the cp command sees literally "/mock/*" which doesn't exist, because you have no file called "*". Somewhat perversely if you had a file called "*" it would dutifully copy it without complaining.

Solution 3 - Bash

cannot stat = file/dir does not exist. Check the path first.

And, you say you want to copy /mock but the error message says mock. Show the real code first.

When I test in ubuntu, cp (GNU coreutils) 8.28, I have no problem with copying all files under a dir to another dir, when both paths are correct.

root@DESKTOP-9NHNV2I:~# cp /root/temp/* /root
root@DESKTOP-9NHNV2I:~# ls
temp  test.txt  test2.txt  test3333.txt

Solution 4 - Bash

cp is used in unix/linux for copy

cp /mock/* ~/projweek this means copy from /mock folder all files to folder projweek that resides in root

This means cp: cannot stat ‘mock/*’: No such file or directory unable to copy all files from mock folder because file or directory not exists on relevant path

Solution 5 - Bash

cp: cannot stat ‘mock/*’: No such file or director
  1. Check that the files exist on the path.
  2. Also to copy all the files in a folder to another location, use . operator like: cp /source/. /dest/

Solution 6 - Bash

When I configured shell script on jenkins(See the following lines), I got this error "cp cannot stat ... No such file or directory".

ssh user@remoteNode
cd /somedir
cp fromdir/xxfile todir/xxfile

The following command solves my problem.

ssh user@remoteNode "cd /somedir; cp fromdir/xxfile todir/xxfile"

Why? Double quotation marks are required. If not, the cp command will be executed locally. Thanks to CDSN blogger Jinking01.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser5647516View Question on Stackoverflow
Solution 1 - BashisapirView Answer on Stackoverflow
Solution 2 - BashxpusostomosView Answer on Stackoverflow
Solution 3 - BashWesternGunView Answer on Stackoverflow
Solution 4 - Bashuser5575822View Answer on Stackoverflow
Solution 5 - BashCaffeine CoderView Answer on Stackoverflow
Solution 6 - BashTansy ZView Answer on Stackoverflow