{"id":1737,"date":"2022-02-12T19:53:31","date_gmt":"2022-02-12T14:23:31","guid":{"rendered":"https:\/\/smarttech101.com\/?p=1737"},"modified":"2023-03-25T00:53:37","modified_gmt":"2023-03-24T19:23:37","slug":"basename-dirname-directory-name","status":"publish","type":"post","link":"https:\/\/smarttech101.com\/basename-dirname-directory-name\/","title":{"rendered":"How to get the basename and directory of a file in Linux"},"content":{"rendered":"\n
Getting the basename of a file (ex – \/usr\/file.mp4’s basename is file.mp4) and the directory, it is in, is very important especially while writing shell scripts. In this article, we will explore how we can do this using basename <\/strong>and dirname<\/strong> commands. At the same time, I will also mention a few basic applications.<\/p>\n\n\n\n Output:<\/p>\n\n\n\n Application:<\/strong> It is highly useful in getting the basename of a script it is used in. I use this to write ‘helps’ in my scripts.<\/p>\n\n\n\n After executing the above script, we get<\/p>\n\n\n\n You can use Output:<\/p>\n\n\n\n You can notice that each output line (in the above example, Output:<\/p>\n\n\n\n You need to append the suffix (i.e. the file extension) next to the file name. Consequently, it will remove the extension along with the directory name.<\/p>\n\n\n\n Syntax:<\/p>\n\n\n\n Example:<\/p>\n\n\n\n Output:<\/p>\n\n\n\n Alternatively, you can use Syntax:<\/p>\n\n\n\n Example:<\/p>\n\n\n\n If NAME contains no \/’s, output is ‘.’ meaning the current directory. For example, <\/p>\n\n\n\n Output:<\/p>\n\n\n\n Application: <\/strong>dirname is used very often to move in the directory of the script. For example, in my rclone.sh script, I use the following command for rclone’s logs.<\/p>\n\n\n\n Just use the names of all the files separated by space. <\/p>\n\n\n\n Output:<\/p>\n\n\n\n In the above output, as you can see, the lines are separated by the newline character. Just like basename, to remove the newline character, use Output:<\/p>\n\n\n\nTable of Contents<\/h2>\n\n\n\n
\n
How to find a basename of a file<\/h2>\n\n\n\n
~$ basename \/usr\/bin\/sort<\/code><\/pre>\n\n\n\n
sort<\/code><\/pre>\n\n\n\n
~$ cat time.sh\n\n#!\/bin\/bash\n\nhelp_page(){\ncat << document\n...\nexample: $(basename \"$0\") -t 30m 10m\n...\ndocument\n}\n\nhelp_page<\/code><\/pre>\n\n\n\n
...\nexample: time.sh -t 30m 10m\n...<\/code><\/pre>\n\n\n\n
How to get basename of multiple files at once<\/h2>\n\n\n\n
--multiple<\/code> or
-a<\/code><\/p>\n\n\n\n
~$ basename --multiple dir\/file1 dir\/file2<\/code><\/pre>\n\n\n\n
file1\nfile2<\/code><\/pre>\n\n\n\n
file1<\/code> and
file2<\/code>) is appended with the newline character (
\\n<\/code>). To remove the
\\n<\/code>, use
--zero<\/code> or
-z<\/code>.<\/p>\n\n\n\n
[ajay@lenovo ~]$ basename --zero --multiple dir\/file1 dir\/file2<\/code><\/pre>\n\n\n\n
file1file2[ajay@lenovo ~]$<\/code><\/pre>\n\n\n\n
How to remove the extension of a file in Linux<\/h2>\n\n\n\n
~$ basename NAME [SUFFIX]<\/code><\/pre>\n\n\n\n
~$ basename dir\/file.mp4 .mp4<\/code><\/pre>\n\n\n\n
file<\/code><\/pre>\n\n\n\n
--suffix=<your_extension><\/code> as well. Here, -s is the same as
--suffix<\/code>. <\/p>\n\n\n\n
~$ basename --suffix=.mp4 dir\/file.mp4<\/code><\/pre>\n\n\n\n
How to find directory of a file using dirname in Linux<\/h2>\n\n\n\n
~$ dirname [OPTION] NAME...\n<\/code><\/pre>\n\n\n\n
~$ dirname '\/home\/ajay\/Documents\/Notes\/Personal Notebooks'\n\n\/home\/ajay\/Documents\/Notes<\/code><\/pre>\n\n\n\n
~$ dirname file.mp4<\/code><\/pre>\n\n\n\n
.<\/code><\/pre>\n\n\n\n
How to find directories of multiple files<\/h2>\n\n\n\n
~$ dirname 'dir1\/file1' 'dir2\/file2'<\/code><\/pre>\n\n\n\n
dir1\ndir2<\/code><\/pre>\n\n\n\n
--zero<\/code> flag.<\/p>\n\n\n\n
[ajay@lenovo ~]$ dirname --zero 'dir1\/file1' 'dir2\/file2'<\/code><\/pre>\n\n\n\n
dir1dir2[ajay@lenovo ~]$ <\/code><\/pre>\n\n\n\n
Conclusion<\/h2>\n\n\n\n