Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is cp: cannot stat error in Unix, I get this error when trying to copy thing from one folder to another

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?

like image 427
user5647516 Avatar asked Dec 13 '15 17:12

user5647516


People also ask

What is CP Cannot stat?

The error usually means the destination file or directory cannot be found by the system, so it cannot retrieve information. If you come across “cannot stat” with “No such file or directory” message, checks the destination path first and then the source path for their correctness.

What is function of cp command in Unix?

cp stands for copy. This command is used to copy files or group of files or directory. It creates an exact image of a file on a disk with different file name.


3 Answers

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
like image 200
isapir Avatar answered Oct 10 '22 05:10

isapir


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.

like image 13
xpusostomos Avatar answered Oct 10 '22 04:10

xpusostomos


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
like image 10
WesternGun Avatar answered Oct 10 '22 06:10

WesternGun