I have the below script which looks for the file in the current directory and evaluates to true if the file exists in the directory and evaluates to false if it does not
#!/bin/bash
printf "\n Please enter a file name "
read num3
if [ -f $num3 ]
then
printf "It is valid script "
else
printf "Invalid file name "
fi
How can i check for the presence of the files in some other directory rather than the present directory in which the script is written ?
5 Answers 5
Try this
#!/bin/bash
printf "\n Please enter a file name "
read num3
printf "\n Please enter the path to check "
read path2check
if find $path2check -name $num3 -print -quit |
grep -q '^'; then
echo "the file exists!"
else
echo "the file does not exist!"
fi
-
I understood it till -print can you please explain the logic from the -quit ?Rohit Saluja– Rohit Saluja2016年02月02日 16:22:09 +00:00Commented Feb 2, 2016 at 16:22
-
@Rohit
man find
Chris Davies– Chris Davies2016年02月02日 16:26:47 +00:00Commented Feb 2, 2016 at 16:26 -
-quit means that the find command will stop as soon as it finds the first instancercjohnson– rcjohnson2016年02月02日 16:27:44 +00:00Commented Feb 2, 2016 at 16:27
-
1Note that
-name "$num3"
(-name $num3
doesn't make sense) is not to find files whose name is$num3
, but files whose name matches the$num3
pattern.Stéphane Chazelas– Stéphane Chazelas2016年02月02日 16:38:18 +00:00Commented Feb 2, 2016 at 16:38 -
printf
there can be replaced withread -p 'Please enter a file name' num3
instead. Also you'd first run the check then command substitute thefind
as in< <( find ... )
Valentin Bajrami– Valentin Bajrami2016年02月02日 19:55:12 +00:00Commented Feb 2, 2016 at 19:55
[ -f $num3 ]
Doesn't make sense as you're applying the split+glob operator to the content of $num3
.
[ -f "$num3" ]
Would check whether the $num3
path (absolute if it starts with a /
, relative to the current working directory if not) resolves to a file that is of type regular or a symlink to a regular file.
If you want to check whether $num3
relative to a given directory is a regular file, just use:
dir=/some/dir
[ -f "$dir/$sum3" ]
You may want to check beforehand that $sum3
doesn't start with a /
or doesn't contain a /
.
Note that if $dir
is /
, that approach will not work on systems that treat //foo/bar
paths specially. So you may want to treat the dir=/
case specially.
case $dir in
/) file=$dir$num3
*) file=$dir/$num3
esac
[ -f "$file" ]
To check that $num3
is a relative path (to a regular file) of any directory in the directory tree rooted at the current directory, best would be to use zsh
instead:
files=(**/$num3(DN-.))
if (($#files > 0)); then
echo "Success: $#files such file(s) found"
else
echo Failure
fi
Consider this example where, i want to add a file if it is present at some location to my composite_firmware.bin image.
step1: export VSPA_IMAGE1=/path/where/image/is/located/file.bin from shell. step2: Add if file is found via script and append at location 0x200000 of composite_firmware.bin
if [ -f "$VSPA_IMAGE1" ]; then
printf "\nAdding $VSPA_IMAGE1 to composite image\n"
dd if=$VSPA_IMAGE1 of=composite_firmware.bin seek=2048 bs=1024
else
printf "\nWarning!!!! export VSPA_IMAGE1 location/path"
fi
If you provide the pathname to the file, your script will already work correctly.
If you don't want to provide a path, you can check the presence of the file in another directory simply by providing that path in your script
if [ -f "/other/dir/$num3" ]
then
echo "It is valid script"
else
echo "Invalid file name"
fi
You can use find command to search through the root directory and see if the file with given name is present or not. This will search through all the directories and sub directories directed from the root
!/bin/bash
printf "\n Please enter a file name "
read num3
if [ find / -type f -name $num3 2>/dev/null| wc -l
-gt 0 ]; then
echo "It is valid script"
else
echo "Invalid file name"
fi