Why does my shell script give me this error?

Viewed 31

I have a shell script, that should move the most recently downloaded file to the current folder

#!/bin/zsh

lastfile=$(ls -t ~/Downloads | head -n 1)
mv "~/Downloads/${lastfile}" .

However, say the most recently dowloaded file in the Downloads folder is called test.png then running the script gives me the error

mv: rename ~/Downloads/test.png to ./test.png: No such file or directory

Anyone know why that is happening?

1 Answers

When using the mv command you can just specify the folder to which you wish to move the file to:

#!/bin/zsh

lastfile=$(ls -t ~/Downloads | head -n 1)
mv "~/Downloads/${lastfile}" .
Related