Bash get basename of filename or directory name

In Bash, you can use the basename command to get the base name of a file or directory name.

The basic syntax is as follows:

basename <path>

For example, to get the base name of a file called /path/to/myfile.txt, you can use the following command:

basename /path/to/myfile.txt

This command will return “myfile.txt”

You can also specify the suffix to be removed from the end of the file name. For example, to remove the “. (https://spellpundit.com) txt” from the above example:

basename /path/to/myfile.txt .txt

This command will return “myfile”

You can also use the dirname command which returns the directory name of a file or directory path.

For example, to get the directory name of a file called /path/to/myfile.txt, you can use the following command:

dirname /path/to/myfile.txt

This command will return “/path/to”

You can also use these commands in combination with variables or command substitution to manipulate file or directory names in a script or shell command.

Leave a Comment