Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a file exists with a filename containing spaces

Tags:

linux

bash

shell

I am testing in Bash for if a file is existing, where the file name is escaped using $(printf '%q' "$FNAME").

This always produces an error using if [ -f $FNAME ] as in the commented example below. How can I test for a filename that contains spaces and other characters?

#!/usr/bin/env bash

# code used in Raspberry Pi Podcasting Jukebox project
# youtube-dl -f 17 --get-filename https://www.youtube.com/watch?v=AgkM5g_Ob-w
# returns "HOW ABUNDANCE WILL CHANGE THE WORLD - Elon Musk 2017-AgkM5g_Ob-w.3gp"

# Purpose: To test if file exists before downloading
# for testing purposes using an existing regular file "abc def ghi"
AFILE="abc def ghi"
TFILE=$(printf '%q' "$AFILE") # Escaping filename using printf
echo $TFILE # returns abc\ def\ ghi
# if [ -f $AFILE ] # this test returns false every time with error [:too many arguments

if [ -f $TFILE ] # This test also returns FALSE with err [: too many arguments
then
  echo "Existing"
  # don't download
else
  echo "Not existing"
  # youtube-dl http://www.youtube.com/watch?v=AgkM5g_Ob-w
fi
like image 574
casperl Avatar asked Dec 13 '17 07:12

casperl


1 Answers

Always quote your file-name, the idea of using %q for escaping the spaces is right, but when used with the [ operator the unquoted $TFILE is split into multiple words causing the -f operand to receive too many arguments when it was actually expecting a single argument. So once you double-quote it, the white-spaces are preserved and a literal single argument is passed in the conditional.

testFile="abc def ghi"
printf -v quotedFile '%q' "$testFile"

if [ -f "$quotedFile" ]; then
    printf 'My quoted file %s exists\n' "$quotedFile"
fi

the above should apply well (the usage of [) in any POSIX compatible shells. But if you are targeting scripts for bash shell alone, you can use the [[ in which quoting is never necessary as it evaluated as an expression. So you can just do

file_with_spaces="abc def ghi"
if [[ -f $file_with_spaces ]]; then
    printf 'My quoted file %s exists\n' "$file_with_spaces"
fi

But in general it doesn't hurt to add quotes to variables in bash. You can always do it.

like image 139
Inian Avatar answered Oct 01 '22 07:10

Inian