{"id":6607,"date":"2024-07-26T06:27:53","date_gmt":"2024-07-26T10:27:53","guid":{"rendered":"https:\/\/www.both.org\/?p=6607"},"modified":"2024-07-26T06:27:53","modified_gmt":"2024-07-26T10:27:53","slug":"how-to-use-the-basename-command","status":"publish","type":"post","link":"http:\/\/www.both.org\/?p=6607","title":{"rendered":"How to use the basename command"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"6607\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><\/div>\n<p>The <code>basename<\/code> command is a GNU core utility for UNIX and UNIX-like systems, and is designed to remove the file suffix and leading directories from a filename. It&#8217;s a reasonable alternative to at least some uses of the <code>cut<\/code> command. You&#8217;re not likely to use it alone, but it&#8217;s an interesting tool to have available to you when constructing a complex command.<\/p>\n\n\n\n<p>The simplest use for <code>basename<\/code> is retrieving just the name of a file, regardless of its path<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ basename ~\/Documents\/Work\/Example\/my_file.pdf\nmy_file.pdf<\/code><\/pre>\n\n\n\n<p>This makes parsing a full path a lot easier than <code>rev<\/code> and <code>cut<\/code> or <code>awk<\/code> alternatives.  I&#8217;ve used <code>basename<\/code> when searching for standardised file names across various storage devices. For instance, this command search my <code>storage<\/code> device for files called <code>reference-chart.webp<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ find \/storage -name `basename ~\/Assets\/reference-chart.webp`<\/code><\/pre>\n\n\n\n<p>It makes more sense when it&#8217;s part of a loop or script:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for FILE in ~\/Assets\/* ;\n  do find \/storage -name `basename $FILE` ; \n  done<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Get multiple filenames with basename<\/h2>\n\n\n\n<p>You can get the basename of several files with the <code>--multiple<\/code> (or <code>-a<\/code> for short) option.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ basename ~\/Documents\/my_file.odt ~\/Documents\/example.txt \nmy_file.odt\nexample.txt<\/code><\/pre>\n\n\n\n<p>The output is not sorted by default. The return values are in the order you provided. The <code>--zero<\/code> (<code>-z<\/code> for short) eliminates the newline, and returns the results with no break between them:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ basename --zero ~\/Documents\/my_file.odt ~\/Documents\/example.txt \nmy_file.odtexample.txt<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Drop a suffix with basename<\/h2>\n\n\n\n<p>The <code>--suffix<\/code> (<code>-s<\/code> for short) option for <code>basename<\/code> excludes the file extension from the command&#8217;s result. To drop the <code>.txt<\/code> extension from a file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ basename --suffix .txt ~\/Documents\/example.txt\nexample<\/code><\/pre>\n\n\n\n<p>Alternately, you can just provide the extension after the command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ basename ~\/Documents\/example.txt .txt\nexample<\/code><\/pre>\n\n\n\n<p>In practise, this is more useful when it&#8217;s part of a larger command. For example, I have a script that converts images from one format to another, according to a specified naming convention.<br>The destination name is dependent upon the existing project, however, the script first establishes the base format of the name, and then converts the image accordingly. Here&#8217;s a simplified version of the command without all the preamble:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>convert ~\/Promo\/12.png \\\n~\/Project\/`basename -s .jpg ~\/Project\/project_080124.jpg`.webp<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Basename is a parsing command<\/h2>\n\n\n\n<p>The <code>basename<\/code> command is meant for basic parsing. It&#8217;s essentially a string function for the shell. There are many other ways to get the same results as <code>basename<\/code>, often with less knowledge about the expected results. It&#8217;s probably a command we could all live without, but it&#8217;s still a handy option to have when faced with a problem. Sometimes, the easiest option is the one that seems the simplest. <\/p>\n\n\n\n<p>There&#8217;s a lot of simplicity in <code>basename<\/code>, but it can be pleasantly powerful when you find the right job for it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The basename command is a GNU core utility for UNIX and UNIX-like systems, and is designed to remove the file suffix and leading directories from a filename. It&#8217;s a reasonable alternative to at least some uses of the cut command. You&#8217;re not likely to use it alone, but it&#8217;s an interesting tool to have available to you when constructing a complex command.<\/p>\n","protected":false},"author":31,"featured_media":4259,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[5],"tags":[151,104,91],"class_list":["post-6607","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","tag-bash","tag-command-line","tag-linux"],"modified_by":"David Both","_links":{"self":[{"href":"http:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/6607","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"http:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6607"}],"version-history":[{"count":2,"href":"http:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/6607\/revisions"}],"predecessor-version":[{"id":6621,"href":"http:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/6607\/revisions\/6621"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/4259"}],"wp:attachment":[{"href":"http:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6607"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}