LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   What does "cp: cannot stat" mean? (https://www.linuxquestions.org/questions/linux-general-1/what-does-cp-cannot-stat-mean-640617/)

homer_3 05-07-2008 03:47 PM

What does "cp: cannot stat" mean?
 
This error doesn't make sense to me. I'm trying to copy a file from one place to the other. It's a jar file with a name like [asdfasdf].jar. I don't know why it had [] around it, but it does. Whenever I try to copy it anywhere I get the error:

cp: cannot stat `[filename].jar': No such file or directory

Does anyone know how I can copy the file?

puntjuh 05-07-2008 03:54 PM

in the command line .. the following name.. with [ ] around it, are .. too keep it simple.. interpreted as spaces.. So you have to put a \ there.


like this: cp file\ name.jar

or in your case: cp \[filename\].jar

or cp [\filename]\.jar i'm not 100% sure at the moment. But anyway the \ is the way too go!

homer_3 05-08-2008 10:29 AM

Since I used tab to finish typing the file name it puts the \s in for me. So I am doing cp \[filename\].jar /destination.

b0uncer 05-08-2008 10:40 AM

The '[' and ']' are one examples of special characters that your shell (probably bash) tries to interpret unless you tell it not to. '{' and '}' are another example: to clarify this, the following two lines would be equivalent
Code:

ls file{1,2}.abc
ls file1.abc file2.abc

There are a lot of things like this - special ways to tell your shell's interpreter to do something "handy". The cost is that some characters are then interpreted in a special manner, and if they exist in a filename (they can exist there, it's just fine because it wouldn't be fair to disallow such filenames just because one shell couldn't deal with them) you need to tell the interpreter that it's part of a filename, not a "special command".

To tell bash, for example, that a string you type is a filename with possibly special characters inside and not a sequence of characters with some "command characters" to be interpreted, you can use (at least) two ways. One was described above: escape each "special" character with a leading slash:
Code:

cp \[file\].abc anotherfile.abc
cp file\ name\ with\ spaces\ in\ it.abc some\ other\ name.abc

The other way is to put (double-)quotation marks around the filename, which has the same effect:
Code:

cp "[file].abc" anotherfile.abc
cp "file name with spaces in it.abc" "some other name.abc"

At least in most cases the latter works, and is often a lot shorter to use. Especially with filenames with spaces it's more handy to type two quotation marks than a dozen slashes.

More:
Code:

man bash
and a few good websites. See linuxcommand.org for a start.

nar_adi 10-06-2009 12:57 PM

"cp: cannot stat"
 
Dear experts..

I hope you don't mind answering my question (so common in many forums) once again. "cp" command does not seem to work when used in a bash shell script. I agree it is something to do with the special characters. I just don't seem to get it! I saw a post on a similar one (w/ a jar file, which appears to be slightly different)

Following are the lines used in a bash script in Linux RHEL5.
#!/bin/bash
f1=/home/user/dir1/f1
cd=/home/user/dir1/sdir2/data/
cp $f1 $cd
cp: cannot stat `/home/user/dir1/f1\r': No such file or directory

Your response is very highly appreciated.

Thanks,
Naradi

AlucardZero 10-06-2009 02:14 PM

Sounds like your bash script is using Windows newlines instead of Unix. Convert them to Unix. If using vi, the command is ":set ff=unix"

tredegar 10-06-2009 02:18 PM

My guess is that you are running a script that was written with a windows editor (eg notepad) so it has incorrect End Of Line (EOL) characters for linux.

Retype the script with a linux editor, and you'll be OK.
Or search on the dos2unix and unix2dos utilities which will make the conversions for you.

In future, please don't post to threads that are more than a year old (like this one) - start a new one.

Welcome to LQ!

nar_adi 10-06-2009 03:49 PM

What does "cp: cannot stat" mean?
 
Thanks for your quick response... Now it makes sense..

Actually those escape characters come from extracted values stored in those variables (e.g. $f1); the extracted values were obtained from another file (an ASCII text) probably created using Windows based editor.

Is there a way to get rid of those escape characters...? I am not sure of the syntax.. I guess it would be something like ${f1//\x/} (for global replacement)

Thanks!
Naradi

tredegar 10-06-2009 03:59 PM

Quote:

Is there a way to get rid of those escape characters...?
I already gave you the name of the tool at post #7: dos2unix

nar_adi 10-06-2009 05:05 PM

I thought dos2unix takes care of files. How do I remove similar escape characters in variables? $var{//x/y} construct...

Thnks.

tredegar 10-06-2009 05:35 PM

Quote:

I thought dos2unix takes care of files. How do I remove similar escape characters in variables? ..... the extracted values were obtained from another file (an ASCII text) probably created using Windows based editor.
Maybe run that file through dos2linux before you extract the variables.

catkin 10-07-2009 02:50 AM

Quote:

Originally Posted by nar_adi (Post 3710214)
I thought dos2unix takes care of files. How do I remove similar escape characters in variables? $var{//x/y} construct...

Thnks.

Code:

"${var/$'\n'/}"

sunriseresources 10-18-2012 10:43 AM

Special Character in Filename
 
When working on a client site there were several files that contained special characters. This caused the error message of "cp: cannot stat".

An easy solution was to use wildcards for a portion of the filename that was OK, for example:

filenamewithspecialcharacters.pdf

cp ./*filenamewith* ./newfilename.pdf

After I verified that this found and copied the correct file, then I removed the old file:

rm ./*filenamewith*


All times are GMT -5. The time now is 11:31 AM.