{"id":13270,"date":"2019-04-01T10:54:18","date_gmt":"2019-04-01T10:54:18","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=13270"},"modified":"2019-04-01T10:54:18","modified_gmt":"2019-04-01T10:54:18","slug":"10-useful-linux-command-line-tricks-for-newbies","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/01\/10-useful-linux-command-line-tricks-for-newbies\/","title":{"rendered":"10 Useful Linux Command Line Tricks for Newbies"},"content":{"rendered":"<p>I remember when I first started using Linux and I was used to the graphical interface of Windows, I truly hated the Linux terminal. Back then I was finding the commands hard to remember and proper use of each one of them. With time I realised the\u00a0beauty, flexibility and usability of the Linux terminal and to be honest a day doesn\u2019t pass without using. Today, I would like to share some useful tricks and tips for Linux new comers to ease their transition to Linux or simply help them learn something new (hopefully).<\/p>\n<div id=\"attachment_15583\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/10-Linux-Commandline-Tricks.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-15583\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/10-Linux-Commandline-Tricks-620x297.jpg\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/10-Linux-Commandline-Tricks-620x297.jpg 620w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/10-Linux-Commandline-Tricks.jpg 720w\" alt=\"10 Linux Commandline Tricks for Newbies\" width=\"620\" height=\"297\" aria-describedby=\"caption-attachment-15583\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-15583\" class=\"wp-caption-text\">10 Linux Commandline Tricks \u2013 Part 2<\/p>\n<\/div>\n<ol>\n<li><a href=\"https:\/\/www.tecmint.com\/5-linux-command-line-tricks\/\">5 Interesting Command Line Tips and Tricks in Linux \u2013 Part 1<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/manage-file-types-and-set-system-time-in-linux\/\" target=\"_blank\" rel=\"noopener\">5 Useful Commands to Manage Linux File Types \u2013 Part 3<\/a><\/li>\n<\/ol>\n<p>This article intends to show you some useful tricks how to use the Linux terminal like a pro with minimum amount of skills. All you need is a Linux terminal and some free time to test these commands.<\/p>\n<h3>1. Find the right command<\/h3>\n<p>Executing the right command can be vital for your system. However in Linux there are so many different command lines that they are often hard to remember. So how do you search for the right command you need? The answer is\u00a0<strong>apropos<\/strong>. All you need to run is:<\/p>\n<pre><strong># apropos &lt;description&gt;<\/strong>\r\n<\/pre>\n<p>Where you should change the \u201c<strong>description<\/strong>\u201d with the actual description of the command you are looking for. Here is a good example:<\/p>\n<pre><strong># apropos \"list directory\"<\/strong>\r\n\r\ndir (1) - list directory contents\r\nls (1) - list directory contents\r\nntfsls (8) - list directory contents on an NTFS filesystem\r\nvdir (1) - list directory contents\r\n<\/pre>\n<p>On the left you can see the commands and on the right their description.<\/p>\n<h3>2. Execute Previous Command<\/h3>\n<p>Many times you will need to execute the same command over and over again. While you can repeatedly press the\u00a0<strong>Up<\/strong>\u00a0key on your keyboard, you can use the\u00a0<a href=\"https:\/\/www.tecmint.com\/history-command-examples\/\" target=\"_blank\" rel=\"noopener\">history command<\/a>\u00a0instead. This command will list all commands you entered since you launched the terminal:<\/p>\n<pre><strong># history<\/strong>\r\n\r\n    1  fdisk -l\r\n    2  apt-get install gnome-paint\r\n    3  hostname tecmint.com\r\n    4  hostnamectl tecmint.com\r\n    5  man hostnamectl \r\n    6  hostnamectl --set-hostname tecmint.com\r\n    7  hostnamectl -set-hostname tecmint.com\r\n    8  hostnamectl set-hostname tecmint.com\r\n    9  mount -t \"ntfs\" -o\r\n   10  fdisk -l\r\n   11  mount -t ntfs-3g \/dev\/sda5 \/mnt\r\n   12  mount -t rw ntfs-3g \/dev\/sda5 \/mnt\r\n   13  mount -t -rw ntfs-3g \/dev\/sda5 \/mnt\r\n   14  mount -t ntfs-3g \/dev\/sda5 \/mnt\r\n   15  mount man\r\n   16  man mount\r\n   17  mount -t -o ntfs-3g \/dev\/sda5 \/mnt\r\n   18  mount -o ntfs-3g \/dev\/sda5 \/mnt\r\n   19  mount -ro ntfs-3g \/dev\/sda5 \/mnt\r\n   20  cd \/mnt\r\n   ...\r\n<\/pre>\n<p>As you will see from the output above, you will receive a list of all commands that you have ran. On each line you have number indicating the row in which you have entered the command. You can recall that command by using:<\/p>\n<pre><strong>!#<\/strong>\r\n<\/pre>\n<p>Where\u00a0<strong>#<\/strong>\u00a0should be changed with the actual number of the command. For better understanding, see the below example:<\/p>\n<pre><strong>!501<\/strong>\r\n<\/pre>\n<p>Is equivalent to:<\/p>\n<pre><strong># history<\/strong><\/pre>\n<h3>3. Use midnight Commander<\/h3>\n<p>If you are\u00a0not used to using commands such\u00a0<strong>cd<\/strong>,\u00a0<strong>cp<\/strong>,\u00a0<strong>mv<\/strong>,\u00a0<strong>rm<\/strong>\u00a0than you can use the\u00a0<strong>midnight command<\/strong>. It is an easy to use visual shell in which you can also use mouse:<\/p>\n<div id=\"attachment_15574\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/mc-command.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-15574\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/mc-command.jpg\" alt=\"Midnight Commander in Action\" width=\"600\" height=\"409\" aria-describedby=\"caption-attachment-15574\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-15574\" class=\"wp-caption-text\">Midnight Commander in Action<\/p>\n<\/div>\n<p>Thanks to the\u00a0<strong>F1 \u2013 F12<\/strong>\u00a0keys, you can easy perform different tasks. Simply check the legend at the bottom. To select a file or folder click the \u201c<strong>Insert<\/strong>\u201d button.<\/p>\n<p>In short the midnight command is called \u201c<strong>mc<\/strong>\u201c. To install\u00a0<strong>mc<\/strong>\u00a0on your system simply run:<\/p>\n<pre><strong>$ sudo apt-get install mc<\/strong>        [On <strong>Debian<\/strong> based systems]\r\n<\/pre>\n<pre><strong># yum install mc<\/strong>                 [On <strong>Fedora<\/strong> based systems]\r\n<\/pre>\n<p>Here is a simple example of using midnight commander. Open\u00a0<strong>mc<\/strong>\u00a0by simply typing:<\/p>\n<pre><strong># mc<\/strong>\r\n<\/pre>\n<p>Now use the\u00a0<strong>TAB<\/strong>\u00a0button to switch between windows \u2013\u00a0<strong>left<\/strong>\u00a0and\u00a0<strong>right<\/strong>. I have a\u00a0<strong>LibreOffice<\/strong>\u00a0file that I will move to \u201c<strong>Software<\/strong>\u201d folder:<\/p>\n<div id=\"attachment_15575\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Midnight-Commander-Move-Files.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-15575\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Midnight-Commander-Move-Files-620x320.jpg\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Midnight-Commander-Move-Files-620x320.jpg 620w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Midnight-Commander-Move-Files-1024x529.jpg 1024w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Midnight-Commander-Move-Files.jpg 1200w\" alt=\"Midnight Commander Move Files\" width=\"620\" height=\"320\" aria-describedby=\"caption-attachment-15575\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-15575\" class=\"wp-caption-text\">Midnight Commander Move Files<\/p>\n<\/div>\n<p>To move the file in the new directory press\u00a0<strong>F6<\/strong>\u00a0button on your keyboard. MC will now ask you for confirmation:<\/p>\n<div id=\"attachment_15576\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Move-Files-to-new-Directory.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-15576\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Move-Files-to-new-Directory-620x318.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Move-Files-to-new-Directory-620x318.png 620w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Move-Files-to-new-Directory-1024x526.png 1024w\" alt=\"Move Files to New Directory\" width=\"620\" height=\"318\" aria-describedby=\"caption-attachment-15576\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-15576\" class=\"wp-caption-text\">Move Files to New Directory<\/p>\n<\/div>\n<p>Once confirmed, the file will be moved in the new destination directory.<\/p>\n<p><strong>Read More<\/strong>:\u00a0<a href=\"https:\/\/www.tecmint.com\/midnight-commander-a-console-based-file-manager-for-linux\/\" target=\"_blank\" rel=\"noopener\">How to Use Midnight Commander File Manager in Linux<\/a><\/p>\n<h3>4. Shutdown Computer at Specific Time<\/h3>\n<p>Sometimes you will need to shutdown your computer some hours after your work hours have ended. You can configure your computer to shut down at specific time by using:<\/p>\n<pre><strong>$ sudo shutdown 21:00<\/strong>\r\n<\/pre>\n<p>This will tell your computer to shut down at the specific time you have provided. You can also tell the system to shutdown after specific amount of minutes:<\/p>\n<pre><strong>$ sudo shutdown +15<\/strong>\r\n<\/pre>\n<p>That way the system will shut down in\u00a0<strong>15<\/strong>\u00a0minutes.<\/p>\n<h3>5. Show Information about Known Users<\/h3>\n<p>You can use a simple command to list your Linux system users and some basic information about them. Simply use:<\/p>\n<pre><strong># lslogins<\/strong>\r\n<\/pre>\n<p>This should bring you the following output:<\/p>\n<pre>UID USER PWD-LOCK PWD-DENY LAST-LOGIN GECOS\r\n0 root 0 0 Apr29\/11:35 root\r\n1 bin 0 1 bin\r\n2 daemon 0 1 daemon\r\n3 adm 0 1 adm\r\n4 lp 0 1 lp\r\n5 sync 0 1 sync\r\n6 shutdown 0 1 Jul19\/10:04 shutdown\r\n7 halt 0 1 halt\r\n8 mail 0 1 mail\r\n10 uucp 0 1 uucp\r\n11 operator 0 1 operator\r\n12 games 0 1 games\r\n13 gopher 0 1 gopher\r\n14 ftp 0 1 FTP User\r\n23 squid 0 1\r\n25 named 0 1 Named\r\n27 mysql 0 1 MySQL Server\r\n47 mailnull 0 1\r\n48 apache 0 1 Apache\r\n...\r\n<\/pre>\n<h3>6. Search for Files<\/h3>\n<p>Searching for files can sometimes be not as easy as you think. A good example for searching for files is:<\/p>\n<pre><strong># find \/home\/user -type f<\/strong>\r\n<\/pre>\n<p>This command will search for all files located in\u00a0<strong>\/home\/user<\/strong>. The\u00a0<strong>find<\/strong>\u00a0command is extremely powerful one and you can pass more options to it to make your search even more detailed. If you want to search for files larger than given size, you can use:<\/p>\n<pre><strong># find . -type f -size 10M<\/strong>\r\n<\/pre>\n<p>The above command will search from current directory for all files that are larger than\u00a0<strong>10 MB<\/strong>. Make sure not to run the command from the root directory of your Linux system as this may cause high I\/O on your machine.<\/p>\n<p>One of the most frequently used combinations that I use\u00a0<strong>find<\/strong>\u00a0with is \u201c<strong>exec<\/strong>\u201d option, which basically allows you to run some actions on the results of the find command.<\/p>\n<p>For example, lets say that we want to find all files in a directory and change their permissions. This can be easily done with:<\/p>\n<pre><strong># find \/home\/user\/files\/ -type f -exec chmod 644 {} \\;<\/strong>\r\n<\/pre>\n<p>The above command will search for all files in the specified directory recursively and will executed\u00a0<strong>chmod<\/strong>command on the found files. I am sure you will find many more uses on this command in future, for now read\u00a0<a href=\"https:\/\/www.tecmint.com\/35-practical-examples-of-linux-find-command\/\" target=\"_blank\" rel=\"noopener\">35 Examples of Linux \u2018find\u2019 Command and Usage<\/a>.<\/p>\n<h3>7. Build Directory Trees with one Command<\/h3>\n<p>You probably know that you can create new directories by using the\u00a0<strong>mkdir<\/strong>\u00a0command. So if you want to create a new folder you will run something like this:<\/p>\n<pre><strong># mkdir new_folder<\/strong>\r\n<\/pre>\n<p>But what, if you want to create\u00a0<strong>5 subfolders<\/strong>\u00a0within that folder? Running\u00a0<strong>mkdir 5<\/strong>\u00a0times in a row is not a good solution. Instead you can use\u00a0<strong>-p<\/strong>\u00a0option like that:<\/p>\n<pre><strong># mkdir -p new_folder\/{folder_1,folder_2,folder_3,folder_4,folder_5}<\/strong>\r\n<\/pre>\n<p>In the end you should have 5 folders located in new_folder:<\/p>\n<pre><strong># ls new_folder\/<\/strong>\r\n\r\nfolder_1 folder_2 folder_3 folder_4 folder_5<\/pre>\n<h3>8. Copy File into Multiple Directories<\/h3>\n<p>File copying is usually performed with the\u00a0<strong>cp<\/strong>\u00a0command. Copying a file usually looks like this:<\/p>\n<pre><strong># cp \/path-to-file\/my_file.txt \/path-to-new-directory\/<\/strong>\r\n<\/pre>\n<p>Now imagine that you need to copy that file in multiple directories:<\/p>\n<pre><strong># cp \/home\/user\/my_file.txt \/home\/user\/1<\/strong>\r\n<strong># cp \/home\/user\/my_file.txt \/home\/user\/2<\/strong>\r\n<strong># cp \/home\/user\/my_file.txt \/home\/user\/3<\/strong>\r\n<\/pre>\n<p>This is a bit absurd. Instead you can solve the problem with a simple one line command:<\/p>\n<pre><strong># echo \/home\/user\/1\/ \/home\/user\/2\/ \/home\/user\/3\/ | xargs -n 1 \u00a0cp \/home\/user\/my_file.txt<\/strong>\r\n<\/pre>\n<h3>9. Deleting Larger Files<\/h3>\n<p>Sometimes files can grow extremely large.\u00a0I have seen cases where a single log file went over\u00a0<strong>250 GB<\/strong>\u00a0large due to poor administrating skills. Removing the file with\u00a0<strong>rm<\/strong>\u00a0utility might not be sufficient in such cases due to the fact that there is extremely large amount of data that needs to be removed. The operation will be a \u201c<strong>heavy<\/strong>\u201d one and should be avoided. Instead, you can go with a really simple solution:<\/p>\n<pre><strong># &gt; \/path-to-file\/huge_file.log<\/strong>\r\n<\/pre>\n<p>Where of course you will need to change the path and the file names with the exact ones to match your case. The above command will simply write an empty output to the file. In more simpler words it will empty the file without causing high I\/O on your system.<\/p>\n<h3>10. Run Same Command on Multiple Linux Servers<\/h3>\n<p>Recently one of our readers asked in our\u00a0<a href=\"http:\/\/www.linuxsay.com\/\" target=\"_blank\" rel=\"noopener\">LinuxSay forum<\/a>,\u00a0how to execute single command to multiple Linux boxes at once using SSH. He had his machines IP addresses looking like this:<\/p>\n<pre>10.0.0.1\r\n10.0.0.2\r\n10.0.0.3\r\n10.0.0.4\r\n10.0.0.5\r\n<\/pre>\n<p>So here is a simple solution of this issue. Collect the IP addresses of the servers in a one file called\u00a0<strong>list.txt<\/strong>\u00a0one under other just as shown above. Then you can run:<\/p>\n<pre><strong># for in $i(cat list.txt); do ssh user@$i 'bash command'; done<\/strong>\r\n<\/pre>\n<p>In the above example you will need to change \u201c<strong>user<\/strong>\u201d with the actual user with which you will be logging and \u201c<strong>bash command<\/strong>\u201d with the actual bash command you wish to execute. The method is better working when you are using\u00a0<a href=\"https:\/\/www.tecmint.com\/ssh-passwordless-login-using-ssh-keygen-in-5-easy-steps\/\" target=\"_blank\" rel=\"noopener\">passwordless authentication with SSH key<\/a>\u00a0to your machines as that way you will not need to enter the password for your user over and over again.<\/p>\n<p>Note that you may need to pass some additional parameters to the SSH command depending on your Linux boxes setup.<\/p>\n<h3>Conclusion<\/h3>\n<p>The above examples are really simple ones and I hope they have helped you to find some of the beauty of Linux and how you can easily perform different operations that can take much more time on other operating systems.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/10-useful-linux-command-line-tricks-for-newbies\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I remember when I first started using Linux and I was used to the graphical interface of Windows, I truly hated the Linux terminal. Back then I was finding the commands hard to remember and proper use of each one of them. With time I realised the\u00a0beauty, flexibility and usability of the Linux terminal and &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/01\/10-useful-linux-command-line-tricks-for-newbies\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;10 Useful Linux Command Line Tricks for Newbies&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-13270","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13270","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=13270"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13270\/revisions"}],"predecessor-version":[{"id":13271,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13270\/revisions\/13271"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=13270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=13270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=13270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}