Bash - Path

1 - About

This article is about the path management in bash. ie /dir/childDir/fileName.

See also: Bash - pathname expansion (Filename expansion)

3 - How to

3.1 - get the name of the file


basename $path

3.2 - from relative to absolute


MYPATH="../whatever"
MYPATH=$(readlink -f ${MYPATH})

3.3 - is Absolute


if [[ "$PATH" = /* ]]; then
echo "absolute path";
else 
echo "relative path";
fi


case $PATH in
  /*) echo "absolute path" ;;
  *) echo "relative path" ;;
esac

3.4 - from Windows to Linux and vice-versa


cygpath -u path # from Windows to Linux
cygpath path # from Linux to Windows

3.5 - File or Directory


if [[ -d ${ARG} ]]; then
    echo "${ARG} is a directory";
elif [[ -f ${ARG} ]]; then
    echo "${ARG} is a file";
else
    echo "${ARG} is not a path";
    exit 1;
fi


Data Science
Data Analysis
Statistics
Data Science
Linear Algebra Mathematics
Trigonometry

Powered by ComboStrap