From Funtoo
Jump to: navigation, search

The basename binary serves the function of finding the basename in a linux filesystem directory string. Such strings are made up of a dirname and a basename. For example, given the string /home/username/Documents/file, the basename is file and the dirname is /home/username/Documents. If you need to find the basename of a path, the basename binary can be very useful. In its most basic form, the command outputs the basename of one path. For example:

# basename /usr/src/linux

returns the string linux.

When dealing with files that end in a suffix, such as .c or .py, for example, it can be nice to removing the suffix when running basename. This can be accomplished by running basename with the -s flag:

# basename -s .py /home/user/

The above command returns the string amazingpython.

Finally, when working with lists of paths to find the basename of, it is necessary to invoke basename with the -a flag:

# basename -a /home/user/directory/file1 /home/user/file2 /bin/executable

The above returns file1 file2 executable.

Other resources

For more information about basename consider reading the basename(1) and basename(3) man pages.