How to Check If Two Paths Are Equal in Bash

How to check if two paths are equal in Bash?

Bash's test commands have a -ef operator for this purpose

if [[ ./ -ef ~ ]]; then ...

if [[ ~/Desktop -ef /home/you/Desktop ]]; then ...

etc...

$ help test | grep -e -ef
FILE1 -ef FILE2 True if file1 is a hard link to file2.

Unix checking if two paths are the same

Use the inode numbers.

touch m n    # Create m and n
ln -s m o # Symlink o to m

ls -lLi m n o # Look at inodes of all files, see o and m are the same.
13132212 -rw-r--r-- 1 mark staff 0 10 Dec 15:18 m
13132213 -rw-r--r-- 1 mark staff 0 10 Dec 15:18 n
13132212 -rw-r--r-- 1 mark staff 0 10 Dec 15:18 o

So, if you want to get the inode numbers in a script, you could do this:

minode=$(ls -Li m | awk '{print $1}')
echo $minode
13132212

oinode=$(ls -Li o | awk '{print $1}')
echo $oinode
13132212

and test like this:

[ $minode -eq $oinode ] && echo equal

How to check if the folders are the same using variables in Bash?

If you only care about string equality, you can strip the trailing \ from VAR_LOG by using ${VAR_LOG%/} expansion (Remove matching suffix pattern). This will strip one (last / from VAR_LOG if present and leave it unchanged when not:

if [ "${VAR_LOG%/}" != "${PWD}" ]; ...

However, you can also (and probably should) use -ef test:

FILE1 -ef FILE2
FILE1 and FILE2 have the same device and inode numbers

I.e.:

if [ ! "${VAR_LOG}" -ef "${PWD}" ]; ...

Ensure both file/directory names refer to the same file.

This is not relevant for directories, but different filenames referring to the same inode (hardlinks) would evaluate to 0 on -ef test.

How to check if all the paths on every line of a file is a valid path in Bash?

Read man test for an explanation of

while read -r line; do
test -d "$line" || echo "$line is not valid"
done < my_paths_file

I used -d for a directory, alternatives are -f for regular files or -e for files and directories.

How to check if two variables in a shell script point to the same folder?


var1="/home/ps/temp/.."
var2="/home/ps/"


if [ "$(readlink -f "$var1")" == "$(readlink -f "$var2")" ];then
echo "they are pointing to same directory...."

else

echo "NOOOOO they are different"

fi

From man readlink :

-f, --canonicalize

canonicalize by following every symlink in every component of the given name recursively; all but the last component must exist

Detect if PATH has a specific directory entry in it

Something really simple and naive:

echo "$PATH"|grep -q whatever && echo "found it"

Where whatever is what you are searching for. Instead of && you can put $? into a variable or use a proper if statement.

Limitations include:

  • The above will match substrings of larger paths (try matching on "bin" and it will probably find it, despite the fact that "bin" isn't in your path, /bin and /usr/bin are)
  • The above won't automatically expand shortcuts like ~

Or using a perl one-liner:

perl -e 'exit(!(grep(m{^/usr/bin$},split(":", $ENV{PATH}))) > 0)' && echo "found it"

That still has the limitation that it won't do any shell expansions, but it doesn't fail if a substring matches. (The above matches "/usr/bin", in case that wasn't clear).



Related Topics



Leave a reply



Submit