{"id":3737,"date":"2024-01-26T02:15:00","date_gmt":"2024-01-26T07:15:00","guid":{"rendered":"https:\/\/www.both.org\/?p=3737"},"modified":"2024-01-22T09:38:20","modified_gmt":"2024-01-22T14:38:20","slug":"top-5-mistakes-every-new-linux-terminal-user-makes","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=3737","title":{"rendered":"Top 5 mistakes every new Linux terminal user makes"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"3737\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">1    <\/span>\r\n<\/div><\/div>\n<p>Learning to use the terminal is an important step in becoming a true power user of Linux, but it&#8217;s easy (and normal) to make mistakes along the way. Here are the top 5 mistakes new terminal users make, and what you can learn from them.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Current working directory<\/h2>\n\n\n\n<p>When you first open a terminal, your current working directory is your home folder. You have access to all those directories you see in your home directory every time you open a file manager (<code>Desktop<\/code>, <code>Documents<\/code>, <code>Downloads<\/code>, <code>Music<\/code>, <code>Pictures<\/code>, and <code>Videos<\/code>). You can verify your location with the <code>pwd<\/code> command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ pwd\n\/home\/seth<\/code><\/pre>\n\n\n\n<p>You can list the files and folders within your current directory with the <code>ls<\/code> or <code>dir<\/code> or <code>tree<\/code> commands:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ls\nDesktop  Documents  Downloads\nMusic    Pictures   Videos<\/code><\/pre>\n\n\n\n<p>But you don&#8217;t usually stay in one place while using the terminal. You frequently move from folder to folder so you can open or modify or edit files. It&#8217;s easy to get lost, forgetting what directory you&#8217;re in and what files are around you.<\/p>\n\n\n\n<p><strong>Lesson learned:<\/strong> When working in the terminal, it&#8217;s important to regularly verify your current working directory with <code>pwd<\/code> so you don&#8217;t accidentally issue a command you intended to run in a different location.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Use interactive options when using wildcards<\/h2>\n\n\n\n<p>Wildcards are great shorthand to make command entry faster, and to perform bulk actions on lots of files. However, they can be dangerous when you get them wrong.<br>It&#8217;s easy to process hundreds of the wrong files by using a wildcard in the wrong directory, or by using a wildcard that&#8217;s too broad.<\/p>\n\n\n\n<p>For example, suppose you want to run a <code>sed<\/code> command on all HTML files in a directory, so you run this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sed --in-place 's\/day\/night\/g' *ml<\/code><\/pre>\n\n\n\n<p>Job done, until you find out that you accidentally ran that command on all your XML files, too.<\/p>\n\n\n\n<p><strong>Lesson learned:<\/strong> Run a safe test command on the wildcard you <em>think<\/em> you want to target before making a change. Some commands have a literal <code>--dry-run<\/code> option. Others have an <code>--interactive<\/code> option that forces the command to prompt you to confirm that you want to carry out the action. Sometimes the logic is reversed: a command refuses to make a major change unless you use a command (for example, <code>sed<\/code> doesn&#8217;t write changes to a file without the <code>--in-place<\/code> option or redirection).<\/p>\n\n\n\n<p>When in doubt, improvise. You can always &#8220;expand&#8221; a wildcard using the <code>echo<\/code> command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ echo .\/*ml\n.\/four.html .\/one.xml .\/three.html .\/two.xml\n$ echo .\/*tml\n.\/four.html .\/three.html<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3. File paths<\/h2>\n\n\n\n<p>Many new terminal users don&#8217;t understand where files are located within the file system. It&#8217;s not a common mistake to make on the desktop because there are visual reminders there. You wouldn&#8217;t try to double-click on a document to open it if there was no icon to double-click. It&#8217;s easy to assume that the terminal application contains all your files all at once, but the terminal is, by design, limited in scope. Were the terminal to have access to every file everywhere on your system all at once, you&#8217;d end up accidentally renaming and moving and copying a lot more files than intended. Specificity is a super power, and it&#8217;s defined by the <em>file path<\/em>.<\/p>\n\n\n\n<p>A file path describes where a file exists in a file system. A full (or &#8220;absolute&#8221;) file path always starts from the single folder at the start of your operating system, indicated by just a <code>\/<\/code>, and then lists each folder within that folder until it traces the path to a specific file. For example, I have a file called <code>IMG_0001.JPG<\/code> in my <code>Pictures<\/code> directory. You probably have a mental image of where that file is and how you&#8217;d get there on the desktop. But for the terminal to understand how to find it, the location must be expressed as <code>\/home\/seth\/Pictures\/IMG_0001.JPG<\/code>.<\/p>\n\n\n\n<p>An absolute file path is definitive. The terminal always understands an absolute file path, no matter what your current working directory is.<\/p>\n\n\n\n<p>The absolute path to a file can be unwieldy, though. Once you understand absolute paths, you can abbreviate any path to a <em>relative<\/em> file path.<\/p>\n\n\n\n<p>A relative file path is based on your current location in the terminal. As long as you&#8217;re in the <code>Pictures<\/code> folder, the full path <code>\/home\/seth\/Pictures\/IMG_0001.JPG<\/code> can be shortened to just <code>IMG_0001.JPG<\/code>, or <code>.\/IMG_0001.JPG<\/code> for added clarity (the <code>.<\/code> indicates no movement from your current location, and the <code>\/<\/code> is a directory separator as usual).<\/p>\n\n\n\n<p>But suppose your current working directory was your home directory. Your <code>Pictures<\/code> folder is located in your home directory, so to get to <code>IMG_0001.JPG<\/code> you have to enter <code>Pictures<\/code> first. The relative path in that case is <code>.\/Pictures\/IMG_0001.JPG<\/code> or just <code>Pictures\/IMG_0001.JPG<\/code>.<\/p>\n\n\n\n<p><strong>Lesson learned:<\/strong> An absolute file path always starts from the start of a file system. A relative file path changes based on your location. The terminal understands both. For new users, the absolute file path is the most explicit and exact way to reference a file, so practice using them until you&#8217;re comfortable with the concept of file paths.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Executable permissions<\/h2>\n\n\n\n<p>By default, most files aren&#8217;t executable. You can&#8217;t run them like an application, because most files are meant to be opened <em>in<\/em> an application. That&#8217;s not true for shell scripts, though.<\/p>\n\n\n\n<p>Shell scripts are text files containing a list of commands, and they&#8217;re meant to be run like an application. They&#8217;re a powerful way to string existing commands together to form a new custom command. However, because a shell script starts out as a regular text file, it&#8217;s not seen by your terminal as an executable entity.<\/p>\n\n\n\n<p>To execute a file as an application, you can grant it executable permission with the <code>chmod<\/code> command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ chmod +x .\/example.sh<\/code><\/pre>\n\n\n\n<p>Alternatively, you can run the file in a sub-shell:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ bash .\/example.sh<\/code><\/pre>\n\n\n\n<p>Notice that in these examples, I use the <code>.\/<\/code> notation as if the <code>example.sh<\/code> shell script exists in my current directory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Typing errors<\/h2>\n\n\n\n<p>It sometimes feels like the more you type, the more you&#8217;re getting done. In a terminal, though, typing too much is one of the best ways to introduce mistakes.<\/p>\n\n\n\n<p>When you try to type a long and complex command, you&#8217;re liable to spell something wrong or use the wrong option. When you try to type a filename or a file path, you might forget to escape special characters (like spaces). The errors aren&#8217;t usually catastrophic, but they&#8217;re frustrating and time consuming.<\/p>\n\n\n\n<p><strong>Lesson learned:<\/strong> There are several ways to ensure you&#8217;re entering the correct commands into your terminal:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Copy and paste:<\/strong> If you&#8217;re using a command you found on a trusted website, copy it in your browser and then paste it into your terminal using <strong>Ctrl<\/strong>+<strong>Shift<\/strong>+<strong>V<\/strong> or right-click and select <strong>Paste<\/strong>.<\/li>\n\n\n\n<li><strong>TAB:<\/strong> You can type part of a command or file path, and then press the <strong>TAB<\/strong> key on your keyboard for either auto-completion or for suggested completions. Use it even when you don&#8217;t think you need it. It&#8217;ll save you errors <em>every single time<\/em>, even when it appears to not work (hint: it&#8217;s not working because you&#8217;re trying to auto-complete something that&#8217;s not where you think it is).<\/li>\n\n\n\n<li><strong>Drag-and-drop:<\/strong> It&#8217;s the 21st century! You can drag a file or folder from anywhere on your computer, and drop it into your terminal. It gets replaced by its absolute path automatically.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Practice makes perfect<\/h2>\n\n\n\n<p>To get good in the terminal, you have to use it as often as you can. You don&#8217;t have to use it for &#8220;serious&#8221; work at first, and you arguably shouldn&#8217;t, but you can and should do simple exercises in the terminal. Understand file paths, get used to wildcards, learn shortcuts, use the <strong>TAB<\/strong> key. The biggest mistake you can make when learning the terminal is to not use the terminal, so open it up every day, do your exercise, and you&#8217;ll be an expert in the terminal in no time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Avoid these comman mistakes, and find out how to become an expert in the terminal.<\/p>\n","protected":false},"author":31,"featured_media":3522,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[5],"tags":[151,91,97],"class_list":["post-3737","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","tag-bash","tag-linux","tag-sysadmin"],"modified_by":"David Both","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/3737","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3737"}],"version-history":[{"count":3,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/3737\/revisions"}],"predecessor-version":[{"id":3796,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/3737\/revisions\/3796"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/3522"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3737"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3737"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}