Shahriar Kabir

Software Engineer

MCPD

OCJP

MCSE

Shahriar Kabir

Software Engineer

MCPD

OCJP

MCSE

Blog Post

10 Best Linux Command for Beginners to Learn in 2022

February 11, 2022 Linux
10 Best Linux Command for Beginners to Learn in 2022

Linux Command for Beginners – You might be thinking about why I am sharing these Linux command options? Well, there are multiple benefits for me writing this article and sharing these Linux examples.  First, it helps me to consolidate my knowledge, and by sharing I have better learned and remembered these Linux command-line options.

Another reason I am sharing these options is in the hope to learn from you all. Everyone knows a trick or two when it comes to the Linux command line and by sharing my preserved tips I am hoping to see gems like these coming from you guys. So, feel free to share Linux command options that have helped you in the past.

Without wasting any more of your time, here are some of the rather unknown but useful options of common Linux commands, I feel I should have known earlier.

Best Linux Command for Beginners to Learn

1. cp -p (preserves timestamp)

You have definitely used the cp command to copy files or directories but do you know how to preserve timestamps while copying files or directories? Well, I didn’t, at least until a few months ago. The cp – p command will preserve the timestamp when you copy a file. It’s very useful, particularly for taking backup while changing files. The timestamp of the backed file is very important for troubleshooting etc.

When I first come to know about this option from a support guy, I was like OMG, how could I miss this option for so many years, but that’s the reality, sometimes we lookup a very important thing in front of our eyes and chase a new shiny thing which doesn’t exist.

2. mkdir -p (create directory structure)

I am sure you have used the mkdir command to create directories and sometimes you might have been frustrated also while creating a complex directory structure one by one. You could have saved a lot of your time there if you knew the mkdir -p option which copies parent directories if it doesn’t exist.

For example, when you copy a file /app/config/app.properties then it will create those directories automatically if they don’t exist.

find -mtime (search files by modified time)

The find command is one of the most powerful commands in Linux and I have spent considerable time learning many useful find command options to improve my productivity. You can find all of them in my earlier post about 10 examples of find command in Linux.

One of them is -mtime which is a shortcut for modified time and can be used to search files that have been modified recently like in hours or days. This is very useful while troubleshooting to check if anyone touched any important files.

3. scp -i (file copy without password using identity file)

This is another useful Linux option of a very common command like SCP which I discovered very late. The SCP command is used to securely copy files and directories from one host to another host.

When you copy files using SCP, it asks for your username and password but if you are doing using script, you better want to do a password-less SCP and that’s where scp -i helps. It can use an identity file to copy files from the remote host rather than using your current username.

The Identity file contains ssh keys which are required to login to the remote host and copy files.

$ scp -i /home/appuser/.ssh/id_rsa_app

datauser@host:/app/data/outbound/stocks.xml

Here the identity file: id_rsa_app contains the private/public key for “datauser”. The beauty of this option is that it won’t ask for a password if you run this command.

4. grep -c (count number of matches)

Many times we are interested in knowing whether the file contains a matching word or not and the grep command just tells you that, but sometimes you also want to know how many times that particular keyword appears in files? I mean, could of the matching keyword.

That’s where grep -c helps. It will tell you the count of the matching keyword.

It’s very useful to find details like how many times a user has logged in or how many files he has downloaded, or how many instruments you have received from upstream, and so on. For example,

$ grep -c “keyword” app.log

will print how many times “keyword” has appeared in the app.log file.

5. find -mtime (search files by modified time)

The find command is one of the most powerful commands in Linux and I have spent considerable time learning many useful find command options to improve my productivity. You can find all of them in my earlier post about 10 examples of find command in Linux.

One of them is -mtime which is a shortcut for modified time and can be used to search files that have been modified recently like in hours or days. This is very useful while troubleshooting to check if anyone touched any important files. For example

$ find -mtime -3

will print the files that have been modified in less than 3 days or in the last three days. You can also search for more than 3 days like using +3 days.

6. cd – (takes you to the previous directory)

We all have used cd but do you know how to go to the previous directory quickly? Well, that’s what “cd -” do. It takes you to the previous directory. Yes, even without any option cd command is powerful. To be honest, I was lucky to know this item a little be earlier and since then I have been using it almost on a daily basis.

$ cd /home/test1/

$ cd /opt/bin/

$ cd –

/home/test1/

7. pwd -P (shows the actual path for soft linked directory)

This is again one of those basic Linux commands you will learn in your first Linux class but how many of you know this option? Let me ask you another way, how do you find the actual path of a folder which is linked? Well, that’s where pwd-P helps.

Just pwd will tell you the current folder with the path you followed, which could be linked also but pwd -P will tell you the actual folder location as shown in the following example:

$ pwd

/com/unicorn/app/current

$ pwd -P

/com/unicorn/app/1.0.23

In this case, the current is actually a link to the version 1.0.23 folder.

8. tail -f (seeing live updates on a file)

This is another useful option for one of the most common Linux commands. If you haven’t come across the tail, it is used to see the content of the file from the bottom, and the tail -f lets you see the live updates. This option is particularly useful to see if your log file is moving and what is logged at the moment.

$ tail -f app.log

will show you the last few lines of log files which will keep updating as and when the process will write on this file.

9. netstat -p (print process id of the process for connections)

This is one of the powerful Linux networking commands generally used to find the process which is listening on a particular port, but do you know how to print the process id which is listening on a port? Well, that’s where netstat -p helps. It prints the process id and name of the program to which each socket belongs. This is useful because you now also kill the process if you don’t want to.

$ netstat -nap | grep LISTEN

This Linux command will print process id and port of all the process which are listening on some port. I mean servers.

10. The for loop ( good for automation of repeated tasks)

This is not exactly a Linux command but a basic bash shell construct which allows you to automate some task. For example, if you have your application running on 5 production host and you want to check if they have enough space or not. Instead of going to each host using ssh and checking manually, which would take around 10 to 15 minutes, you can just run this shell script and get this done in seconds.

$ for h in host1, host2, host3, host4, host5;

do ssh “${h}” “df -h /app/logs”;

done

This is one of the most basic shell scripts you will see but it’s so powerful that it has saved me tons of time in the past.

That’s all about some of the useful options of common Linux commands you should have known earlier. I really get amazed with Linux commands whenever I discover these kinds of gem. I know these are just the tip of the iceberg and there are many more useful options of popular Linux commands like find, grep, ssh, curl,  lsof, df, chmod, tar which many programmers like me didn’t know.

If you think you know a command option that is not so popular but very useful feel free to share it with us in the comments.

Write a comment