{"id":5252,"date":"2018-07-04T20:35:26","date_gmt":"2018-07-04T16:35:26","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-basename-command\/"},"modified":"2018-07-04T20:35:26","modified_gmt":"2018-07-04T16:35:26","slug":"linux-basename-command-tutorial-for-beginners-with-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-basename-command-tutorial-for-beginners-with-examples\/","title":{"rendered":"Linux basename Command Tutorial for Beginners (with Examples)"},"content":{"rendered":"<p>Sometimes, while working on the command line (especially when dealing with shell scripts), you might be interested in only the filename, but what&#8217;s available to you is the complete path of the file. So, the requirement is to strip the directory component of the path. You&#8217;ll be glad to know there exists a command &#8211; <strong>basename<\/strong> &#8211; that does this job.<\/p>\n<p>In this tutorial, we&#8217;ll be discussing the basics of &#8216;basename&#8217; using some easy to understand examples. But before we jump on to that, it&#8217;s worth mentioning that all examples here have been tested on an Ubuntu 16.04 LTS machine.<\/p>\n<h2 id=\"linux-basename-command\">Linux basename command<\/h2>\n<p>The basename command allows you to strip off components from filenames that aren&#8217;t required. Following is the tool&#8217;s syntax:<\/p>\n<p class=\"command\">basename NAME [SUFFIX]<br \/>basename OPTION&#8230; NAME&#8230;<\/p>\n<p>And here&#8217;s what the man page says about it:<\/p>\n<pre readability=\"7\">basename - strip directory and suffix from filenames<p>Print NAME with any leading directory components removed. If specified, also remove a trailing <br\/>SUFFIX.<\/p><\/pre>\n<p>Following are some Q&amp;A-styled examples that should give you a good idea on how the basename command works.<\/p>\n<h2 id=\"q-how-does-basename-command-work\">Q1. How does basename command work?<\/h2>\n<p>By default, if you run the &#8216;basename&#8217; command with a full path to a file as an input, the command returns the filename in output. For example, when I executed the following command:<\/p>\n<p class=\"command\">basename \/home\/himanshu\/Downloads\/analytics.pdf<\/p>\n<p>I got the following output:<\/p>\n<p class=\"command\">analytics.pdf<\/p>\n<h2 id=\"q-can-basename-handle-multiple-inputs\">Q2. Can basename handle multiple inputs?<\/h2>\n<p>Yes, it can. But for this to happen, you need to use the -a command line option. For example, when I executed the following command:<\/p>\n<p class=\"command\">basename -a \/home\/himanshu\/Downloads\/analytics.pdf \/home\/himanshu\/Pictures\/test.png<\/p>\n<p>And here&#8217;s the output I got:<\/p>\n<pre>analytics.pdf<br\/>test.png<\/pre>\n<h2 id=\"q-how-to-make-basename-strip-filenbspextension-as-well\">Q3. How to make basename strip file\u00a0extension as well?<\/h2>\n<p>Sometimes, you might only want to fetch the filename, but not its extension. Well, this can be done using the -s command line option (which requires you to pass the suffix as input).<\/p>\n<p>For example, the following command:<\/p>\n<p class=\"command\">basename -s .pdf \/home\/himanshu\/Downloads\/analytics.pdf<\/p>\n<p>produced this output:<\/p>\n<pre>analytics<\/pre>\n<h2 id=\"q-how-to-make-each-line-output-end-with-nul\">Q4. How to make each line output end with NUL?<\/h2>\n<p>By default, the newline character is used\u00a0as a separator in output. However, if you want, you can force basename to use NUL as the separator. This can be done using the -z command line option.<\/p>\n<p>For example:<\/p>\n<p class=\"command\">basename -az \/home\/himanshu\/Downloads\/analytics.pdf \/home\/himanshu\/Pictures\/test.png<\/p>\n<p>And here&#8217;s the output:<\/p>\n<pre>analytics.pdftest.png<\/pre>\n<p>So you don&#8217;t see the NUL character here. For that, you need to redirect the output to a file, and then open the file in an editor like <a href=\"https:\/\/www.howtoforge.com\/vim-basics\" target=\"_blank\" rel=\"noopener noreferrer\">vim<\/a>. Here&#8217;s what vim showed:<\/p>\n<p><a class=\"fancybox\" id=\"img-basename-nul\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/basename-nul.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/07\/linux-basename-command-tutorial-for-beginners-with-examples.png\" alt=\"\" title=\"\"><\/a><\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>As you can see, basename isn&#8217;t a very feature-rich command &#8211; the options it provides are limited, and most of those have been discussed here in this tutorial. Once you are done practicing these, head to the tool&#8217;s\u00a0<a href=\"https:\/\/linux.die.net\/man\/1\/basename\" target=\"_blank\" rel=\"noopener noreferrer\">man page<\/a> to learn more about it.<\/p>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<p>\n<a href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.howtoforge.com%2Flinux-basename-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/07\/linux-basename-command-tutorial-for-beginners-with-examples-1.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-basename-command%2F&amp;text=Linux+basename+Command+Tutorial+for+Beginners+%28with+Examples%29&amp;via=howtoforgecom&amp;related=howtoforgecom\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/07\/linux-basename-command-tutorial-for-beginners-with-examples-2.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/howtoforgecom\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/07\/linux-basename-command-tutorial-for-beginners-with-examples-3.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/plus.google.com\/share?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-basename-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/07\/linux-basename-command-tutorial-for-beginners-with-examples-4.png\" height=\"20\" alt=\"\" title=\"\"><\/a>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes, while working on the command line (especially when dealing with shell scripts), you might be interested in only the filename, but what&#8217;s available to you is the complete path of the file. So, the requirement is to strip the directory component of the path. You&#8217;ll be glad to know there exists a command &#8211; &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-5252","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/5252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=5252"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/5252\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=5252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=5252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=5252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}