LLI Wiki — My Digital Knowledge Base

Bash: get the directory and file name from a path

Use dirname and basename to split a path in a shell script.

To extract the directory or the file name from a path in a shell script, use dirname and basename.

$ VAR=/home/user/mydir/file.c
$ DIR=$(dirname "${VAR}")

$ echo "${DIR}"
/home/user/mydir

$ basename "${VAR}"
file.c
Mastodon LLIMastodon SocialFosstodon