Linux: Get Directory Name From File’s Absolute Path

For Linux newbie, Maybe you need to know how to get a file’s directory name from file’s full path(absolute path), in this post, you’ll see how to get directory name from file’s absolute path when deveoping a shell script on linux?

Linux Get Directory Name From Full Path


You can use linux dirname command to parse the directory name from file’s full path. the syntax is as followss:

dirname <full path>

​The below examples will show you to parse a files directory path from its full path.

[root@devops home]# dirname /home/adir/aa.txt 

/home/adir

You can use this dirname command in a shell script, see below:

#!/bin/bash

file=/home/adir/aa.txt

DirP= $(dirname $file)

echo "$DirP"

Done…

You might also like:

Sidebar



back to top