dirname

dirname

The dirname command in Linux is used to extract the directory name from a given file path. The file path can be absolute or relative. The dirname command will remove the last component of the file path and print the remaining path to the standard output.

The dirname command is used in the following syntax:

dirname [options] file_path

The file_path is the path to the file that you want to extract the directory name from.

The options can be used to specify the following:

  • -s : Print the directory name without trailing slashes.

  • -v : Print the directory name and the file path.

For example, the following code will extract the directory name from the file path /home/user/Documents/file.txt:

dirname /home/user/Documents/file.txt

This code will print the directory name /home/user/Documents to the standard output.

The dirname command is a simple and useful command that can be used to extract the directory name from a file path. It is a valuable command to know, especially if you need to use the directory name in other commands or scripts.

Here are some additional things to note about the dirname command:

  • The dirname command can be used to extract the directory name from any file path.

  • The dirname command is a simple and useful command.

  • The dirname command is available on all Linux distributions.

I hope this helps! Let me know if you have any other questions.

help

Last updated