Essential Linux Commands

Linux Sep 22, 2011 2 Comments

LinuxFollowing are some of the most essential Linux commands which I often use and might be a one page reference for you as well.

Use these Linux commands at your own risks. Some of these commands, specially the rm commands, might do harm if not used properly without additional checks. So always think twice before you run the rm commands.

I use these commands all the times on my centOS dedicated servers whenever I need it and they work flawlessly.


Show file sizes in KB, MB, GB

ls -lah

List only directories

ls -l | egrep `^d’

List and sort by date

ls -t

ls -lt

Sort by file size

ls -S

List and count number of files

ls -1 | wc -l


Copy a large number of files without prompt

yes | for file in /source_destination/*; do cp “$file” /destination_directory/; done


find . -exec grep “searchterm” ‘{}’ \; -print

Above command will find a searchterm in all files in the present directory and will print it on screen.

If you want to search for a term in specific folders then use following command.

find /home/*/public_html/ -exec grep “searchterm” ‘{}’ \; –print

Above command comes very handy when you want to search for any term in all of your websites hosted on a single host. It also helps you to find any malware of hack scripts.

You can capture the result in a file using this command instead of displaying on the screen

find /home/*/public_html/ -exec grep “searchterm” ‘{}’ \; –print > result.txt

Find .htaccess files in /home
find /home/*/ -type f -name .htaccess

Searches for .htaccess files in /home/user top level folders and doesn’t search in the subfolders.
find /home -maxdepth 2 -type f -name .htaccess

find /home/*/public_html/wp-content/ -maxdepth 1 -type f -name .htaccess

Search for large files

Following command will search for files with size more than 500M. You can use G and k for Giga bytes and Kilo bytes respectively.

find . -type f -size +500M -exec ls -lh {} \; | awk ‘{ print $9 “: ” $5 }’

Find files modified by date

find . -iname “*” -mtime -10 -print

This command will find all files modified in last 10 days in current directory.


To remove duplicate lines from a file

cat inp_file | sort | uniq > out_file

File Listings

Show last 100 lines of a file

tail file.txt -n 100

IP Address related/Netowrking

/etc/ips –  Contains the IP address and Netmasks.

If you want to change any entries in this file then you will have to run following commands

service ipaliases restart

/etc/init.d/ipaliases reload


Find public IP address of local PC


Disk Space

df -k

This shows the disk space in kilo blocks. The following command shows the disk paces used and available.

df -hP

df -h |grep “[0-9]G”  this is very handy command, it lists all the files in which has G size.

du -h |grep “^[0-9,]\+G” shows the file sizes in G

du -sh /home/*  – this will show the sizes for the folders in /home folder.

du -sh /home/* |grep “^[0-9,]\+G”

cd /

du -sch *

This shows the directory sizes in M/G

du -h shows files in K/M/G sizes.

If / is full then remove the unused log files from /var/log/*

Remove all error_log file from all accounts

rm -rf /home/*/www/error_log

Hard Drive

S.M.A.R.T tool

S.M.A.R.T tool is a an excellent tool to obtain hard drive details and also to check hard drive’s overall health. If you have installed and configured this utility then it will also send you the alert if hard drive fails.

smartctl -H /dev/sda

Checks the overall health of hard drive. It will also report any hard disk failure.

smartctl -i /dev/sda

With this command you can obtain all the information on a hard drive.

Short test for hard drive
smartctl –test=short /dev/sda

Long test for hard drive
smartctl –test=long /dev/sda

Full test for hard drive
smartctl -a /dev/sda


To restart pure-FTP

service pure-ftpd start

Check FTP service status

service pure-ftpd status

Bulk delete

First enter prompt command in ftp mode and then run following command to delete files in bulk. prompt turns of interactive mode.

mdel *


How to find the default php.ini file location.

php -r “phpinfo();” | grep Configuration

Configuration File (php.ini) Path => /usr/local/lib
Loaded Configuration File => /usr/local/lib/php.ini

As suggested by another Linux expert this command also does the same job.

php -i | grep Configuration

Flush a Slow Query Log in MySQL

MySQL config file path /etc/my.cnf

cd /var/lib/mysql

mv mysqladmin flush-logs mysqladmin flush-logs.old

mysqladmin flush-logs


List all queued messages

#exim -bp

Remove all queued messages

#exim -bp | exiqgrep -i | xargs exim -Mrm

Cleaning large eximstats mysql database

If eximstats directory (/var/lib/mysql/eximstats) contains large files and it is eating up space then celan it up by following commands

#use eximstats
>delete from sends;
>delete from smtp;
>delete from failures;
>delete from defers;

Deleting log files and freeing up space

In case your / mount is full and you need to delete some files

yum clean all

cd /usr/local/apache/logs; rm -rf *_log

cd /usr/local/apache/domlogs; rm -rf *

cd /var/log/; rm backup_all.log; rm chkservd.log;rm exim_mainlog.*; rm exim_paniclog.*; rm exim_rejectlog.*

go to /var/lib/mysql/
Remove following temp files

ls -laS /home/*/www/error_log |more
rm /home/*/www/error_log

Exim – Mailserver

Restarting Exim

/etc/rc.d/init.d/exim restart

lftp commands

mrm – Multiple file remove, rm doesn’t accept wildcard so use mrm.

In order to delete multiple files/directory supporting wildcard try glob before rm.

glob -a rm -r <file/directory><wild_card>

Problem in connecting to lftp

If you connect to a server using lftp and see following error

#`ls’ at 0 [Delaying before reconnect: 85]

Then add following line in /etc/sysconfig/iptables-config file


restart iptable and it should work.

If it says error for nf_conntrack_ftp module then try following



To start firewall

# chkconfig iptables on
# service iptables start

To check status

# service iptables status

To stop firewall

# service iptables stop

To restart

# service iptables restart


Kill a process by name

#killall -v lftp

Kill a process by PID

There are various commands to kill a process by name. The one which works for me in centOS is following

First find out the pids of all the processes. The following command will find all the pids of processes named lftp

# pidof lftp
23656 23655 23653 23651 23648 23645

Run simple old kill -9 command followed by pids of the processes.

#kill -9 23656 23655 23653 23651 23648 23645


Script to enable DKIM and SPF for al accounts

for i in `ls /var/cpanel/users` ;do /usr/local/cpanel/bin/dkim_keys_install $i ;done
for i in `ls /var/cpanel/users` ;do /usr/local/cpanel/bin/spf_installer $i ;done

Default holding page


Linux Command Line Editing

Alt-d Delete the word, starting at the current cursor position.

Alt-Backspace Delete every character from the current cursor position to the
first character in the word.


2 Responses to “ Essential Linux Commands ”
  1. Dave

    I’m going to have to address some of these.

    Much simpler than: find . -exec grep “searchterm” ‘{}’ ; -print

    is grep -r “searchterm” .

    The example using find is handy when you only want to grep through particular files such as those ending in .txt or .php:

    find . -name ‘*.php’ -exec grep “searchterm” ‘{}’ ; -print

    The bit about /etc/ips is not necessarily true. It depends on what you have installed. Those commands may not work at all on other distros. In fact, this is true of several of these commands.

    The command rm -rf /home/*/www/error_log is dangerous. If any file or directory in your home folder has a space in it you won’t delete that path and you may well delete some other path. Let’s imagine I’ve created a folder in /home called “Trash bin”. If you run that command from /, it will try to delete /bin/www/error_log.

    An easier version of php -r “phpinfo();” is php -i.

    cd /usr/local/apache/domlogs; rm -rf * is another dangerous command. If the first command fails (say, the directory doesn’t exist or you don’t have permission to cd there) the second command will still run and will delete everything wherever you are. You can use && instead of ; in between commands so that the second one will only run if the first one succeeded but frankly I’d much rather type the two commands separately, preferably with an ls -l in between.

    The files named like mysql-bin.000012 are not temporary files. They are MySQL’s binary log. If you are using MySQL replication, these files are sent to the slaves and then the commands in them are replayed so the slaves are the same as the master. Once they have been sent to all slaves, they are not needed. But you should delete them with the PURGE command from within MySQL, not deleting them from the filesystem. This allows MySQL to update the index. If you are not running MySQL replication, you should turn off binary logging. You can also tell MySQL to keep a certain number of days worth of these logs and automatically delete older ones with log_expire_days.

    • Ajay

      Tx Dave,
      Your comments are valuable and realistic.

      The commands I’ve listed above are those commands which I often use on my centOS server and they work perfectly well.

      I agree that some of them might not work and can cause harm if not used properly.

Leave a Reply