The basename command
The basename command prints the last part of a path, the file’s name…
The basename command prints the last part of a path, the file’s name, and can strip a suffix such as .txt.
A practice shell in your browser that behaves like bash on Linux for these commands. Nothing you type leaves the page.
Usage
basename PATH [SUFFIX] basename -s SUFFIX PATH...
Strip the folders (and a suffix) from a path. Prints the last part of a path: the file’s name. Give a suffix such as .txt to remove that too. It only works on the text you give it; the file doesn’t have to exist.
How to use basename
Get the file name from a path
basename /home/learner/projects/app/main.jsmain.js
Remove the extension
basename /home/learner/notes.txt .txtnotes
basename -s .jpg beach.jpg cat.jpgbeach cat
Options
| -s SUFFIX, --suffix=SUFFIX | remove SUFFIX from every name (implies -a) |
|---|---|
| -a, --multiple | treat every argument as a path |
More examples
basename /home/learner/notes.txtnotes.txt
Questions about basename
Does the file have to exist?
No. basename and dirname only work on the text of the path.
Related commands
Every everyday command on one page, to print or keep open: the Linux commands cheat sheet.