<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>OS | Haobin Tan</title><link>https://haobin-tan.netlify.app/tags/os/</link><atom:link href="https://haobin-tan.netlify.app/tags/os/index.xml" rel="self" type="application/rss+xml"/><description>OS</description><generator>Hugo Blox Builder (https://hugoblox.com)</generator><language>en-us</language><lastBuildDate>Tue, 20 Aug 2024 00:00:00 +0000</lastBuildDate><image><url>https://haobin-tan.netlify.app/media/icon_hu7d15bc7db65c8eaf7a4f66f5447d0b42_15095_512x512_fill_lanczos_center_3.png</url><title>OS</title><link>https://haobin-tan.netlify.app/tags/os/</link></image><item><title>Introduction to Linux</title><link>https://haobin-tan.netlify.app/docs/coding/linux/getting-started/01_intro_linux/</link><pubDate>Thu, 15 Aug 2024 00:00:00 +0000</pubDate><guid>https://haobin-tan.netlify.app/docs/coding/linux/getting-started/01_intro_linux/</guid><description>&lt;h2 id="linux-and-unix">Linux and Unix&lt;/h2>
&lt;p>&lt;strong>Operating System (OS)&lt;/strong> is software that&lt;/p>
&lt;ul>
&lt;li>
&lt;p>manages computer hardware and resources&lt;/p>
&lt;/li>
&lt;li>
&lt;p>allows interaction with hardware to perform useful tasks&lt;/p>
&lt;/li>
&lt;/ul>
&lt;p>&lt;strong>Unix&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>
&lt;p>A family of operating systems&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Popular Unix-based OS:&lt;/p>
&lt;ul>
&lt;li>
&lt;p>HP-UX&lt;/p>
&lt;/li>
&lt;li>
&lt;p>IBM AIX&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Apple macOS&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;p>&lt;strong>Linux&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Family of Unix-like OSs (usually specific distribution)&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Originally developed as an effort to create a free, open-source Unix OS&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Key features&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Free and open source&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Secure&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Multi-user&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Multitasking&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Portability&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>Use cases&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Android&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Supercomputers&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Data centers and cloud services&lt;/p>
&lt;/li>
&lt;li>
&lt;p>PCs&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h2 id="linux-distributions">Linux Distributions&lt;/h2>
&lt;p>Linux distribution&lt;/p>
&lt;ul>
&lt;li>
&lt;p>A specific flavor of Linux OS&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Also referred to as &lt;strong>Distro&lt;/strong>&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Core component: Linux kernel&lt;/p>
&lt;/li>
&lt;/ul>
&lt;p>Linux distro differences&lt;/p>
&lt;ul>
&lt;li>
&lt;p>System utilities&lt;/p>
&lt;/li>
&lt;li>
&lt;p>GUI&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Shell commands&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Support types&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Community vs. enterprise&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Long-tem support (LTS) vs. rolling release&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;p>Common/popular distro&lt;/p>
&lt;ul>
&lt;li>
&lt;p>&lt;strong>Debian&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>
&lt;p>One of the earliest-rooted distros: first release in 1993 (named version 0.01) and the first official, stable release in 1996 (version 1.1)&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Features&lt;/p>
&lt;ul>
&lt;li>Stable, reliable, and fully open source&lt;/li>
&lt;li>Supports many computer architectures (or types of hardware)&lt;/li>
&lt;/ul>
&lt;p>-&amp;gt; Highly regarded in the server space&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Largest community-run distro&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Ubuntu&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Debian-based, used a lot of the same tools as the Debian OS&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Three official editions&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Desktop: for personal computers, laptops, and workstations&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Server:for simple file servers or multinode clouds&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Core: for the Internet of Things (IoT)&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Red Hat Linux&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>
&lt;p>A &amp;ldquo;core&amp;rdquo; Linux distro (meaning that it is not derived from another Linux distro)&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Stable, reliable, and fully open source&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Ships as &lt;em>&lt;strong>Red Hat Enterprise Linux (or RHEL)&lt;/strong>&lt;/em>, an edition focused entirely on enterprise customers&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Fedora&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Supports many architectures&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Very reliable and secure&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Actively developed, with a large and growing community&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>SUSE Enterprise&lt;/strong> (a.k.a SLE)&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Available in two editions&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Serve (SLES)&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Desktop (SLED)&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>Supports many architecture (e.g., ARM for Raspberry Pi)&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Uses the SUSE Package Hub -&amp;gt; Enable users to install packages that aren&amp;rsquo;t officially part of SLE&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Arch Linux&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Do-it-yourself (DIY) approach&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Highly configurable -&amp;gt; Requires strong understanding of Linux and system tools&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Not focus on stability -&amp;gt; easy access to the newest software&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h2 id="linux-architecture">Linux Architecture&lt;/h2>
&lt;p>
&lt;figure >
&lt;div class="flex justify-center ">
&lt;div class="w-100" >&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/linux_5_layer_arch.PNG" alt="linux_5_layer_arch" loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Layer&lt;/th>
&lt;th>Function&lt;/th>
&lt;th>Tasks / Jobs&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>UI&lt;/td>
&lt;td>Allows users to interact with the system using a keyboard or mouse&lt;/td>
&lt;td>Use a web browser to send an Email&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Application&lt;/td>
&lt;td>Includes system daemons, programming languages, shells, user apps, tools&lt;/td>
&lt;td>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Operating system&lt;/td>
&lt;td>Responsible for jobs that are vital for system stability such as job scheduling and keeping track of time&lt;/td>
&lt;td>&lt;li>Assign software to users &lt;br/>&lt;li>Help detect errors and prevent failures&lt;br/>&lt;li>Perform file management tasks&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Kernel&lt;/td>
&lt;td>&lt;li>Core component of the OS, responsible for managing memory, processing, and securtiy&lt;br/>&lt;li>Lowest-level software, starts on boot&lt;br/>&lt;li>Bridge between apps and hardware&lt;/td>
&lt;td>&lt;li>Memory management&lt;br/>&lt;li>Process management&lt;br/>&lt;li>Device drivers&lt;br/>&lt;li>Security&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Hardward&lt;/td>
&lt;td>Includes all the physical or electronic devices in the computer such as processors, memory modules, input devices, and storage&lt;/td>
&lt;td>&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;h3 id="linux-filesystem">Linux filesystem&lt;/h3>
&lt;ul>
&lt;li>
&lt;p>Collections of files in your machine&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Begins at root directory (&lt;code>\&lt;/code>)&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Tree-like structure&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Assigns appropriate access rights&lt;/p>
&lt;/li>
&lt;/ul>
&lt;p>
&lt;figure >
&lt;div class="flex justify-center ">
&lt;div class="w-100" >&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/linux_filesystem.PNG" alt="linux_filesystem" loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;ul>
&lt;li>
&lt;p>&lt;code>/bin&lt;/code>&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Contains user binary files, which contain the code your machine reads to run programs and execute commands.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>It’s called &amp;ldquo;slash bin&amp;rdquo; to signify that it exists directly below the root directory.&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>&lt;code>/usr&lt;/code>: contains user programs&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>/home&lt;/code>: Your personal working directory where you should store all your personal files&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>/boot&lt;/code>: Contains your system boot files, the instructions vital for system startup&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>/media&lt;/code>: Contains files related to temporary media such as CD or USB drives that are connected to the system&lt;/p>
&lt;/li>
&lt;/ul>
&lt;h2 id="linux-terminal">Linux Terminal&lt;/h2>
&lt;p>&lt;strong>Shell&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>
&lt;p>An OS-level application that interpreets commands.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>You can use shell command to (e.g.)&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Move and copy files&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Write to and read from files&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Extract and filter data&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Search for data&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>Popular shells&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Bash&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Zsh&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;p>&lt;strong>Terminal&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>
&lt;p>An application to interact with the shell&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Enter commands and receive output from them&lt;/p>
&lt;/li>
&lt;/ul>
&lt;h3 id="linux-system-communication">Linux system communication&lt;/h3>
&lt;p>
&lt;figure >
&lt;div class="flex justify-center ">
&lt;div class="w-100" >&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/linux_terminal_communication.PNG" alt="linux_terminal_communication" loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;ol>
&lt;li>
&lt;p>User enters the command in a terminal, which is then relayed to the shell.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>The core components of the operating system and kernel translate the command for the hardware to perform.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>When the hardware completes the command, the kernel reads any changes or results and sends them back via the shell to the terminal for the user’s information.&lt;/p>
&lt;/li>
&lt;/ol>
&lt;h3 id="linux-filesystem-paths">Linux filesystem paths&lt;/h3>
&lt;p>The filesystem is the human-readable directory or file location.&lt;/p>
&lt;ul>
&lt;li>
&lt;p>&lt;code>a/b&lt;/code> indicates that the file or directory named &amp;ldquo;b&amp;rdquo; is located inside the directory named &amp;ldquo;a&amp;rdquo;.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Special paths&lt;/p>
&lt;ul>
&lt;li>
&lt;p>&lt;code>/&lt;/code>: Root directory&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>~&lt;/code>: Home directory, an important subdirectory of the root directory&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>..&lt;/code>: Parent of current directory&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>.&lt;/code>: Current directory&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>Filesystem-related commands&lt;/p>
&lt;ul>
&lt;li>
&lt;p>&lt;code>cd&lt;/code> : &lt;u>c&lt;/u>hange &lt;u>d&lt;/u>irectory&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>pwd&lt;/code>: print path name for &lt;u>p&lt;/u>resent &lt;u>w&lt;/u>orking &lt;u>d&lt;/u>irectory&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>ls&lt;/code>: list the contents of the directory&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h2 id="creating-and-editing-text-files">Creating and Editing Text Files&lt;/h2>
&lt;p>Popular text editors&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Command-line text editors&lt;/p>
&lt;ul>
&lt;li>
&lt;p>GNU nano: use &lt;code>nano &amp;lt;filename&amp;gt;&lt;/code> to open a text file in GNU nano from the command prompt&lt;/p>
&lt;/li>
&lt;li>
&lt;p>vi&lt;/p>
&lt;/li>
&lt;li>
&lt;p>vim&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Insert mode&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Press &lt;code>i&lt;/code> to enter Insert mode&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Type some text&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Press Esc to exist Insert mode and switch to Command mode. Now the text is written to the buffer at the cursor location.&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>Command mode&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Enter &lt;code>:sav &amp;lt;filename&amp;gt;&lt;/code> to create a file and write the buffer to the file&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Enter &lt;code>:w&lt;/code> to write the buffer to the file without exiting&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Enter &lt;code>:q&lt;/code> to quit vim session&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Enter &lt;code>:q!&lt;/code> to quit without saving&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>GUI-based text editors&lt;/p>
&lt;ul>
&lt;li>gedit&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>Command-line or GUI: emacs&lt;/p>
&lt;/li>
&lt;/ul>
&lt;h2 id="install-software-and-updates">Install Software and Updates&lt;/h2>
&lt;p>&lt;strong>Packages&lt;/strong>: Archive files for installing new software or updating existing software&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Deb packages (.deb files): For Debian-based distributions (e.g., Debian, Ubuntu, and Mint)&lt;/p>
&lt;/li>
&lt;li>
&lt;p>RPM packages (.rpm files): For Red Hat-based distributions (e.g., CentOS/RHEL, Fedora, and openSUSE)&lt;/p>
&lt;blockquote>
&lt;p>RPM: Red Hat Package Manager&lt;/p>
&lt;/blockquote>
&lt;/li>
&lt;/ul>
&lt;p>.deb and .rpm formats are equivalent. You can use &lt;code>alien&lt;/code> to convert it&lt;/p>
&lt;ul>
&lt;li>
&lt;p>RPM to deb&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">alien &amp;lt;package-name&amp;gt;.rpm
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;/li>
&lt;li>
&lt;p>deb to RPM&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">alien -r &amp;lt;package-name&amp;gt;.deb
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;/li>
&lt;/ul>
&lt;p>&lt;strong>Package managers&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Manage the download and installation of packages&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Available for different Linux distros&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Can be GUI-based or command-line tools&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Benefits&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Automatically resolve dependencies&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Notify you when updates are available&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Automatic or manual installation&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;p>For deb-based&lt;/p>
&lt;ul>
&lt;li>GUI-based: Update Manager&lt;/li>
&lt;li>Command line-based: &lt;code>apt&lt;/code>
&lt;ul>
&lt;li>&lt;code>sudo apt update&lt;/code>: Find available packages for your distro
&lt;ul>
&lt;li>The output of this command lists each available package, builds a dependency tree, and lets you know how many packages can be upgraded&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;code>sudo apt upgrade&lt;/code>: Upgrad all installed packages on a system&lt;/li>
&lt;li>&lt;code>sudo apt install &amp;lt;package_name&amp;gt;&lt;/code>: Install a specific package&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;p>For RPM-based&lt;/p>
&lt;ul>
&lt;li>GUI-based: PackageKit&lt;/li>
&lt;li>Command line-based: &lt;code>yum&lt;/code>
&lt;ul>
&lt;li>&lt;code>sudo yum update&lt;/code>: Update all packages in system&lt;/li>
&lt;li>&lt;code>sudo yum install&amp;lt;package_name&amp;gt;&lt;/code>: Install a specific package&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul></description></item><item><title>Information, Navigation, and Management Commands</title><link>https://haobin-tan.netlify.app/docs/coding/linux/getting-started/02_info_navi_management_commands/</link><pubDate>Thu, 15 Aug 2024 00:00:00 +0000</pubDate><guid>https://haobin-tan.netlify.app/docs/coding/linux/getting-started/02_info_navi_management_commands/</guid><description>&lt;h2 id="overview">Overview&lt;/h2>
&lt;h3 id="what-is-a-shell">What is a shell?&lt;/h3>
&lt;ul>
&lt;li>
&lt;p>User interface for running commands&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Interactive language&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Scripting language, can be used to automate tasks&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Default shell: &lt;strong>Bash&lt;/strong>. Other shells: sh, ksh, tcsh, zsh, and fish. Check shell with&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">printenv SHELL
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This returns the path to the default shell program&lt;/p>
&lt;/li>
&lt;/ul>
&lt;h3 id="shell-command-applications">Shell command applications&lt;/h3>
&lt;details class="spoiler " id="spoiler-0">
&lt;summary class="cursor-pointer">Getting information&lt;/summary>
&lt;div class="rounded-lg bg-neutral-50 dark:bg-neutral-800 p-2">
&lt;ul>
&lt;li>&lt;code>whoam&lt;/code>: User name&lt;/li>
&lt;li>&lt;code>id&lt;/code>: User ID and group ID&lt;/li>
&lt;li>&lt;code>uname&lt;/code>: OS name&lt;/li>
&lt;li>&lt;code>ps&lt;/code>: Running processes&lt;/li>
&lt;li>&lt;code>top&lt;/code>: Resource usage&lt;/li>
&lt;li>&lt;code>df&lt;/code>: Mounted file systems&lt;/li>
&lt;li>&lt;code>man&lt;/code>: Reference manual&lt;/li>
&lt;li>&lt;code>date&lt;/code>: Today&amp;rsquo;s date&lt;/li>
&lt;/ul>
&lt;/div>
&lt;/details>
&lt;details class="spoiler " id="spoiler-1">
&lt;summary class="cursor-pointer">Navigating and working with files and directories&lt;/summary>
&lt;div class="rounded-lg bg-neutral-50 dark:bg-neutral-800 p-2">
&lt;p>Files&lt;/p>
&lt;ul>
&lt;li>Files
&lt;ul>
&lt;li>&lt;code>cp&lt;/code>: Copy fiile&lt;/li>
&lt;li>&lt;code>mv&lt;/code>: Change file name or path&lt;/li>
&lt;li>&lt;code>rm&lt;/code>: Remove file&lt;/li>
&lt;li>&lt;code>touch&lt;/code>: Create empty file, update file timestamp&lt;/li>
&lt;li>&lt;code>chmod&lt;/code>: Change/modify file permissions&lt;/li>
&lt;li>&lt;code>ws&lt;/code>: Get count of lines, words, characters in file&lt;/li>
&lt;li>&lt;code>grep&lt;/code>: Return lines in file matching pattern&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>Directory
&lt;ul>
&lt;li>&lt;code>ls&lt;/code>: List files and directories&lt;/li>
&lt;li>&lt;code>find&lt;/code>: Fined files in directory tree&lt;/li>
&lt;li>&lt;code>pwd&lt;/code>: Get present working directory&lt;/li>
&lt;li>&lt;code>mkdir&lt;/code>: Make directory&lt;/li>
&lt;li>&lt;code>cd&lt;/code>: Change directory&lt;/li>
&lt;li>&lt;code>rmdir&lt;/code>: Remove directory&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;/div>
&lt;/details>
&lt;details class="spoiler " id="spoiler-2">
&lt;summary class="cursor-pointer">Printing file and string contents&lt;/summary>
&lt;div class="rounded-lg bg-neutral-50 dark:bg-neutral-800 p-2">
&lt;ul>
&lt;li>&lt;code>cat&lt;/code>: Print file contents&lt;/li>
&lt;li>&lt;code>more&lt;/code>: Print file contents page-by-page&lt;/li>
&lt;li>&lt;code>head&lt;/code>: Print first N lines of file&lt;/li>
&lt;li>&lt;code>tail&lt;/code>: print last N lines of file&lt;/li>
&lt;li>&lt;code>echo&lt;/code>: Print string or variable value&lt;/li>
&lt;/ul>
&lt;/div>
&lt;/details>
&lt;details class="spoiler " id="spoiler-3">
&lt;summary class="cursor-pointer">File compression and archiving&lt;/summary>
&lt;div class="rounded-lg bg-neutral-50 dark:bg-neutral-800 p-2">
&lt;ul>
&lt;li>&lt;code>tar&lt;/code>: Archive a set of files&lt;/li>
&lt;li>&lt;code>zip&lt;/code>: Compress a set of files&lt;/li>
&lt;li>&lt;code>unzip&lt;/code>: Extract files from a compressed zip archive&lt;/li>
&lt;/ul>
&lt;/div>
&lt;/details>
&lt;details class="spoiler " id="spoiler-4">
&lt;summary class="cursor-pointer">Performing network operations&lt;/summary>
&lt;div class="rounded-lg bg-neutral-50 dark:bg-neutral-800 p-2">
&lt;ul>
&lt;li>&lt;code>hostname&lt;/code>: Print hostname&lt;/li>
&lt;li>&lt;code>ping&lt;/code>: Send packts to URL and print response&lt;/li>
&lt;li>&lt;code>ifconfig&lt;/code>: Display or configure system network interfaces&lt;/li>
&lt;li>&lt;code>curl&lt;/code>: Display contents of file at a URL&lt;/li>
&lt;li>&lt;code>wget&lt;/code>: Download file from URL&lt;/li>
&lt;/ul>
&lt;/div>
&lt;/details>
&lt;details class="spoiler " id="spoiler-5">
&lt;summary class="cursor-pointer">Monitoring performance and status of the system, its components and applications&lt;/summary>
&lt;div class="rounded-lg bg-neutral-50 dark:bg-neutral-800 p-2">
&lt;/div>
&lt;/details>
&lt;details class="spoiler " id="spoiler-6">
&lt;summary class="cursor-pointer">Running batch jobs, such as ETL operations.&lt;/summary>
&lt;div class="rounded-lg bg-neutral-50 dark:bg-neutral-800 p-2">
&lt;/div>
&lt;/details>
&lt;h2 id="informational-command">Informational Command&lt;/h2>
&lt;ul>
&lt;li>Display user information&lt;/li>
&lt;li>Verify identity or identify user account&lt;/li>
&lt;/ul>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Command&lt;/th>
&lt;th>Usage&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>&lt;code>whoami&lt;/code>&lt;/td>
&lt;td>Return user name&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>id&lt;/code>&lt;/td>
&lt;td>User or Group ID&lt;br />&lt;li> &lt;code>id -u&lt;/code> returns the numerical ID of the user&lt;br />&lt;li> &lt;code>id -u -n&lt;/code> returns the name corresponding to the numerical user ID&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>uname&lt;/code> (Unix name)&lt;/td>
&lt;td>Returns OS information&lt;br />&lt;li> Identify system or diagnose issues&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>df&lt;/code> (disk free)&lt;/td>
&lt;td>Show disk usage to monitor disk usage or check space&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>ps&lt;/code> (process status)&lt;/td>
&lt;td>Monitor or manage processes&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>top&lt;/code> (table of processes)&lt;/td>
&lt;td>Task manger: Monitor system performance and reosurce usage&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>echo&lt;/code>&lt;/td>
&lt;td>Print string or variable value&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>date&lt;/code>&lt;/td>
&lt;td>Display system date and time&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>man &amp;lt;command&amp;gt;&lt;/code> (manual)&lt;/td>
&lt;td>Show manual for any command&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;div class="flex px-4 py-3 mb-6 rounded-md bg-primary-100 dark:bg-primary-900">
&lt;span class="pr-3 pt-1 text-primary-600 dark:text-primary-300">
&lt;svg height="24" viewBox="0 0 370 391" xmlns="http://www.w3.org/2000/svg">&lt;g clip-rule="evenodd" fill-rule="evenodd">&lt;path d="m207.5 22.4 114.4 66.6c13.5 7.9 21.9 22.4 21.9 38v136.4c0 17.3-9.3 33.3-24.5 41.8l-113.5 63.9a49.06 49.06 0 0 1 -48.5-.2l-104.5-60.1c-16.4-9.5-26.6-27-26.6-45.9v-129.5c0-19.1 9.9-36.8 26.1-46.8l102.8-63.5c16-9.9 36.2-10.1 52.4-.7z" fill="#ff4088" stroke="#c9177e" stroke-width="27" />&lt;path d="m105.6 298.2v-207.2h43.4v75.5h71.9v-75.5h43.5v207.2h-43.5v-90.6h-71.9v90.6z" fill="#fff" />&lt;/g>&lt;/svg>
&lt;/span>
&lt;span class="dark:text-neutral-300">&lt;p>
is a free and open-source collaborative documentation effort, which provides documentation that is more accessible than the traditional man pages, along with practical examples.&lt;/p>
&lt;p>Example&lt;/p>
&lt;p>&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/tldr-dark.png" alt="Screenshot of the tldr client displaying the tar command.">&lt;/p>
&lt;/span>
&lt;/div>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">ls
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="file-and-directory-navigation-commands">File and Directory Navigation Commands&lt;/h2>
&lt;p>&lt;code>pwd&lt;/code>, &lt;code>cd&lt;/code>&lt;/p>
&lt;p>&lt;code>ls&lt;/code>&lt;/p>
&lt;p>
&lt;figure >
&lt;div class="flex justify-center ">
&lt;div class="w-100" >&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/%E6%88%AA%E5%B1%8F2024-08-16%2017.53.20.png" alt="截屏2024-08-16 17.53.20" loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;p>&lt;code>find&lt;/code>: Find files in directory tress.&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Example:&lt;/p>
&lt;p>
&lt;figure >
&lt;div class="flex justify-center ">
&lt;div class="w-100" >&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/%E6%88%AA%E5%B1%8F2024-08-16%2017.21.33-20240820124520725-20240820124535626.png" alt="截屏2024-08-16 17.21.33" loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;ul>
&lt;li>&lt;code>iname&lt;/code>: case insensitive&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h2 id="file-and-directory-management-commands">File and Directory Management Commands&lt;/h2>
&lt;p>&lt;code>mkdir&lt;/code>&lt;/p>
&lt;p>&lt;code>rm&lt;/code>&lt;/p>
&lt;div class="flex px-4 py-3 mb-6 rounded-md bg-primary-100 dark:bg-primary-900">
&lt;span class="pr-3 pt-1 text-primary-600 dark:text-primary-300">
&lt;svg height="24" viewBox="0 0 370 391" xmlns="http://www.w3.org/2000/svg">&lt;g clip-rule="evenodd" fill-rule="evenodd">&lt;path d="m207.5 22.4 114.4 66.6c13.5 7.9 21.9 22.4 21.9 38v136.4c0 17.3-9.3 33.3-24.5 41.8l-113.5 63.9a49.06 49.06 0 0 1 -48.5-.2l-104.5-60.1c-16.4-9.5-26.6-27-26.6-45.9v-129.5c0-19.1 9.9-36.8 26.1-46.8l102.8-63.5c16-9.9 36.2-10.1 52.4-.7z" fill="#ff4088" stroke="#c9177e" stroke-width="27" />&lt;path d="m105.6 298.2v-207.2h43.4v75.5h71.9v-75.5h43.5v207.2h-43.5v-90.6h-71.9v90.6z" fill="#fff" />&lt;/g>&lt;/svg>
&lt;/span>
&lt;span class="dark:text-neutral-300">If you want to remove multiple files, for example by using a wildcard to find all filenames matching a pattern, it&amp;rsquo;s best practice to confirm or deny each deletion by including the &lt;code>-i&lt;/code> option, which creates a prompt to ask for confirmation before every deletion.&lt;/span>
&lt;/div>
&lt;div class="flex px-4 py-3 mb-6 rounded-md bg-yellow-100 dark:bg-yellow-900">
&lt;span class="pr-3 pt-1 text-red-400">
&lt;svg height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">&lt;path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M12 9v3.75m-9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0zM12 15.75h.007v.008H12z"/>&lt;/svg>
&lt;/span>
&lt;span class="dark:text-neutral-300">Be careful when deleting files or directories! There is normally no way to restore a deleted file once it is deleted, as there is no trash folder. This is why you should always back up, or archive, your important files. You will learn more about archiving files soon.&lt;/span>
&lt;/div>
&lt;p>&lt;code>rmdir&lt;/code>: Remove empty directory&lt;/p>
&lt;p>&lt;code>touch&lt;/code>: Create empty file, update file date&lt;/p>
&lt;p>&lt;code>cp&lt;/code>: Copy file or directory (&lt;code>-r&lt;/code>) to destination&lt;/p>
&lt;p>&lt;code>mv&lt;/code>: Move file or directory&lt;/p>
&lt;ul>
&lt;li>
&lt;p>when the source and target directories are the same, you can use &lt;code>mv&lt;/code> to rename a file.
Example: use &lt;code>mv&lt;/code> to rename &lt;code>users.txt&lt;/code> to &lt;code>user-info.txt&lt;/code> by entering the following command:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">mv users.txt user-info.txt
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;/li>
&lt;/ul>
&lt;div class="flex px-4 py-3 mb-6 rounded-md bg-yellow-100 dark:bg-yellow-900">
&lt;span class="pr-3 pt-1 text-red-400">
&lt;svg height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">&lt;path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M12 9v3.75m-9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0zM12 15.75h.007v.008H12z"/>&lt;/svg>
&lt;/span>
&lt;span class="dark:text-neutral-300">You should always use caution when moving a file. If the target file already exists, it will be overwritten, or replaced, by the source file.&lt;/span>
&lt;/div>
&lt;p>&lt;code>chmod&lt;/code> (change mode): Change file permissions&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Specify which permissions to change with a combination of the following characters:&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Option&lt;/th>
&lt;th>Description&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>&lt;code>r&lt;/code>, &lt;code>w&lt;/code>, &lt;code>x&lt;/code>&lt;/td>
&lt;td>&lt;strong>Permissions&lt;/strong>: read, write, and execute&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>u&lt;/code>,&lt;code>g&lt;/code>, &lt;code>o &lt;/code>&lt;/td>
&lt;td>&lt;strong>User categories&lt;/strong>: user, group, and all others&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>+&lt;/code>, &lt;code>-&lt;/code>&lt;/td>
&lt;td>&lt;strong>Operations&lt;/strong>: grant and revoke&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;/li>
&lt;li>
&lt;p>Example&lt;/p>
&lt;p>
&lt;figure >
&lt;div class="flex justify-center ">
&lt;div class="w-100" >&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/%E6%88%AA%E5%B1%8F2024-08-16%2017.43.31.png" alt="截屏2024-08-16 17.43.31" loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;/li>
&lt;/ul>
&lt;h3 id="managing-file-permissions-and-ownership">Managing File Permissions and Ownership&lt;/h3>
&lt;p>Three possible levels of file ownership in Linux: &lt;strong>user&lt;/strong>, &lt;strong>group&lt;/strong>, and &lt;strong>other&lt;/strong>.&lt;/p>
&lt;ul>
&lt;li>Whoever creates a file, namely the &lt;strong>user&lt;/strong> at the time of creation, becomes the owner of that file by default.&lt;/li>
&lt;li>A &lt;strong>group&lt;/strong> of users can also share ownership of a file.&lt;/li>
&lt;li>The &lt;strong>other&lt;/strong> category essentially refers anyone in the universe with access to your Linux machine - careful when assigning ownership permission to this level!&lt;/li>
&lt;/ul>
&lt;p>Only an official owner of a file is allowed to change its permissions. This means that only owners can decide who can read the file, write to it, or execute it.&lt;/p>
&lt;h4 id="viewing-file-permissions">Viewing file permissions&lt;/h4>
&lt;p>Example:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ &lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;Who can read this file?&amp;#34;&lt;/span> &amp;gt; my_new_file
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">$ more my_new_file
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">Who can &lt;span class="nb">read&lt;/span> this file?
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">$ ls -l my_new_file
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">-rw-r--r-- &lt;span class="m">1&lt;/span> theia users &lt;span class="m">25&lt;/span> Dec &lt;span class="m">22&lt;/span> 17:47 x
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Here we&amp;rsquo;ve echoed the string &lt;code>&amp;quot;Who can read this file?&amp;quot;&lt;/code> into a new file called &lt;code>my_new_file&lt;/code>. The next line uses the &lt;code>more&lt;/code> command to print the contents of the new file.&lt;/p>
&lt;p>Finally, the &lt;code>ls&lt;/code> command with the &lt;code>-l&lt;/code> option displays the file&amp;rsquo;s (default) permissions: &lt;code>rw-r--r--&lt;/code>.&lt;/p>
&lt;ul>
&lt;li>The first three characters (&lt;code>rw-&lt;/code>) define the &lt;strong>user&lt;/strong> permissions. You, being the user, have the permission &lt;code>rw-&lt;/code>, which means you have read and write permissions by default, but do not have execution permissions. Otherwise there would be an &lt;code>x&lt;/code> in place of the last &lt;code>-&lt;/code>.&lt;/li>
&lt;li>The next three (&lt;code>r--&lt;/code>) the &lt;strong>group&lt;/strong> pemissions. The final three (&lt;code>r--&lt;/code>) the &lt;strong>other&lt;/strong> permissions.&lt;/li>
&lt;/ul>
&lt;p>Looking at the entire line, &lt;code>rw-r--r--&lt;/code>, you can see that anyone can read the file, nobody can execute it, and you are the only user that can write to it.&lt;/p>
&lt;div class="flex px-4 py-3 mb-6 rounded-md bg-primary-100 dark:bg-primary-900">
&lt;span class="pr-3 pt-1 text-primary-600 dark:text-primary-300">
&lt;svg height="24" viewBox="0 0 370 391" xmlns="http://www.w3.org/2000/svg">&lt;g clip-rule="evenodd" fill-rule="evenodd">&lt;path d="m207.5 22.4 114.4 66.6c13.5 7.9 21.9 22.4 21.9 38v136.4c0 17.3-9.3 33.3-24.5 41.8l-113.5 63.9a49.06 49.06 0 0 1 -48.5-.2l-104.5-60.1c-16.4-9.5-26.6-27-26.6-45.9v-129.5c0-19.1 9.9-36.8 26.1-46.8l102.8-63.5c16-9.9 36.2-10.1 52.4-.7z" fill="#ff4088" stroke="#c9177e" stroke-width="27" />&lt;path d="m105.6 298.2v-207.2h43.4v75.5h71.9v-75.5h43.5v207.2h-43.5v-90.6h-71.9v90.6z" fill="#fff" />&lt;/g>&lt;/svg>
&lt;/span>
&lt;span class="dark:text-neutral-300">&lt;em>The&lt;/em> &lt;code>-&lt;/code> &lt;em>at the very beginning of the line in the terminal means that the permissions are referring to a file. If you were getting the permissions to a directory, you would see a&lt;/em> &lt;code>d&lt;/code> &lt;em>in the front for &amp;ldquo;directory&amp;rdquo;.&lt;/em>&lt;/span>
&lt;/div>
&lt;h4 id="directory-permissions">Directory permissions&lt;/h4>
&lt;p>The permissions for directories are similar but distinct for files. Though directories use the same &lt;code>rwx&lt;/code> format, the symbols have slightly different meanings.&lt;/p>
&lt;p>The following table illustrates the meanings of each permission for directories:&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Directory Permission&lt;/th>
&lt;th>Permissible action(s)&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>&lt;code>r&lt;/code>&lt;/td>
&lt;td>List directory contents using &lt;code>ls&lt;/code> command&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>w&lt;/code>&lt;/td>
&lt;td>Add or remove files or directories&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>x&lt;/code>&lt;/td>
&lt;td>Enter directory using &lt;code>cd&lt;/code> command&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;h4 id="making-a-file-private">Making a file private&lt;/h4>
&lt;p>You can revoke read permissions from your group and all other users by using the &lt;code>chmod&lt;/code> command.&lt;/p>
&lt;p>Example:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ chmod go-r my_new_file
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">$ ls -l my_new_file
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">-rw------- &lt;span class="m">1&lt;/span> theia users &lt;span class="m">24&lt;/span> Dec &lt;span class="m">22&lt;/span> 18:49 my_new_file
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>In the &lt;code>chmod&lt;/code> command, &lt;code>go-r&lt;/code> is the permission change to be applied, which in this case means removing for the group (&lt;code>g&lt;/code>) and others (&lt;code>o&lt;/code>) the read (&lt;code>r&lt;/code>) permission.&lt;/p>
&lt;h4 id="executable-file">Executable file&lt;/h4>
&lt;p>A Linux file is executable if it contains instructions that can be directly interpreted by the operating system. Basically, an exectuable file is a ready-to-run program. They&amp;rsquo;re also referred to as &lt;strong>binaries&lt;/strong> or &lt;strong>executables&lt;/strong>.&lt;/p>
&lt;p>&lt;strong>Script&lt;/strong> is a particular kind of executable. &lt;strong>shell scripting&lt;/strong>, or more specifically &lt;strong>Bash scripting&lt;/strong>, which is writing scripts in Bash (&lt;em>born-again shell&lt;/em>), a very popular shell scripting language. A shell script is a plain text file that can be interpreted by a shell.&lt;/p>
&lt;p>Formally speaking, for a text file to be considered an executable shell script for a given user, it needs to have two things:&lt;/p>
&lt;ol>
&lt;li>Execute permissions set for that user&lt;/li>
&lt;li>A directive, called a &amp;ldquo;shebang&amp;rdquo;, in its first line to declare itself to the operating system as a binary&lt;/li>
&lt;/ol></description></item><item><title>Text Files, Networking, and Archiving Commands</title><link>https://haobin-tan.netlify.app/docs/coding/linux/getting-started/02_01_text_network_archiving_commands/</link><pubDate>Sat, 17 Aug 2024 00:00:00 +0000</pubDate><guid>https://haobin-tan.netlify.app/docs/coding/linux/getting-started/02_01_text_network_archiving_commands/</guid><description>&lt;h2 id="text-file-commands">Text File Commands&lt;/h2>
&lt;h3 id="viewing-file-content">Viewing file content&lt;/h3>
&lt;p>&lt;code>cat&lt;/code> (catenate): Print entire file contents&lt;/p>
&lt;ul>
&lt;li>Limitation: If the file content is long, the output will take up the entire terminal window&lt;/li>
&lt;/ul>
&lt;p>&lt;code>more&lt;/code>: Print file content page-by-page&lt;/p>
&lt;p>&lt;code>head&lt;/code>: Print first 10 lines of file&lt;/p>
&lt;ul>
&lt;li>&lt;code>head -n &amp;lt;N&amp;gt;&lt;/code>: Print first N lines of file&lt;/li>
&lt;/ul>
&lt;p>&lt;code>tail&lt;/code>: Print last 10 lines of file&lt;/p>
&lt;ul>
&lt;li>&lt;code>tail -n &amp;lt;N&amp;gt;&lt;/code>: Print last N lines of file&lt;/li>
&lt;/ul>
&lt;p>&lt;code>wc&lt;/code> (word count): Count lines, words, characters&lt;/p>
&lt;ul>
&lt;li>&lt;code>-l&lt;/code>/&lt;code>-w&lt;/code>/&lt;code>-c&lt;/code>: View only line/word/character count&lt;/li>
&lt;/ul>
&lt;h3 id="wrangling-text-files">Wrangling text files&lt;/h3>
&lt;p>&lt;code>sort&lt;/code>: Sort the lines of a file alpha-numerically and prints the sorted result to standard output.&lt;/p>
&lt;ul>
&lt;li>&lt;code>-r&lt;/code>: Reverse order&lt;/li>
&lt;/ul>
&lt;p>&lt;code>uniq&lt;/code> (unique): Filter out the repeated lines&lt;/p>
&lt;ul>
&lt;li>Note: &lt;code>uniq&lt;/code> only removes duplicated lines if they are consecutive!&lt;/li>
&lt;/ul>
&lt;p>&lt;code>grep&lt;/code> (global regular expression print): Return lines of a file matching a specified pattern&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Example&lt;/p>
&lt;p>
&lt;figure >
&lt;div class="flex justify-center ">
&lt;div class="w-100" >&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/%E6%88%AA%E5%B1%8F2024-08-17%2013.38.36.png" alt="截屏2024-08-17 13.38.36" loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Some frequently used options for &lt;code>grep&lt;/code> include:&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Option&lt;/th>
&lt;th>Description&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>&lt;code>-n&lt;/code>&lt;/td>
&lt;td>Along with the matching lines, also print the line numbers&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>-c&lt;/code>&lt;/td>
&lt;td>Get the count of matching lines&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>-i&lt;/code>&lt;/td>
&lt;td>Ignore the case of the text while matching&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>-v&lt;/code>&lt;/td>
&lt;td>Print all lines which do not contain the pattern&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>-w&lt;/code>&lt;/td>
&lt;td>Match only if the pattern matches whole words&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;/li>
&lt;/ul>
&lt;p>&lt;code>cut&lt;/code>: Extract a section from each file&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Example&lt;/p>
&lt;ul>
&lt;li>Extract the second to ninth characters from every line&lt;/li>
&lt;/ul>
&lt;p>
&lt;figure >
&lt;div class="flex justify-center ">
&lt;div class="w-100" >&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/%E6%88%AA%E5%B1%8F2024-08-17%2013.41.17.png" alt=" " loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Extract the last names&lt;/p>
&lt;p>
&lt;figure >
&lt;div class="flex justify-center ">
&lt;div class="w-100" >&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/%E6%88%AA%E5%B1%8F2024-08-17%2013.42.29.png" alt="截屏2024-08-17 13.42.29" loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;ul>
&lt;li>&lt;code>-d ' '&lt;/code>: Specify space as the field delimiter&lt;/li>
&lt;li>&lt;code>-f2&lt;/code>: Return the second filed from each line&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;p>&lt;code>paste&lt;/code>: Merge lines from different files&lt;/p>
&lt;p>&lt;code>tr&lt;/code>: Trim repeated characters to a single character.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ &lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;There are too many spaces in this sentence.&amp;#34;&lt;/span> &lt;span class="p">|&lt;/span> tr -s &lt;span class="s2">&amp;#34; &amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">There are too many spaces in this sentence.
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;code>xargs&lt;/code>: Trim leading and trailing spaces from a string.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ &lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34; Never start or end a sentence with a space. &amp;#34;&lt;/span> &lt;span class="p">|&lt;/span> xargs
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">Never start or end a sentence with a space.
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;code>rev&lt;/code> : Reverse the order of characters on a line of text.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ &lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;.sdrawkcab saw ecnetnes sihT&amp;#34;&lt;/span> &lt;span class="p">|&lt;/span> rev
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">This sentence was backwards.
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="networking-commands">Networking Commands&lt;/h2>
&lt;h3 id="brief-introduction-to-networking">Brief introduction to networking&lt;/h3>
&lt;h4 id="computer-networks">Computer networks&lt;/h4>
&lt;p>A &lt;strong>computer network&lt;/strong> is a set of computers that are able to communicate with each other and share &lt;strong>resources&lt;/strong> provided by &lt;strong>network nodes&lt;/strong>. Examples of computer networks include &lt;strong>Local Area Networks (LANs)&lt;/strong>, &lt;strong>Wide Area Networks (WANs)&lt;/strong>, and the entire Internet. The Internet, or World Wide Web, is essentially a giant network of computer networks.&lt;/p>
&lt;p>A network &lt;strong>resource&lt;/strong> is any object, such as a file or document, which can be &lt;em>identified&lt;/em> by the network.&lt;/p>
&lt;ul>
&lt;li>An object is &lt;em>&lt;strong>identifiable&lt;/strong>&lt;/em> if it can be assigned a &lt;em>unique&lt;/em> name and address that the network can use to identify and access it.&lt;/li>
&lt;/ul>
&lt;p>A &lt;strong>network node&lt;/strong> is any device that participates in a network.&lt;/p>
&lt;ul>
&lt;li>A network can include any device which is not necessarily a computer but is part of the network’s infrastructure. Examples of network nodes include modems, network switches, hubs, and wifi hotspots.&lt;/li>
&lt;/ul>
&lt;h4 id="host-clients-and-servers">Host, clients, and servers&lt;/h4>
&lt;p>A &lt;strong>host&lt;/strong> is a special type of node in a computer network - it is a computer that can function as a &lt;strong>server&lt;/strong> or a &lt;strong>client&lt;/strong> on a network.&lt;/p>
&lt;p>A &lt;strong>server&lt;/strong> is a host computer that is able to accept a connection from a &lt;strong>client&lt;/strong> host and fulfill certain resource requests made by the client.&lt;/p>
&lt;p>Many hosts can perform either role, acting as both client and server.&lt;/p>
&lt;h4 id="packets-and-pings">Packets and pings&lt;/h4>
&lt;p>A &lt;strong>network packet&lt;/strong> is a formatted chunk of data that can be transmitted over a network.&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Today&amp;rsquo;s computer networks typically use communication protocols that are based on such packets of information. Every packet consists of two types of data:&lt;/p>
&lt;/li>
&lt;li>
&lt;p>the &lt;strong>control information&lt;/strong>&lt;/p>
&lt;/li>
&lt;li>
&lt;p>the &lt;strong>payload&lt;/strong>.&lt;/p>
&lt;/li>
&lt;/ul>
&lt;p>The control information is data about how and where to deliver the payload, such as the source and destination network addresses, while the payload is the message being sent.&lt;/p>
&lt;p>The &lt;code>ping&lt;/code> command works by sending special &amp;rsquo;echo request&amp;rsquo; packets to a host and waiting for a response from the host.&lt;/p>
&lt;blockquote>
&lt;p>&lt;code>ping&lt;/code> is a utility available on most operating systems that have networking capability. Linux has its own implementation of the &lt;code>ping&lt;/code> command that&amp;rsquo;s used to test and troubleshoot connectivity to other network hosts.&lt;/p>
&lt;/blockquote>
&lt;h4 id="urls-and-ip-adresses">URLs and IP adresses&lt;/h4>
&lt;p>IP stands for &amp;ldquo;Internet Protocol&amp;rdquo; which defines the format of data transmitted over the internet or a local network.&lt;/p>
&lt;p>An &lt;strong>IP address&lt;/strong> is a code used to uniquely identify any host on a network.&lt;/p>
&lt;blockquote>
&lt;p>An IP address can be used to establish a connection to a host and exchange packets with it, for example using the &lt;code>ping&lt;/code> command. In addition to their payload, IP packets - a type of network packet that conforms to the Internet Protocol - contain the IP addresses of the source and destination hosts.&lt;/p>
&lt;/blockquote>
&lt;p>A &lt;strong>URL&lt;/strong>, more commonly known as a web address, stands for &lt;em>Uniform Resource Locator&lt;/em>.&lt;/p>
&lt;blockquote>
&lt;p>A URL uniquely identifies a web resource and enables access to that resource. Typically the resource that a URL points to is a web page, but it can also be used for tasks such as transferring files, sending emails, and accessing databases.&lt;/p>
&lt;/blockquote>
&lt;blockquote>
&lt;p>For example, the URL to the Wikipedia page for URL is &lt;code>https://en.wikipedia.org/wiki/URL&lt;/code>. Just like for a typical URL, its format indicates a protocol (&lt;code>https&lt;/code>), a hostname (&lt;code>en.wikipedia.org&lt;/code>), and a file name (&lt;code>/wiki/URL&lt;/code>).&lt;/p>
&lt;/blockquote>
&lt;h3 id="networking-commands-1">Networking commands&lt;/h3>
&lt;p>&lt;code>hostname&lt;/code>: Print host name&lt;/p>
&lt;ul>
&lt;li>
&lt;p>&lt;code>-s&lt;/code>: Drop the domain suffix&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>-i&lt;/code>: Provide the IP address of the hostname&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Example&lt;/p>
&lt;p>
&lt;figure >
&lt;div class="flex justify-center ">
&lt;div class="w-100" >&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/%E6%88%AA%E5%B1%8F2024-08-17%2014.57.31.png" alt="截屏2024-08-17 14.57.31" loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;/li>
&lt;/ul>
&lt;p>&lt;code>ifconfig&lt;/code> (Interface configuration): Display or configure the system network interfaces&lt;/p>
&lt;ul>
&lt;li>&lt;code>eth0&lt;/code>: SHow ethernet config information&lt;/li>
&lt;/ul>
&lt;p>&lt;code>ping&lt;/code>: Send ICMP packets to URL and print response&lt;/p>
&lt;ul>
&lt;li>&lt;code>c&lt;/code>: Return a set number of ping results&lt;/li>
&lt;/ul>
&lt;p>&lt;code>curl&lt;/code> (Client URL): Transer data to and from URL(s)&lt;/p>
&lt;p>&lt;code>wget&lt;/code> (Web get): Download file(s) from a URL&lt;/p>
&lt;ul>
&lt;li>More focused than &lt;code>curl&lt;/code>, supports recursive file downloads&lt;/li>
&lt;/ul>
&lt;h2 id="archiving-and-compression-commands">Archiving and Compression Commands&lt;/h2>
&lt;p>Archiving and compression are distinct processes, which are usually combined&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Archiving&lt;/strong>: Process of storing information that you don&amp;rsquo;t use regularly but want to preserve
&lt;ul>
&lt;li>An “archive file” is a collection of data files and directories that are stored as a single file.&lt;/li>
&lt;li>Archiving makes the collection more portable and serves as a backup in case of loss or corruption.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>Compressing: Reducing the size of a file by taking advantage of redundancy in its information content.
&lt;ul>
&lt;li>Advantages
&lt;ul>
&lt;li>Preserve storage space&lt;/li>
&lt;li>Speed up data transfer&lt;/li>
&lt;li>Reduce bandwidth load&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;p>&lt;code>tar&lt;/code> (tape archiver): Archive and extract files, allows you to pack multiple files and directories into a single archive file.&lt;/p>
&lt;ul>
&lt;li>&lt;code>-c&lt;/code>: Create a new archive&lt;/li>
&lt;li>&lt;code>-f&lt;/code>: Tell tar to interpret its input from the file rather than from the default, which is standard input&lt;/li>
&lt;li>&lt;code>-z&lt;/code>: Compress the archive file with &lt;code>gzip&lt;/code>&lt;/li>
&lt;li>&lt;code>-v&lt;/code>: Verbosely list files processed&lt;/li>
&lt;li>&lt;code>tar -tf&lt;/code>: List archive contents&lt;/li>
&lt;li>&lt;code>tar -xf&lt;/code>: Extract (&amp;ldquo;untag&amp;rdquo;) file and folders&lt;/li>
&lt;li>&lt;code>tar -xzf&lt;/code>: Decompress and extract&lt;/li>
&lt;/ul>
&lt;p>&lt;code>zip&lt;/code>: Compress files and directories to an archive&lt;/p>
&lt;div class="flex px-4 py-3 mb-6 rounded-md bg-primary-100 dark:bg-primary-900">
&lt;span class="pr-3 pt-1 text-primary-600 dark:text-primary-300">
&lt;svg height="24" viewBox="0 0 370 391" xmlns="http://www.w3.org/2000/svg">&lt;g clip-rule="evenodd" fill-rule="evenodd">&lt;path d="m207.5 22.4 114.4 66.6c13.5 7.9 21.9 22.4 21.9 38v136.4c0 17.3-9.3 33.3-24.5 41.8l-113.5 63.9a49.06 49.06 0 0 1 -48.5-.2l-104.5-60.1c-16.4-9.5-26.6-27-26.6-45.9v-129.5c0-19.1 9.9-36.8 26.1-46.8l102.8-63.5c16-9.9 36.2-10.1 52.4-.7z" fill="#ff4088" stroke="#c9177e" stroke-width="27" />&lt;path d="m105.6 298.2v-207.2h43.4v75.5h71.9v-75.5h43.5v207.2h-43.5v-90.6h-71.9v90.6z" fill="#fff" />&lt;/g>&lt;/svg>
&lt;/span>
&lt;span class="dark:text-neutral-300">&lt;p>&lt;code>zip&lt;/code> compresses files prior to bundling them, Whereas &lt;code>tar&lt;/code>, with the &lt;code>-z&lt;/code> option, achieves compression by applying &lt;code>gzip&lt;/code> on the entire tarball, but only after bundling it.&lt;/p>
&lt;p>&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/%E6%88%AA%E5%B1%8F2024-08-17%2022.33.23.png" alt="截屏2024-08-17 22.33.23">&lt;/p>
&lt;/span>
&lt;/div>
&lt;p>&lt;code>unzip&lt;/code>: Extract and decompress zipped archive&lt;/p></description></item><item><title>Shell Scripting</title><link>https://haobin-tan.netlify.app/docs/coding/linux/getting-started/03_intro_shell_scripting/</link><pubDate>Thu, 15 Aug 2024 00:00:00 +0000</pubDate><guid>https://haobin-tan.netlify.app/docs/coding/linux/getting-started/03_intro_shell_scripting/</guid><description>&lt;h2 id="shell-scripting-basics">Shell Scripting Basics&lt;/h2>
&lt;p>A &lt;strong>script&lt;/strong> is a list of commands that can be interpreted and run by a program called &lt;strong>scripting  language&lt;/strong>, which is usually not compiled but interpreted at runtime instead.&lt;/p>
&lt;ul>
&lt;li>Generally slower to run than compiled languages, but they are also much easier and faster to develop&lt;/li>
&lt;li>Widely used to automate processes, such as
&lt;ul>
&lt;li>ETL jobs&lt;/li>
&lt;li>File backups and archiving&lt;/li>
&lt;li>System administration tasks&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;p>Shell script is an executable text file with an interpreter directive, a.k.a. &amp;ldquo;shebang&amp;rdquo; (&lt;code>#!&lt;/code>) directive, which has the form &amp;ldquo;pound, bang, interpreter&amp;rdquo; plus an optional argument&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="cp">#!interpreter [optional-arg]
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ul>
&lt;li>&lt;code>interpreter&lt;/code>: An absolute path to an executable program&lt;/li>
&lt;li>&lt;code>optional-arg&lt;/code>: A string representing a single argument&lt;/li>
&lt;/ul>
&lt;p>Example:&lt;/p>
&lt;ul>
&lt;li>&lt;code>#!/bin/sh&lt;/code> invokes the Bourne shell or other compatible shell program, from the bin directory.&lt;/li>
&lt;li>&lt;code>#!/bin/bash&lt;/code> invokes the BAsh shell&lt;/li>
&lt;li>&lt;code>#!/usr/bin/env python3&lt;/code>&lt;/li>
&lt;/ul>
&lt;h2 id="shell-variables">Shell Variables&lt;/h2>
&lt;p>&lt;strong>Shell variables&lt;/strong> offer a powerful way to store and later access or modify information such as numbers, character strings, and other data structures by name.&lt;/p>
&lt;p>Example&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ &lt;span class="nv">firstname&lt;/span>&lt;span class="o">=&lt;/span>Jeff
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">$ &lt;span class="nb">echo&lt;/span> &lt;span class="nv">$firstname&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">Jeff
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The first line assigns the value &lt;code>Jeff&lt;/code> to a new variable called &lt;code>firstname&lt;/code>. The next line accesses and displays the value of the variable, using the &lt;code>echo&lt;/code> command along with the special character &lt;code>$&lt;/code> in front of the variable name to extract its value, which is the string &lt;code>Jeff&lt;/code>. Thus, we have created a new shell variable called &lt;code>firstname&lt;/code> for which the value is &lt;code>Jeff&lt;/code>.&lt;/p>
&lt;p>Another way to create a shell variable is to use the &lt;code>read&lt;/code> command, which reads user input into a shell variable.&lt;/p>
&lt;p>Example:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ &lt;span class="nb">read&lt;/span> lastname
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">Grossman
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">$ &lt;span class="nb">echo&lt;/span> &lt;span class="nv">$lastname&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">Grossman
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The &lt;code>read&lt;/code> command is particularly useful in shell scripting. You can use it within a shell script to prompt users to input information, which is then stored in a shell variable and available for use by the shell script while it is running.&lt;/p>
&lt;h2 id="filters-pipes-and-variables">Filters, Pipes, and Variables&lt;/h2>
&lt;h4 id="filters">Filters&lt;/h4>
&lt;p>&lt;strong>Filters&lt;/strong> are shell commands, which&lt;/p>
&lt;ul>
&lt;li>Take input from standard input&lt;/li>
&lt;li>Send output to standard output&lt;/li>
&lt;li>Transform input data into output data&lt;/li>
&lt;li>Examples: &lt;code>wc&lt;/code>. &lt;code>cat&lt;/code>. &lt;code>more&lt;/code>, &lt;code>head&lt;/code>, &lt;code>sort&lt;/code>, &lt;code>grep&lt;/code>&lt;/li>
&lt;li>Filters can be chain together!&lt;/li>
&lt;/ul>
&lt;h4 id="pipe-pipeline">Pipe (Pipeline)&lt;/h4>
&lt;p>&lt;strong>Pipe command (&lt;code>|&lt;/code>)&lt;/strong> : for chaining filter commands&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Use pattern&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="o">[&lt;/span>&lt;span class="nb">command&lt;/span> 1&lt;span class="o">]&lt;/span> &lt;span class="p">|&lt;/span> &lt;span class="o">[&lt;/span>&lt;span class="nb">command&lt;/span> 2&lt;span class="o">]&lt;/span> &lt;span class="p">|&lt;/span> &lt;span class="o">[&lt;/span>&lt;span class="nb">command&lt;/span> 3&lt;span class="o">]&lt;/span> ... &lt;span class="p">|&lt;/span> &lt;span class="o">[&lt;/span>&lt;span class="nb">command&lt;/span> n&lt;span class="o">]&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>E.g.,&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nb">command&lt;/span> &lt;span class="m">1&lt;/span> &lt;span class="p">|&lt;/span> &lt;span class="nb">command&lt;/span> &lt;span class="m">2&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Output of &lt;code>command 1&lt;/code> is input of &lt;code>command 2&lt;/code>&lt;/p>
&lt;p>&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/%E6%88%AA%E5%B1%8F2024-08-18%2014.46.10.png" alt="截屏2024-08-18 14.46.10">&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Example&lt;/p>
&lt;p>&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/%E6%88%AA%E5%B1%8F2024-08-18%2014.01.00.png" alt="截屏2024-08-18 14.01.00">&lt;/p>
&lt;/li>
&lt;/ul>
&lt;h4 id="variables">Variables&lt;/h4>
&lt;ul>
&lt;li>
&lt;p>Scope limited to shell&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>set&lt;/code>: List all variables and their definitions that are visible to the current shell&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Define shell variables&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nv">var_name&lt;/span>&lt;span class="o">=&lt;/span>value &lt;span class="c1"># Note: NO space around &amp;#34;=&amp;#34;!&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Clear/Delete a variable\&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nb">unset&lt;/span> var_name
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Example&lt;/p>
&lt;p>&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/%E6%88%AA%E5%B1%8F2024-08-18%2014.09.56.png" alt="截屏2024-08-18 14.09.56">&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Environment variables&lt;/strong>: Just like shell variables, except they have extended scope. They persist in any child processes spawned by the shell in which they originate.&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Extend shell variable to environment variable:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nb">export&lt;/span> var_name
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Example&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nb">export&lt;/span> GREETINGS
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>makes the &lt;code>GREETINGS&lt;/code> variable defined in the example above an environment variable&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>env&lt;/code>: List all environment variables&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h2 id="useful-features-of-the-bash-shell">Useful Features of the Bash Shell&lt;/h2>
&lt;h3 id="metacharacters">Metacharacters&lt;/h3>
&lt;p>&lt;strong>Metacharacters&lt;/strong> are special characters that have meaning to the shell.&lt;/p>
&lt;ul>
&lt;li>
&lt;p>&lt;code>#&lt;/code>: Precedes a comment that the shell ignores&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>;&lt;/code>: Seprate commands typed on the same line, e.g.,&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ &lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;Hello&amp;#34;&lt;/span> &lt;span class="p">;&lt;/span> whoami
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;/li>
&lt;li>
&lt;p>&lt;code>*&lt;/code>: Filename expansion wildcard, represents any number of consecutive characters within a filename pattern&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>?&lt;/code>: Single character wildcard in filename expansion (a single-character version of &lt;code>*&lt;/code> ), e.g.,&lt;/p>
&lt;p>&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/%E6%88%AA%E5%B1%8F2024-08-18%2014.59.31.png" alt="截屏2024-08-18 14.59.31">&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>\&lt;/code>: Escape unique character interpretation&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>&amp;quot; &amp;quot;&lt;/code>: Interpret literally, interpret metacharacters within string&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>' '&lt;/code>: Interpret literally, escape all metacharacters within string&lt;/p>
&lt;/li>
&lt;/ul>
&lt;p>Example:&lt;/p>
&lt;p>&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/%E6%88%AA%E5%B1%8F2024-08-18%2015.36.03.png" alt="截屏2024-08-18 15.36.03">&lt;/p>
&lt;h3 id="io-redirection">I/O redirection&lt;/h3>
&lt;p>&lt;strong>Input/Output&lt;/strong>, or &lt;strong>I/O redirection&lt;/strong>, refers to a set of features used for redirecting either the standard input, the keyboard, or the standard output, the terminal.&lt;/p>
&lt;ul>
&lt;li>&lt;code>&amp;gt;&lt;/code>: Redirect output to the file. It also creates the file if it doesn’t exist and overwrites its contents if it already exists.&lt;/li>
&lt;li>&lt;code>&amp;gt;&amp;gt;&lt;/code>: Append output to the file&lt;/li>
&lt;li>&lt;code>2&amp;gt;&lt;/code>: Ridirect standard error to file&lt;/li>
&lt;li>&lt;code>2&amp;gt;&amp;gt;&lt;/code>: Append standard error to file&lt;/li>
&lt;li>&lt;code>&amp;lt;&lt;/code>: Redirect file contents to standard input&lt;/li>
&lt;/ul>
&lt;p>Example&lt;/p>
&lt;p>&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/%E6%88%AA%E5%B1%8F2024-08-18%2015.40.10.png" alt="截屏2024-08-18 15.40.10">&lt;/p>
&lt;h3 id="command-substitution">Command substitution&lt;/h3>
&lt;ul>
&lt;li>
&lt;p>Replace the command with its output: &lt;code>$(command)$&lt;/code> or &lt;code>command&lt;/code>. Example:&lt;/p>
&lt;p>&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/%E6%88%AA%E5%B1%8F2024-08-18%2015.42.02.png" alt="截屏2024-08-18 15.42.02">&lt;/p>
&lt;/li>
&lt;/ul>
&lt;h3 id="command-line-arguments">Command line arguments&lt;/h3>
&lt;ul>
&lt;li>Program arguments specified on the command line&lt;/li>
&lt;li>A way to pass arguments to a shell script&lt;/li>
&lt;/ul>
&lt;p>Example&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ ./MyBashScript.sh arg1 arg2
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="batch-vs-concurrent-modes">Batch vs. concurrent modes&lt;/h3>
&lt;p>&lt;strong>Batch mode&lt;/strong>: Commands run &lt;em>sequentially&lt;/em>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nb">command&lt;/span> 1&lt;span class="p">;&lt;/span> &lt;span class="nb">command&lt;/span> &lt;span class="m">2&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Command 2 only runs after command 1 is completed.&lt;/p>
&lt;p>&lt;strong>Concurrent mode&lt;/strong>: Commands run in &lt;em>parallel&lt;/em>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nb">command&lt;/span> &lt;span class="m">1&lt;/span> &lt;span class="p">&amp;amp;&lt;/span> &lt;span class="nb">command&lt;/span> &lt;span class="m">2&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The ampersand operator, after command 1, directs command 1 to operate in the background and passes control to command 2 in the foreground.&lt;/p>
&lt;h2 id="advanced-bash-scripting">Advanced Bash Scripting&lt;/h2>
&lt;h3 id="conditionals">Conditionals&lt;/h3>
&lt;p>Bash script conditionals use the following &lt;code>if&lt;/code>-&lt;code>then&lt;/code>-&lt;code>else&lt;/code> syntax:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="k">if&lt;/span> &lt;span class="o">[&lt;/span> condition &lt;span class="o">]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">then&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> statement_block_1
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">else&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> statement_block_2
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">fi&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="flex px-4 py-3 mb-6 rounded-md bg-primary-100 dark:bg-primary-900">
&lt;span class="pr-3 pt-1 text-primary-600 dark:text-primary-300">
&lt;svg height="24" viewBox="0 0 370 391" xmlns="http://www.w3.org/2000/svg">&lt;g clip-rule="evenodd" fill-rule="evenodd">&lt;path d="m207.5 22.4 114.4 66.6c13.5 7.9 21.9 22.4 21.9 38v136.4c0 17.3-9.3 33.3-24.5 41.8l-113.5 63.9a49.06 49.06 0 0 1 -48.5-.2l-104.5-60.1c-16.4-9.5-26.6-27-26.6-45.9v-129.5c0-19.1 9.9-36.8 26.1-46.8l102.8-63.5c16-9.9 36.2-10.1 52.4-.7z" fill="#ff4088" stroke="#c9177e" stroke-width="27" />&lt;path d="m105.6 298.2v-207.2h43.4v75.5h71.9v-75.5h43.5v207.2h-43.5v-90.6h-71.9v90.6z" fill="#fff" />&lt;/g>&lt;/svg>
&lt;/span>
&lt;span class="dark:text-neutral-300">&lt;ul>
&lt;li>You must always put spaces around your condition within the square brackets &lt;code>[&lt;/code> &lt;code>]&lt;/code>.&lt;/li>
&lt;li>Every &lt;code>if&lt;/code> condition block must be paired with a &lt;code>fi&lt;/code> to tell Bash where the condition block ends.&lt;/li>
&lt;li>The &lt;code>else&lt;/code> block is optional but recommended. If the condition evaluates to &lt;code>false&lt;/code> without an &lt;code>else&lt;/code> block, then nothing happens within the &lt;code>if&lt;/code> condition block. Consider options such as echoing a comment in &lt;code>statement_block_2&lt;/code> to indicate that the condition was evaluated as &lt;code>false&lt;/code>.&lt;/li>
&lt;/ul>
&lt;/span>
&lt;/div>
&lt;p>Example: the condition is checking whether the number of command-line arguments read by some Bash script, &lt;code>$#&lt;/code>, is equal to 2.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="k">if&lt;/span> &lt;span class="o">[[&lt;/span> &lt;span class="nv">$#&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="m">2&lt;/span> &lt;span class="o">]]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">then&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;number of arguments is equal to 2&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">else&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;number of arguments is not equal to 2&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">fi&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Notice: the use of the double square brackets, which is the syntax required for making integer comparisons in the condition &lt;code>[[ $# == 2 ]]&lt;/code>.&lt;/p>
&lt;div class="flex px-4 py-3 mb-6 rounded-md bg-primary-100 dark:bg-primary-900">
&lt;span class="pr-3 pt-1 text-primary-600 dark:text-primary-300">
&lt;svg height="24" viewBox="0 0 370 391" xmlns="http://www.w3.org/2000/svg">&lt;g clip-rule="evenodd" fill-rule="evenodd">&lt;path d="m207.5 22.4 114.4 66.6c13.5 7.9 21.9 22.4 21.9 38v136.4c0 17.3-9.3 33.3-24.5 41.8l-113.5 63.9a49.06 49.06 0 0 1 -48.5-.2l-104.5-60.1c-16.4-9.5-26.6-27-26.6-45.9v-129.5c0-19.1 9.9-36.8 26.1-46.8l102.8-63.5c16-9.9 36.2-10.1 52.4-.7z" fill="#ff4088" stroke="#c9177e" stroke-width="27" />&lt;path d="m105.6 298.2v-207.2h43.4v75.5h71.9v-75.5h43.5v207.2h-43.5v-90.6h-71.9v90.6z" fill="#fff" />&lt;/g>&lt;/svg>
&lt;/span>
&lt;span class="dark:text-neutral-300">&lt;p>&lt;code>$&lt;/code> can be used to access command-line arguments passed to the script.&lt;/p>
&lt;p>Examples:&lt;/p>
&lt;ul>
&lt;li>&lt;code>$0&lt;/code>: The name of the script.&lt;/li>
&lt;li>&lt;code>$1&lt;/code>, &lt;code>$2&lt;/code>, &lt;code>$3&lt;/code>, &amp;hellip;: The first, second, third, etc., arguments passed to the script.&lt;/li>
&lt;li>&lt;code>$#&lt;/code>: The number of arguments passed.&lt;/li>
&lt;li>&lt;code>$@&lt;/code> or &lt;code>$*&lt;/code>: All arguments as a list.&lt;/li>
&lt;li>&lt;code>$?&lt;/code>: The exit status of the last command.&lt;/li>
&lt;li>&lt;code>$$&lt;/code>: The process ID (PID) of the current script.&lt;/li>
&lt;/ul>
&lt;/span>
&lt;/div>
&lt;p>You can also make string comparisons. For example, assume you have a variable called &lt;code>string_var&lt;/code> that has the value &lt;code>&amp;quot;Yes&amp;quot;&lt;/code> assigned to it. Then the following statement evaluates to &lt;code>true&lt;/code>:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="sb">`&lt;/span>&lt;span class="o">[&lt;/span> &lt;span class="nv">$string_var&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="s2">&amp;#34;Yes&amp;#34;&lt;/span> &lt;span class="o">]&lt;/span>&lt;span class="sb">`&lt;/span> &lt;span class="c1"># Only need single square brackets for string comparison&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>You can also include multiple conditions to be satified by using the &lt;strong>&amp;ldquo;and&amp;rdquo; operator &lt;code>&amp;amp;&amp;amp;&lt;/code>&lt;/strong> or the &amp;ldquo;or&amp;rdquo; operator &lt;code>||&lt;/code>. Example&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="k">if&lt;/span> &lt;span class="o">[&lt;/span> condition1 &lt;span class="o">]&lt;/span> &lt;span class="o">&amp;amp;&amp;amp;&lt;/span> &lt;span class="o">[&lt;/span> condition2 &lt;span class="o">]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">then&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;conditions 1 and 2 are both true&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">else&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;one or both conditions are false&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">fi&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">if&lt;/span> &lt;span class="o">[&lt;/span> condition1 &lt;span class="o">]&lt;/span> &lt;span class="o">||&lt;/span> &lt;span class="o">[&lt;/span> condition2 &lt;span class="o">]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">then&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;conditions 1 or 2 are true&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">else&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;both conditions are false&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">fi&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;code>if&lt;/code>-&lt;code>then&lt;/code>-&lt;code>elif&lt;/code>-&lt;code>else&lt;/code> Syntax:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="k">if&lt;/span> &lt;span class="o">[&lt;/span> condition &lt;span class="o">]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">then&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> statement_block_1
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">elif&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> statement_block_2
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">else&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> statement_block_3
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">fi&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="logical-operators">Logical Operators&lt;/h3>
&lt;ul>
&lt;li>
&lt;p>&lt;code>==&lt;/code>: is equal to&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># If a variable a has a value of 2, the following condition evaluates to true; otherwise it evalutes to false.&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nv">$a&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="m">2&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;/li>
&lt;li>
&lt;p>&lt;code>!=&lt;/code>: is not equal to&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>&amp;lt;=&lt;/code> or &lt;code>-le&lt;/code>: is less than or equal to&lt;/p>
&lt;/li>
&lt;/ul>
&lt;p>More see: &lt;a href="https://tldp.org/LDP/abs/html/comparison-ops.html">Advanced Bash-Scripting Guide&lt;/a>&lt;/p>
&lt;h3 id="arithmetic-calculations">Arithmetic calculations&lt;/h3>
&lt;p>You can perform integer addition, subtraction, multiplication, and division using the notation &lt;code>$(())&lt;/code>.&lt;/p>
&lt;p>Example&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="k">$((&lt;/span>&lt;span class="m">3&lt;/span>&lt;span class="o">+&lt;/span>&lt;span class="m">2&lt;/span>&lt;span class="k">))&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nv">a&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="m">3&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nv">b&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="m">2&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nv">c&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="k">$((&lt;/span>&lt;span class="nv">$a&lt;/span>&lt;span class="o">+&lt;/span>&lt;span class="nv">$b&lt;/span>&lt;span class="k">))&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="nv">$c&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="flex px-4 py-3 mb-6 rounded-md bg-yellow-100 dark:bg-yellow-900">
&lt;span class="pr-3 pt-1 text-red-400">
&lt;svg height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">&lt;path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M12 9v3.75m-9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0zM12 15.75h.007v.008H12z"/>&lt;/svg>
&lt;/span>
&lt;span class="dark:text-neutral-300">Bash natively handles integer arithmetic but does not handle floating-point arithmetic. As a result, it will always truncate the decimal portion of a calculation result.&lt;/span>
&lt;/div>
&lt;p>Basic arithmetic operators:&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Symbol&lt;/th>
&lt;th>Operation&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>&lt;code>+&lt;/code>&lt;/td>
&lt;td>addition&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>-&lt;/code>&lt;/td>
&lt;td>subtraction&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>*&lt;/code>&lt;/td>
&lt;td>multiplication&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>/&lt;/code>&lt;/td>
&lt;td>division&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;h3 id="arrays">Arrays&lt;/h3>
&lt;p>An array is a space-delimited list contained in parentheses. To create an array, declare its name and contents:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nv">my_array&lt;/span>&lt;span class="o">=(&lt;/span>&lt;span class="m">1&lt;/span> &lt;span class="m">2&lt;/span> &lt;span class="s2">&amp;#34;three&amp;#34;&lt;/span> &lt;span class="s2">&amp;#34;four&amp;#34;&lt;/span> 5&lt;span class="o">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>If you want to add items to your array after creating it, you can add to your array by appending one element at a time:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nv">my_array&lt;/span>&lt;span class="o">+=(&lt;/span>&lt;span class="s2">&amp;#34;six&amp;#34;&lt;/span>&lt;span class="o">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nv">my_array&lt;/span>&lt;span class="o">+=(&lt;/span>7&lt;span class="o">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This adds elements &lt;code>&amp;quot;six&amp;quot;&lt;/code> and &lt;code>7&lt;/code> to the array &lt;code>my_array&lt;/code>.&lt;/p>
&lt;p>By using indexing, you can access individual or multiple elements of an array:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># print the first item of the array:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="si">${&lt;/span>&lt;span class="nv">my_array&lt;/span>&lt;span class="p">[0]&lt;/span>&lt;span class="si">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># print the third item of the array:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="si">${&lt;/span>&lt;span class="nv">my_array&lt;/span>&lt;span class="p">[2]&lt;/span>&lt;span class="si">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># print all array elements:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="si">${&lt;/span>&lt;span class="nv">my_array&lt;/span>&lt;span class="p">[@]&lt;/span>&lt;span class="si">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Get the array length:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nv">arr_len&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="si">${#&lt;/span>&lt;span class="nv">arr&lt;/span>&lt;span class="p">[@]&lt;/span>&lt;span class="si">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>or&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nv">arr_len&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="si">${#&lt;/span>&lt;span class="nv">arr&lt;/span>&lt;span class="p">[*]&lt;/span>&lt;span class="si">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Here &lt;code>#&lt;/code> gives the lenght of the array (or string).&lt;/p>
&lt;h3 id="for-loops">&lt;code>for&lt;/code> loops&lt;/h3>
&lt;p>You can use a &lt;code>for&lt;/code> loop along with indexing to iterate over all elements of an array.&lt;/p>
&lt;p>Syntax&lt;/p>
&lt;ul>
&lt;li>The &lt;code>for&lt;/code> loop requires a &lt;code>; do&lt;/code> component in order to cycle through the loop.&lt;/li>
&lt;li>You need to terminate the &lt;code>for&lt;/code> loop block with a &lt;code>done&lt;/code> statement.&lt;/li>
&lt;/ul>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="k">for&lt;/span> item in &lt;span class="si">${&lt;/span>&lt;span class="nv">arr&lt;/span>&lt;span class="p">[@]&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="p">;&lt;/span> &lt;span class="k">do&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1"># Handle $item&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">done&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="k">for&lt;/span> i in &lt;span class="si">${&lt;/span>&lt;span class="p">!arr[@]&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="p">;&lt;/span> &lt;span class="k">do&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1"># Handle ${arr[$i]}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">done&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Another way to implement a &lt;code>for&lt;/code> loop when you know how many iterations you want is as follows. For example, the following code prints the number 0 through 6.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nv">N&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="m">6&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">for&lt;/span> &lt;span class="o">((&lt;/span> &lt;span class="nv">i&lt;/span>&lt;span class="o">=&lt;/span>0&lt;span class="p">;&lt;/span> i&amp;lt;&lt;span class="o">=&lt;/span>&lt;span class="nv">$N&lt;/span>&lt;span class="p">;&lt;/span> i++ &lt;span class="o">))&lt;/span> &lt;span class="p">;&lt;/span> &lt;span class="k">do&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nb">echo&lt;/span> &lt;span class="nv">$i&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">done&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="scheduling-jobs-using-cron">Scheduling Jobs Using Cron&lt;/h2>
&lt;p>Job scheduling: Schedule jobs to run automatically at certain times. E.g., &amp;ldquo;Backup script to run every Sunday at 2AM&amp;rdquo;.&lt;/p>
&lt;p>&lt;strong>Cron&lt;/strong> is the general name of the tool that runs scheduled jobs consisting of shell commands or shell scripts.&lt;/p>
&lt;p>&lt;strong>Crond&lt;/strong> is the daemon or service that interprets “crontab files” every minute and submits the corresponding jobs to cron at scheduled&lt;/p>
&lt;p>times.&lt;/p>
&lt;p>A &lt;strong>crontab&lt;/strong>, short for “cron table,” is a simple text file containing jobs and schedule data. Crontab is also a command that invokes a text editor to allow you to edit a crontab file.&lt;/p>
&lt;h4 id="adding-jobs-in-crontab">Adding jobs in Crontab&lt;/h4>
&lt;p>Enter &lt;code>crontab -e&lt;/code> opens the default text editor and allows you to specify a new schedule and a new command.&lt;/p>
&lt;p>Syntax:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">m h dom mon dow &lt;span class="nb">command&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Five time-and-date fields &lt;code>m h dom mon dow&lt;/code> stand for: minute, hour, day of month, month, and day of week. All five positions must have either a numeric entry or an asterisk (&lt;code>*&lt;/code>), which is a wildcard symbol that means “any.”&lt;/p>
&lt;p>The five time-and-date fields cannot contain spaces and their allowed values are as follows:&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Field&lt;/th>
&lt;th>Allowed values&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>minute&lt;/td>
&lt;td>0-59&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>hour&lt;/td>
&lt;td>0-23, 0 = midnight&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>day&lt;/td>
&lt;td>1-31&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>month&lt;/td>
&lt;td>1-12&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>weekday&lt;/td>
&lt;td>0-6, 0 = Sunday&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>Example: Append the current date to the file ‘sundays.txt’ at 15:30 every Sunday.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="m">30&lt;/span> &lt;span class="m">15&lt;/span> * * &lt;span class="m">0&lt;/span> date &amp;gt;&amp;gt; sundays.txt
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Then save and exit the editor.&lt;/p>
&lt;h4 id="viewing-and-removing-cron-jobs">Viewing and removing Cron jobs&lt;/h4>
&lt;p>Viewing: &lt;code>crontab -l&lt;/code>&lt;/p>
&lt;p>Removing&lt;/p>
&lt;ul>
&lt;li>Use &lt;code>crontab -e&lt;/code> to open the editor and remove specific line(s)&lt;/li>
&lt;li>&lt;code>crontabl r&lt;/code>: Remove all cron jobs&lt;/li>
&lt;/ul></description></item><item><title>Cheatsheet</title><link>https://haobin-tan.netlify.app/docs/coding/linux/getting-started/04_cheatsheet/</link><pubDate>Tue, 20 Aug 2024 00:00:00 +0000</pubDate><guid>https://haobin-tan.netlify.app/docs/coding/linux/getting-started/04_cheatsheet/</guid><description>&lt;h2 id="introduction-to-linux">Introduction to Linux&lt;/h2>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Command&lt;/th>
&lt;th>Syntax&lt;/th>
&lt;th>Description&lt;/th>
&lt;th>Example&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>List&lt;/td>
&lt;td>&lt;code>ls [OPTIONS] [FILE/DIRECTORY]&lt;/code>&lt;/td>
&lt;td>List files and directories at path&lt;/td>
&lt;td>&lt;code>ls /home/user/documents&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Print Working Directory&lt;/td>
&lt;td>&lt;code>pwd&lt;/code>&lt;/td>
&lt;td>Print present working directory&lt;/td>
&lt;td>&lt;code>pwd&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Change Directory&lt;/td>
&lt;td>&lt;code>cd [DIRECTORY]&lt;/code>&lt;/td>
&lt;td>Change current directory&lt;/td>
&lt;td>&lt;code>cd /home/user/documents&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Super user do&lt;/td>
&lt;td>&lt;code>sudo [COMMAND]&lt;/code>&lt;/td>
&lt;td>Run command with superuser privileges&lt;/td>
&lt;td>&lt;code>sudo apt update&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Text Editor&lt;/td>
&lt;td>&lt;code>nano [FILE]&lt;/code>&lt;/td>
&lt;td>Open file with Nano text editor&lt;/td>
&lt;td>&lt;code>nano myfile.txt&lt;/code>&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;h2 id="introduction-to-linux-commands">Introduction to Linux Commands&lt;/h2>
&lt;h3 id="informational-navigational--management-commands">Informational, Navigational, &amp;amp; Management Commands&lt;/h3>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Command&lt;/th>
&lt;th>Syntax&lt;/th>
&lt;th>Description&lt;/th>
&lt;th>Example&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>Who Am I&lt;/td>
&lt;td>&lt;code>whoami&lt;/code>&lt;/td>
&lt;td>Return username&lt;/td>
&lt;td>&lt;code>whoami&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>User ID&lt;/td>
&lt;td>&lt;code>id&lt;/code>&lt;/td>
&lt;td>Return current user or group ID&lt;/td>
&lt;td>&lt;code>id&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>System Information&lt;/td>
&lt;td>&lt;code>uname [OPTIONS]&lt;/code>&lt;/td>
&lt;td>Display system information&lt;/td>
&lt;td>&lt;code>uname -a&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Manual Pages&lt;/td>
&lt;td>&lt;code>man [COMMAND]&lt;/code>&lt;/td>
&lt;td>Display manual page for a command&lt;/td>
&lt;td>&lt;code>man ls&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Curl&lt;/td>
&lt;td>&lt;code>curl [OPTIONS] [URL]&lt;/code>&lt;/td>
&lt;td>Transfer data from or to server&lt;/td>
&lt;td>&lt;code>curl https://some_website.com&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Date&lt;/td>
&lt;td>&lt;code>date [OPTIONS]&lt;/code>&lt;/td>
&lt;td>Display current date and time&lt;/td>
&lt;td>&lt;code>date&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Find&lt;/td>
&lt;td>&lt;code>find [DIRECTORY] [OPTIONS]&lt;/code>&lt;/td>
&lt;td>Find files and directories at specified path&lt;/td>
&lt;td>&lt;code>find /home/user -name '*.txt'&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Make Directory&lt;/td>
&lt;td>&lt;code>mkdir [DIRECTORY]&lt;/code>&lt;/td>
&lt;td>Create new directory&lt;/td>
&lt;td>&lt;code>mkdir myfolder&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Remove Directory&lt;/td>
&lt;td>&lt;code>rmdir [DIRECTORY]&lt;/code>&lt;/td>
&lt;td>Remove empty directory&lt;/td>
&lt;td>&lt;code>rmdir myfolder&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Process Status&lt;/td>
&lt;td>&lt;code>ps [OPTIONS]&lt;/code>&lt;/td>
&lt;td>Display process status information&lt;/td>
&lt;td>&lt;code>ps -ef&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Table of Processes&lt;/td>
&lt;td>&lt;code>top&lt;/code>&lt;/td>
&lt;td>Display live system resource usage&lt;/td>
&lt;td>&lt;code>top&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Disk Usage&lt;/td>
&lt;td>&lt;code>df [OPTIONS] [FILESYSTEM]&lt;/code>&lt;/td>
&lt;td>Display disk space usage&lt;/td>
&lt;td>&lt;code>df -h&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Create Empty File&lt;/td>
&lt;td>&lt;code>touch [FILE]&lt;/code>&lt;/td>
&lt;td>Create new file or update timestamp&lt;/td>
&lt;td>&lt;code>touch myfile.txt&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Copy&lt;/td>
&lt;td>&lt;code>cp [OPTIONS] [SOURCE] [DESTINATION]&lt;/code>&lt;/td>
&lt;td>Copy files or directories from source to destination&lt;/td>
&lt;td>&lt;code>cp myfile.txt /home/user/documents&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Move&lt;/td>
&lt;td>&lt;code>mv [OPTIONS] [SOURCE] [DESTINATION]&lt;/code>&lt;/td>
&lt;td>Move or rename files and directories&lt;/td>
&lt;td>&lt;code>mv myfile.txt /home/user/documents&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Remove&lt;/td>
&lt;td>&lt;code>rm [OPTIONS] [FILE/DIRECTORY]&lt;/code>&lt;/td>
&lt;td>Remove files&lt;/td>
&lt;td>&lt;code>rm my_scratch_file.txt&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;/td>
&lt;td>&lt;/td>
&lt;td>Remove nonempty directory&lt;/td>
&lt;td>&lt;code>rm -r path_to_temp_directory&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;/td>
&lt;td>&lt;code>rmdir [OPTIONS] [DIRECTORY]&lt;/code>&lt;/td>
&lt;td>Remove empty directory&lt;/td>
&lt;td>&lt;code>rmdir path_to_my_directory&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Change Mode&lt;/td>
&lt;td>&lt;code>chmod [OPTIONS] [MODE] [FILE]&lt;/code>&lt;/td>
&lt;td>Change file or directory permissions&lt;/td>
&lt;td>&lt;code>chmod u+x myfile.txt&lt;/code>&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;h3 id="working-with-text-files-networking--archiving-commands">Working with Text Files, Networking &amp;amp; Archiving Commands&lt;/h3>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Command&lt;/th>
&lt;th>Syntax&lt;/th>
&lt;th>Description&lt;/th>
&lt;th>Example&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>Concatenate&lt;/td>
&lt;td>&lt;code>cat [FILE]&lt;/code>&lt;/td>
&lt;td>Display the contents of a file&lt;/td>
&lt;td>&lt;code>cat myfile.txt&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;/td>
&lt;td>&lt;/td>
&lt;td>Concatentate and display contents of multiple files&lt;/td>
&lt;td>&lt;code>cat file1 file2&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>More&lt;/td>
&lt;td>&lt;code>more [FILE]&lt;/code>&lt;/td>
&lt;td>Display file one screen at a time&lt;/td>
&lt;td>&lt;code>more myfile.txt&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Head&lt;/td>
&lt;td>&lt;code>head [OPTIONS] [FILE]&lt;/code>&lt;/td>
&lt;td>Display first N lines of file&lt;/td>
&lt;td>&lt;code>head -5 myfile.txt&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Tail&lt;/td>
&lt;td>&lt;code>tail [OPTIONS] [FILE]&lt;/code>&lt;/td>
&lt;td>Display last N lines of file&lt;/td>
&lt;td>&lt;code>tail -5 myfile.txt&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Echo&lt;/td>
&lt;td>&lt;code>echo [ARGUMENTS]&lt;/code>&lt;/td>
&lt;td>Display arguments in console&lt;/td>
&lt;td>&lt;code>echo Hello, World!&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Sort&lt;/td>
&lt;td>&lt;code>sort [OPTIONS] [FILE]&lt;/code>&lt;/td>
&lt;td>Alphanumerically sort file contents&lt;/td>
&lt;td>&lt;code>sort file.txt&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Unique&lt;/td>
&lt;td>&lt;code>uniq [OPTIONS] [FILE]&lt;/code>&lt;/td>
&lt;td>Report or remove consecutively repeated lines in file&lt;/td>
&lt;td>&lt;code>uniq file.txt&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Word Count&lt;/td>
&lt;td>&lt;code>wc [OPTIONS] [FILE]&lt;/code>&lt;/td>
&lt;td>Print the number of lines, words, and characters in a file&lt;/td>
&lt;td>&lt;code>wc file.txt&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Grep&lt;/td>
&lt;td>&lt;code>grep [OPTIONS] PATTERN [FILE]&lt;/code>&lt;/td>
&lt;td>Search for a specified pattern in a file&lt;/td>
&lt;td>&lt;code>grep &amp;quot;hello&amp;quot; file.txt&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Paste&lt;/td>
&lt;td>&lt;code>paste [OPTIONS] [FILE1] [FILE2]&lt;/code>&lt;/td>
&lt;td>Merge lines of files side by side&lt;/td>
&lt;td>&lt;code>paste file1.txt file2.txt&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Cut&lt;/td>
&lt;td>&lt;code>cut [OPTIONS] [FILE]&lt;/code>&lt;/td>
&lt;td>Remove sections from each line of a file&lt;/td>
&lt;td>&lt;code>cut -d&amp;quot;:&amp;quot; -f1 /etc/passwd&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Tar&lt;/td>
&lt;td>&lt;code>tar [OPTIONS] [FILE]&lt;/code>&lt;/td>
&lt;td>Archive files together into a single file&lt;/td>
&lt;td>&lt;code>tar -czvf archive.tar.gz /directory&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Zip&lt;/td>
&lt;td>&lt;code>zip [OPTIONS] [FILE]&lt;/code>&lt;/td>
&lt;td>Compress files into a zip archive&lt;/td>
&lt;td>&lt;code>zip archive.zip file1.txt file2.txt&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Unzip&lt;/td>
&lt;td>&lt;code>unzip [OPTIONS] [FILE]&lt;/code>&lt;/td>
&lt;td>Uncompress files from a zip archive&lt;/td>
&lt;td>&lt;code>unzip archive.zip&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Hostname&lt;/td>
&lt;td>&lt;code>hostname&lt;/code>&lt;/td>
&lt;td>Print the name of the current host system&lt;/td>
&lt;td>&lt;code>hostname&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Ping&lt;/td>
&lt;td>&lt;code>ping [OPTIONS] HOSTNAME/IP&lt;/code>&lt;/td>
&lt;td>Send ICMP ECHO_REQUEST packets to a network host&lt;/td>
&lt;td>&lt;code>ping google.com&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Ifconfig&lt;/td>
&lt;td>&lt;code>ifconfig [INTERFACE]&lt;/code>&lt;/td>
&lt;td>Display or configure network interface parameters&lt;/td>
&lt;td>&lt;code>ifconfig&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>IP&lt;/td>
&lt;td>&lt;code>ip [OPTIONS]&lt;/code>&lt;/td>
&lt;td>Show or manipulate routing, devices, policy routing, and tunnels&lt;/td>
&lt;td>&lt;code>ip addr&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Curl&lt;/td>
&lt;td>&lt;code>curl [OPTIONS] URL&lt;/code>&lt;/td>
&lt;td>Transfer data from or to a server&lt;/td>
&lt;td>&lt;code>curl https://some_website.com&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Wget&lt;/td>
&lt;td>&lt;code>wget [OPTIONS] URL&lt;/code>&lt;/td>
&lt;td>Download files from the web&lt;/td>
&lt;td>&lt;code>wget https://some_website.com/some_file.txt&lt;/code>&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;h2 id="introduction-to-shell-scripting">Introduction to Shell Scripting&lt;/h2>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Command&lt;/th>
&lt;th>Syntax&lt;/th>
&lt;th>Description&lt;/th>
&lt;th>Example&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>Shebang&lt;/td>
&lt;td>&lt;code>#!/bin/[shell]&lt;/code>&lt;/td>
&lt;td>First line of shell script&lt;/td>
&lt;td>&lt;code>#!/bin/bash&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Pipe&lt;/td>
&lt;td>filter1 | filter2&lt;/td>
&lt;td>Chain any number of filters&lt;/td>
&lt;td>`ls&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Locate executable&lt;/td>
&lt;td>&lt;code>which [EXECUTABLE]&lt;/code>&lt;/td>
&lt;td>Display location of &lt;code>bash&lt;/code> executable&lt;/td>
&lt;td>&lt;code>which bash&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Bash&lt;/td>
&lt;td>&lt;code>bash [SCRIPT]&lt;/code>&lt;/td>
&lt;td>Interpret and run script using Bash shell&lt;/td>
&lt;td>&lt;code>bash script.txt&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Set&lt;/td>
&lt;td>&lt;code>set [OPTION]&lt;/code>&lt;/td>
&lt;td>List all shell variables&lt;/td>
&lt;td>&lt;code>set&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Define variable&lt;/td>
&lt;td>&lt;code>[VARIABLE_NAME]=[VALUE]&lt;/code>&lt;/td>
&lt;td>Define shell variable by name and assign value&lt;/td>
&lt;td>&lt;code>name=&amp;quot;John&amp;quot;&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Read&lt;/td>
&lt;td>&lt;code>read [VARIABLE]&lt;/code>&lt;/td>
&lt;td>Read from standard input and store result in variable&lt;/td>
&lt;td>&lt;code>read name&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Env&lt;/td>
&lt;td>&lt;code>env&lt;/code>&lt;/td>
&lt;td>Print all environment variables and their values&lt;/td>
&lt;td>&lt;code>env&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Export&lt;/td>
&lt;td>&lt;code>export [VARIABLE]&lt;/code>&lt;/td>
&lt;td>Extend scope of local variable to all child processes&lt;/td>
&lt;td>&lt;code>export name&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Crontab&lt;/td>
&lt;td>&lt;code>crontab [OPTIONS]&lt;/code>&lt;/td>
&lt;td>Open crontab default editor&lt;/td>
&lt;td>&lt;code>crontab -e&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;/td>
&lt;td>&lt;/td>
&lt;td>List all cron jobs&lt;/td>
&lt;td>&lt;code>crontab -l&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Schedule tasks to run at specified times using cron daemon&lt;/td>
&lt;td>&lt;code>m h dom mon dow command&lt;/code>&lt;/td>
&lt;td>Append date/time to file every Sunday at 6:15 pm&lt;/td>
&lt;td>&lt;code>15 18 * * 0 date &amp;gt;&amp;gt; sundays.txt&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;/td>
&lt;td>&lt;/td>
&lt;td>Back up home directory every Monday at 3:00 am&lt;/td>
&lt;td>&lt;code>0 3 * * 1 tar -cvf my_backup_path\my_archive.tar.gz $HOME\&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;/td>
&lt;td>&lt;/td>
&lt;td>Run shell script first minute of first day of each month&lt;/td>
&lt;td>&lt;code>1 0 1 * * ./My_Script.sh &lt;/code>&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table></description></item><item><title>export</title><link>https://haobin-tan.netlify.app/docs/coding/linux/linux-learn/export/</link><pubDate>Thu, 17 Dec 2020 00:00:00 +0000</pubDate><guid>https://haobin-tan.netlify.app/docs/coding/linux/linux-learn/export/</guid><description>&lt;h2 id="export-command">&lt;code>export&lt;/code> command&lt;/h2>
&lt;ul>
&lt;li>
&lt;p>A built-in command of the Bash shell&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Ensure environment variables and functions to be passed to child processes&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Syntax&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nb">export&lt;/span> &lt;span class="o">[&lt;/span>-f&lt;span class="o">]&lt;/span> &lt;span class="o">[&lt;/span>-n&lt;span class="o">]&lt;/span> &lt;span class="o">[&lt;/span>name&lt;span class="o">[=&lt;/span>value&lt;span class="o">]&lt;/span> ...&lt;span class="o">]&lt;/span> or &lt;span class="nb">export&lt;/span> -p
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ul>
&lt;li>&lt;code>-f&lt;/code> : Names are exported as functions&lt;/li>
&lt;li>&lt;code>-n&lt;/code> : Remove names from export list&lt;/li>
&lt;li>&lt;code>-p&lt;/code> : List of all names that are exported in the current shell&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h2 id="usage">Usage&lt;/h2>
&lt;h3 id="display-all-the-exported-environment-variables-of-your-system">Display all the exported environment variables of your system&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nb">export&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="display-all-exported-variable-on-current-shell">Display all exported variable on current shell&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nb">export&lt;/span> -p
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>If you want to check specified variable:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nb">export&lt;/span> -p &lt;span class="p">|&lt;/span> grep &amp;lt;var&amp;gt;
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="using-export-with-functions">Using export with functions&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nb">export&lt;/span> -f function_name
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Example&lt;/p>
&lt;p>&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/linux-export-command3.png" alt="linux-export-command3">&lt;/p>
&lt;h3 id="assign-a-value-before-exporting-a-function-or-variable">Assign a value before exporting a function or variable&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nb">export&lt;/span> name&lt;span class="o">[=&lt;/span>value&lt;span class="o">]&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="set-an-environment-variable">&lt;strong>Set an environment variable&lt;/strong>&lt;/h3>
&lt;p>To create a new variable, use the export command followed by a variable name and its value.&lt;/p>
&lt;p>Syntax:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nb">export&lt;/span> &lt;span class="nv">NAME&lt;/span>&lt;span class="o">=&lt;/span>VALUE
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Example:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nb">export&lt;/span> &lt;span class="nv">PROJECT_DIR&lt;/span>&lt;span class="o">=&lt;/span>main/app
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">export&lt;/span> -p &lt;span class="p">|&lt;/span> grep PROJECT_DIR
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">declare -x PROJECT_DIR=&amp;#34;main/app&amp;#34;
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="flex px-4 py-3 mb-6 rounded-md bg-primary-100 dark:bg-primary-900">
&lt;span class="pr-3 pt-1 text-primary-600 dark:text-primary-300">
&lt;svg height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">&lt;path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="m11.25 11.25l.041-.02a.75.75 0 0 1 1.063.852l-.708 2.836a.75.75 0 0 0 1.063.853l.041-.021M21 12a9 9 0 1 1-18 0a9 9 0 0 1 18 0m-9-3.75h.008v.008H12z"/>&lt;/svg>
&lt;/span>
&lt;span class="dark:text-neutral-300">Variables set directly with &lt;code>export &lt;/code>are temporary variables, which means that the value defined for that variable will not take effect when you exit the current shell.&lt;/span>
&lt;/div>
&lt;h2 id="environment-varaible-path">Environment varaible &lt;code>PATH&lt;/code>&lt;/h2>
&lt;p>The &lt;code>PATH &lt;/code>variable is &lt;strong>an environment variable that contains an ordered list of paths&lt;/strong> that Unix/Linux will search for executables when running a command. Using these paths means that we do not have to specify an absolute path when running a command.&lt;/p>
&lt;p>For example&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="nv">$PATH&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Here &lt;code>:&lt;/code> is the separator. The &lt;code>PATH&lt;/code> variable is itself a &lt;strong>list of folders&lt;/strong> that are &amp;ldquo;walked&amp;rdquo; through when you run a command. In this case, the folders on &lt;code>PATH&lt;/code> are:&lt;/p>
&lt;ul>
&lt;li>&lt;code>/usr/lib/lightdm/lightdm&lt;/code>&lt;/li>
&lt;li>&lt;code>/usr/local/sbin&lt;/code>&lt;/li>
&lt;li>&lt;code>/usr/local/bin&lt;/code>&lt;/li>
&lt;/ul>
&lt;p>Linux/Unix traverses these folders in order until finding an executable.&lt;/p>
&lt;h3 id="adding-a-new-path">Adding a New Path&lt;/h3>
&lt;p>We can add a new path to the PATH variable using the &lt;a href="https://linux.die.net/man/1/export">&lt;em>export&lt;/em>&lt;/a> command.&lt;/p>
&lt;p>To append a new path, we reassign PATH with our &lt;strong>new path &lt;code>&amp;lt;new-path&amp;gt;&lt;/code> at the end&lt;/strong>:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nb">export&lt;/span> &lt;span class="nv">PATH&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="nv">$PATH&lt;/span>:&amp;lt;new-path&amp;gt;
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>For example, let&amp;rsquo;s say we&amp;rsquo;re gonna add a new path &lt;code>usr/local/bin&lt;/code> to the&lt;code>PATH&lt;/code> variable:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nb">export&lt;/span> &lt;span class="nv">PATH&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="nv">$PATH&lt;/span>:usr/local/bin
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="reference">Reference&lt;/h2>
&lt;ul>
&lt;li>
&lt;p>&lt;a href="https://www.javatpoint.com/linux-export-command">Linux export command&lt;/a>&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;a href="https://www.runoob.com/linux/linux-comm-export.html">Linux export 命令&lt;/a>&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;a href="https://stackoverflow.com/questions/35737627/what-does-the-colon-do-in-path">What does the colon do in PATH&lt;/a>&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;a href="https://www.baeldung.com/linux/path-variable">Adding a Path to the Linux PATH Variable&lt;/a>&lt;/p>
&lt;/li>
&lt;/ul></description></item><item><title>tee</title><link>https://haobin-tan.netlify.app/docs/coding/linux/linux-learn/tee/</link><pubDate>Fri, 18 Dec 2020 00:00:00 +0000</pubDate><guid>https://haobin-tan.netlify.app/docs/coding/linux/linux-learn/tee/</guid><description>&lt;p>&lt;strong>The &lt;code>tee&lt;/code> command reads from the standard input and writes to both standard output and one or more files at the same time.&lt;/strong>&lt;/p>
&lt;p>&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/tee-featured-400x200.png" alt="How to Use the Tee Command in Linux - Make Tech Easier">&lt;/p>
&lt;p>&lt;code>tee&lt;/code> is mostly used in combination with other commands through piping.&lt;/p>
&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/tee-pipe-20201218182022663.png" alt="Introduction to the tee Command - Baeldung on Linux" style="zoom:80%;" />
&lt;h2 id="syntax">Syntax&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-sh" data-lang="sh">&lt;span class="line">&lt;span class="cl">tee &lt;span class="o">[&lt;/span>OPTIONS&lt;span class="o">]&lt;/span> &lt;span class="o">[&lt;/span>FILE&lt;span class="o">]&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Copy&lt;/p>
&lt;ul>
&lt;li>&lt;code>OPTIONS&lt;/code>
&lt;ul>
&lt;li>&lt;code>-a&lt;/code> (&lt;code>--append&lt;/code>) : Do not overwrite the files instead append to the given files.&lt;/li>
&lt;li>&lt;code>-i&lt;/code> (&lt;code>--ignore-interrupts&lt;/code>) : Ignore interrupt signals.&lt;/li>
&lt;li>Use &lt;code>tee --help&lt;/code> to view all available options.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;code>FILE&lt;/code> One or more files. Each of which the output data is written to.&lt;/li>
&lt;/ul>
&lt;h2 id="usage">Usage&lt;/h2>
&lt;h3 id="display-output-and-write-it-in-a-file">Display output and write it in a file&lt;/h3>
&lt;p>The most basic usage of the &lt;code>tee&lt;/code> command is to display the standard output (&lt;code>stdout&lt;/code>) of a program and write it in a file.&lt;/p>
&lt;p>Example:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ &lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;Hello world!&amp;#34;&lt;/span> &lt;span class="p">|&lt;/span> tee hello-world.txt
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">Hello World!
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ cat hello-world.txt
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">Hello World!
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The output is piped to the &lt;code>tee&lt;/code> command, which displays the output to the terminal and writes the same information to the file &lt;code>hello-world.txt&lt;/code>.&lt;/p>
&lt;h3 id="write-to-multiple-files">Write to Multiple Files&lt;/h3>
&lt;p>The &lt;code>tee&lt;/code> command can also write to multiple files. To do so, specify a list of files separated by space as arguments:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ &lt;span class="nb">command&lt;/span> &lt;span class="p">|&lt;/span> tee file1.out file2.out file3.out
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="append-to-file">Append to File&lt;/h3>
&lt;p>By default, the &lt;code>tee&lt;/code> command will overwrite the specified file. Use the &lt;code>-a&lt;/code> (&lt;code>--append&lt;/code>) option to &lt;a href="https://linuxize.com/post/bash-append-to-file/">append the output to the file&lt;/a> :&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ &lt;span class="nb">command&lt;/span> &lt;span class="p">|&lt;/span> tee -a file.out
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>E.g.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ &lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;Hello world!&amp;#34;&lt;/span> &lt;span class="p">|&lt;/span> tee hello-world.txt
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">Hello world!
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ cat hello-world.txt
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">Hello World!
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ &lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;Hi world!&amp;#34;&lt;/span> &lt;span class="p">|&lt;/span> tee -a hello-world.txt
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">Hi world!
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ cat hello-world.txt
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">Hello World!
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">Hi world!
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="ignore-interrupt">Ignore Interrupt&lt;/h3>
&lt;p>To ignore interrupts use the &lt;code>-i&lt;/code> (&lt;code>--ignore-interrupts&lt;/code>) option. This is useful when stopping the command during execution with &lt;code>CTRL+C&lt;/code> and want &lt;code>tee&lt;/code> to exit gracefully.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ &lt;span class="nb">command&lt;/span> &lt;span class="p">|&lt;/span> tee -i file.out
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="hide-the-output">Hide the Output&lt;/h3>
&lt;p>If you don’t want &lt;code>tee&lt;/code> to write to the standard output, you can redirect it to &lt;code>/dev/null&lt;/code>:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ &lt;span class="nb">command&lt;/span> &lt;span class="p">|&lt;/span> tee file.out &amp;gt;/dev/null
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="reference">Reference&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://linuxize.com/post/linux-tee-command/">Linux Tee Command with Examples&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://www.runoob.com/linux/linux-comm-tee.html">Linux tee命令&lt;/a>&lt;/li>
&lt;/ul></description></item><item><title>chmod</title><link>https://haobin-tan.netlify.app/docs/coding/linux/linux-learn/chmod/</link><pubDate>Tue, 05 Jan 2021 00:00:00 +0000</pubDate><guid>https://haobin-tan.netlify.app/docs/coding/linux/linux-learn/chmod/</guid><description>&lt;h2 id="check-permissions">Check permissions&lt;/h2>
&lt;p>In terminal: &lt;code>ls –l [file_name]&lt;/code>&lt;/p>
&lt;p>For example:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ ls -l
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">-rw-r--r-- 1 EckoTan staff 12 Jan 5 18:02 hello-world.txt
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ul>
&lt;li>&lt;code>-rw-r--r--&lt;/code> : file permission&lt;/li>
&lt;li>&lt;code>EckoTan&lt;/code> : owner/creator of the file&lt;/li>
&lt;li>&lt;code>staff&lt;/code> : the group to which that owner belongs to&lt;/li>
&lt;li>&lt;code>Jan 5 18:02&lt;/code> : date of creation&lt;/li>
&lt;li>&lt;code>hello-world.txt&lt;/code> : the file&lt;/li>
&lt;/ul>
&lt;h2 id="permissions">Permissions&lt;/h2>
&lt;p>The permission settings, grouped in a string of characters (&lt;code>-&lt;/code>, &lt;code>r&lt;/code>, &lt;code>w&lt;/code>, &lt;code>x&lt;/code>), are classified into four sections:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>File type&lt;/strong>. There are three possibilities for the type. It can either be a regular file (&lt;strong>–&lt;/strong>), a directory (&lt;strong>d&lt;/strong>) or a link (&lt;strong>i&lt;/strong>).&lt;/li>
&lt;li>&lt;strong>File permission of the user (owner)&lt;/strong>&lt;/li>
&lt;li>&lt;strong>File permission of the owner’s group&lt;/strong>&lt;/li>
&lt;li>&lt;strong>File permission of other users&lt;/strong>&lt;/li>
&lt;/ol>
&lt;p>&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/file-permission-syntax-explained.jpg" alt="file-permission-syntax-explained">&lt;/p>
&lt;p>In permission of user, group, and others, the characters &lt;strong>-&lt;/strong>, &lt;strong>r&lt;/strong>, &lt;strong>w&lt;/strong>, and &lt;strong>x&lt;/strong> stand for &lt;strong>None&lt;/strong>, &lt;strong>read&lt;/strong>, &lt;strong>write&lt;/strong>, and &lt;strong>execute&lt;/strong>.&lt;/p>
&lt;ul>
&lt;li>&lt;code>-&lt;/code>: permission is NOT granted&lt;/li>
&lt;li>&lt;code>r&lt;/code>: &lt;strong>Read&lt;/strong> permissions. The file can be opened, and its content viewed. But it can not be modified (nor added/removed)&lt;/li>
&lt;li>&lt;code>w&lt;/code>: &lt;strong>Write&lt;/strong> permissions. The file can be edited, modified, and deleted.&lt;/li>
&lt;li>&lt;code>x&lt;/code>: &lt;strong>Execute&lt;/strong> permissions. If the file is a script or a program, it can be run (executed).This option is mainly used for running scripts.&lt;/li>
&lt;/ul>
&lt;h3 id="example">Example&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">-rw-r--r-- 1 EckoTan staff 12 Jan 5 18:02 hello-world.txt
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;code>hello-world.txt&lt;/code> is a regular file with read and write permission assigned to the owner, but gives read-only access to the group and others&lt;/p>
&lt;h2 id="change-permissions">Change permissions&lt;/h2>
&lt;p>The command that executes such tasks is the &lt;strong>&lt;code>chmod&lt;/code>&lt;/strong> command.&lt;/p>
&lt;p>Syntax:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ chmod &lt;span class="o">[&lt;/span>permission&lt;span class="o">]&lt;/span> &lt;span class="o">[&lt;/span>file_name&lt;span class="o">]&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>There are two ways to define permission:&lt;/p>
&lt;ol>
&lt;li>using &lt;strong>symbols&lt;/strong> (alphanumerical characters)&lt;/li>
&lt;li>using the &lt;strong>octal notation method&lt;/strong>&lt;/li>
&lt;/ol>
&lt;h3 id="define-file-permission-with-symbolic-mode">Define File Permission with Symbolic Mode&lt;/h3>
&lt;p>To use &lt;code>chmod&lt;/code> to set permissions, we need to tell it:&lt;/p>
&lt;ul>
&lt;li>
&lt;p>&lt;strong>Who&lt;/strong>: Who we are setting permissions for?&lt;/p>
&lt;ul>
&lt;li>&lt;code>u&lt;/code> : User, meaning the owner of the file.&lt;/li>
&lt;li>&lt;code>g&lt;/code> : Group, meaning members of the group the file belongs to.&lt;/li>
&lt;li>&lt;code>o&lt;/code> : Others, meaning people not governed by the &lt;code>u&lt;/code> and &lt;code>g&lt;/code> permissions.&lt;/li>
&lt;li>&lt;code>a&lt;/code> : All, meaning all of the above.&lt;/li>
&lt;/ul>
&lt;p>(If none of these are used, &lt;code>chmod&lt;/code> behaves as if “&lt;code>a&lt;/code>” had been used.)&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>What&lt;/strong>: What change are we making? Are we adding or removing the permission?&lt;/p>
&lt;ul>
&lt;li>&lt;code>–&lt;/code> : &lt;strong>Removes&lt;/strong> the permission.&lt;/li>
&lt;li>&lt;code>+&lt;/code> : Grants the permission. The permission is &lt;strong>added&lt;/strong> to the existing permissions.&lt;/li>
&lt;li>&lt;code>=&lt;/code> : &lt;strong>Set&lt;/strong> a permission and remove others.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Which&lt;/strong>: Which of the permissions are we setting?&lt;/p>
&lt;ul>
&lt;li>&lt;code>r&lt;/code>&lt;/li>
&lt;li>&lt;code>w&lt;/code>&lt;/li>
&lt;li>&lt;code>x&lt;/code>&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h4 id="example-1">Example&lt;/h4>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ ls -l
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">-rw-r--r-- 1 EckoTan staff 12 Jan 5 18:02 hello-world.txt
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>We want to give group members to have write permission on &lt;code>hello-world.txt&lt;/code>:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ chmod g+w hello-world.txt
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ ls -l
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">-rw-rw-r-- 1 EckoTan staff 12B Jan 5 18:02 hello-world.txt
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>We want the permission settings look like this&lt;/p>
&lt;ul>
&lt;li>&lt;code>u&lt;/code>: read, write, execute&lt;/li>
&lt;li>&lt;code>g&lt;/code>: read, write&lt;/li>
&lt;li>&lt;code>o&lt;/code>: read, write&lt;/li>
&lt;/ul>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ chmod &lt;span class="nv">u&lt;/span>&lt;span class="o">=&lt;/span>rwx,g&lt;span class="o">=&lt;/span>rw,o&lt;span class="o">=&lt;/span>rw hello-world.txt
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ ls -l
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">-rwxrw-rw- 1 EckoTan staff 12B Jan 5 18:02 hello-world.txt
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="define-permission-using-numerical-shorthand">Define permission using numerical shorthand&lt;/h3>
&lt;p>Another way to specify permission is by using the &lt;strong>octal/numeric&lt;/strong> format. This option is faster, as it requires less typing, although it is not as straightforward as the previous method.&lt;/p>
&lt;p>The following example shows how it works:&lt;/p>
&lt;p>&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/rwx-standard-unix-permission-bits.png" alt="img">&lt;/p>
&lt;p>Using this method, each of the three permissions (&lt;code>r&lt;/code>, &lt;code>w&lt;/code>, &lt;code>x&lt;/code>) is represented by one of the bits in the binary equivalent of the decimal number.&lt;/p>
&lt;p>In this example,&lt;/p>
&lt;ul>
&lt;li>The permission of user/owner is &lt;code>rwx&lt;/code>. This could be represented as &lt;code>111&lt;/code>, which equals 7 in decimal.&lt;/li>
&lt;li>The permission of group is &lt;code>r-x&lt;/code>. This could be represented as &lt;code>101&lt;/code>, which equals 5 in decimal.&lt;/li>
&lt;li>The permission of others is &lt;code>r--&lt;/code>. This could be represented as `1010, which equals 4 in decimal.&lt;/li>
&lt;/ul>
&lt;p>Therefore, to achieve the same permission settings as&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ chmod &lt;span class="nv">u&lt;/span>&lt;span class="o">=&lt;/span>rwx,g&lt;span class="o">=&lt;/span>rx,o&lt;span class="o">=&lt;/span>r hello-world.txt
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>We can also type:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ chmod &lt;span class="m">754&lt;/span> hello-world.txt
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">-rwxr-xr-- 1 EckoTan staff 12B Jan 5 18:02 hello-world.txt
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="reference">Reference&lt;/h2>
&lt;ul>
&lt;li>
&lt;p>&lt;a href="https://www.howtogeek.com/437958/how-to-use-the-chmod-command-on-linux/">How to Use the chmod Command on Linux&lt;/a>&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;a href="https://phoenixnap.com/kb/linux-file-permissions">Linux File Permission Tutorial: How To Check And Change Permissions&lt;/a>&lt;/p>
&lt;/li>
&lt;/ul></description></item></channel></rss>