Following 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.
List
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
Copy a large number of files without prompt
yes | for file in /source_destination/*; do cp “$file” /destination_directory/; done
Search/Find
find . -exec grep “searchterm” ‘{}’ \; -print
Add -s option to ignore warnings
find . -exec grep -s “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.
Sort
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
/scripts/rebuildippool
Find public IP address of local PC
curl ifconfig.me
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
FTP
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 *
PHP
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
Exim
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
#mysql
#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
mysql-bin.000012
mysql-bin.000013
mysql-bin.000014
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
#`ls’ at 0 [Delaying before reconnect: 85]
Then add following line in /etc/sysconfig/iptables-config file
IPTABLES_MODULES=
"nf_conntrack_ftp"
restart iptable and it should work.
If it says error for nf_conntrack_ftp module then try following
IPTABLES_MODULES=
""
iptable
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
Process
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
WHM/cPanel
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
/usr/local/apache/htdocs
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.
Leave a Reply