Skip to content
Advertisement

Bash No such file or directory error

When I do

for files in ~/Desktop/mydir/*.text; 
do
           mycommand  "$files" > "out.txt"
done

It works fine. But when I do the same thing with

mycommand "$files" > "~/Desktop/out.txt"

I get ~/Desktop/out.txt: No such file or directory. Why?

Advertisement

Answer

Take the tilde out of the quotes so it can be properly expanded:

mycommand "$files" > ~/Desktop/out.txt

You can also use ${HOME} if any subsequent part of your path contains spaces and you want to quote the whole path:

mycommand "$files" > "${HOME}/Desktop/out.txt"
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement