Getting an error cp: cannot stat when trying to copy files from one folder to another [closed]
Asked Answered
W

6

98

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?

Wat answered 13/12, 2015 at 17:39 Comment(2)
In my case folder folder name was in correct :DSherlene
In my case source dir exist but emptySerena
G
130

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
Galvanotropism answered 9/7, 2018 at 6:45 Comment(6)
good to have this answer here, if you come by search, but not exactly the case of OP, as OP didn't use quotes. star inside double quotes "*" is taken literally , as a file with a star (*) in its name. End it does not exist.Gwynethgwynne
ok, could you please edit the answer, so it will be valid also for the OP's question?Gwynethgwynne
Note that what's true for * also goes for ~. For some reason the only way I could get cp "~/stuff" dest to work was cp "$HOME/stuff" dest.Schaper
This does not answer the question at all.Downbow
You can find several solutions to cp with * in a script here.Dingle
This answers the question in that OP should write cp "/mock/"* ~/projweek, which I'd never ever expected!Spinule
P
30

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.

Peanuts answered 5/8, 2020 at 12:46 Comment(2)
You blow my mind!Macpherson
You can find several solutions to cp with * in a script here.Dingle
B
12

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
Byroad answered 5/8, 2019 at 12:18 Comment(0)
M
6
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/
Maxie answered 12/7, 2021 at 19:30 Comment(0)
D
5

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

Discus answered 13/12, 2015 at 17:46 Comment(2)
So how can I am amend this? Both mock and projweek are in the root @LifeSaverWat
Try ls /mock. Do you get an error? If so, your path to /mock is incorrect. If not, make sure that /mock actually has files in it.Put
M
1

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.

Middlings answered 24/12, 2021 at 3:32 Comment(1)
This particular problem is a symptom of #37587311Mitochondrion

© 2022 - 2024 — McMap. All rights reserved.