Automation QA Testing Course Content

10 Powerful Command-Line Options of Common Linux Commands

 10 Powerful Command-Line Options of Common Linux Commands

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


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

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 day


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

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

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

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



No comments:

Post a Comment

Note: Only a member of this blog may post a comment.