Bash get basename of filename or directory name

www.lau‮tt‬uri.com
Bash get basename of filename or directory name

To get the basename of a filename or directory name in Bash, you can use the basename command. This command will strip the directory path and any suffix from the given filename or directory name, and return only the base name.

Here is an example of how to use the basename command to get the basename of a filename:

basename /path/to/myfile.txt

In this example, the basename command is used with the /path/to/myfile.txt argument, which is the path to the file. The command will strip the directory path and the .txt suffix from the filename, and return only the base name, which is myfile.

You can also use the basename command to get the basename of a directory name. For example:

basename /path/to/mydir/

In this example, the basename command is used with the /path/to/mydir/ argument, which is the path to the directory. The command will strip the parent directory path and the trailing / character from the directory name, and return only the base name, which is mydir.

Note that the basename command only works with regular files and directories. It does not work with special files such as symbolic links or block devices. Consult the basename command's documentation for more information.

Created Time:2017-10-16 14:38:39  Author:lautturi