Every file in Linux contains a path to mention where it belongs to. The path structure is /root/test_file.txt something like this. In this path /root is the directory where the file test_file.txt is resides. If you want to refer to this file in Linux use the full path as /root/test_file.txt. Regarding file path we have two concepts in Linux. One is absolute path and second one is relative path. These paths naming conventions we use while working with paths in Linux. If you want to open a file named access.log available in /var/log/ path, then use cat command to go to open the file as cat /var/log/access.log file. This is called using of absolute paths. For using relative paths concept take the same file name mentioned above. To open that access.log file in /var/log/ path use cd command to go inside the /var/log/ path and open the file name called access.log using the cat command.
Please check this article for more information about Linux filesystem hierarchy
If you want to print the file name from the absolute path or the full file path use basename built in command in Linux. This command will print the file name from a path mentioned. Let us check the basename command options available with their examples below.
Basename command syntax:
basename NAME [SUFFIX]
basename OPTION NAME
How to print filename or basename from a path:
To print a basename or file name from a specified path use basename command.
$ basename /var/log/httpd/access.log
access.log
How to print filenames from multiple file paths in Linux:
For suppose we have bunch of files containing there paths. To print only the file names from the file paths use -a or –multiple option in basename command as below.
$ basename -a /opt/data/test_file.txt /var/tmp/new_file.txt /tmp/reports/data.txt
test_file.txt
new_file.txt
data.txt
$ basename --multiple /opt/data/test_file.txt /var/tmp/new_file.txt /tmp/reports/data.txt
test_file.txt
new_file.txt
data.txt
How to print filenames from multiple paths mentioned in a file in Linux:
Till above example we are passing the file paths to basename command in the command line. For support if you have bunch file paths configured in a file and to print the basenames or file names we can print the file names by reading the file in basename command.
Below is the file content in /root/file_names_data.txt
$ cat /root/file_names_data.txt
/opt/test/file.txt
/var/log/message
/tmp/data/file.txt
/var/tmp/data.txt
Check this detailed article about all cat command options with examples in Linux
Printing the file names by reading file paths from a file in basename command as below.
$ basename -a $(cat /root/file_names_data.txt)
file.txt
message
file.txt
data.txt
How to print filename by removing their suffix in Linux:
By default basename command prints the file names from a path. For some of the files there are file extensions also available. If you don’t want to print file extensions in the output use -s or –suffix option in basename command.
$ basename -s .txt /opt/data/reports.txt
$ basename -s .csv /var/tmp/date_logs.csv
$ basename --suffix=.txt /var/log/reports.txt
Printing the file name by removing suffix as .txt
$ basename -s .txt /opt/data/reports.txt
reports
Printing the file name by removing suffix as .csv
$ basename -s .csv /var/tmp/date_logs.csv
date_logs
Printing the file name by removing suffix as .txt
$ basename --suffix=.txt /var/log/reports.txt
reports
How to print the filenames in a single line instead of new line in Linux:
By default file names generating from basename command will be displayed as one file name per line. If you want to print all the file names in a single line by removing or replacing new line character to NULL use -z or –zero option along with -a option to mention multiple files. If you are passing only one path to -z option then -a option is not required.
$ basename -za /opt/data/reports.txt /var/tmp/date_logs.csv /tmp/local_fle.txt
reports.txtdate_logs.csvlocal_fle.txt