I want to mv
the latest file to the specified directory.
ls -1t ~/Downloads | head -1 | gsed 's|^|~/Downloads/|g' | xargs -I {} mv {} ~/test/
Output:
mv: rename ~/Downloads/latest.jpg to /Users/home/test: No such file or directory
But following command does work:
mv ~/Downloads/latest.jpg ~/test/
So I think the reason it doesn’t work is the xargs command.
My environment is MacOS.
New contributor
user28644649 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
This is a proper way to do it.
Don’t parse ls
output like this.
array_list_temp=( * )
readarray -td '' array_list_temp_files < <(
perl -l0e '
print join "",
sort { -M $a <=> -M $b }
grep -f, @ARGV
' -- "${array_list_temp[@]}"
)
echo mv "${array_list_temp_files[0]}" ~ /test/
Remove echo
when happy with the output.
From https://unix.stackexchange.com/a/736246/12574