[SOLVED] cd command in shell script returns: No such file or directory
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
cd command in shell script returns: No such file or directory
I have a script that I am running that copies files from one source folder to a destination folder.
The script is in a crontab file it's only function is to copy files
Within the script, I perform a change directory to the /tmp folder and execute the cp command to copy the file
cd /tmp
cp cputest_donotremove.txt "/apps/opt/archive/tmp/demo/staging/encrypt01.tmp"
cp cputest_donotremove.txt "/apps/opt/archive/tmp/demo/staging/encrypt02.tmp"
This is the result:
[seeasown@sg3sacovn05 staging]$ ./movefiles.sh
: No such file or directory /tmp
Any idea why the cd command fails? the /tmp directory exists, in the correct case and spelling so that is not the issue. I've also tried
typing this using UltraEdit, notepad, and even vi editor on the linux box; Gives the same result.
Why is Linux not able to process the cd statement?
I've also tried typing this using UltraEdit, notepad, and even vi editor on the linux box; Gives the same result.
Why is Linux not able to process the cd statement?
Linux can process the cd command, I promise!
You say you've tried UltraEdit and notepad. Was your first attempt using a file created with either of those programs?
Windows and Linux use different line endings for files. Using a file with Windows line endings in a Linux environment can cause some strange errors. You can convert Windows line endings to Linux line endings with the dos2unix command in Linux.
When you tried using vi, did you create a new file from scratch, or did you load one of the files created with UltraEdit/notepad? If you opened one of the existing files, vi may have detected the line ending format and worked around it. If you created the file from scratch--completely from scratch--in vi, then line endings would not be the problem.
If you are unsure, copy paste this code directly into a native Linux editor, save it, make the file executable, and run it:
Code:
#!/bin/bash
cd /tmp
cp cputest_donotremove.txt "/apps/opt/archive/tmp/demo/staging/encrypt01.tmp"
cp cputest_donotremove.txt "/apps/opt/archive/tmp/demo/staging/encrypt02.tmp"
EDIT:
As an unrelated follow-on, be careful about commands and paths in cron files. The PATH environment variable for a cron job is very different than the PATH environment variable you have when typing commands at a terminal. It would be good for you to either (1) get in the habit of using full, absolute paths to any command you use in a cron script or (2) automatically adding a longer, more useful PATH declaration in your crontab file.
Last edited by Dark_Helmet; 02-06-2012 at 05:53 PM.
Hi Cory,
First, when you want to quote a script it is easy to make it clear if you use quote or code tags.
for example
Quote:
I have a script that I am running that copies files from one source folder to a destination folder.
The script is in a crontab file it's only function is to copy files
Within the script, I perform a change directory to the /tmp folder and execute the cp command to copy the file
cd /tmp
cp cputest_donotremove.txt "/apps/opt/archive/tmp/demo/staging/encrypt01.tmp"
cp cputest_donotremove.txt "/apps/opt/archive/tmp/demo/staging/encrypt02.tmp"
This is the result:
[seeasown@sg3sacovn05 staging]$ ./movefiles.sh
: No such file or directory /tmp
Any idea why the cd command fails? the /tmp directory exists, in the correct case and spelling so that is not the issue. I've also tried
typing this using UltraEdit, notepad, and even vi editor on the linux box; Gives the same result.
Why is Linux not able to process the cd statement?
Thanks,
Corey
Second: You might want to use the command
Code:
cat -vte ./movefiles.sh
to reveal any hidden characters that might be killing you.
Third: Why do a script from crontab with
Code:
cd /tmp
cp cputest_donotremove.txt "/apps/opt/archive/tmp/demo/staging/encrypt01.tmp"
cp cputest_donotremove.txt "/apps/opt/archive/tmp/demo/staging/encrypt02.tmp"
Third: Why do a script from crontab with
Code:
cd /tmp
cp cputest_donotremove.txt "/apps/opt/archive/tmp/demo/staging/encrypt01.tmp"
cp cputest_donotremove.txt "/apps/opt/archive/tmp/demo/staging/encrypt02.tmp"
instead of
Code:
cp /tmp/cputest_donotremove.txt /apps/opt/archive/tmp/demo/staging/encrypt01.tmp
cp /tmp/cputest_donotremove
Response:
The actual source path is /apps/fileport/opt/archive/tmp/demo/staging/ and the target is
/apps/fileport/opt/archive/tmp/demo/inbound/encrypt01.tmp
I just framed the question using tmp to avoid the response of someone asking to make sure the path is correct and in right case and so forth. So I just said /tmp to get around that.
The script when I use the cp command
cp /apps/fileport/opt/archive/tmp/demo/staging/encrypt01.tmp /apps/fileport/opt/archive/tmp/demo/inbound/encrypt01.tmp
is too long to fit on one line as you can see here and so for some God forsaken reason a /r gets appended to the target file so I end up with a file that looks like encrypt01.tmp? when I do an ls on the directory and it looks like encrypt01.tmp/r when I do a dir on the directory.
In other words the cp statment in the script has a return character and that becomes part of the target file name.
So to get around that whole mess I added the cd statement before the cp statment so that I could fit the cp on one line. I even tried making variables at the top of the script source=/apps/fileport/opt/archive/tmp/demo/staging/encrypt01.tmp and then using $source and got the same problem No such file or directory so when I edit the script in vi, and copy the path to the clipboard for sanity check, it works fine but when it is in the script, I get the No such file or directory. Strange.
I used cat -vte ./movefiles.sh to see if there were any nonviewable characters in the script and there weren't any. No trailing spaces on any line just the $ after the last character in the cd statment. Also I used dos2unix after moving the file over from notepad > filezilla client > Linux server. No change in result.
One thing I noticed was the error message looked like: No such file or directoryapps/fileport (where it hacked off the leading slash, it's as if it's treating the slash in front of apps like an escape character or something. I will give #!/bin/bash a try and see if that changes anything .
No more all nighters on this one, I'm desperate!! Any ideas? What is a newb to do
The application PATH dose make a BIG difference
in the first post it looked like a REQUIRED system folder is missing
Quote:
No such file or directory /tmp
for that to be missing there is something VERY wrong someplace
however
copying /apps/fileport/opt/archive/tmp/demo/staging/
to
/apps/fileport/opt/archive/tmp/demo/inbound/encrypt01.tmp
is a VERY different matter
do YOU ( as in your normal user account ) have read/write access to a folder mounted to /
namely "/apps "
in order to copy "/apps/fileport/opt/archive/tmp/demo/staging/encrypt01.tmp "
into "/apps/fileport/opt/archive/tmp/demo/inbound/encrypt01.tmp"
that script has to be owned by a user that HAS read/write access to "/apps/fileport/opt/archive/tmp/demo"
Folders in / are normally owned by root
chron should have the proper access set BUT what did YOU set the script as ?
who has the ownership of the script ?
Quote:
is too long to fit on one line as you can see here and so for some God forsaken reason a /r gets appended to the target file so I end up with a file that looks like encrypt01.tmp? when I do an ls on the directory and it looks like encrypt01.tmp/r when I do a dir on the directory.
what program are you using to write that ? MS windows notepad?
turn on the "show line numbers" for what ever it is
On MS windows i like SciTE
it is a nice FULL code editor http://www.scintilla.org/SciTE.html
also you can add a \ ( escape it)to the end of one long line to continue on the next
example
Code:
cp /apps/fileport/opt/archive/tmp/demo/staging/encrypt01.tmp /apps/fileport/opt/archive/tmp/demo/inbound/encrypt01.tmp
------------------
would look like this
-----------------
cp /apps/fileport/opt/archive/tmp/demo/staging/encrypt01.tmp \
/apps/fileport/opt/archive/tmp/demo/inbound/encrypt01.tmp
also if you use MS windows to edit a linux script
Windows WILL add all kinds of junk to the line endings
so USE "dos2unix"
and do not even think of using MS office to edit a script
Ok, you fooled me by providing sample lines completely different than the real lines, but many of the hints and comments above (both mine and from other contributors ) are still valid.
RE:
Quote:
The actual source path is /apps/fileport/opt/archive/tmp/demo/staging/ and the target is
/apps/fileport/opt/archive/tmp/demo/inbound/encrypt01.tmp
thus getting the advantages of full path names without generating very long command lines.
An alternate script might actually test and report issues, somewhat like this
Code:
#!/bin/bash
PATH=/bin:/usr/bin:/usr/local/bin
# expand on the path to reach all of the binaries you need, the above should be a good start.
BASE=/apps/fileport/opt/archive/tmp/demo
if [ ! -d ${BASE} ] ; then
echo "I cannot find the base folder ${BASE}!"
exit 1
fi
SRC=${BASE}/staging/encrypt01.tmp
TRG=${BASE}/inbound/encrypt01.tmp
if [ ! -f ${SRC} ] ; then
echo "The source file $SRC seems to be missing!"
exit 2
else
cp ${SRC} ${TRG}
fi
I want to sincerly thank all you guys! wpeckham's script solution worked like a charm. Nothing like getting the prompt back with no error messages after spending hours on this. Is there a good shell scripting book I can read or website to go to for more background on shell scripting?
I'm inspired to learn and share as you all have done and grateful the support and suggestions.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.