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:

[[email protected] /]# 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:

[[email protected] /]# 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:

[[email protected] /]# mkdir -p tmp/rose/hosting/bestvps

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

[[email protected] /]# 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:

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

To copy files into multiple directories run:

[[email protected] ~]# 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:

[[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

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:

[[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 the permissions for every file/directory inside the corresponding document tree.

Remove all emails from Exim’s mail queue:

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

To find world-writable files on your server:

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

To find world-writable directories in /home:

[[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 processes that are run by postfix. So I execute:

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

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

[[email protected] /]# 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?

[[email protected] /]# $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:

[[email protected] /]# vim scp://[email protected]/path/to/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,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:

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

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

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?

[[email protected] /]# 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.

[[email protected] /]# 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.

[[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

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

[[email protected] /]# 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. You can also follow us on Twitter, Facebook, Google+ and LinkedIn, where we frequently post quick Linux tips and tricks with our #RoseTips hashtag.

Tips and Tricks
MySQL Performance Optimization
Tips and Tricks
5 Essential Tips for Securing Your WordPress Sites
Tips and Tricks
How to setup MySQL replication on CentOS
There are currently no comments.

 
Free Migration to RoseHosting. Award Winning Linux VPS Hosting with Fully Managed Support. Coupon RHBLOG50 for 50% offOrder Now
+