<?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: Tsuji Daishiro</title>
    <description>The latest articles on DEV Community by Tsuji Daishiro (@tutuz).</description>
    <link>https://dev.to/tutuz</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%2F375781%2F1b85bb4b-1f42-4b4f-aabf-b7e1ac9edc57.jpeg</url>
      <title>DEV Community: Tsuji Daishiro</title>
      <link>https://dev.to/tutuz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tutuz"/>
    <language>en</language>
    <item>
      <title>How to hacking terminal with Bash</title>
      <dc:creator>Tsuji Daishiro</dc:creator>
      <pubDate>Thu, 30 Apr 2020 14:54:04 +0000</pubDate>
      <link>https://dev.to/tutuz/how-to-hacking-terminal-with-bash-55ho</link>
      <guid>https://dev.to/tutuz/how-to-hacking-terminal-with-bash-55ho</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;When a super hacker is logged in on the server and is doing something, I want to hack the terminal, and you maybe want to hack the terminal also.&lt;/p&gt;

&lt;p&gt;Other purpose of looking at the terminal is to visually check that commands are typed correctly on the server by several people. But, when there is a physical distance, it is difficult to project the screen on the monitor.&lt;/p&gt;

&lt;p&gt;One way to do this is to ask to see the terminal, but it's smart to have a little tool to reference it. So, I made a ttycopy that runs in a shell.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--81WfkoyC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/24369487/80723449-9d808b80-8b3b-11ea-9425-8f42ff0f16f0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--81WfkoyC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/24369487/80723449-9d808b80-8b3b-11ea-9425-8f42ff0f16f0.png" width="35%"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/d-tsuji/ttycopy"&gt;https://github.com/d-tsuji/ttycopy&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It runs in the Bash, so you can copy and paste a ttycopy implementation to make it work casually.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sample
&lt;/h2&gt;

&lt;p&gt;ttycopy works as follows. In fact, there are two terminals, and after ttycopy, the operation of the other terminal is shown.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fmuDZZIT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://user-images.githubusercontent.com/24369487/80721827-a6705d80-8b39-11ea-89d6-bfcb96234668.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fmuDZZIT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://user-images.githubusercontent.com/24369487/80721827-a6705d80-8b39-11ea-89d6-bfcb96234668.gif" alt="ttycopy"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Detail
&lt;/h2&gt;

&lt;p&gt;I would like to tell you how this is being processing. The basic flow of the process is as follows.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Check the PID of the tty's login process&lt;/li&gt;
&lt;li&gt;Peering into a system call using the strace command against PID&lt;/li&gt;
&lt;li&gt;Print a formatted string of the read system call output by strace&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Implementation with Bash
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1.Check the PID of the tty's login process
&lt;/h3&gt;

&lt;p&gt;The shell implementation is as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;pid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;ps fauwwx | &lt;span class="nb"&gt;grep &lt;/span&gt;sshd.&lt;span class="k"&gt;*${&lt;/span&gt;&lt;span class="nv"&gt;tty&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt; | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="nb"&gt;grep&lt;/span&gt; | &lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s1"&gt;'s/^[a-zA-Z0-9]\+[ \n\r\f\t]\+\([0-9]\+\).*/\1/'&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This captures the PID using a regular expression with a string of the following process tree that can be retrieved when grep is done. You can use regular expressions to make it look neat.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;[tsuji@localhost ~]$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;ps fauwwx | &lt;span class="nb"&gt;grep &lt;/span&gt;sshd.
&lt;span class="go"&gt;root      1531  0.0  0.1  82568  6236 ?        Ss   15:29   0:00 /usr/sbin/sshd -D
root      2830  0.0  0.2 149824  8916 ?        Ss   15:29   0:01  \_ sshd: root@pts/0
root     13315  0.0  0.2 158980 10280 ?        Ss   18:58   0:00  \_ sshd: root@notty
root     14956  0.0  0.2 154512  9352 ?        Ss   20:10   0:00  \_ sshd: tsuji [priv]
tsuji    14959  0.0  0.1 154512  4092 ?        S    20:10   0:00      \_ sshd: tsuji@pts/1
tsuji    15012  0.0  0.0 112672  2268 pts/1    S+   20:11   0:00              \_ grep --color=auto sshd.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  2.Peering into a system call using the strace command against PID
&lt;/h3&gt;

&lt;p&gt;The shell implementation is as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;strace &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="nb"&gt;read&lt;/span&gt; &lt;span class="nt"&gt;-s16384&lt;/span&gt; &lt;span class="nt"&gt;-q&lt;/span&gt; &lt;span class="nt"&gt;-xx&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; &lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;pid&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt; 2&amp;gt;&amp;amp;1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;What we are doing is using &lt;a href="http://man7.org/linux/man-pages/man1/strace.1.html"&gt;strace&lt;/a&gt; to retrieve the read system call issued by the PID associated with the tty we just retrieved, and since various other system calls are called besides the read system call, only the read system call is extracted using the &lt;code&gt;-e read&lt;/code&gt; option of strace.&lt;/p&gt;

&lt;p&gt;The read system call was the system call defined below, and the results that can be obtained with starce are also as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;unistd.h&amp;gt;
&lt;/span&gt;
&lt;span class="kt"&gt;ssize_t&lt;/span&gt; &lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;fd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;For example, suppose the following command is issued in the referring terminal&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;fetched terminal
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;[&lt;/span&gt;tsuji@localhost gomi]&lt;span class="nv"&gt;$ &lt;/span&gt;hoge
bash: hoge: コマンドが見つかりませんでした...
&lt;span class="o"&gt;[&lt;/span&gt;tsuji@localhost gomi]&lt;span class="err"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you refer to the read system call with &lt;code&gt;strace -p ${PID} -e read&lt;/code&gt;, the output is as follows.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;straing terminal
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;[root@localhost ttycopy]#&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;strace &lt;span class="nt"&gt;-p&lt;/span&gt; 14959 &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="nb"&gt;read&lt;/span&gt;
&lt;span class="go"&gt;strace: Process 14959 attached
read(3, "\0\0\0\20\356\202\375C&amp;amp;&amp;amp;\357q\276\210pZ)\300\26M\357T\313\303k\6p\232\351\263\32\224"..., 16384) = 36
read(11, "h", 16384)                    = 1
read(3, "\0\0\0\20\235\230\204Ud\36)\370\266\233\362\305\2219\253g\335M\23\212\374h\250i@\235/\216"..., 16384) = 36
read(11, "o", 16384)                    = 1
&lt;/span&gt;&lt;span class="gp"&gt;read(3, "\0\0\0\20\324\357\304\vn\357BbW\241m\220yS\236\362\301\337\337\237c\203\245\223\221\253;&lt;/span&gt;,&lt;span class="s2"&gt;"..., 16384) = 36
&lt;/span&gt;&lt;span class="go"&gt;read(11, "g", 16384)                    = 1
read(3, "\0\0\0\20\344\215\235\300\226\236\0\323\376\r\217,\257\322\326w\323R\264\3}\266\7q\315\215\344\346"..., 16384) = 36
read(11, "e", 16384)                    = 1
read(3, "\0\0\0\20^``\333\263h\372Z\336\335Y2\250\203\335\221\372faj\177\260f\302Sb\35\354"..., 16384) = 36
read(11, "\r\n", 16384)                 = 2
read(11, "bash: hoge: \343\202\263\343\203\236\343\203\263\343\203\211\343\201\214\350\246\213\343\201"..., 16384) = 62
&lt;/span&gt;&lt;span class="gp"&gt;read(11, "\33]0;&lt;/span&gt;tsuji@localhost:~/gomi&lt;span class="se"&gt;\7&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;tsuj&lt;span class="s2"&gt;"..., 16384) = 51
&lt;/span&gt;&lt;span class="go"&gt;^Cstrace: Process 14959 detached
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The example above shows the strings &lt;code&gt;strace: Process 14959 attached&lt;/code&gt; and &lt;code&gt;strace: Process 14959 detached&lt;/code&gt;, but the strace option &lt;code&gt;-q&lt;/code&gt; is used to suppress this output. Also, the string is converted to hexadecimal with the &lt;code&gt;-x&lt;/code&gt; option to make it easier to handle when displaying it.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.Print a formatted string of the read system call output by strace
&lt;/h3&gt;

&lt;p&gt;The goal is to display the contents of the second argument of the read system call obtained in step 2. The shell implementation is as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-une&lt;/span&gt; &lt;span class="s2"&gt;"s/^read([0-9]&lt;/span&gt;&lt;span class="se"&gt;\+&lt;/span&gt;&lt;span class="s2"&gt;, &lt;/span&gt;&lt;span class="se"&gt;\"\(&lt;/span&gt;&lt;span class="s2"&gt;.*&lt;/span&gt;&lt;span class="se"&gt;\)\"&lt;/span&gt;&lt;span class="s2"&gt;.*/echo -n &lt;/span&gt;&lt;span class="se"&gt;\$&lt;/span&gt;&lt;span class="s2"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\1&lt;/span&gt;&lt;span class="s2"&gt;'/p"&lt;/span&gt; | bash
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;First, the first part &lt;code&gt;sed -une "s/^read([0-9]\+, \"\(.*\)\".*/echo -n \$'\1'/p"&lt;/code&gt;. It captures the second argument of the read system call in a regular expression and gives it the strings &lt;code&gt;echo -n&lt;/code&gt; and &lt;code&gt;$''&lt;/code&gt;. Therefore, we can get the following string&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The string output to the source terminal
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;[&lt;/span&gt;tsuji@localhost gomi]&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;hoge
hoge
&lt;span class="o"&gt;[&lt;/span&gt;tsuji@localhost gomi]&lt;span class="err"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Strace the terminal string to be output after processing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;echo -n $&lt;/span&gt;&lt;span class="s1"&gt;'\x00\x00\x00\x10\xc6\x1e\x56\xc6\x1f\x11\x7e\x57\x11\xaf\xdb\x2a\x91\x32\x84\x8e\x6e\x5b\x12\xc1\x72\x94\x36\x17\x12\xbb\x7c\xab\x4b\xdd\x19\x33'&lt;/span&gt;
&lt;span class="gp"&gt;echo -n $&lt;/span&gt;&lt;span class="s1"&gt;'c'&lt;/span&gt;
&lt;span class="gp"&gt;echo -n $&lt;/span&gt;&lt;span class="s1"&gt;'\x00\x00\x00\x10\x5d\x68\x72\x7c\x74\xca\x3c\xd1\x57\xfc\x14\x7d\x55\x34\x66\x15\x03\xcb\x26\x7c\x17\xbc\x7f\x7a\xf5\x25\x40\xed\xa8\x21\x39\xb3'&lt;/span&gt;
&lt;span class="gp"&gt;echo -n $&lt;/span&gt;&lt;span class="s1"&gt;'a'&lt;/span&gt;
&lt;span class="gp"&gt;echo -n $&lt;/span&gt;&lt;span class="s1"&gt;'\x00\x00\x00\x10\x05\x3d\x4a\xc2\x76\x1c\xd4\x23\x2a\x17\xc6\xa1\x1c\xf2\xdb\x14\x75\x1c\x7d\xb7\x21\xfb\xfc\xcd\x2d\x5c\xef\x06\x6c\x97\x01\x28'&lt;/span&gt;
&lt;span class="gp"&gt;echo -n $&lt;/span&gt;&lt;span class="s1"&gt;'t'&lt;/span&gt;
&lt;span class="gp"&gt;echo -n $&lt;/span&gt;&lt;span class="s1"&gt;'\x00\x00\x00\x10\x66\xb0\x8c\x40\x10\xa6\xf3\x9b\x36\x75\xd5\xc1\x65\x63\x94\x4f\x77\xd9\x10\x6d\xcf\xbb\x48\xed\x8b\x43\x58\x20\x54\x08\xde\x9b'&lt;/span&gt;
&lt;span class="gp"&gt;echo -n $&lt;/span&gt;&lt;span class="s1"&gt;' '&lt;/span&gt;
&lt;span class="gp"&gt;echo -n $&lt;/span&gt;&lt;span class="s1"&gt;'\x00\x00\x00\x10\x60\x6e\xb6\x06\x43\x16\xf5\x75\x89\x90\xb6\x42\x2c\xfe\x8b\x97\xae\xad\x47\x26\xf9\x39\xfc\xd2\x84\x37\xde\x0d\xe5\x32\xbc\x80'&lt;/span&gt;
&lt;span class="gp"&gt;echo -n $&lt;/span&gt;&lt;span class="s1"&gt;'h'&lt;/span&gt;
&lt;span class="gp"&gt;echo -n $&lt;/span&gt;&lt;span class="s1"&gt;'\x00\x00\x00\x10\x00\xe4\x3d\xb7\xd9\x79\x2e\x46\x80\xd5\xa5\xc2\xa7\x9a\xc7\x0c\xe1\x58\x7b\xd5\x97\xff\x00\xab\x72\x51\xa4\xbb\xab\x7d\xd1\xaf'&lt;/span&gt;
&lt;span class="gp"&gt;echo -n $&lt;/span&gt;&lt;span class="s1"&gt;'o'&lt;/span&gt;
&lt;span class="gp"&gt;echo -n $&lt;/span&gt;&lt;span class="s1"&gt;'\x00\x00\x00\x10\xf8\x1d\xce\xe6\x7f\x1a\x43\x94\xa2\xde\x3d\x3c\xb5\xe9\xb9\x94\x39\x43\x63\xfd\xa9\x1f\x45\x83\x64\x5c\x3a\xdf\xa8\x1a\xa4\x86'&lt;/span&gt;
&lt;span class="gp"&gt;echo -n $&lt;/span&gt;&lt;span class="s1"&gt;'g'&lt;/span&gt;
&lt;span class="gp"&gt;echo -n $&lt;/span&gt;&lt;span class="s1"&gt;'\x00\x00\x00\x10\x90\x69\x42\xa9\x48\x99\x0c\x52\xe9\x49\xbd\x4e\xa5\x17\x01\xff\xac\xec\x29\x75\x2c\xc0\x2b\x7c\x07\x85\xf2\x2f\xce\x71\x8f\x46'&lt;/span&gt;
&lt;span class="gp"&gt;echo -n $&lt;/span&gt;&lt;span class="s1"&gt;'e'&lt;/span&gt;
&lt;span class="gp"&gt;echo -n $&lt;/span&gt;&lt;span class="s1"&gt;'\x00\x00\x00\x10\xd9\x91\x38\x08\x0b\x95\x78\xd4\x80\x51\xa2\xe8\xef\x20\x06\x45\xa9\x3c\xf0\xa3\x10\x7d\x06\x32\x2d\x31\x53\x57\x40\x77\x1b\x29'&lt;/span&gt;
&lt;span class="gp"&gt;echo -n $&lt;/span&gt;&lt;span class="s1"&gt;'\r\n'&lt;/span&gt;
&lt;span class="gp"&gt;echo -n $&lt;/span&gt;&lt;span class="s1"&gt;'\x68\x6f\x67\x65\x0d\x0a\x1b\x5d\x30\x3b\x74\x73\x75\x6a\x69\x40\x6c\x6f\x63\x61\x6c\x68\x6f\x73\x74\x3a\x7e\x2f\x67\x6f\x6d\x69\x07'&lt;/span&gt;
&lt;span class="gp"&gt;echo -n $&lt;/span&gt;&lt;span class="s1"&gt;'[tsuji@localhost gomi]$ '&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;-n&lt;/code&gt; option of &lt;code&gt;echo -n&lt;/code&gt; is to avoid line breaks when interpreting. the &lt;code&gt;$''&lt;/code&gt; form of Bash has the following effect. excerpt from the Man page&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded as follows:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;ref: &lt;a href="https://linux.die.net/man/1/bash"&gt;https://linux.die.net/man/1/bash&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This can be used to expand to the meaning that the string holds.&lt;/p&gt;

&lt;p&gt;All that's left to do is to let the shell interpret the &lt;code&gt;echo&lt;/code&gt; string with the &lt;code&gt;| bash&lt;/code&gt; to display the string.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;I was able to casually share the terminal by retrieving a string of arguments for the read system call in strace and restoring them in real time. I think it's an educational tool because you can see other people's work in real time. If you want to hack the terminal, you should try it.&lt;/p&gt;

&lt;p&gt;If you like, you can add a star to &lt;a href="https://github.com/d-tsuji/ttycopy"&gt;ttycopy&lt;/a&gt; to increase my motivation of development.&lt;/p&gt;

&lt;p&gt;Thank you!&lt;/p&gt;

</description>
      <category>bash</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>terminal</category>
    </item>
    <item>
      <title>How to get filepath recursively in Go</title>
      <dc:creator>Tsuji Daishiro</dc:creator>
      <pubDate>Wed, 29 Apr 2020 00:35:18 +0000</pubDate>
      <link>https://dev.to/tutuz/how-to-get-filepath-recursively-in-go-438b</link>
      <guid>https://dev.to/tutuz/how-to-get-filepath-recursively-in-go-438b</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;You sometimes want to get a list of the filepath in directory. One of the ways to achieve shell find-like behavior in Go is to use &lt;a href="https://golang.org/pkg/io/ioutil/#ReadDir"&gt;ioutil.ReadDir&lt;/a&gt;. Another way is to use &lt;a href="https://golang.org/pkg/path/filepath/#Walk"&gt;filepath.Walk&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This is a common way to use ioutil.ReadDir, but I often see implementations that use panic or log.Fatal for error handling. In Go, the correct error handling is to return the error to the caller and output the error at an entry point such as main.go.&lt;/p&gt;

&lt;p&gt;Therefore, this article introduces an example implementation of correct error handling using ioutil.ReadDir.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;p&gt;As an example, we want to scan a directory with three files, as follow.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mypkg
├── dir_b
│   └── file_b
├── dir_c
│   └── file_c
└── file_a
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;dir.go&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The point is to call Dirwalk recursively in the case of a directory and search until you reach a leaf (file) in the directory tree. It then merges the paths that reach the leaf into the caller's path one after the other.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;dir_test.go&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can see that the list of files can be obtained as expected by the following tests.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go test -v
=== RUN   TestDirwalk
--- PASS: TestDirwalk (0.01s)
PASS
ok      github.com/d-tsuji/go-sandbox/mypkg   0.016s
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Here's an example implementation that can retrieve all the files under a directory and handle errors with care. I hope this has been helpful to you.&lt;/p&gt;

</description>
      <category>go</category>
      <category>filepath</category>
      <category>dfs</category>
    </item>
  </channel>
</rss>
