Linux dirname Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the dirname command in Linux to extract the directory name from a given file path. The lab covers the purpose and syntax of the dirname command, as well as how to use it in combination with other Linux commands to retrieve the directory name. The lab provides practical examples to help you understand the usage of the dirname command in real-world scenarios.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/FileandDirectoryManagementGroup -.-> linux/pwd("`Directory Displaying`") linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") subgraph Lab Skills linux/cat -.-> lab-422640{{"`Linux dirname Command with Practical Examples`"}} linux/echo -.-> lab-422640{{"`Linux dirname Command with Practical Examples`"}} linux/cd -.-> lab-422640{{"`Linux dirname Command with Practical Examples`"}} linux/pwd -.-> lab-422640{{"`Linux dirname Command with Practical Examples`"}} linux/mkdir -.-> lab-422640{{"`Linux dirname Command with Practical Examples`"}} end

Understand the Purpose and Syntax of dirname Command

In this step, you will learn about the purpose and syntax of the dirname command in Linux. The dirname command is used to extract the directory name from a given file path.

The basic syntax of the dirname command is:

dirname FILE

Where FILE is the file path from which you want to extract the directory name.

For example, let's say you have a file path /home/labex/project/file.txt. If you run the dirname command on this file path, it will output the directory name:

$ dirname /home/labex/project/file.txt
/home/labex/project

As you can see, the dirname command has extracted the directory name /home/labex/project from the given file path.

You can also use the dirname command with variables or other Linux commands to get the directory name. For example:

$ file_path="/home/labex/project/file.txt"
$ dirname $file_path
/home/labex/project

In this example, we first store the file path in a variable file_path, and then use the dirname command to extract the directory name from the variable.

Retrieve the Directory Name from a File Path

In this step, you will learn how to use the dirname command to retrieve the directory name from a file path.

Let's start by creating a sample file path:

$ cd ~/project
$ touch file.txt
$ echo "This is a sample file." > file.txt
$ file_path=$(pwd)/file.txt
$ echo $file_path
/home/labex/project/file.txt

Now, we can use the dirname command to extract the directory name from the file_path variable:

$ dir_name=$(dirname $file_path)
$ echo $dir_name
/home/labex/project

As you can see, the dirname command has extracted the directory name /home/labex/project from the file path stored in the file_path variable.

You can also use the dirname command directly with a file path:

$ dirname /home/labex/project/file.txt
/home/labex/project

This will give you the same result as the previous example.

The dirname command is particularly useful when you need to perform operations on the directory part of a file path, such as changing the current working directory or creating a new directory.

Combine dirname with Other Linux Commands

In this step, you will learn how to combine the dirname command with other Linux commands to perform more complex operations.

One common use case is to change the current working directory to the directory containing a file. You can do this by combining dirname with the cd command:

$ file_path="/home/labex/project/file.txt"
$ cd $(dirname $file_path)
$ pwd
/home/labex/project

In this example, we first store the file path in the file_path variable. Then, we use the dirname command to extract the directory name, and pass it as an argument to the cd command to change the current working directory.

Another example is to create a new directory based on the directory name of a file path. You can do this by combining dirname with the mkdir command:

$ file_path="/home/labex/project/file.txt"
$ new_dir=$(dirname $file_path)/new_dir
$ mkdir $new_dir
$ ls -l
total 4
drwxr-xr-x 2 labex labex 4096 Apr 12 12:34 new_dir
-rw-r--r-- 1 labex labex   22 Apr 12 12:34 file.txt

In this example, we first use dirname to extract the directory name from the file_path variable. Then, we create a new directory name by appending /new_dir to the directory name. Finally, we use the mkdir command to create the new directory.

The dirname command can be combined with many other Linux commands, such as find, grep, sed, and awk, to perform more complex file and directory operations. The key is to understand how to use dirname to extract the directory name from a file path, and then use that information to perform the desired operation.

Summary

In this lab, you learned about the purpose and syntax of the dirname command in Linux, which is used to extract the directory name from a given file path. You practiced using the dirname command with file paths, variables, and other Linux commands to retrieve the directory name. You also learned how to combine the dirname command with other commands to perform more complex operations on file paths.

The key learning points from this lab include understanding the basic syntax of the dirname command, using it to extract the directory name from a file path, and integrating it with other Linux commands to automate common tasks related to file and directory management.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like