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:
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.