Ad space (banner)
🖥️Linux Lessons
Lesson 46 / 61

Extracting Just the Filename with basename

basename is for when "I want to extract just the filename part from a long file path." basename takes the given path and extracts only the trailing filename (or folder name) part.

Use it in the form basename path. To get just the filename with the extension stripped off too, give a second argument, like basename path extension.

Running basename /home/user/project/notes.txt extracts and displays just notes.txt, the trailing part of the path.

A common early mistake is confusing this with dirname (getting the folder part), covered next. It helps to remember: basename is the tail end, dirname is the front end.

In real work, this is used a lot inside shell scripts, to extract just the extension or filename from a given file path and branch processing accordingly.

Linux
Try it (type this into the pseudo terminal)
basename /home/user/project/notes.txt

🧑‍💻 You can type this command into the Linux pseudo terminal to try it yourself (this page itself has no interactive terminal).

Ad space (banner)
Ad space (in-article)