Reputation: 425
Given a filename (the presented as full name, namely, the path to the file from the current folder), how can I check if exists there (in the folder of the file "filename") , file with name f
?
Upvotes: 1
Views: 1374
Reputation: 531
So if I understand you correctly, you want to check for the existence of file "f" knowing the path to a neighbor file.
Here is a bash shell-script (let's call it "findNeighborFile.sh") that does the trick:
#!/bin/bash
neighbor=$1
target=$2
directory=$(dirname "${neighbor}")
if [ -f "$neighbor" ]; then
echo "$neighbor is present"
if [ -f "$directory/$target" ]; then
echo "$directory/$target is present"
else
echo "$directory/$target is not present"
fi
else
echo "$neighbor is not present"
if [ -f "$directory/$target" ]; then
echo "$directory/$target is present"
else
echo "$directory/$target is not present"
fi
fi
The script takes in two arguments: first the neighbor file path, second the target file you're looking for.
Let's say you have a directory called "test" located in the same directory as the script, and "test" contains two files "f1", "f2". now you can try different test-cases:
Both files exist:
./findNeighborFile.sh ./test/f1 f2
./test/f1 is present
./test/f2 is present
Target does not exist:
./findNeighborFile.sh ./test/f1 f3
./test/f1 is present
./test/f3 is not present
Neighbor does not exist:
./test/f3 is not present
./test/f2 is present
Neither file exists:
./findNeighborFile.sh ./test/f3 f4
./test/f3 is not present
./test/f4 is not present
Upvotes: 0
Reputation: 2166
if [[ -e $(dirname "${startname}")/"f" ]]; then
echo "exists"
fi
to check that f
exists in the same directory as ${startname}
is.
Upvotes: 0
Reputation: 1071
If what you want is, given "/long/path/name.txt", determine whether a file named "name.txt" exists in the current directory, then:
LONG=/long/path/name.txt
SHORT=${LONG##*/}
if [ -f "$SHORT" ]; then
echo file exists
else
echo file does not exist
fi
Upvotes: 1