Bash get filename from given path on Linux or Unix

www.lautt‮ru‬i.com
Bash get filename from given path on Linux or Unix

To get the filename from a given path in Bash on a Linux or Unix-like system, you can use the basename command. This command will strip the directory path from the given path and return only the filename.

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

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 from the path and return only the filename, which is myfile.txt.

You can also use the basename command to get the filename from a path that ends with a / character, which indicates a directory. 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 path, and return only the last component of the path, 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