<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/"><generator uri="https://jekyllrb.com/" version="4.4.1">Jekyll</generator><link href="https://mehmandarov.com/tag/command-line/feed.xml" rel="self" type="application/atom+xml"/><link href="https://mehmandarov.com/tag/command-line/" rel="alternate" type="text/html"/><updated>2018-12-30T21:01:00+01:00</updated><id>https://mehmandarov.com/tag/command-line/feed.xml</id><title type="html">Rustam Mehmandarov - tag: command line</title><subtitle type="text">Posts tagged &quot;command line&quot; on Rustam Mehmandarov.</subtitle><author><name>Rustam Mehmandarov</name></author><entry><title type="html">Navigating and Editing the Command Line &#8211;&#160;Bash Edition</title><link href="https://mehmandarov.com/navigating-and-editing-the-command-line/" rel="alternate" type="text/html" title="Navigating and Editing the Command Line &#8211;&#160;Bash Edition"/><published>2018-12-30T21:01:00+01:00</published><updated>2018-12-30T21:01:00+01:00</updated><id>https://mehmandarov.com/navigating-and-editing-the-command-line</id><content type="html" xml:base="https://mehmandarov.com/navigating-and-editing-the-command-line/"><![CDATA[<p><em>A cheat sheet for moving around and editing your command line &#8211;&#160;Bash Edition.</em></p>

<ul>
  <li><a href="#moving-around-the-command-line">Moving Around the Command Line</a></li>
  <li><a href="#editing-commands-in-the-command-line">Editing Commands in the Command Line</a></li>
  <li><a href="#bonus">Bonus</a></li>
</ul>

<hr />

<p>Using the command line can simplify and even automate many of the operations we do on a computer. However, using the command line can mean quite a bit of typing and a possibly large number of parameters. In this post, I would like to focus on how to navigate the cursor and edit the command line, while leaving all the other Bash tricks for the future posts.</p>

<p>I also have created simple graphics to illustrate some of the main shortcuts listed below. This (hi-res) image can be printed for future reference.</p>

<p><em>Note:</em> Please note that all commands containing <code class="language-plaintext highlighter-rouge">ALT</code> combinations might not work depending on your system configuration, and most definitely not work on MacOS. Normally, it is because these combinations are mapped to something else. However, you can still use the same shortcuts simply by replacing <code class="language-plaintext highlighter-rouge">ALT</code> with <code class="language-plaintext highlighter-rouge">ESC</code>.</p>

<p><img src="/assets/images/posts-images/2018-12-30-bash_navigation.png" alt="Navigating and Editing the Command Line (Bash Edition)" /></p>
<figcaption class="caption"> Navigating and Editing the Command Line (Bash Edition)</figcaption>

<hr />

<h2 id="moving-around-the-command-line">Moving Around the Command Line</h2>

<p>So, let&#8217;s first speak about how to move the cursor around &#8211; because using just arrow keys is often not the most optimal way of navigating. Sometimes you might want to go to the beginning of the line, to the end of the line, or simply jump from one <em>word</em> to another, where <em>word</em> &#8211; in this context &#8211; is set of characters separated by spaces (or sometimes other special characters), or as <a href="https://linux.die.net/man/1/bash">documentation</a> states it:</p>

<blockquote>
  <p>A sequence of characters considered as a single unit by the shell. Also known as a token.</p>
</blockquote>

<figure class="highlight"><pre><code class="language-text" data-lang="text"># Moving the cursor &#8211; fast
CTRL+a         Go to the beginning of the line (same as Home)
CTRL+e         Go to the End of the line (same as End)
ALT+b / ESC+b  Go one word back (to the left)
ALT+f / ESC+f  Go one word forward (to the right)

# Moving the cursor &#8211;&#160;one character at a time
CTRL+f         Go forward one character
CTRL+b         Go backward one character

# Using history
CTRL+r         Backwards search in previously executed commands (history)
CTRL+p         Previous command (same as Up arrow)
CTRL+n         Next command (same as Down arrow)</code></pre></figure>

<hr />

<h2 id="editing-commands-in-the-command-line">Editing Commands in the Command Line</h2>

<p>Now that we are able to navigate freely along the command line, it is time to do some modifications. Here, we will see how to delete, cut, paste, and swap words and characters.</p>

<figure class="highlight"><pre><code class="language-text" data-lang="text"># Deleting whole words
ALT+Del        Delete the word before (to the left of) the cursor
ALT+d / ESC+d  Delete the word after (to the right of) the cursor
CTRL+w         Cut the word before the cursor to the clipboard

# Deleting parts of the line
CTRL+k         Cut the line after the cursor to the clipboard
CTRL+u         Cut/delete the line before the cursor to the clipboard

# Deleting single characters
CTRL+d         Delete character under the cursor (same as Delete key)
CTRL+h         Delete character before the cursor (same as Backspace key)

# Paste, Undo, revert, and more
CTRL+l         Clear the screen (similar to the 'clear' command)
CTRL+y         Paste the last thing to be cut (yank)
CTRL+_         Undo
ALT+r / ESC+r  Revert the changes and replace with the line as it was 
                in History.

# Swap 'em!
CTRL+t         Swap the last two characters before the cursor
ALT+t / ESC+t  Swap current word with previous
 
# Convert to UPPER, lower, or Sentence case
ALT+u / ESC+u  Capitalise characters from the cursor to the end of 
                the current word and move to the end of the word.
ALT+l / ESC+l  Lower the case of characters from the cursor to the
                end of the current word and move to the end of the word.
ALT+c / ESC+c  Capitalize the character under the cursor position 
                and move to the end of the word.</code></pre></figure>

<hr />

<h2 id="bonus">Bonus</h2>

<p>First, the most obvious &#8211; you can always find more gems in the <code class="language-plaintext highlighter-rouge">man</code> pages for Bash both in your terminal and online (for instance on this <a href="https://linux.die.net/man/1/bash">mirror</a>). To view it in your terminal, type:</p>

<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="nv">$ </span>man bash</code></pre></figure>

<p>Now, over to something different. Since we have been talking about the command line and shells it is worth mentioning some less-known (and sometimes <em>&#8220;as a curiosity&#8221;</em>) shortcuts in another terminal &#8211; Command Prompt, <code class="language-plaintext highlighter-rouge">cmd.exe</code>:</p>

<figure class="highlight"><pre><code class="language-text" data-lang="text">Function keys in cmd.exe:
  - F1: Pastes the last executed command (character by character)
  - F2: Pastes the last executed command (up to the entered character)
  - F3: Pastes the last executed command
  - F4: Deletes current prompt text up to the entered character
  - F5: Pastes recently executed commands (does not cycle)
  - F6: Pastes ^Z to the prompt
  - F7: Displays a selectable list of previously executed commands
  - F8: Pastes recently executed commands (cycles)
  - F9: Asks for the number of the command from the F7 list to paste</code></pre></figure>

<p><em>Good luck! Try them out and let me know how that goes!</em></p>

<hr />]]></content><author><name>Rustam Mehmandarov</name></author><summary type="html">A cheat sheet for navigating and editing the command line in Bash.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://mehmandarov.com/assets/images/posts-images/laptop.jpg"/><category term="blog"/><category term="command line"/><category term="bash"/><category term="field notes"/><category term="english"/></entry><entry><title type="html">Docker Command Line Survival Guide: The Absolute Basics</title><link href="https://mehmandarov.com/docker-cmd-survival-guide/" rel="alternate" type="text/html" title="Docker Command Line Survival Guide: The Absolute Basics"/><published>2017-07-27T08:23:00+02:00</published><updated>2017-07-27T08:23:00+02:00</updated><id>https://mehmandarov.com/docker-cmd-survival-guide</id><content type="html" xml:base="https://mehmandarov.com/docker-cmd-survival-guide/"><![CDATA[<p><em>A brief introduction to ten essential and absolute basic Docker commands to get you started, and keep you going in the command-line interface.</em></p>

<ul>
  <li><a href="#getting-started">Getting Started</a></li>
  <li><a href="#commands-files-and-folders-inside-a-container">Commands, Files, and Folders Inside a Container</a></li>
  <li><a href="#cleanup">Cleanup</a></li>
</ul>

<hr />

<p>In this post, I decided to share some of the basic commands you might need to get started with Docker. This is neither an extensive list of the commands available, nor all of the commands you might need. This is merely me sharing a prettified list of my cheat sheet for Docker basics with <em>you</em>.</p>

<h2 id="getting-started">Getting Started</h2>

<p>Before we get started, it might be a good idea to note that all of the commands below are written without <code class="language-plaintext highlighter-rouge">sudo</code>. If your installation is not running without <code class="language-plaintext highlighter-rouge">sudo</code> (assuming that you are running Linux), you might want to check out the <a href="https://docs.docker.com/engine/installation/linux/linux-postinstall/" target="_blank">post-installation guide for Linux</a> in the Docker docs.</p>

<h4 id="1-check-if-everything-works">1. Check if Everything Works</h4>

<p>First things first, you can use this simple command to check that your installation is fine. <strong><em>Note:</em></strong> Make sure you have right CPU architecture for your images. Raspberry Pi (ARM) things will not run on x86 architecture, and vice versa.</p>

<p>For x86:</p>

<figure class="highlight"><pre><code class="language-shell_session" data-lang="shell_session"><span class="gp">$</span><span class="w"> </span>docker run docker/whalesay cowsay Hello World!</code></pre></figure>

<p>For Raspberry Pi / AMD:</p>

<figure class="highlight"><pre><code class="language-shell_session" data-lang="shell_session"><span class="gp">$</span><span class="w"> </span>docker run <span class="nt">-d</span> <span class="nt">-p</span> 80:80 hypriot/rpi-busybox-httpd</code></pre></figure>

<p><img src="/assets/images/posts-images/2017-07-27_helloworld-rpi.png" alt="Hello World" class="bigger-image" /></p>

<h4 id="2-list-containers">2. List Containers</h4>

<p>After creating containers, first thing you might want to do is to see what containers you have up and running. To list all running containers you can use:</p>

<figure class="highlight"><pre><code class="language-shell_session" data-lang="shell_session"><span class="gp">$</span><span class="w"> </span>docker ps</code></pre></figure>

<p>This command will give you a list similar to this:</p>

<figure class="highlight"><pre><code class="language-text" data-lang="text">CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS                    PORTS                NAMES
e85753d57a67        easypi/dokuwiki-arm         "/bin/sh -c 'php-f..."   1 days ago          Up 23 hours               0.0.0.0:80-&gt;80/tcp   mywiki</code></pre></figure>

<p>However, it will not show you any stopped containers. To list <em>all</em> local containers use the <code class="language-plaintext highlighter-rouge">-a</code> option:</p>

<figure class="highlight"><pre><code class="language-shell_session" data-lang="shell_session"><span class="gp">$</span><span class="w"> </span>docker ps <span class="nt">-a</span></code></pre></figure>

<p>The output will be more like this (note that is shows also stopped, or even failed containers):</p>

<figure class="highlight"><pre><code class="language-text" data-lang="text">CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS                    PORTS                NAMES
573193cf1d5e        hypriot/rpi-busybox-httpd   "/bin/busybox http..."   2 days ago          Exited (0) 5 hours ago                         mytest
e85753d57a67        easypi/dokuwiki-arm         "/bin/sh -c 'php-f..."   1 days ago          Up 23 hours               0.0.0.0:80-&gt;80/tcp   mywiki</code></pre></figure>

<p>More on <code class="language-plaintext highlighter-rouge">docker ps</code> in the <a href="https://docs.docker.com/engine/reference/commandline/ps/" target="_blank">Docker docs</a>.</p>

<h4 id="3-list-images">3. List Images</h4>

<p>To list all the images available on your system, simply do this:</p>

<figure class="highlight"><pre><code class="language-shell_session" data-lang="shell_session"><span class="gp">$</span><span class="w"> </span>docker images</code></pre></figure>

<h4 id="4-containers-vs-images">4. Containers vs. Images?</h4>

<p>What is the difference between containers and images, you might wonder? Well, I have a <a href="https://docs.docker.com/engine/userguide/storagedriver/imagesandcontainers/" target="_blank">link</a> for you. This will hopefully help you to understand how Docker manages the data within your images and containers.</p>

<h4 id="5-starting-and-stopping-containers">5. Starting and Stopping Containers</h4>

<p>Another two basic commands &#8211;&#160;<a href="https://docs.docker.com/engine/reference/commandline/start/" target="_blank">starting</a> and <a href="https://docs.docker.com/engine/reference/commandline/stop/" target="_blank">stopping</a> containers:</p>

<figure class="highlight"><pre><code class="language-shell_session" data-lang="shell_session"><span class="gp">$</span><span class="w"> </span>docker start &lt;container_id&gt;
<span class="gp">$</span><span class="w"> </span>docker stop &lt;container_id&gt;</code></pre></figure>

<p><strong><em>Note:</em></strong> The <code class="language-plaintext highlighter-rouge">docker run</code> command first creates a writeable container layer over the specified image, and then starts it using the specified command. That is, <code class="language-plaintext highlighter-rouge">docker run</code> is equivalent to the API&#8217;s <code class="language-plaintext highlighter-rouge">/containers/create</code>, and then <code class="language-plaintext highlighter-rouge">/containers/&lt;id&gt;/start</code>.</p>

<hr />

<h2 id="commands-files-and-folders-inside-a-container">Commands, Files, and Folders Inside a Container</h2>

<h4 id="6-run-any-command-from-a-container">6. Run Any Command from a Container</h4>
<p>You can <a href="https://docs.docker.com/engine/reference/commandline/exec/" target="_blank">run any command</a> in a running container just knowing its ID (or name):</p>

<figure class="highlight"><pre><code class="language-shell_session" data-lang="shell_session"><span class="gp">$</span><span class="w"> </span>docker <span class="nb">exec</span> <span class="nt">-it</span> &lt;container_id_or_name&gt; <span class="nb">echo</span> <span class="s2">"Hello from container!"</span></code></pre></figure>

<h4 id="7-getting-into-containers">7. Getting Into Containers</h4>

<p>Since you can run any command, then you can (obviously) also run a shell from a container; if you have any. This will be a bit similar to running an <code class="language-plaintext highlighter-rouge">ssh</code> command to connect remotely to a regular Linux box (given you have <code class="language-plaintext highlighter-rouge">bash</code> or <code class="language-plaintext highlighter-rouge">sh</code> in the container):</p>

<figure class="highlight"><pre><code class="language-shell_session" data-lang="shell_session"><span class="gp">$</span><span class="w"> </span>docker <span class="nb">exec</span> <span class="nt">-it</span> &lt;container_id_or_name&gt; bash
<span class="gp">$</span><span class="w"> </span><span class="c"># or:</span>
<span class="gp">$</span><span class="w"> </span>docker <span class="nb">exec</span> <span class="nt">-it</span> &lt;container_id_or_name&gt; sh</code></pre></figure>

<h4 id="8-copy-files-from-and-to-containers">8. Copy Files From and To Containers</h4>

<p>Another useful trick you might need is to copy some files to and from a container. Your friend here is the <code class="language-plaintext highlighter-rouge">docker cp</code> command (<a href="https://docs.docker.com/edge/engine/reference/commandline/cp/" target="_blank">link to the docs</a>):</p>

<figure class="highlight"><pre><code class="language-shell_session" data-lang="shell_session"><span class="gp">$</span><span class="w"> </span><span class="c"># To container:</span>
<span class="gp">$</span><span class="w"> </span>docker <span class="nb">cp </span>foo.txt &lt;container_name&gt;:/foo.txt
<span class="gp">$</span><span class="w"> </span><span class="c"># From container:</span>
<span class="gp">$</span><span class="w"> </span>docker <span class="nb">cp</span> &lt;container_name&gt;:/foo.txt foo.txt</code></pre></figure>

<hr />

<h2 id="cleanup">Cleanup</h2>

<p>After playing round with all the images and containers, you might realize that you have quite a collection of these on your drive, just taking up space.</p>

<h4 id="9-remove-containers">9. Remove Containers</h4>

<p>To remove the unused or unwanted containers, you can run the <code class="language-plaintext highlighter-rouge">docker rm</code> command with the IDs of those images. The IDs can be retrieved with the <code class="language-plaintext highlighter-rouge">docker ps -a</code> command, mentioned above.</p>

<figure class="highlight"><pre><code class="language-shell_session" data-lang="shell_session"><span class="gp">$</span><span class="w"> </span>docker <span class="nb">rm</span> &lt;container_id&gt;</code></pre></figure>

<h4 id="10-remove-images">10. Remove Images</h4>

<p>The <code class="language-plaintext highlighter-rouge">docker rmi</code> command followed by the IDs of images will help you to remove the unused or unwanted images. The abovementioned <code class="language-plaintext highlighter-rouge">docker images</code> command will help you finding the correct IDs for the images in question.</p>

<figure class="highlight"><pre><code class="language-shell_session" data-lang="shell_session"><span class="gp">$</span><span class="w"> </span>docker rmi &lt;container_id&gt;</code></pre></figure>

<p><em>Have fun!</em></p>

<hr />]]></content><author><name>Rustam Mehmandarov</name></author><summary type="html">Ten essential Docker commands to get you started with the command-line interface.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://mehmandarov.com/assets/images/posts-images/street-art-container_small.jpeg"/><category term="blog"/><category term="docker"/><category term="command line"/><category term="field notes"/><category term="english"/></entry></feed>
