Linux Quick Commands: Tips and Tricks

Linux terminal is always difficult to work with, especially in the beginning when you are still new to Linux. I remember my first contact with the Linux terminal like it was yesterday. Eventually, I interacted with a machine that responded to whatever I typed. I started to learn basic commands like “cd”, “ps” “w” “mkdir” that were around when I entered the digitalized world of computers. Then I discovered some great combinations of commands to ease the administration of the server / services or configurations.

Linux is the OS that is used almost everywhere in our world. It is an extremely flexible system due to its openness, which allows anyone to contribute. It’s much faster than Windows out of the box, and unlike Windows, you can get Linux for free. The astonishing fact is that a whopping 96.4% of the world’s 500 largest supercomputers run Linux. It can be found anywhere from smart refrigerators to self-driving cars.

If that doesn’t convince you to start learning Linux, just imagine the many jobs available for a Linux system and network engineers, Linux kernel developers, or any on-the-go liaison on the subject. So what it’s never too late to start learning

The purpose of this article is not to praise (which I actually did) Linux as the best and most flexible operating system, but to provide some of the tips and tricks that I have learned over the years for Linux users, whether they are newbies or intermediate users. For absolute beginners, we have some excellent articles on basic Shell commands and 10 basic Linux commands that you can check in advance.

A Linux interface command or terminal is a vast ocean of possibilities. You can do a lot of things using commands that some people find intimidating given the large number of commands available on the system. The good thing is that you don’t need to memorize anything, because you can use commands such as “apropos” or “history” to get a list of commands that you may or have used in the past.

So let’s get started.

In order to use the Linux terminal, you must have a Linux VPS (preferably with full root access) or a local Linux machine that you can use. Thus, open a terminal or connect to a Linux server.

First, check the user you are logged in with with the whoami command:

[[email protected] /]# whoami
root

This way you have root access, okay. You have all the necessary privileges and access to any command on a Linux system. Be careful though, with wide powers comes a lot of responsibility. Root access can always turn into a nightmare if the user is not careful with the commands when he is running as the root user.

First, let’s check all the directories and how long they are in the / etc directory. Use the du command along with some necessary flags:

[[email protected] /]# du -chsx /etc/* | sort -rh | head -6

We ran this command on our CentOS 7 VPS system and got below output:

26M     total
14M     /etc/httpd
6.6M    /etc/udev
1.4M    /etc/pki
650K    /etc/services
311K    /etc/sysconfig

Create a parent directory along with your attachments with one command:

[[email protected] /]# mkdir -p tmp/andreyex/programmer

With &&, you define the commands to be executed after successful execution of the first ones in the list on the left. Simple example:

[[email protected] /]# cd tmp/andreuex/programmer && ls -lat

If the first command is not successful for some reason, then the second will not run.

What if you want to list all directories in your user’s home directory? Use this excellent command:

[[email protected] /]# find $HOME -type d -ls | less

To copy files to multiple directories:

[[email protected] ~]# echo /usr/dir1 /var/dir2 /nas/dir3 |  xargs -n 1 cp -v /path/to/file

Check how many connections and their IPs are connected to your webserver port 80:

[[email protected] /]# netstat -plane | grep :80 | awk '{print $5}' | grep -Eo '([0-9]{1,3}.){3}[0-9]{1,3}'| sort | uniq -c | sort -n

Recursively changes to file and directory permissions by running the following command in the parent directory where you want to change permissions. For example, if you have a running WordPress site in the / var / www / html / directory, navigate to that directory and run:

To recursively change file permissions:

[[email protected] /]# find . -type f -exec chmod 644 {} ;

Directories:

[[email protected] /]# find . -type d -exec chmod 755 {} ;

These two commands are especially useful when you need to quickly set permissions for each file / directory within the corresponding directory tree.

Remove all email messages from the mail queue in Exim:

[[email protected] /]# exim -bp | exiqgrep -i | xargs exim -Mrm

To find world-writable files on the server:

[[email protected] /]# find / -type f -perm -o+w -exec ls -l {} ;

To find world-writable files in the / home directory:

[[email protected] /]# find /home -type d -perm -o+w -exec ls -ld {} ;

Sometimes you need to list processes with a common name. For example, I need to list all the processes that are started using postfix. I will do the following:

[[email protected] /]# ps -ef | grep postfix | grep -v grep | awk '{print $2}'

If I want to terminate processes, I will use the command:

[[email protected] /]# kill -9 `ps -ef | grep postfix | grep -v grep | awk '{print $2}'`

If you want to delete all files in a directory that don’t match a specific file extension?

[[email protected] /]# $rm !(*.html | *.php | *.png)

This command will remove all files that are not .html, .php or .png

Editing a file on a remote host with a Vim text editor:

[[email protected] /]# vim scp://[email protected]/path/to/my_file

Replace all instances of a given word with the one you want without opening the file with a text editor:

[[email protected] /]# perl -pi -e 's,AndreyEx,BestProgrammer' my_file.php

The above command will replace AndreyEx with BestProgrammer in my_file.php file.

After I wanted to know the commands and their flags, but I don’t know which one to use. Thus, I used the command to generate random man pages:

[[email protected] /]# man $(ls /bin | shuf | head -1)

It might be interesting to randomly learn new commands and leave the choice on your Linux machine.

Sometimes, during hectic hours of work, you have created or modified files, but you do not remember which one you created / modified. So why not use a command to list just today’s files?

[[email protected] /]# ls -al --time-style=+%D | grep `date +%D`

will list today’s files in the output in a specific format.

You can open a chat session with another authorized user on your Linux machine.

[[email protected] /]# write furious

So if furious is logged in, then this command will put you on a blank line where whatever you type will be sent to another user.

A very useful command that I use whatis. Its output provides an overview of what the team is doing.

[[email protected] /]# whatis man
man (1)              - an interface to the on-line reference manuals
[[email protected] /]# whatis pwd
pwd (1)              - print name of current/working directory

Last but not least, a personal favorite that you can use to check your disk write speed.

[[email protected] /]# dd if=/dev/zero of=/tmp/my_output.txt bs=8k count=256k conv=fdatasync; rm -rf /tmp/my_output.txt

That’s all. I hope you find some use in these Linux command tricks, which are just a glimpse of the command capabilities offered in Linux. Of course, if you have any tricks, share them in the comments below.

Sidebar