2

I am creating a script in Centos 7 to move the latest file within a directory to another directory. The original directory that I'm copying from contains a valid file, however when I try to move or copy the file it errors out saying the file does not exist. I know the file does exist as I prove below. Why does it fail and what I can do to fix it?

If I run this line from my script in the shell the $( expands the output into the variable as expected:

NEW=$(ls -Art /home/user/directory/ | tail -1)

I can prove this to myself be echoing the value of the variable like so:
echo $NEW

file.tar.gz

Then I try to move the file to a different directory:

mv $NEW /usr/local/directory/

..and this is where I get the error. Note that the error message explicitly names the file it cannot find:

mv: cannot stat ‘file.tar.gz’: No such file or directory

The shell appears to be telling me that it can't find the file and then naming the file it can't find.

I have tried replacing the backticks with parentheses but same result. I have tried changing the permissions of both the file and the directories above it to pretty much every permutation I can think of and also changed ownership to user.user

I have tried running the command as both root and user, same result each time. I will appreciate any attempt to help resolve this.

  • What are the back slashes, and use of back ticks is deprecated, you should do NEW=$(ls -Art /home/user/directory/ | tail -1) – ctrl-alt-delor Aug 12 '15 at 15:02
  • I did not use back slashes originally, someone else kindly edited my question to include them. However I duly note your suggestion to use brackets – Nik-Nak Aug 12 '15 at 15:05
  • Back ticks are hard to do on stackoverflow as they are used to quote code. May be you question had a formatting error, that they tried to fix. – ctrl-alt-delor Aug 12 '15 at 15:13
6

It looks that you are not in the directory where file is.

You use

 ls -Art /home/user/directory/    

which return into NEW only the filename part, not the directory part.

Your move command should be

mv /home/user/directory/$NEW  /usr/local/directory/
  • Thank you Archemar, this has made me realize that I need to specify an exact location for the source file! It works! – Nik-Nak Aug 12 '15 at 14:28
  • Interestingly I have a similar problem with SCP giving me the same error, however I have three separate files to transfer and strangely enough two of them transfer fine using the original syntax. I'm wondering whether I should upload the script and error here or whether I should open a new question? – Nik-Nak Aug 12 '15 at 14:59
  • Post a new question if context is very different. – Archemar Aug 12 '15 at 16:59
2

Alternatively ;

NEW="/home/user/directory/"`ls -Art /home/user/directory/ | tail -1`

Then your mv command as usual.

mv $NEW /usr/local/directory/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.