dirname $file
is what you are looking for
dirname $file
will output
stuff/backup
which is the opposite of basename
:
basename $file
would output
file.zip
Using ${file%/*}
like suggested by Urvin/LuFFy is technically better since you won't rely on an external command. To get the basename in the same way you could do ${file##*/}
. It's unnecessary to use an external command unless you need to.
file="/stuff/backup/file.zip"
filename=${1##*/} # file.zip
directory=${1%/*} # /stuff/backup
It would also be fully POSIX compliant this way. Hope it helps! :-)
For getting directorypath
from the filepath
:
file="stuff/backup/file.zip"
dirPath=${file%/*}/
echo ${dirPath}
Simply use $ dirname /home/~username/stuff/backup/file.zip
It will return /home/~username/stuff/backup/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With