Video summary
The `basename` command is a standard GNU core utility designed to simplify file path manipulation by stripping away directory components and suffixes from filenames. When executed without arguments, it returns an error because it requires at least one argument specifying a file or directory path. Its primary function is to isolate the final component of a given path, effectively removing all preceding directories to return just the name of the file or the rightmost directory in the case of a folder path.
This utility proves particularly valuable when writing scripts that process lists of files containing full paths, as it allows developers to extract only the essential filename without the unnecessary directory context. For instance, if provided with a complex path like `/home/dt/macho.sh`, the command can be instructed to remove the `.sh` extension by appending the suffix to the input, resulting in just `macho`. Similarly, when given a path consisting entirely of directories, such as `/directory one/directory two/directory three`, it returns only `directory three`, discarding all parent folders.
The interface for `basename` is intentionally straightforward, featuring very few flags or options compared to other system utilities, which makes it easy to learn and integrate into various automation tasks. Users who wish to explore its full capabilities or understand specific technical details can consult the manual page by typing `man basename` in their terminal. Ultimately, this simple tool serves as an efficient solution for cleaning up file paths and extracting specific naming components needed for system administration and scripting workflows.
Read the full video transcript
One of the standard GNU core utilities
is the base name command. What does base
name do? Well, it strips directories and
suffixes from file names. Let me show
you what I'm talking about here. If I go
into the terminal, if I type base name
without any other arguments and I just
hit enter, I get an error because base
name, you do have to give it the path to
some file or directory. For example, if
I type base name and I give it a path to
a particular file on the system, maybe I
go into this particular lengthy path
here to this file here called license.
It's actually one of my GitLab repos
here. And if I take the base name here,
what it will do is it will strip out all
the directories that were part of the
path and return just the file name by
itself. And this is very useful
sometimes when you're doing scripting.
Sometimes when you're scripting, often
times you're getting lists of files that
will include the full path, but you
don't need the full path. You just want
the base names. You can also use base
name on paths to directories if it's a
directory that is in the path rather
than a file. For example, if I do base
name and then directory one {slash}
directory two {slash} directory three,
what will that return? Well, it will
return directory three in this case, you
know, the right most directory and it
will strip out all of the parent
directories. And then one neat trick you
can do with base name, if you give it a
path to a file that has an extension.
For example, if I do base name and then
{slash} home {slash} dt {slash}
macho.sh.
This is a shell script that I have in my
home directory here. If I want to get
the base name of this file macho.sh, I
would just hit enter as is. But if I
want the file name without the
extension, I would do a space and then
.sh
and it will give me the base name, the
file name in this case, minus the .sh
and it just strips that away. So, there
you have it. Base name, it's a pretty
simple application. There's not a lot of
flags and options with it. If you do
want to learn more about basename, type
man basename in the terminal to read the
man page.