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


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) Commented Aug 12, 2015 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
    Commented Aug 12, 2015 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. Commented Aug 12, 2015 at 15:13

3 Answers 3


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
    Commented Aug 12, 2015 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
    Commented Aug 12, 2015 at 14:59
  • Post a new question if context is very different.
    – Archemar
    Commented Aug 12, 2015 at 16:59

Alternatively ;

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

Then your mv command as usual.

mv "$NEW" /usr/local/directory/

If you try ls -Art /home/user/directory by itself, you will notice that it outputs only filenames with no directory path component. This means that your NEW variable lacks the /home/user/directory bit at the start of the pathname to the file.

Adding this to NEW would make your command work, assuming "nice" filenames (no newlines or other whitespace, nor any filename globbing characters), and assuming you're happy taking whatever filename that tail outputs, even if it's the name of a directory.

A better way to solve your issue of moving the most recently modified regular file from a directory to another is to use the zsh shell:

mv /home/user/directory/*(.Dom[1]) /usr/local/directory

The (.Dom[1]) is a globbing qualifier that makes the preceding pattern only match regular files (.), makes it possibly match hidden names (D, acts like the dotglob shell option in bash), orders the resulting list by modification timestamp (om), and picks out the first element from the sorted list ([1]).

From bash or any other shell:

zsh -c 'mv /home/user/directory/*(.Dom[1]) /usr/local/directory'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .