Quick Linux Tips and Tricks

quick linux tips and tricks

Using a Linux terminal is always difficult, especially at the beginning when everything is new to the user. I remember my first contact with a Linux terminal like it was yesterday. After all, I was interacting with a machine that responded to everything I typed. I started learning the basic commands such as “cd”, “ps” “w” “mkdir” which at the time it was like I entered another world, a digitalized one which I’m just starting to uncover. The excitement I felt back then still hits me when I discover some great combination of commands to ease the server/service administration or configuration.

Linux is an all-present OS in our world. It is an extremely flexible system due to its open source nature which allows anyone to contribute. It’s way faster than Windows out of the box, and unlike Windows, you can get Linux for free. An astonishing fact is that a whopping 96.4% of the top 500 supercomputers in the world are running on Linux. It can be found anywhere, from smart refrigerators to self-driving cars.

If this does not convince you to start learning Linux, just imagine the plethora of available jobs for Linux system and network engineers, kernel developers or any Linux-connected-job for that matter. And the beauty of it is that it’s never too late to start learning.

The purpose of this article is not to laud (which I actually did) Linux as the best and most flexible operating system, but to provide some tips and tricks that I learned over the years to Linux users whether they are beginners or intermediate users. For the absolute beginners out there, we have some fine articles on Basic Shell Commands and 10 Basic Linux Commands that you can check beforehand.

A Linux command interface or a terminal is a vast ocean of possibilities. You can do lots of stuff using the commands, which some people find intimidating considering the large amount of commands available at the tips of their fingers. The good thing is that you don’t have to memorize anything because you can use commands such as “apropos” or “history” to get a list of commands that you can use or used in the past respectively.

So let’s start typing, shall we?

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

First, check the user with which you are logged in. The whoami command can be used:

[root@vps /]# whoami
root

So you have root access, good. You have all the required privileges and access to every corner of the corresponding Linux system. Be careful, though, with great power comes great responsibility. Root access can always turn into a nightmare if the user is not careful with the commands he runs as root.

For starters, let’s check the top disk-consuming directories in /etc. Use the du command along with some needed flags:

[root@vps /]# du -chsx /etc/* | sort -rh | head -6

We ran this command into our CentOS 7 VPS command shell and got the below output:

27M     total
15M     /etc/httpd
6.7M    /etc/udev
1.5M    /etc/pki
660K    /etc/services
312K    /etc/sysconfig

Create a parent directory along with their children using a single command:

[root@vps /]# mkdir -p tmp/rose/hosting/bestvps

By using && you are defining commands by their successful execution. A simple example:

[root@vps /]# cd tmp/rose/hosting/bestvps && ls -lat

If the first command does not succeed for some reason, then the second one will not be executed.

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

[root@vps /]# find $HOME -type d -ls | less

To copy files into multiple directories run:

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

Check how many connections and from which IP are made to your web server port 80:

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

Make recursive changes on permissions for files and directories by running the below command into the location of the parent directory that you want to change permissions for. For example, if you have your WordPress powered site in /var/www/html/, navigate to that directory and run:

For a recursive change of file permissions:

[root@vps /]# find . -type f -exec chmod 644 {} \;

Directories:

[root@vps /]# find . -type d -exec chmod 755 {} \;

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

Remove all emails from Exim’s mail queue:

[root@vps /]# exim -bp | exiqgrep -i | xargs exim -Mrm

To find world-writable files on your server:

[root@vps /]# find / -type f -perm -o+w -exec ls -l {} \;

To find world-writable directories in /home:

[root@vps /]# 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 processes that are run by postfix. So I execute:

[root@vps /]# ps -ef | grep postfix | grep -v grep | awk '{print $2}'

Then If I want to terminate those same processes this command will be used:

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

Want to delete all files in a directory that don’t match a certain file extension?

[root@vps /]# $rm !(*.html | *.php | *.png)

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

Edit a file on a remote host using the Vim text editor:

[root@vps /]# vim scp://username@host/path/to/file

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

[root@vps /]# perl -pi -e 's,RoseHosting,BestManagedVPS' file.php

The above command will replace RoseHosting with BestManagedVPS in the file.php file.

Once I wanted to learn commands and their flags, but I didn’t know which one to start with. So I used a command to generate random man pages:

[root@vps /]# man $(ls /bin | shuf | head -1)

It can be fun to randomly learn new commands and leave the choice to your Linux machine.

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS

Sometimes during hectic work hours you created or modified files, but you didn’t remember which one you’ve created/modified. So why not use a command to list today’s files only?

[root@vps /]# ls -al --time-style=+%D | grep `date +%D`

will list today’s files in an output in particular format.

You can have a chat session with another logged in user into your Linux machine. Yes, you’ve read it right.

[root@vps /]# write jeffrey

So if jeffrey is logged in, this command will place you on a blank line where everything you type will be sent to the other user.

A very useful command that I use is whatis. Its output provides a summary of what a command does.

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

And last but not least a personal favorite of mine that you can use to check your disk write speed.

[root@vps /]# dd if=/dev/zero of=/tmp/output.txt bs=8k count=256k conv=fdatasync; rm -rf /tmp/output.txt

That’s it for now. I hope you’ll find some usage in these Linux tricks with commands which are just a glimpse of the possibilities that Linux commands offer. Of course, if you have some tricks up your sleeve, share them in the comments section below.

Leave a Comment