basename
Files and directories
Usage
basename [path] [suffix]Strips the directory part off a path and can drop a suffix too; it is pure string work and never checks whether the file exists.
Common flags
| Flag | Meaning |
|---|---|
| -a | Treat every argument as its own path |
| -s | Strip the given suffix from each result |
| -z | GNU: end each line with NUL instead of a newline |
Examples
basename /var/log/nginx/access.logPrints access.log.
basename report.tar.gz .gzPrints report.tar.
basename -s .md -a a.md b.mdPrints a and b.
Moving, deleting and finding. Some of these do not ask twice, which is why -i becomes a habit.
How to read this
- Square brackets [ ] mark a part you may leave out.
- An ellipsis … means you can list more than one.
- Flags are case-sensitive — in some commands -r and -R do different things.
Questions
Q. What does basename do?
Strips the directory part off a path and can drop a suffix too; it is pure string work and never checks whether the file exists.
Q. How do I type it?
basename [path] [suffix] — square brackets mark the parts you can leave out.
Q. How many flags are worth knowing?
3 are listed here; the full set is in man basename. This command sits under Files and directories.
Related commands
man page: man basename