Search code examples
bashls

Bash ls command not working in shell script


I'm writing a shell script, and it also has to check if a file with a certain extension exists in a different directory. I'm running an ls command like this:

TEMPLATE_FOLDER="$HOME/Downloads/" # Folder to check
file="py" # Extension only
amount_files=$(ls -1 "${TEMPLATE_FOLDER}*.${file}" 2>/dev/null | wc -l) # How many files in the directory

It keeps telling me that ls: /Users/hussein/Downloads/*.py: No such file or directory even though when I copy that exact path into the terminal itself, it works perfectly fine.

If anyone can help with this, I would appreciate it very much.


Solution

  • ls -1 "${TEMPLATE_FOLDER}*.${file}"
    

    Globs are not expanded if they're quoted. Try:

    ls -1 "${TEMPLATE_FOLDER}"*."${file}"