Wednesday, January 27, 2021

10 Examples of Essential Linux Commands for Programmers

Hello guys, you might have read many articles about popular Linux commands on the internet but this article is a bit different. In this article, I am going to share some lesser-known options of essential Linux commands like find, grep, mkdir, etc, which many Linux users, including programmers, don't know, even after using the command for quite some time. For example, cp -p is an option that I discovered quite late but ever since I discovered it I have been using it regularly as it preserve timestamp and I can see when this file was originally created. I am going to share 10 such Linux command examples in this post, which I am sure you will love it and if you already know them, then there is a good chance that you have something similar to share with us in the comments section. 

If you have done software development and support on Linux hosts then you may come across the power of Linux commands. They are the most important tool for working server-side application and in the command line and a good knowledge of both basic and advanced Linux commands goes a long way in becoming a better programmer and IT professional. 

I personally love learning new Linux commands to improve my toolset and productivity and every time I find some useful options or a new command, I feel very excited. Today, I am going to share some of the most useful options of very popular Linux commands you will think should have known earlier.

These are the commands, many of us use on a daily basis like cp, mkdir, grep, netstat, SCP, but still, there are some useful options that we don't know. At least I didn't know many of them even after using these UNIX commands for years.

These command-line options will not make you a Linux superuser but will definitely arm you with some good information that will save your day in the future, but if you are not familiar with those commands itself then I also suggest you go through a comprehensive course like Linux Mastery: Master the Linux Command Line in 11.5 Hours to learn Linux commands from scratch. These options will make more sense if you know about command in advance.




10 Powerful Command-Line Options of Common Linux Commands

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.

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.

$ cp - p $file

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 chasing 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.

$ mkdir - p

If the mkdir command is new to you then I highly recommend you to join a comprehensive Linux course like Learn Linux in 5 Days and Level Up Your Career on Udemy to learn these essential Linux commands and learn how to create a complex directory structure in a single command.

10 essential Linux command line options for beginners



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. For more grep examples, I suggest you check out my post 10 grep examples for beginners


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. You can further see  Linux Mastery: Master the Linux Command Line in 11.5 Hours to learn more about Essential Linux commands for developers. 

10 Examples of Essential Linux Commands for Beginners



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. If you want to learn more about tail and other important Linux commands, you can also check the Getting Started with the Linux Command Line course on Pluralsight by David Clinton.

10 examples of Linux commands for beginners

Btw, you would need a Pluralsight membership to watch this course, alternatively, you can use their 10-day free trial to access this course for free. 



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. If you want to learn more you can further see this Linux tutorial on the same topic. 


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. 

On the same note, I highly recommend programmers to learn shell scripting, particularly bash shell scripting, If you need a resource, I recommend this interactive shell scripting course form Educative - Bash for Programmers which will teach you everything you need to write shell script from scratch, including the platform your you can execute shell script on the browser. 

best course to learn shell script for programmers



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 the 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, curllsofdf, 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.


Related Linux Command Tutorials
  • 10 Best Linux Courses for Programmers and Developers (Courses)
  • My favorite tips to work fast in Linux (tips)
  • How to get an IP address from the hostname and vice-versa in Linux (command)
  • 5 Best courses to learn Bash Scripting (courses)
  • 10 examples of the xargs command in Linux (examples)
  • 5 Free Courses to learn Linux for Beginners (Free courses)
  • 10 examples of date command in Linux (examples)
  • How to create, update and delete soft link in UNIX (command)
  • 10 examples of Vim in UNIX (examples)
  • My Favorite Courses to learn VIM Editor in-depth (courses)
  • 10 examples of cut command in Linux (examples)
  • 5 examples of sort command in Linux (examples)
  • 6 Free Courses to learn Bash scripting in-depth (free courses)
  • 5 examples of kill command in Linux (examples)
  • 10 Books every Linux Power user should read (books)

Thanks for reading this article so far. If you like these Essential Linux commands for beginners then please share them with your friends and colleagues. If you have any questions or feedback then please drop a note.

P. S. - If you are new to Linux and looking for a free online training course to learn essential commands and concepts in Linux then I also suggest you check out Learn The Linux Command Line: Basic Commands course on Udemy. It's completely free and more than 50,000 students have enrolled in this course. All you need is a free Udemy account to join this course.

No comments :

Post a Comment