{"id":744,"date":"2020-12-22T17:34:59","date_gmt":"2020-12-22T16:34:59","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=744"},"modified":"2022-09-02T10:23:35","modified_gmt":"2022-09-02T08:23:35","slug":"the-linux-command-handbook","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2020\/12\/22\/the-linux-command-handbook\/","title":{"rendered":"The Linux Command Handbook"},"content":{"rendered":"\n<h4>What is Linux?<\/h4>\n\n\n\n<p>Linux is an operating system, like macOS or Windows.<\/p>\n\n\n\n<p>It powers the vast majority of the servers that compose the Internet. It&#8217;s the base upon which everything is built. But not just that. Android is based on (a modified version of) Linux.<\/p>\n\n\n\n<p>The Linux &#8220;core&#8221; (called a&nbsp;<em>kernel<\/em>) was born in 1991 in Finland, and it has come a really long way from its humble beginnings. It went on to be the kernel of the GNU Operating System, creating the duo GNU\/Linux.<\/p>\n\n\n\n<p>There&#8217;s one thing about Linux that corporations like Microsoft, Apple, and Google will never be able to offer: the freedom to do whatever you want with your computer.<\/p>\n\n\n\n<p>Linux is also special because there&#8217;s not just &#8220;one Linux&#8221;, like is the case with Windows or macOS. Instead, we have&nbsp;<strong>distributions<\/strong>.<\/p>\n\n\n\n<p>A &#8220;distro&#8221; is made by a company or organization and packages the Linux core with additional programs and tooling.<\/p>\n\n\n\n<p>For example you have Debian, Red Hat, and Ubuntu, probably the most popular distributions.<\/p>\n\n\n\n<h4>What is a Linux shell?<\/h4>\n\n\n\n<p>A shell is a command interpreter that exposes an interface to the user to work with the underlying operating system.<\/p>\n\n\n\n<p>It allows you to execute operations using text and commands, and it provides users advanced features like being able to create scripts.<\/p>\n\n\n\n<p>This is important: shells let you perform things in a more optimized way than a GUI (Graphical User Interface) could ever possibly let you do. Command line tools can offer many different configuration options without being too complex to use.<\/p>\n\n\n\n<p>There are many different kind of shells. This post focuses on Unix shells, the ones that you will find commonly on Linux and macOS computers.<\/p>\n\n\n\n<p>Many different kind of shells were created for those systems over time, and a few of them dominate the space: Bash, Csh, Zsh, Fish and many more!<\/p>\n\n\n\n<p>All shells originate from the Bourne Shell, called&nbsp;<code>sh<\/code>. &#8220;Bourne&#8221; because its creator was<strong> Steve Bourne<\/strong>.<\/p>\n\n\n\n<p>Bash means&nbsp;<em>Bourne-again shell<\/em>.&nbsp;<code><span class=\"has-inline-color has-vivid-red-color\">sh<\/span><\/code>&nbsp;was proprietary and not open source, and Bash was created in 1989 to create a free alternative for the GNU project and the Free Software Foundation. Since projects had to pay to use the Bourne shell, Bash became very popular.<\/p>\n\n\n\n<p>If you use a Mac, try opening your Mac terminal. By default it runs ZSH (or, pre-Catalina, Bash).<\/p>\n\n\n\n<p>You can set up your system to run any kind of shell \u2013 for example I use the Fish shell.<\/p>\n\n\n\n<p>Each single shell has its own unique features and advanced usage, but they all share a common functionality: they can let you execute programs, and they can be programmed.<\/p>\n\n\n\n<p>In the rest of this handbook we&#8217;ll see in detail the most common commands you will use.<\/p>\n\n\n\n<h4>The Linux&nbsp;<span class=\"has-inline-color has-vivid-cyan-blue-color\"><code>man<\/code>&nbsp;<\/span>command<\/h4>\n\n\n\n<p>Every time I don&#8217;t know how to use a command, I type&nbsp;<code>man &lt;command&gt;<\/code>&nbsp;to get the manual:<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-07-04-at-18.42.40.png\" alt=\"\" width=\"580\" height=\"436\"\/><\/figure>\n\n\n\n<p>This is a man (from&nbsp;<em>_manual_<\/em>) page. Man pages are an essential tool to learn as a developer. They contain so much information that sometimes it&#8217;s almost too much.<br>The above screenshot is just 1 of 14 screens of explanation for the&nbsp;<code>ls<\/code>&nbsp;command.<\/p>\n\n\n\n<p>Most of the time when I need to learn a command quickly I use this site called&nbsp;<strong>tldr pages<\/strong>:&nbsp;<a href=\"https:\/\/tldr.sh\/\">https:\/\/tldr.sh<\/a>. It&#8217;s a command you can install, which you then run like this:&nbsp;<code>tldr &lt;command&gt;<\/code>. It gives you a very quick overview of a command, with some handy examples of common usage scenarios:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-07-at-07.35.41.png\" alt=\"\"\/><\/figure>\n\n\n\n<h4>The Linux&nbsp;<code><span class=\"has-inline-color has-vivid-cyan-blue-color\">ls<\/span><\/code>&nbsp;command<\/h4>\n\n\n\n<p>Inside a folder you can list all the files that the folder contains using the&nbsp;<code>ls<\/code>&nbsp;command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ls<\/code><\/pre>\n\n\n\n<p>If you add a folder name or path, it will print that folder&#8217;s contents:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ls \/bin<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screenshot-2019-02-09-at-18.50.14.png\" alt=\"Screenshot-2019-02-09-at-18.50.14\"\/><\/figure>\n\n\n\n<p><code>ls<\/code>&nbsp;accepts a lot of options. One of my favorite combinations is&nbsp;<code>-al<\/code>. Try it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ls -al \/bin\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screenshot-2019-02-09-at-18.49.52.png\" alt=\"Screenshot-2019-02-09-at-18.49.52\"\/><\/figure>\n\n\n\n<p>Compared to the plain&nbsp;<code>ls<\/code>&nbsp;command, this returns much more information.<\/p>\n\n\n\n<p>You have, from left to right:<\/p>\n\n\n\n<ul><li>the file permissions (and if your system supports ACLs, you get an ACL flag as well)<\/li><li>the number of links to that file<\/li><li>the owner of the file<\/li><li>the group of the file<\/li><li>the file size in bytes<\/li><li>the file&#8217;s last modified datetime<\/li><li>the file name<\/li><\/ul>\n\n\n\n<p>This set of data is generated by the&nbsp;<code>l<\/code>&nbsp;option. The&nbsp;<code>a<\/code>&nbsp;option instead also shows the hidden files.<\/p>\n\n\n\n<p>Hidden files are files that start with a dot (<code>.<\/code>).<\/p>\n\n\n\n<h4 id=\"the-linux-cd-command\">The Linux&nbsp;<span class=\"has-inline-color has-vivid-cyan-blue-color\"><code>cd<\/code>&nbsp;<\/span>command<\/h4>\n\n\n\n<p>Once you have a folder, you can move into it using the&nbsp;<code>cd<\/code>&nbsp;command.&nbsp;<code>cd<\/code>&nbsp;means&nbsp;<strong>c<\/strong>hange&nbsp;<strong>d<\/strong>irectory. You invoke it specifying a folder to move into. You can specify a folder name, or an entire path.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir fruits\ncd fruits\n<\/code><\/pre>\n\n\n\n<p>Now you are in the&nbsp;<code>fruits<\/code>&nbsp;folder.<\/p>\n\n\n\n<p>You can use the&nbsp;<code>..<\/code>&nbsp;special path to indicate the parent folder:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cd .. #back to the home folder\n<\/code><\/pre>\n\n\n\n<p>The # character indicates the start of the comment, which lasts for the entire line after it&#8217;s found.<\/p>\n\n\n\n<p>You can use it to form a path:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir fruits\nmkdir cars\ncd fruits\ncd ..\/cars\n<\/code><\/pre>\n\n\n\n<p>There is another special path indicator which is&nbsp;<code>.<\/code>, and indicates the&nbsp;<strong>current<\/strong>&nbsp;folder.<\/p>\n\n\n\n<p>You can also use absolute paths, which start from the root folder&nbsp;<code>\/<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cd \/etc\n<\/code><\/pre>\n\n\n\n<h4 id=\"the-linux-pwd-command\">The Linux&nbsp;<code><span class=\"has-inline-color has-vivid-cyan-blue-color\">pwd<\/span><\/code>&nbsp;command<\/h4>\n\n\n\n<p>Whenever you feel lost in the filesystem, call the&nbsp;<code>pwd<\/code>&nbsp;command to know where you are:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pwd\n<\/code><\/pre>\n\n\n\n<p>It will print the current folder path.<\/p>\n\n\n\n<h4 id=\"the-linux-mkdir-command\">The Linux&nbsp;<code><span class=\"has-inline-color has-vivid-cyan-blue-color\">mkdir<\/span><\/code>&nbsp;command<\/h4>\n\n\n\n<p>You create folders using the&nbsp;<code>mkdir<\/code>&nbsp;command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir fruits\n<\/code><\/pre>\n\n\n\n<p>You can create multiple folders with one command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir dogs cars\n<\/code><\/pre>\n\n\n\n<p>You can also create multiple nested folders by adding the&nbsp;<code>-p<\/code>&nbsp;option:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir -p fruits\/apples\n<\/code><\/pre>\n\n\n\n<p>Options in UNIX commands commonly take this form. You add them right after the command name, and they change how the command behaves. You can often combine multiple options, too.<\/p>\n\n\n\n<p>You can find which options a command supports by typing&nbsp;<code>man &lt;commandname&gt;<\/code>. Try now with&nbsp;<code>man mkdir<\/code>&nbsp;for example (press the&nbsp;<code>q<\/code>&nbsp;key to esc the man page). Man pages are the amazing built-in help for UNIX.<\/p>\n\n\n\n<h4 id=\"the-linux-rmdir-command\">The Linux<span class=\"has-inline-color has-vivid-cyan-blue-color\">&nbsp;<code>rmdir<\/code><\/span>&nbsp;command<\/h4>\n\n\n\n<p>Just as you can create a folder using&nbsp;<code>mkdir<\/code>, you can delete a folder using&nbsp;<code>rmdir<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir fruits\nrmdir fruits\n<\/code><\/pre>\n\n\n\n<p>You can also delete multiple folders at once:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir fruits cars\nrmdir fruits cars\n<\/code><\/pre>\n\n\n\n<p>The folder you delete must be empty.<\/p>\n\n\n\n<p>To delete folders with files in them, we&#8217;ll use the more generic&nbsp;<code>rm<\/code>&nbsp;command which deletes files and folders, using the&nbsp;<code>-rf<\/code>&nbsp;option:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rm -rf fruits cars\n<\/code><\/pre>\n\n\n\n<p>Be careful as this command does not ask for confirmation and it will immediately remove anything you ask it to remove.<\/p>\n\n\n\n<p>There is no&nbsp;<strong>bin<\/strong>&nbsp;when removing files from the command line, and recovering lost files can be hard.<\/p>\n\n\n\n<h4 id=\"the-linux-mv-command\">The Linux&nbsp;<span class=\"has-inline-color has-vivid-cyan-blue-color\"><code>mv<\/code>&nbsp;<\/span>command<\/h4>\n\n\n\n<p>Once you have a file, you can move it around using the&nbsp;<code>mv<\/code>&nbsp;command. You specify the file current path, and its new path:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>touch test\nmv pear new_pear\n<\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>pear<\/code>&nbsp;file is now moved to&nbsp;<code>new_pear<\/code>. This is how you&nbsp;<strong>rename<\/strong>&nbsp;files and folders.<\/p>\n\n\n\n<p>If the last parameter is a folder, the file located at the first parameter path is going to be moved into that folder. In this case, you can specify a list of files and they will all be moved in the folder path identified by the last parameter:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>touch pear\ntouch apple\nmkdir fruits\nmv pear apple fruits #pear and apple moved to the fruits folder\n<\/code><\/pre>\n\n\n\n<h4 id=\"the-linux-cp-command\">The Linux&nbsp;<code><span class=\"has-inline-color has-vivid-cyan-blue-color\">cp<\/span><\/code>&nbsp;command<\/h4>\n\n\n\n<p>You can copy a file using the&nbsp;<code>cp<\/code>&nbsp;command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>touch test\ncp apple another_apple\n<\/code><\/pre>\n\n\n\n<p>To copy folders you need to add the&nbsp;<code>-r<\/code>&nbsp;option to recursively copy the whole folder contents:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir fruits\ncp -r fruits cars\n<\/code><\/pre>\n\n\n\n<h4 id=\"the-linux-open-command\">The Linux&nbsp;<code><span class=\"has-inline-color has-vivid-cyan-blue-color\">open<\/span><\/code>&nbsp;command<\/h4>\n\n\n\n<p>The&nbsp;<code>open<\/code>&nbsp;command lets you open a file using this syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>open &lt;filename&gt;\n<\/code><\/pre>\n\n\n\n<p>You can also open a directory, which on macOS opens the Finder app with the current directory open:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>open &lt;directory name&gt;\n<\/code><\/pre>\n\n\n\n<p>I use it all the time to open the current directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>open .\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>The special&nbsp;<code>.<\/code>&nbsp;symbol points to the current directory, as&nbsp;<code>..<\/code>&nbsp;points to the parent directory<\/p><\/blockquote>\n\n\n\n<p>The same command can also be be used to run an application:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>open &lt;application name&gt;\n<\/code><\/pre>\n\n\n\n<h4 id=\"the-linux-touch-command\">The Linux&nbsp;<code><span class=\"has-inline-color has-vivid-cyan-blue-color\">touch<\/span><\/code>&nbsp;command<\/h4>\n\n\n\n<p>You can create an empty file using the&nbsp;<code>touch<\/code>&nbsp;command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>touch apple\n<\/code><\/pre>\n\n\n\n<p>If the file already exists, it opens the file in write mode, and the timestamp of the file is updated.<\/p>\n\n\n\n<h4 id=\"the-linux-find-command\">The Linux&nbsp;<span class=\"has-inline-color has-vivid-cyan-blue-color\"><code>find<\/code>&nbsp;<\/span>command<\/h4>\n\n\n\n<p>The&nbsp;<code>find<\/code>&nbsp;command can be used to find files or folders matching a particular search pattern. It searches recursively.<\/p>\n\n\n\n<p>Let&#8217;s learn how to use it by example.<\/p>\n\n\n\n<p>Find all the files under the current tree that have the&nbsp;<code>.js<\/code>&nbsp;extension and print the relative path of each file that matches:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find . -name '*.js'\n<\/code><\/pre>\n\n\n\n<p>It&#8217;s important to use quotes around special characters like&nbsp;<code>*<\/code>&nbsp;to avoid the shell interpreting them.<\/p>\n\n\n\n<p>Find directories under the current tree matching the name &#8220;src&#8221;:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find . -type d -name src\n<\/code><\/pre>\n\n\n\n<p>Use&nbsp;<code>-type f<\/code>&nbsp;to search only files, or&nbsp;<code>-type l<\/code>&nbsp;to only search symbolic links.<\/p>\n\n\n\n<p><code>-name<\/code>&nbsp;is case sensitive. use&nbsp;<code>-iname<\/code>&nbsp;to perform a case-insensitive search.<\/p>\n\n\n\n<p>You can search under multiple root trees:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find folder1 folder2 -name filename.txt\n<\/code><\/pre>\n\n\n\n<p>Find directories under the current tree matching the name &#8220;node_modules&#8221; or &#8216;public&#8217;:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find . -type d -name node_modules -or -name public\n<\/code><\/pre>\n\n\n\n<p>You can also exclude a path using&nbsp;<code>-not -path<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find . -type d -name '*.md' -not -path 'node_modules\/*'\n<\/code><\/pre>\n\n\n\n<p>You can search files that have more than 100 characters (bytes) in them:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find . -type f -size +100c\n<\/code><\/pre>\n\n\n\n<p>Search files bigger than 100KB but smaller than 1MB:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find . -type f -size +100k -size -1M\n<\/code><\/pre>\n\n\n\n<p>Search files edited more than 3 days ago:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find . -type f -mtime +3\n<\/code><\/pre>\n\n\n\n<p>Search files edited in the last 24 hours:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find . -type f -mtime -1\n<\/code><\/pre>\n\n\n\n<p>You can delete all the files matching a search by adding the&nbsp;<code>-delete<\/code>&nbsp;option. This deletes all the files edited in the last 24 hours:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find . -type f -mtime -1 -delete\n<\/code><\/pre>\n\n\n\n<p>You can execute a command on each result of the search. In this example we run&nbsp;<code>cat<\/code>&nbsp;to print the file content:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find . -type f -exec cat {} \\;\n<\/code><\/pre>\n\n\n\n<p>Notice the terminating&nbsp;<code>\\;<\/code>.&nbsp;<code>{}<\/code>&nbsp;is filled with the file name at execution time.<\/p>\n\n\n\n<h4 id=\"the-linux-ln-command\">The Linux&nbsp;<code><span class=\"has-inline-color has-vivid-cyan-blue-color\">ln<\/span><\/code>&nbsp;command<\/h4>\n\n\n\n<p>The&nbsp;<code>ln<\/code>&nbsp;command is part of the Linux file system commands.<\/p>\n\n\n\n<p>It&#8217;s used to create links. What is a link? It&#8217;s like a pointer to another file, or a file that points to another file. You might be familiar with Windows shortcuts. They&#8217;re similar.<\/p>\n\n\n\n<p>We have 2 types of links:&nbsp;<strong>hard links<\/strong>&nbsp;and&nbsp;<strong>soft links<\/strong>.<\/p>\n\n\n\n<h4 id=\"hardlinks\">Hard links<\/h4>\n\n\n\n<p>Hard links are rarely used. They have a few limitations: you can&#8217;t link to directories, and you can&#8217;t link to external filesystems (disks).<\/p>\n\n\n\n<p>A hard link is created using the following syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ln &lt;original&gt; &lt;link&gt;\n<\/code><\/pre>\n\n\n\n<p>For example, say you have a file called recipes.txt. You can create a hard link to it using:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ln recipes.txt newrecipes.txt\n<\/code><\/pre>\n\n\n\n<p>The new hard link you created is indistinguishable from a regular file:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-02-at-11.26.21.png\" alt=\"Screen-Shot-2020-09-02-at-11.26.21\"\/><\/figure>\n\n\n\n<p>Now any time you edit any of those files, the content will be updated for both.<\/p>\n\n\n\n<p>If you delete the original file, the link will still contain the original file content, as that&#8217;s not removed until there is one hard link pointing to it.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-02-at-11.26.07.png\" alt=\"Screen-Shot-2020-09-02-at-11.26.07\"\/><\/figure>\n\n\n\n<h4 id=\"softlinks\">Soft links<\/h4>\n\n\n\n<p>Soft links are different. They are more powerful as you can link to other filesystems and to directories. But keep in mind that when the original is removed, the link will be broken.<\/p>\n\n\n\n<p>You create soft links using the&nbsp;<code>-s<\/code>&nbsp;option of&nbsp;<code>ln<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ln -s &lt;original&gt; &lt;link&gt;\n<\/code><\/pre>\n\n\n\n<p>For example, say you have a file called recipes.txt. You can create a soft link to it using:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ln -s recipes.txt newrecipes.txt\n<\/code><\/pre>\n\n\n\n<p>In this case you can see there&#8217;s a special&nbsp;<code>l<\/code>&nbsp;flag when you list the file using&nbsp;<code>ls -al<\/code>. The file name has a&nbsp;<code>@<\/code>&nbsp;at the end, and it&#8217;s also colored differently if you have colors enabled:<\/p>\n\n\n\n<p><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-02-at-11.27.18.png\" alt=\"Screen-Shot-2020-09-02-at-11.27.18\"><br>Now if you delete the original file, the links will be broken, and the shell will tell you &#8220;No such file or directory&#8221; if you try to access it:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-02-at-11.27.03.png\" alt=\"Screen-Shot-2020-09-02-at-11.27.03\"\/><\/figure>\n\n\n\n<h4 id=\"the-linux-gzip-command\">The Linux&nbsp;<span class=\"has-inline-color has-vivid-cyan-blue-color\"><code>gzip<\/code>&nbsp;<\/span>command<\/h4>\n\n\n\n<p>You can compress a file using the gzip compression protocol named&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/LZ77_and_LZ78\">LZ77<\/a>&nbsp;using the&nbsp;<code>gzip<\/code>&nbsp;command.<\/p>\n\n\n\n<p>Here&#8217;s the simplest usage:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gzip filename\n<\/code><\/pre>\n\n\n\n<p>This will compress the file, and append a&nbsp;<code>.gz<\/code>&nbsp;extension to it. The original file is deleted.<\/p>\n\n\n\n<p>To prevent this, you can use the&nbsp;<code>-c<\/code>&nbsp;option and use output redirection to write the output to the&nbsp;<code>filename.gz<\/code>&nbsp;file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gzip -c filename &gt; filename.gz\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>The&nbsp;<code>-c<\/code>&nbsp;option specifies that the output will go to the standard output stream, leaving the original file intact.<\/p><\/blockquote>\n\n\n\n<p>Or you can use the&nbsp;<code>-k<\/code>&nbsp;option:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gzip -k filename\n<\/code><\/pre>\n\n\n\n<p>There are various levels of compression. The more the compression, the longer it will take to compress (and decompress). Levels range from 1 (fastest, worst compression) to 9 (slowest, better compression), and the default is 6.<\/p>\n\n\n\n<p>You can choose a specific level with the&nbsp;<code>-&lt;NUMBER&gt;<\/code>&nbsp;option:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gzip -1 filename\n<\/code><\/pre>\n\n\n\n<p>You can compress multiple files by listing them:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gzip filename1 filename2\n<\/code><\/pre>\n\n\n\n<p>You can compress all the files in a directory, recursively, using the&nbsp;<code>-r<\/code>&nbsp;option:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gzip -r a_folder\n<\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>-v<\/code>&nbsp;option prints the compression percentage information. Here&#8217;s an example of it being used along with the&nbsp;<code>-k<\/code>&nbsp;(keep) option:<\/p>\n\n\n\n<p><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-09-at-15.55.42.png\" alt=\"Screen-Shot-2020-09-09-at-15.55.42\"><br><code>gzip<\/code>&nbsp;can also be used to decompress a file, using the&nbsp;<code>-d<\/code>&nbsp;option:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gzip -d filename.gz<\/code><\/pre>\n\n\n\n<h4 id=\"the-linux-tar-command\">The Linux&nbsp;<code><span class=\"has-inline-color has-vivid-cyan-blue-color\">tar<\/span><\/code>&nbsp;command<\/h4>\n\n\n\n<p>The&nbsp;<code>tar<\/code>&nbsp;command is used to create an archive, grouping multiple files in a single file.<\/p>\n\n\n\n<p>Its name comes from the past and means&nbsp;<em>tape archive<\/em>&nbsp;(back when archives were stored on tapes).<\/p>\n\n\n\n<p>This command creates an archive named&nbsp;<code>archive.tar<\/code>&nbsp;with the content of&nbsp;<code>file1<\/code>&nbsp;and&nbsp;<code>file2<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tar -cf archive.tar file1 file2\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>The&nbsp;<code>c<\/code>&nbsp;option stands for&nbsp;<em>create<\/em>. The&nbsp;<code>f<\/code>&nbsp;option is used to write to file the archive.<\/p><\/blockquote>\n\n\n\n<p>To extract files from an archive in the current folder, use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tar -xf archive.tar\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>the&nbsp;<code>x<\/code>&nbsp;option stands for&nbsp;<em>extract<\/em>.<\/p><\/blockquote>\n\n\n\n<p>And to extract them to a specific directory, use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tar -xf archive.tar -C directory\n<\/code><\/pre>\n\n\n\n<p>You can also just list the files contained in an archive:<\/p>\n\n\n\n<p><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-09-at-16.56.33.png\" alt=\"Screen-Shot-2020-09-09-at-16.56.33\"><br><code>tar<\/code>&nbsp;is often used to create a&nbsp;<strong>compressed archive<\/strong>, gzipping the archive.<\/p>\n\n\n\n<p>This is done using the&nbsp;<code>z<\/code>&nbsp;option:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tar -czf archive.tar.gz file1 file2\n<\/code><\/pre>\n\n\n\n<p>This is just like creating a tar archive, and then running&nbsp;<code>gzip<\/code>&nbsp;on it.<\/p>\n\n\n\n<p>To unarchive a gzipped archive, you can use&nbsp;<code>gunzip<\/code>, or&nbsp;<code>gzip -d<\/code>, and then unarchive it. But&nbsp;<code>tar -xf<\/code>&nbsp;will recognize it&#8217;s a gzipped archive, and do it for you:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tar -xf archive.tar.gz\n<\/code><\/pre>\n\n\n\n<h4 id=\"the-linux-alias-command\">The Linux&nbsp;<code><span class=\"has-inline-color has-vivid-cyan-blue-color\">alias<\/span><\/code>&nbsp;command<\/h4>\n\n\n\n<p>It&#8217;s common to always run a program with a set of options that you like using.<\/p>\n\n\n\n<p>For example, take the&nbsp;<code>ls<\/code>&nbsp;command. By default it prints very little information:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-03-at-15.21.00.png\" alt=\"Screen-Shot-2020-09-03-at-15.21.00\"\/><\/figure>\n\n\n\n<p>But if you use the&nbsp;<code>-al<\/code>&nbsp;option it will print something more useful, including the file modification date, the size, the owner, and the permissions. It will also list hidden files (files starting with a&nbsp;<code>.<\/code>):<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-03-at-15.21.08.png\" alt=\"Screen-Shot-2020-09-03-at-15.21.08\"\/><\/figure>\n\n\n\n<p>You can create a new command, for example I like to call it&nbsp;<code>ll<\/code>, that is an alias to&nbsp;<code>ls -al<\/code>.<\/p>\n\n\n\n<p>You do it like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>alias ll='ls -al'\n<\/code><\/pre>\n\n\n\n<p>Once you do, you can call&nbsp;<code>ll<\/code>&nbsp;just like it was a regular UNIX command:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-03-at-15.22.51.png\" alt=\"Screen-Shot-2020-09-03-at-15.22.51\"\/><\/figure>\n\n\n\n<p>Now calling&nbsp;<code>alias<\/code>&nbsp;without any option will list the aliases defined:<\/p>\n\n\n\n<p><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-03-at-15.30.19.png\" alt=\"Screen-Shot-2020-09-03-at-15.30.19\"><br>The alias will work until the terminal session is closed.<\/p>\n\n\n\n<p>To make it permanent, you need to add it to the shell configuration. This could be&nbsp;<code>~\/.bashrc<\/code>&nbsp;or&nbsp;<code>~\/.profile<\/code>&nbsp;or&nbsp;<code>~\/.bash_profile<\/code>&nbsp;if you use the Bash shell, depending on the use case.<\/p>\n\n\n\n<p>Be careful with quotes if you have variables in the command: if you use double quotes, the variable is resolved at definition time. If you use use single quotes, it&#8217;s resolved at invocation time. Those 2 are different:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>alias lsthis=\"ls $PWD\"\nalias lscurrent='ls $PWD'\n<\/code><\/pre>\n\n\n\n<p>$PWD refers to the current folder the shell is in. If you now navigate away to a new folder,&nbsp;<code>lscurrent<\/code>&nbsp;lists the files in the new folder, whereas&nbsp;<code>lsthis<\/code>&nbsp;still lists the files in the folder where you were when you defined the alias.<\/p>\n\n\n\n<h4 id=\"the-linux-cat-command\">The Linux&nbsp;<span class=\"has-inline-color has-vivid-cyan-blue-color\"><code>cat<\/code>&nbsp;<\/span>command<\/h4>\n\n\n\n<p>Similar to&nbsp;<a href=\"https:\/\/www.freecodecamp.org\/news\/unix-command-tail\/\"><code>tail<\/code><\/a>&nbsp;in some ways, we have&nbsp;<code>cat<\/code>. Except&nbsp;<code>cat<\/code>&nbsp;can also add content to a file, and this makes it super powerful.<\/p>\n\n\n\n<p>In its simplest usage,&nbsp;<code>cat<\/code>&nbsp;prints a file&#8217;s content to the standard output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat file\n<\/code><\/pre>\n\n\n\n<p>You can print the content of multiple files:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat file1 file2\n<\/code><\/pre>\n\n\n\n<p>and using the output redirection operator&nbsp;<code>&gt;<\/code>&nbsp;you can concatenate the content of multiple files into a new file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat file1 file2 &gt; file3\n<\/code><\/pre>\n\n\n\n<p>Using&nbsp;<code>&gt;&gt;<\/code>&nbsp;you can append the content of multiple files into a new file, creating it if it does not exist:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat file1 file2 &gt;&gt; file3\n<\/code><\/pre>\n\n\n\n<p>When you&#8217;re looking at source code files it&#8217;s helpful to see the line numbers. You can have&nbsp;<code>cat<\/code>&nbsp;print them using the&nbsp;<code>-n<\/code>&nbsp;option:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat -n file1\n<\/code><\/pre>\n\n\n\n<p>You can only add a number to non-blank lines using&nbsp;<code>-b<\/code>, or you can also remove all the multiple empty lines using&nbsp;<code>-s<\/code>.<\/p>\n\n\n\n<p><code>cat<\/code>&nbsp;is often used in combination with the pipe operator&nbsp;<code>|<\/code>&nbsp;to feed a file&#8217;s content as input to another command:&nbsp;<code>cat file1 | anothercommand<\/code>.<\/p>\n\n\n\n<h4 id=\"the-linux-less-command\">The Linux&nbsp;<code><span class=\"has-inline-color has-vivid-cyan-blue-color\">less<\/span><\/code>&nbsp;command<\/h4>\n\n\n\n<p>The&nbsp;<code>less<\/code>&nbsp;command is one I use a lot. It shows you the content stored inside a file, in a nice and interactive UI.<\/p>\n\n\n\n<p>Usage:&nbsp;<code>less &lt;filename&gt;<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screenshot-2019-02-10-at-09.11.05.png\" alt=\"Screenshot-2019-02-10-at-09.11.05\"\/><\/figure>\n\n\n\n<p>Once you are inside a&nbsp;<code>less<\/code>&nbsp;session, you can quit by pressing&nbsp;<code>q<\/code>.<\/p>\n\n\n\n<p>You can navigate the file contents using the&nbsp;<code>up<\/code>&nbsp;and&nbsp;<code>down<\/code>&nbsp;keys, or using the&nbsp;<code>space bar<\/code>&nbsp;and&nbsp;<code>b<\/code>&nbsp;to navigate page by page. You can also jump to the end of the file pressing&nbsp;<code>G<\/code>&nbsp;and jump back to the start by pressing&nbsp;<code>g<\/code>.<\/p>\n\n\n\n<p>You can search contents inside the file by pressing&nbsp;<code>\/<\/code>&nbsp;and typing a word to search. This searches&nbsp;<em>forward<\/em>. You can search backwards using the&nbsp;<code>?<\/code>&nbsp;symbol and typing a word.<\/p>\n\n\n\n<p>This command just visualises the file&#8217;s content. You can directly open an editor by pressing&nbsp;<code>v<\/code>. It will use the system editor, which in most cases is&nbsp;<code>vim<\/code>.<\/p>\n\n\n\n<p>Pressing the&nbsp;<code>F<\/code>&nbsp;key enters&nbsp;<em>follow mode<\/em>, or&nbsp;<em>watch mode<\/em>. When the file is changed by someone else, like from another program, you get to see the changes&nbsp;<em>live<\/em>.<\/p>\n\n\n\n<p>This doesn&#8217;t happen by default, and you only see the file version at the time you opened it. You need to press&nbsp;<code>ctrl-C<\/code>&nbsp;to quit this mode. In this case the behaviour is similar to running the&nbsp;<code>tail -f &lt;filename&gt;<\/code>&nbsp;command.<\/p>\n\n\n\n<p>You can open multiple files, and navigate through them using&nbsp;<code>:n<\/code>&nbsp;(to go to the next file) and&nbsp;<code>:p<\/code>&nbsp;(to go to the previous).<\/p>\n\n\n\n<h4 id=\"the-linux-tail-command\">The Linux<span class=\"has-inline-color has-vivid-cyan-blue-color\">&nbsp;<code>tail<\/code><\/span>&nbsp;command<\/h4>\n\n\n\n<p>The best use case of tail in my opinion is when called with the&nbsp;<code>-f<\/code>&nbsp;option. It opens the file at the end, and watches for file changes.<\/p>\n\n\n\n<p>Any time there is new content in the file, it is printed in the window. This is great for watching log files, for example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tail -f \/var\/log\/system.log\n<\/code><\/pre>\n\n\n\n<p>To exit, press&nbsp;<code>ctrl-C<\/code>.<\/p>\n\n\n\n<p>You can print the last 10 lines in a file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tail -n 10 &lt;filename&gt;\n<\/code><\/pre>\n\n\n\n<p>You can print the whole file content starting from a specific line using&nbsp;<code>+<\/code>&nbsp;before the line number:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tail -n +10 &lt;filename&gt;\n<\/code><\/pre>\n\n\n\n<p><code>tail<\/code>&nbsp;can do much more and as always my advice is to check&nbsp;<code>man tail<\/code>.<\/p>\n\n\n\n<h4 id=\"the-linux-wc-command\">The Linux&nbsp;<code><span class=\"has-inline-color has-vivid-cyan-blue-color\">wc<\/span><\/code>&nbsp;command<\/h4>\n\n\n\n<p>The&nbsp;<code>wc<\/code>&nbsp;command gives us useful information about a file or input it receives via pipes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo test &gt;&gt; test.txt\nwc test.txt\n1       1       5 test.txt\n<\/code><\/pre>\n\n\n\n<p>Example via pipes, we can count the output of running the&nbsp;<code>ls -al<\/code>&nbsp;command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ls -al | wc\n6      47     284\n<\/code><\/pre>\n\n\n\n<p>The first column returned is the number of lines. The second is the number of words. The third is the number of bytes.<\/p>\n\n\n\n<p>We can tell it to just count the lines:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wc -l test.txt\n<\/code><\/pre>\n\n\n\n<p>or just the words:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wc -w test.txt\n<\/code><\/pre>\n\n\n\n<p>or just the bytes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wc -c test.txt\n<\/code><\/pre>\n\n\n\n<p>Bytes in ASCII charsets equate to characters. But with non-ASCII charsets, the number of characters might differ because some characters might take multiple bytes (for example this happens in Unicode).<\/p>\n\n\n\n<p>In this case the&nbsp;<code>-m<\/code>&nbsp;flag will help you get the correct value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wc -m test.txt\n<\/code><\/pre>\n\n\n\n<h4 id=\"the-linux-grep-command\">The Linux&nbsp;<code><span class=\"has-inline-color has-vivid-cyan-blue-color\">grep<\/span><\/code>&nbsp;command<\/h4>\n\n\n\n<p>The&nbsp;<code>grep<\/code>&nbsp;command is a very useful tool. When you master it, it will help you tremendously in your day to day coding.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>If you&#8217;re wondering,&nbsp;<code>grep<\/code>&nbsp;stands for&nbsp;<em>global regular expression print<\/em>.<\/p><\/blockquote>\n\n\n\n<p>You can use&nbsp;<code>grep<\/code>&nbsp;to search in files, or combine it with pipes to filter the output of another command.<\/p>\n\n\n\n<p>For example here&#8217;s how we can find the occurences of the&nbsp;<code>document.getElementById<\/code>&nbsp;line in the&nbsp;<code>index.md<\/code>&nbsp;file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grep -n document.getElementById index.md\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-04-at-09.42.10.png\" alt=\"Screen-Shot-2020-09-04-at-09.42.10\"\/><\/figure>\n\n\n\n<p>Using the&nbsp;<code>-n<\/code>&nbsp;option it will show the line numbers:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grep -n document.getElementById index.md\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-04-at-09.47.04.png\" alt=\"Screen-Shot-2020-09-04-at-09.47.04\"\/><\/figure>\n\n\n\n<p>One very useful thing is to tell grep to print 2 lines before and 2 lines after the matched line to give you more context. That&#8217;s done using the&nbsp;<code>-C<\/code>&nbsp;option, which accepts a number of lines:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grep -nC 2 document.getElementById index.md\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-04-at-09.44.35.png\" alt=\"Screen-Shot-2020-09-04-at-09.44.35\"\/><\/figure>\n\n\n\n<p>Search is case sensitive by default. Use the&nbsp;<code>-i<\/code>&nbsp;flag to make it insensitive.<\/p>\n\n\n\n<p>As mentioned, you can use grep to filter the output of another command. We can replicate the same functionality as above using:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>less index.md | grep -n document.getElementById\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-04-at-09.43.15.png\" alt=\"Screen-Shot-2020-09-04-at-09.43.15\"\/><\/figure>\n\n\n\n<p>The search string can be a regular expression, and this makes&nbsp;<code>grep<\/code>&nbsp;very powerful.<\/p>\n\n\n\n<p>Another thing you might find very useful is to invert the result, excluding the lines that match a particular string, using the&nbsp;<code>-v<\/code>&nbsp;option:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-04-at-09.42.04.png\" alt=\"Screen-Shot-2020-09-04-at-09.42.04\"\/><\/figure>\n\n\n\n<h4><span class=\"has-inline-color has-vivid-cyan-blue-color\">pgrep <\/span>command<\/h4>\n\n\n\n<p>You can use\u00a0<strong>pgrep<\/strong>\u00a0command to find process ID (PID) too:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pgrep process\r\npgrep firefox\r\npgrep vim<\/code><\/pre>\n\n\n\n<p><span class=\"has-inline-color has-vivid-red-color\"><strong>How to kill process in Linux from the command line<\/strong><\/span>?<\/p>\n\n\n\n<p>Linux and Unix-like operating system support the standard terminate signals listed below:<\/p>\n\n\n\n<ol><li><kbd>SIGHUP<\/kbd>\u00a0(1) \u2013 Hangup detected on controlling terminal or death of controlling process. Use SIGHUP to\u00a0<strong>reload configuration files and open\/close log<\/strong>\u00a0files.<\/li><li><kbd>SIGKILL<\/kbd>\u00a0(9) \u2013 Kill signal. Use SIGKILL as a\u00a0<strong>last resort<\/strong>\u00a0to kill process. It will not save data or cleaning kill the process.<\/li><li><kbd>SIGTERM<\/kbd>\u00a0(15) \u2013 Termination signal. It is\u00a0<strong>the default and safest way<\/strong>\u00a0to kill process.<\/li><\/ol>\n\n\n\n<p>also I recommend the syntax :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kill -15 PID<\/code><\/pre>\n\n\n\n<p><strong>PS: <\/strong><em>The\u00a0<strong>kill<\/strong>\u00a0and\u00a0<strong>killall<\/strong>\u00a0command support more than 60 signals. However, most users only need to use signal 9 or 15. To get a full list of signals, type:<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kill -l<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/09\/image-6.png\" alt=\"\" class=\"wp-image-2157\" width=\"650\" height=\"267\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/09\/image-6.png 721w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/09\/image-6-300x123.png 300w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/figure>\n\n\n\n<h4 id=\"the-linux-sort-command\">The Linux\u00a0<code><span class=\"has-inline-color has-vivid-cyan-blue-color\">sort<\/span><\/code>\u00a0command<\/h4>\n\n\n\n<p>Suppose you have a text file which contains the names of dogs:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-07-at-07.56.28.png\" alt=\"Screen-Shot-2020-09-07-at-07.56.28\"\/><\/figure>\n\n\n\n<p>This list is unordered.<\/p>\n\n\n\n<p>The&nbsp;<code>sort<\/code>&nbsp;command helps you sort them by name:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-07-at-07.57.08.png\" alt=\"Screen-Shot-2020-09-07-at-07.57.08\"\/><\/figure>\n\n\n\n<p>Use the&nbsp;<code>r<\/code>&nbsp;option to reverse the order:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-07-at-07.57.28.png\" alt=\"Screen-Shot-2020-09-07-at-07.57.28\"\/><\/figure>\n\n\n\n<p>Sorting by default is case sensitive, and alphabetic. Use the&nbsp;<code>--ignore-case<\/code>&nbsp;option to sort case insensitive, and the&nbsp;<code>-n<\/code>&nbsp;option to sort using a numeric order.<\/p>\n\n\n\n<p>If the file contains duplicate lines:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-07-at-07.59.03.png\" alt=\"Screen-Shot-2020-09-07-at-07.59.03\"\/><\/figure>\n\n\n\n<p>You can use the&nbsp;<code>-u<\/code>&nbsp;option to remove them:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-07-at-07.59.16.png\" alt=\"Screen-Shot-2020-09-07-at-07.59.16\"\/><\/figure>\n\n\n\n<p><code>sort<\/code>&nbsp;does not just work on files, as many UNIX commands do \u2013 it also works with pipes. So you can use it on the output of another command. For example you can order the files returned by&nbsp;<code>ls<\/code>&nbsp;with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ls | sort\n<\/code><\/pre>\n\n\n\n<p><code>sort<\/code>&nbsp;is very powerful and has lots more options, which you can explore by calling&nbsp;<code>man sort<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-07-at-08.01.27.png\" alt=\"Screen-Shot-2020-09-07-at-08.01.27\"\/><\/figure>\n\n\n\n<h4 id=\"the-linux-uniq-command\">The Linux&nbsp;<code><span class=\"has-inline-color has-vivid-cyan-blue-color\">uniq<\/span><\/code>&nbsp;command<\/h4>\n\n\n\n<p><code>uniq<\/code>&nbsp;is a command that helps you sort lines of text.<\/p>\n\n\n\n<p>You can get those lines from a file, or using pipes from the output of another command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>uniq dogs.txt\n\nls | uniq\n<\/code><\/pre>\n\n\n\n<p>You need to consider this key thing:&nbsp;<code>uniq<\/code>&nbsp;will only detect adjacent duplicate lines.<\/p>\n\n\n\n<p>This implies that you will most likely use it along with&nbsp;<code>sort<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sort dogs.txt | uniq\n<\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>sort<\/code>&nbsp;command has its own way to remove duplicates with the&nbsp;<code>-u<\/code>&nbsp;(<em>unique<\/em>) option. But&nbsp;<code>uniq<\/code>&nbsp;has more power.<\/p>\n\n\n\n<p>By default it removes duplicate lines:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-07-at-08.39.35.png\" alt=\"Screen-Shot-2020-09-07-at-08.39.35\"\/><\/figure>\n\n\n\n<p>You can tell it to only display duplicate lines, for example, with the&nbsp;<code>-d<\/code>&nbsp;option:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sort dogs.txt | uniq -d\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-07-at-08.36.50.png\" alt=\"Screen-Shot-2020-09-07-at-08.36.50\"\/><\/figure>\n\n\n\n<p>You can use the&nbsp;<code>-u<\/code>&nbsp;option to only display non-duplicate lines:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-07-at-08.38.50.png\" alt=\"Screen-Shot-2020-09-07-at-08.38.50\"\/><\/figure>\n\n\n\n<p>You can count the occurrences of each line with the&nbsp;<code>-c<\/code>&nbsp;option:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-07-at-08.37.15.png\" alt=\"Screen-Shot-2020-09-07-at-08.37.15\"\/><\/figure>\n\n\n\n<p>Use the special combination:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sort dogs.txt | uniq -c | sort -nr\n<\/code><\/pre>\n\n\n\n<p>to then sort those lines by most frequent:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-07-at-08.37.49.png\" alt=\"Screen-Shot-2020-09-07-at-08.37.49\"\/><\/figure>\n\n\n\n<h4 id=\"the-linux-diff-command\">The Linux&nbsp;<span class=\"has-inline-color has-vivid-cyan-blue-color\"><code>diff<\/code>&nbsp;<\/span>command<\/h4>\n\n\n\n<p><code>diff<\/code>&nbsp;is a handy command. Suppose you have 2 files, which contain almost the same information, but you can&#8217;t find the difference between the two.<\/p>\n\n\n\n<p><code>diff<\/code>&nbsp;will process the files and will tell you what&#8217;s the difference.<\/p>\n\n\n\n<p>Suppose you have 2 files:&nbsp;<code>dogs.txt<\/code>&nbsp;and&nbsp;<code>moredogs.txt<\/code>. The difference is that&nbsp;<code>moredogs.txt<\/code>&nbsp;contains one more dog name:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-07-at-08.55.18.png\" alt=\"Screen-Shot-2020-09-07-at-08.55.18\"\/><\/figure>\n\n\n\n<p><code>diff dogs.txt moredogs.txt<\/code>&nbsp;will tell you the second file has one more line, line 3 with the line&nbsp;<code>Vanille<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-07-at-08.56.05.png\" alt=\"Screen-Shot-2020-09-07-at-08.56.05\"\/><\/figure>\n\n\n\n<p>If you invert the order of the files, it will tell you that the second file is missing line 3, whose content is&nbsp;<code>Vanille<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-07-at-08.56.10.png\" alt=\"Screen-Shot-2020-09-07-at-08.56.10\"\/><\/figure>\n\n\n\n<p>Using the&nbsp;<code>-y<\/code>&nbsp;option will compare the 2 files line by line:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-07-at-08.57.56.png\" alt=\"Screen-Shot-2020-09-07-at-08.57.56\"\/><\/figure>\n\n\n\n<p>The&nbsp;<code>-u<\/code>&nbsp;option however will be more familiar to you, because that&#8217;s the same used by the Git version control system to display differences between versions:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-07-at-08.58.23.png\" alt=\"Screen-Shot-2020-09-07-at-08.58.23\"\/><\/figure>\n\n\n\n<p>Comparing directories works in the same way. You must use the&nbsp;<code>-r<\/code>&nbsp;option to compare recursively (going into subdirectories):<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-07-at-09.01.07.png\" alt=\"Screen-Shot-2020-09-07-at-09.01.07\"\/><\/figure>\n\n\n\n<p>In case you&#8217;re interested in which files differ, rather than the content, use the&nbsp;<code>r<\/code>&nbsp;and&nbsp;<code>q<\/code>&nbsp;options:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-07-at-09.01.30.png\" alt=\"Screen-Shot-2020-09-07-at-09.01.30\"\/><\/figure>\n\n\n\n<p>There are many more options you can explore in the man page by running&nbsp;<code>man diff<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-07-at-09.02.32.png\" alt=\"Screen-Shot-2020-09-07-at-09.02.32\"\/><\/figure>\n\n\n\n<h4 id=\"the-linux-chmod-command\">The Linux&nbsp;<span class=\"has-inline-color has-vivid-cyan-blue-color\"><code>chmod<\/code>&nbsp;<\/span>command<\/h4>\n\n\n\n<p>Every file in the Linux \/ macOS Operating Systems (and UNIX systems in general) has 3 permissions: read, write, and execute.<\/p>\n\n\n\n<p>Go into a folder, and run the&nbsp;<code>ls -al<\/code>&nbsp;command.<\/p>\n\n\n\n<p><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-03-at-18.49.22.png\" alt=\"Screen-Shot-2020-09-03-at-18.49.22\"><br>The weird strings you see on each file line, like&nbsp;<code>drwxr-xr-x<\/code>, define the permissions of the file or folder.<\/p>\n\n\n\n<p>Let&#8217;s dissect it.<\/p>\n\n\n\n<p>The first letter indicates the type of file:<\/p>\n\n\n\n<ul><li><code>-<\/code>&nbsp;means it&#8217;s a normal file<\/li><li><code>d<\/code>&nbsp;means it&#8217;s a directory<\/li><li><code>l<\/code>&nbsp;means it&#8217;s a link<\/li><\/ul>\n\n\n\n<p>Then you have 3 sets of values:<\/p>\n\n\n\n<ul><li>The first set represents the permissions of the&nbsp;<strong>owner<\/strong>&nbsp;of the file<\/li><li>The second set represents the permissions of the members of the&nbsp;<strong>group<\/strong>&nbsp;the file is associated to<\/li><li>The third set represents the permissions of the&nbsp;<strong>everyone else<\/strong><\/li><\/ul>\n\n\n\n<p>Those sets are composed by 3 values.&nbsp;<code>rwx<\/code>&nbsp;means that specific&nbsp;<em>persona<\/em>&nbsp;has read, write and execution access. Anything that is removed is swapped with a&nbsp;<code>-<\/code>, which lets you form various combinations of values and relative permissions:&nbsp;<code>rw-<\/code>,&nbsp;<code>r--<\/code>,&nbsp;<code>r-x<\/code>, and so on.<\/p>\n\n\n\n<p>You can change the permissions given to a file using the&nbsp;<code>chmod<\/code>&nbsp;command.<\/p>\n\n\n\n<p><code>chmod<\/code>&nbsp;can be used in 2 ways. The first is using symbolic arguments, the second is using numeric arguments. Let&#8217;s start with symbols first, which is more intuitive.<\/p>\n\n\n\n<p>You type&nbsp;<code>chmod<\/code>&nbsp;followed by a space, and a letter:<\/p>\n\n\n\n<ul><li><code>a<\/code>&nbsp;stands for&nbsp;<em>all<\/em><\/li><li><code>u<\/code>&nbsp;stands for&nbsp;<em>user<\/em><\/li><li><code>g<\/code>&nbsp;stands for&nbsp;<em>group<\/em><\/li><li><code>o<\/code>&nbsp;stands for&nbsp;<em>others<\/em><\/li><\/ul>\n\n\n\n<p>Then you type either&nbsp;<code>+<\/code>&nbsp;or&nbsp;<code>-<\/code>&nbsp;to add a permission, or to remove it. Then you enter one or more permission symbols (<code>r<\/code>,&nbsp;<code>w<\/code>,&nbsp;<code>x<\/code>).<\/p>\n\n\n\n<p>All followed by the file or folder name.<\/p>\n\n\n\n<p>Here are some examples:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>chmod a+r filename #everyone can now read\nchmod a+rw filename #everyone can now read and write\nchmod o-rwx filename #others (not the owner, not in the same group of the file) cannot read, write or execute the file\n<\/code><\/pre>\n\n\n\n<p>You can apply the same permissions to multiple personas by adding multiple letters before the&nbsp;<code>+<\/code>\/<code>-<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>chmod og-r filename #other and group can't read any more\n<\/code><\/pre>\n\n\n\n<p>In case you are editing a folder, you can apply the permissions to every file contained in that folder using the&nbsp;<code>-r<\/code>&nbsp;(recursive) flag.<\/p>\n\n\n\n<p>Numeric arguments are faster but I find them hard to remember when you are not using them day to day. You use a digit that represents the permissions of the persona. This number value can be a maximum of 7, and it&#8217;s calculated in this way:<\/p>\n\n\n\n<ul><li><code>1<\/code>&nbsp;if has execution permission<\/li><li><code>2<\/code>&nbsp;if has write permission<\/li><li><code>4<\/code>&nbsp;if has read permission<\/li><\/ul>\n\n\n\n<p>This gives us 4 combinations:<\/p>\n\n\n\n<ul><li><code>0<\/code>&nbsp;no permissions<\/li><li><code>1<\/code>&nbsp;can execute<\/li><li><code>2<\/code>&nbsp;can write<\/li><li><code>3<\/code>&nbsp;can write, execute<\/li><li><code>4<\/code>&nbsp;can read<\/li><li><code>5<\/code>&nbsp;can read, execute<\/li><li><code>6<\/code>&nbsp;can read, write<\/li><li><code>7<\/code>&nbsp;can read, write and execute<\/li><\/ul>\n\n\n\n<p>We use them in pairs of 3, to set the permissions of all the 3 groups altogether:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>chmod 777 filename\nchmod 755 filename\nchmod 644 filename\n<\/code><\/pre>\n\n\n\n<h4 id=\"the-linux-umask-command\">The Linux<span class=\"has-inline-color has-vivid-cyan-blue-color\">&nbsp;<code>umask<\/code><\/span>&nbsp;command<\/h4>\n\n\n\n<p>When you create a file, you don&#8217;t have to decide permissions up front. Permissions have defaults.<\/p>\n\n\n\n<p>Those defaults can be controlled and modified using the&nbsp;<code>umask<\/code>&nbsp;command.<\/p>\n\n\n\n<p>Typing&nbsp;<code>umask<\/code>&nbsp;with no arguments will show you the current umask, in this case&nbsp;<code>0022<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-04-at-09.04.19.png\" alt=\"Screen-Shot-2020-09-04-at-09.04.19\"\/><\/figure>\n\n\n\n<p>What does&nbsp;<code>0022<\/code>&nbsp;mean? That&#8217;s an octal value that represents the permissions.<\/p>\n\n\n\n<p>Another common value is&nbsp;<code>0002<\/code>.<\/p>\n\n\n\n<p>Use&nbsp;<code>umask -S<\/code>&nbsp;to see a human-readable notation:<\/p>\n\n\n\n<p><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-04-at-09.08.18.png\" alt=\"Screen-Shot-2020-09-04-at-09.08.18\"><br>In this case, the user (<code>u<\/code>), owner of the file, has read, write and execution permissions on files.<\/p>\n\n\n\n<p>Other users belonging to the same group (<code>g<\/code>) have read and execution permission, same as all the other users (<code>o<\/code>).<\/p>\n\n\n\n<p>In the numeric notation, we typically change the last 3 digits.<\/p>\n\n\n\n<p>Here&#8217;s a list that gives a meaning to the number:<\/p>\n\n\n\n<ul><li><code>0<\/code>&nbsp;read, write, execute<\/li><li><code>1<\/code>&nbsp;read and write<\/li><li><code>2<\/code>&nbsp;read and execute<\/li><li><code>3<\/code>&nbsp;read only<\/li><li><code>4<\/code>&nbsp;write and execute<\/li><li><code>5<\/code>&nbsp;write only<\/li><li><code>6<\/code>&nbsp;execute only<\/li><li><code>7<\/code>&nbsp;no permissions<\/li><\/ul>\n\n\n\n<p>Note that this numeric notation differs from the one we use in&nbsp;<code>chmod<\/code>.<\/p>\n\n\n\n<p>We can set a new value for the mask setting the value in numeric format:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>umask 002\n<\/code><\/pre>\n\n\n\n<p>or you can change a specific role&#8217;s permission:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>umask g+r\n<\/code><\/pre>\n\n\n\n<h4 id=\"the-linux-du-command\">The Linux&nbsp;<span class=\"has-inline-color has-vivid-cyan-blue-color\"><code>du<\/code>&nbsp;<\/span>command<\/h4>\n\n\n\n<p>The&nbsp;<code>du<\/code>&nbsp;command will calculate the size of a directory as a whole:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>du\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-04-at-08.11.30.png\" alt=\"Screen-Shot-2020-09-04-at-08.11.30\"\/><\/figure>\n\n\n\n<p>The&nbsp;<code>32<\/code>&nbsp;number here is a value expressed in bytes.<\/p>\n\n\n\n<p>Running&nbsp;<code>du *<\/code>&nbsp;will calculate the size of each file individually:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-04-at-08.12.35.png\" alt=\"Screen-Shot-2020-09-04-at-08.12.35\"\/><\/figure>\n\n\n\n<p>You can set&nbsp;<code>du<\/code>&nbsp;to display values in MegaBytes using&nbsp;<code>du -m<\/code>, and GigaBytes using&nbsp;<code>du -g<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<code>-h<\/code>&nbsp;option will show a human-readable notation for sizes, adapting to the size:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-04-at-08.14.40.png\" alt=\"Screen-Shot-2020-09-04-at-08.14.40\"\/><\/figure>\n\n\n\n<p>Adding the&nbsp;<code>-a<\/code>&nbsp;option will print the size of each file in the directories, too:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-04-at-08.20.12.png\" alt=\"Screen-Shot-2020-09-04-at-08.20.12\"\/><\/figure>\n\n\n\n<p>A handy thing is to sort the directories by size:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>du -h &lt;directory&gt; | sort -nr\n<\/code><\/pre>\n\n\n\n<p>and then piping to&nbsp;<code>head<\/code>&nbsp;to only get the first 10 results:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-04-at-08.22.25.png\" alt=\"Screen-Shot-2020-09-04-at-08.22.25\"\/><\/figure>\n\n\n\n<h4 id=\"the-linux-df-command\">The Linux&nbsp;<code><span class=\"has-inline-color has-vivid-cyan-blue-color\">df<\/span><\/code>&nbsp;command<\/h4>\n\n\n\n<p>The&nbsp;<code>df<\/code>&nbsp;command is used to get disk usage information.<\/p>\n\n\n\n<p>Its basic form will print information about the volumes mounted:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-08-at-08.40.39.png\" alt=\"Screen-Shot-2020-09-08-at-08.40.39\"\/><\/figure>\n\n\n\n<p>Using the&nbsp;<code>-h<\/code>&nbsp;option (<code>df -h<\/code>) will show those values in a human-readable format:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-08-at-08.40.50.png\" alt=\"Screen-Shot-2020-09-08-at-08.40.50\"\/><\/figure>\n\n\n\n<p>You can also specify a file or directory name to get information about the specific volume it lives on:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-08-at-08.41.27.png\" alt=\"Screen-Shot-2020-09-08-at-08.41.27\"\/><\/figure>\n\n\n\n<h4 id=\"the-linux-crontab-command\">The Linux<span class=\"has-inline-color has-vivid-cyan-blue-color\">&nbsp;<code>crontab<\/code><\/span>&nbsp;command<\/h4>\n\n\n\n<p>Cron jobs are jobs that are scheduled to run at specific intervals. You might have a command perform something every hour, or every day, or every 2 weeks. Or on weekends.<\/p>\n\n\n\n<p>They are very powerful, especially when used on servers to perform maintenance and automations.<\/p>\n\n\n\n<p>The&nbsp;<code>crontab<\/code>&nbsp;command is the entry point to work with cron jobs.<\/p>\n\n\n\n<p>The first thing you can do is to explore which cron jobs are defined by you:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>crontab -l\n<\/code><\/pre>\n\n\n\n<p>You might have none, like me:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-09-at-17.54.31.png\" alt=\"Screen-Shot-2020-09-09-at-17.54.31\"\/><\/figure>\n\n\n\n<p>Run<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>crontab -e\n<\/code><\/pre>\n\n\n\n<p>to edit the cron jobs, and add new ones.<\/p>\n\n\n\n<p>By default this opens with the default editor, which is usually&nbsp;<code>vim<\/code>. I like&nbsp;<code>nano<\/code>&nbsp;more. You can use this line to use a different editor:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>EDITOR=nano crontab -e\n<\/code><\/pre>\n\n\n\n<p>Now you can add one line for each cron job.<\/p>\n\n\n\n<p>The syntax to define cron jobs is kind of scary. This is why I usually use a website to help me generate it without errors:&nbsp;<a href=\"https:\/\/crontab-generator.org\/\">https:\/\/crontab-generator.org\/<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-09-at-18.03.57.png\" alt=\"Screen-Shot-2020-09-09-at-18.03.57\"\/><\/figure>\n\n\n\n<p>You pick a time interval for the cron job, and you type the command to execute.<\/p>\n\n\n\n<p>I chose to run a script located in&nbsp;<code>\/Users\/flavio\/test.sh<\/code>&nbsp;every 12 hours. This is the crontab line I need to run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>* *\/12 * * * \/Users\/flavio\/test.sh &gt;\/dev\/null 2&gt;&amp;1\n<\/code><\/pre>\n\n\n\n<p>I run&nbsp;<code>crontab -e<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>EDITOR=nano crontab -e\n<\/code><\/pre>\n\n\n\n<p>and I add that line, then I press&nbsp;<code>ctrl-X<\/code>&nbsp;and press&nbsp;<code>y<\/code>&nbsp;to save.<\/p>\n\n\n\n<p>If all goes well, the cron job is set up:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-09-at-18.06.19.png\" alt=\"Screen-Shot-2020-09-09-at-18.06.19\"\/><\/figure>\n\n\n\n<p>Once this is done, you can see the list of active cron jobs by running:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>crontab -l\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-09-at-18.07.00.png\" alt=\"Screen-Shot-2020-09-09-at-18.07.00\"\/><\/figure>\n\n\n\n<p>You can remove a cron job running&nbsp;<code>crontab -e<\/code>&nbsp;again, removing the line and exiting the editor:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-09-at-18.07.40.png\" alt=\"Screen-Shot-2020-09-09-at-18.07.40\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-09-at-18.07.49.png\" alt=\"Screen-Shot-2020-09-09-at-18.07.49\"\/><\/figure>\n\n\n\n<h4 id=\"the-linux-env-command\">The Linux&nbsp;<span class=\"has-inline-color has-vivid-cyan-blue-color\"><code>env<\/code>&nbsp;<\/span>command<\/h4>\n\n\n\n<p>The&nbsp;<code>env<\/code>&nbsp;command can be used to pass environment variables without setting them on the outer environment (the current shell).<\/p>\n\n\n\n<p>Suppose you want to run a Node.js app and set the&nbsp;<code>USER<\/code>&nbsp;variable to it.<\/p>\n\n\n\n<p>You can run<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>env USER=flavio node app.js\n<\/code><\/pre>\n\n\n\n<p>and the&nbsp;<code>USER<\/code>&nbsp;environment variable will be accessible from the Node.js app via the Node&nbsp;<code>process.env<\/code>&nbsp;interface.<\/p>\n\n\n\n<p>You can also run the command clearing all the environment variables already set, using the&nbsp;<code>-i<\/code>&nbsp;option:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>env -i node app.js\n<\/code><\/pre>\n\n\n\n<p>In this case you will get an error saying&nbsp;<code>env: node: No such file or directory<\/code>&nbsp;because the&nbsp;<code>node<\/code>&nbsp;command is not reachable, as the&nbsp;<code>PATH<\/code>&nbsp;variable used by the shell to look up commands in the common paths is unset.<\/p>\n\n\n\n<p>So you need to pass the full path to the&nbsp;<code>node<\/code>&nbsp;program:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>env -i \/usr\/local\/bin\/node app.js\n<\/code><\/pre>\n\n\n\n<p>Try with a simple&nbsp;<code>app.js<\/code>&nbsp;file with this content:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(process.env.NAME)\nconsole.log(process.env.PATH)\n<\/code><\/pre>\n\n\n\n<p>You will see the output as<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>undefined\nundefined\n<\/code><\/pre>\n\n\n\n<p>You can pass an env variable:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>env -i NAME=flavio node app.js\n<\/code><\/pre>\n\n\n\n<p>and the output will be<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>flavio\nundefined\n<\/code><\/pre>\n\n\n\n<p>Removing the&nbsp;<code>-i<\/code>&nbsp;option will make&nbsp;<code>PATH<\/code>&nbsp;available again inside the program:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-10-at-16.55.17.png\" alt=\"Screen-Shot-2020-09-10-at-16.55.17\"\/><\/figure>\n\n\n\n<p>The&nbsp;<code>env<\/code>&nbsp;command can also be used to print out all the environment variables. If run with no options:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>env\n<\/code><\/pre>\n\n\n\n<p>it will return a list of the environment variables set, for example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>HOME=\/Users\/flavio\nLOGNAME=flavio\nPATH=\/usr\/local\/bin:\/usr\/bin:\/bin:\/usr\/sbin:\/sbin:\/Library\/Apple\/usr\/bin\nPWD=\/Users\/flavio\nSHELL=\/usr\/local\/bin\/fish\n<\/code><\/pre>\n\n\n\n<p>You can also make a variable inaccessible inside the program you run, using the&nbsp;<code>-u<\/code>&nbsp;option. For example this code removes the&nbsp;<code>HOME<\/code>&nbsp;variable from the command environment:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>env -u HOME node app.js\n<\/code><\/pre>\n\n\n\n<h4 id=\"the-linux-printenv-command\">The Linux&nbsp;<code><span class=\"has-inline-color has-vivid-cyan-blue-color\">printenv<\/span><\/code>&nbsp;command<\/h4>\n\n\n\n<p>Here&#8217;s a quick guide to the&nbsp;<code>printenv<\/code>&nbsp;command, used to print the values of environment variables<\/p>\n\n\n\n<p>In any shell there are a good number of environment variables, set either by the system, or by your own shell scripts and configuration.<\/p>\n\n\n\n<p>You can print them all to the terminal using the&nbsp;<code>printenv<\/code>&nbsp;command. The output will be something like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>HOME=\/Users\/flavio\nLOGNAME=flavio\nPATH=\/usr\/local\/bin:\/usr\/bin:\/bin:\/usr\/sbin:\/sbin:\/Library\/Apple\/usr\/bin\nPWD=\/Users\/flavio\nSHELL=\/usr\/local\/bin\/fish\n<\/code><\/pre>\n\n\n\n<p>with a few more lines, usually.<\/p>\n\n\n\n<p>You can append a variable name as a parameter, to only show that variable value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>printenv PATH\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.freecodecamp.org\/news\/content\/images\/2020\/10\/Screen-Shot-2020-09-10-at-16.31.20.png\" alt=\"Screen-Shot-2020-09-10-at-16.31.20\"\/><\/figure>\n\n\n\n<h2 id=\"conclusion\"> <\/h2>\n\n\n\n<p>Remember that you can download this handbook in PDF \/ ePub \/ Mobi format &nbsp;<a href=\"https:\/\/flaviocopes.com\/page\/linux-commands-handbook\/\">here<\/a>&nbsp;if you want. ( Author:  &nbsp;<a href=\"https:\/\/twitter.com\/flaviocopes\">@flaviocopes<\/a>)<\/p>\n\n\n\n<h5>Reference: <\/h5>\n\n\n\n<p><a href=\"https:\/\/flaviocopes.com\/\">https:\/\/flaviocopes.com\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Linux? Linux is an operating system, like macOS or Windows. It powers the vast majority of the servers that compose the Internet. It&#8217;s the base upon which everything is built. But not just that. Android is based on (a modified version of) Linux. The Linux &#8220;core&#8221; (called a&nbsp;kernel) was born in 1991 in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":789,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[169],"tags":[172,175,95,171,174,173,170],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/744"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=744"}],"version-history":[{"count":4,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/744\/revisions"}],"predecessor-version":[{"id":2158,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/744\/revisions\/2158"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/789"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}