Cp: cannot stat 'New_Journal.odt': No such file or directory

This copies a file from my 18.04 to my 20.04 installation. When I do it manually.

cp New_Journal.odt /media/andy/5b4b2ae5-9aaa-4559-9f41-afb313998c75/home/andy/Documents/

It works fine.

Do I need to do something else so bash knows the directory is there?

Thanks.

./test.sh: line : ---------------------------------------------: command not found
cp: cannot stat 'New_Journal.odt': No such file or directory

#!/bin/bash 

UbuntuMate_20_04_ReminderFox_Dir="/media/andy/5b4b2ae5-9aaa-4559-9f41-afb313998c75/home/andy/.mozilla/seamonkey/7g4136n1.default/reminderfox/"

Documents_Folder_20_04="/media/andy/5b4b2ae5-9aaa-4559-9f41-afb313998c75/home/andy/Documents/"
---------------------------------------------

cd $DOCS
zip -u -q Ubuntu_Documents.zip *.txt *.doc *.rtf *.html *.png *.pdf *.odt *.ods *.odg *.csv *.xls *.jpg 
# Copy New_Journal.odt to the 20.04 installation
cp New_Journal.odt $Documents_Folder_20_04

Bash is looking for the 'New_Journal.odt' file in '/media/andy' and can not find it.

I persume you have 'New_Journal.odt' in "$DOCS", but you never told bash what "$DOCS" is.

So the command 'cd' gets executed with an empty argument (i.e. $DOCS), and it changes therefore to '/media/andy'

b.t.w. It is a good habit to always surround your variables with doublequotes (except in very very special cases) so that it is not susceptible to wordsplitting (like with filenames with spaces) and code injection. (see xkcd: Exploits of a Mom)

Also, if you want to get rid of 'command not found' , start the '- - - - - -' line with a '#'

happy hacking ! :slight_smile:

1 Like

Sorry my script posted did not include but is in my script.

DOCS="/home/andy/Documents" # shell variable

Do you mean this?
DOCS=""/home/andy/Documents"" # shell variable

No, that part is ok. It is already quoted :slight_smile:

I actually ment this:

cp New_Journal.odt $Documents_Folder_20_04

would be better like this:

cp New_Journal.odt "$Documents_Folder_20_04"

The same for:

cd $DOCS

would be better like this:

cd "$DOCS"

If you are sure that "New_Journal.odt" is in the directory "$DOCS", it should work.

Best to post the complete script, then maybe I can find out why it's not working.

1 Like

Well, I put extra quotes around my variables and it still works.

I fixed the error but forgot what I did. :slight_smile:

Getting a little old.

1 Like