Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a file exists inside a "Variable" Path

I am trying to find if a file exist in an iPhone application directory

Unfortunately, apps directory differs from a device to another

On my device, i use the following command to see if the file exists:

if  [[ -f "/var/mobile/Applications/D0D2B991-3CDA-457B-9187-1F02A84FF3AB/AppName.app/filename.txt" ]]; then 
    echo "The File Exists"; 
else
    echo "The File Does Not Exist";
fi 

I want a command that would automatically search if the file exist without the need to specify the "variable" name inside the path.

I tried this:

if  [[ -f "/var/mobile/Applications/*/AppName.app/filename.txt" ]]; then 
    echo "The File Exists"; 
else 
    echo "The File Does Not Exist"; 
fi 

But no luck, it didn't find the file,

Maybe because i have 2 path of /var/mobile/Applications/*/AppName.app/ since i have cloned the app.

I would like to get a way to be able to find if the file filename.txt exists inside any folder named AppName.app inside this directory /var/mobile/Applications/*/

like image 578
Fouad Avatar asked May 09 '26 05:05

Fouad


2 Answers

You can do this as follows:

[[ $(find /var/mobile/Applications/*/AppName.app/ -name filename.txt -print -quit | wc -l) -gt 0 ]] && echo "The File Exists" || echo "The File Does Not Exist"
like image 194
Sergey Evstifeev Avatar answered May 11 '26 02:05

Sergey Evstifeev


The -f test can only take one argument. You would need to put it in a loop to check if some glob exists and its matches some regular file, i.e.

shopt -s nullglob
found=
for file in /var/mobile/Applications/*/AppName.app/filename.txt; do
  [[ -f $file ]] && found=: && break
done
[[ -n $found ]] && echo "The File Exists" || echo "The File Does Not Exist"

If you're not sure specifically where the file is located you can use find, doing something like below which will exit early if found. (should work for gnu find, haven't tested on bsd)

if [[ -f $(find /some_root_directory -type f -name 'filename.txt' -print -quit) ]]; then
    echo "The File Exists"
else
    echo "The File Does Not Exist"
fi
like image 34
Reinstate Monica Please Avatar answered May 11 '26 00:05

Reinstate Monica Please



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!