<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sergey Lukin</title>
    <description>The latest articles on DEV Community by Sergey Lukin (@sergeylukin).</description>
    <link>https://dev.to/sergeylukin</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F116170%2Fb2328ba1-4c29-4755-aca8-74be7b9a84e9.jpeg</url>
      <title>DEV Community: Sergey Lukin</title>
      <link>https://dev.to/sergeylukin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sergeylukin"/>
    <language>en</language>
    <item>
      <title>Renaming bulks of files in ZSH</title>
      <dc:creator>Sergey Lukin</dc:creator>
      <pubDate>Thu, 25 Nov 2021 01:50:23 +0000</pubDate>
      <link>https://dev.to/sergeylukin/renaming-bulks-of-files-in-zsh-46oc</link>
      <guid>https://dev.to/sergeylukin/renaming-bulks-of-files-in-zsh-46oc</guid>
      <description>&lt;p&gt;Recently I decided to clean my Netlify functions file names and instead of having &lt;code&gt;&amp;lt;function name&amp;gt;/&amp;lt;function name&amp;gt;.js&lt;/code&gt; files having &lt;code&gt;&amp;lt;function name&amp;gt;/index.js&lt;/code&gt; ones.&lt;/p&gt;

&lt;p&gt;As usual, assuming we're talking about using CLI, I could either complete this task by rapidly running &lt;code&gt;mv x/x x/index.js&lt;/code&gt; n times, or I could spend a bit more time fiddling with shell scripting, awk, sed, or any other relevant tool of choice and strengthen my CLI skills. The choice was obvious :)&lt;/p&gt;

&lt;p&gt;So assuming following files structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- my-func/
  - my-func.js
- my-func2/
  - my-func2.js
  - foobar.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We'd like to seamlessly transform it into (note that &lt;code&gt;foobar.js&lt;/code&gt; should stay untouched):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- my-func/
  - index.js
- my-func2/
  - index.js
  - foobar.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is my final one-liner (ZSH compliant):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in ./*/*.js; do if [[ $i:t:r == $(echo $i:h | cut -c3-) ]]; then mv -- "$i" "$i:h/index.js"; fi; done
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The interesting parts that required some research (in my case) are the :t :r :h modifiers.&lt;/p&gt;

&lt;p&gt;Turns out if you have path in variable, you can extract parts of it by using modifiers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$myFilePath:t -&amp;gt; prints only the filename with extension
$myFilePath:t:r -&amp;gt; prints only the filename without extension
$myFilePath:h -&amp;gt; prints path to the file
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so having this we can easily iterate through the files with &lt;code&gt;for in ./*/*.js&lt;/code&gt; and filter out only the files we need by comparing filename (&lt;code&gt;$i:t:r&lt;/code&gt;) with the parent directory name (&lt;code&gt;$i:h&lt;/code&gt;) and then moving the file to "$i:h/index.js".&lt;/p&gt;

&lt;p&gt;Mission accomplished + one more trick learned :)&lt;/p&gt;

&lt;p&gt;Happy coding and shell scripting :)&lt;/p&gt;

</description>
      <category>cli</category>
      <category>zsh</category>
      <category>unix</category>
    </item>
  </channel>
</rss>
