Most useful commands for Linux

* Continuously check Apache error log file:
$ tail -f /var/log/httpd/error_log

* View first 15 lines from MySQL log:
$ head -15 /var/log/mysqld.log

* vi keyboard shortcuts
=> jump to end of line
$
=> start of line
0
=> Delete rest of line
D
=> Repeat the last command given:
. (dot)

=> add ‘maal’ to the end of every line. 1 is line 1, $ is the last line
:1,$ s/$/maal/

=> put ‘bingo’ at the start of lines 5-10
:5,10 s/^/bingo/

=> change foo to bar for all occurrences in the rest of the file from where the cursor is
:s/foo/bar/g

=> Delete current line and got into insert mode.
C

=> Remove the ^M from files that came from windows:
:se ff=unix

=> Turn on/off display of line numbers:
:set nu
:set nonu

=> if you want actual line numbers in your file:
:%!cat -n

=> find the word under cursor
* (star)

* screen command
Just type screen and your problems are solved 🙂
$ cat ~/.screenrc
# no annoying audible bell, please
vbell on

# detach on hangup
autodetach on

# don’t display the copyright page
startup_message off

# emulate .logout message
pow_detach_msg «Screen session of $LOGNAME $:cr:$:nl:ended.»

# advertise hardstatus support to $TERMCAP
termcapinfo xterm* ti@:te@

# make the shell in every window a login shell
shell -$SHELL

defscrollback 10000

# Extend the vt100 desciption by some sequences.

termcap vt* AF=E[3%dm:AB=E[4%dm
caption always
caption string ‘%{= wk}[ %{k}%H %{k}][%= %{= wk}%?%-Lw%?%{r}(%{r}%n*%f%t%?(%u)%?%{r})%{k}%?%+Lw%?%?%= %{k}][%{b} %d/%m %{k}%c %{k}]’

# keybindings

bind -k F5 prev
bind -k F6 next

netstat
* Display total number of internet (port 80) connections:
$ netstat -an |grep :80 |wc -l

* Display all ports your machine listening on:
$ netstat -ant | grep LISTEN

nmap
* Scan a machine on your LAN with nmap and know which ports are open on it:
$ nmap ip

find
* find top 10 largest files in /var:
$ find /var -type f -ls | sort -k 7 -r -n | head -10

* find all files having size more than 5 GB in /var/log/:
$ find /var/log/ -type f -size +5120M -exec ls -lh {} ;

* find all today’s files and copy them to another directory:
$ find /home/me/files -ctime 0 -print -exec cp {} /mnt/backup/{} ;

* find all temp files older than a week and delete:
$ find /temp/ -mtime +7-type f | xargs /bin/rm -f

* find and rename all mp3 files by changing their uppercase names to lowercase:
$ find /home/me/music/ -type f -name *.mp3 -exec rename ‘y/[A-Z]/[a-z]/

grep
* Print Apache’s documentroot directory name:
$ grep -i documentroot /etc/httpd/conf/httpd.conf

* View file contents without comments and empty lines:
$ grep -Ev “^$|^#” /etc/my.cnf

* print only IP address assigned to the interface:
$ ifconfig eth0 | grep ‘inet addr:’ | cut -d’:’ -f2 | awk ‘{ print $1}’

* How many email messages sent for a particular date:
$ cat /var/log/maillog | grep «status=sent» | grep «May 25» | wc -l

* Find out a running process/daemon from process list (thanks to staranneph for recalling this):
ps -ef | grep mysql

* You can also note cpu/mem usage by using above. like in below command output, you can see that Plesk’s statistics process is utilizing more than 18% cpu alone:
[root@myserver ~]# ps aux | grep statistics
root 8183 18.4 0.0 58384 2848 ? D 04:05 3:00 /usr/local/psa/admin/sbin/statistics

Backup MySQL Database to a file

Backing up your database is a very important system administration task, and should generally be run from a cron job at scheduled intervals. We will use the mysqldump utility included with mysql to dump the contents of the database to a text file that can be easily re-imported.

Syntax:

mysqldump -h localhost -u root -pmypassword databasename > dumpfile.sql

Example:

mysqldump -h localhost -u root -p2Uad7as9 database01 > dumpfile.sql

This will give you a text file containing all the commands required to recreate the database.

A «live» view of a logfile on Linux

This approach works for any linux operating system, including Ubuntu, and is probably most often used in conjunction with web development work.

tail -f /path/thefile.log

This will give you a scrolling view of the logfile. As new lines are added to the end, they will show up in your console screen.

For Ruby on Rails, for instance, you can view the development logfile by running the command from your project directory:

tail -f log/development.log

As with all linux apps, Ctrl+C will stop it.

Backup your bootsector

Messing with bootloaders, dual-booting and various other scary processes can leave you with a messed up bootsector. Why not create a backup of it while you can:

dd if=/dev/hda of=bootsector.img bs=512 count=1

Obviously you should change the device to reflect your boot drive (it may be sda for SCSI). Also, be very careful not to get things the wrong way around – you can easily damage your drive! To restore use:

dd if=bootsector.img of=/dev/hda

Transferring files without ftp or scp

Need to transfer a directory to another server but do not have FTP or SCP access? Well this little trick will help out using the netcat utility. On the destination server run:

nc -l -p 1234 | uncompress -c | tar xvfp -

And on the sending server run:

tar cfp - /some/dir | compress -c | nc -w 3 [destination] 1234

Now you can transfer directories without FTP and without needing root access.

Faster Hard drives

You may know that the hdparm tool can be used to speed test your disk and change a few settings. It can also be used to optimise drive performance, and turn on some features that may not be enabled by default. Before we start though, be warned that changing drive options can cause data corruption, so back up all your important data first. Testing speed is done with:

hdparm -Tt /dev/hda

You’ll see something like:

/dev/hda:
Timing buffer-cache reads:   128 MB in  1.64 seconds =78.05 MB/sec
Timing buffered disk reads:  64 MB in 18.56 seconds = 3.45MB/sec

Now we can try speeding it up. To find out which options your drive is currently set to use, just pass hdparm the device name:

hdparm /dev/hda
 /dev/hda:
 multcount    =  16 (on)
 I/O support  =  0 (default 16-bit)
 unmaskirq    =  0 (off)
 using_dma    =  0 (off)
 keepsettings =  0 (off)
 readonly     =  0 (off)
 readahead    =  8 (on)
 geometry     = 40395/16/63, sectors = 40718160, start = 0

This is a fairly default setting. Most distros will opt for safe options that will work with most hardware. To get more speed, you may want to enable dma mode, and certainly adjust I/O support. Most modern computers support mode 3, which is a 32-bit transfer mode that can nearly double throughput. You might want to try

hdparm -c3 -d1/dev/hda

Then rerun the speed check to see the difference. Check out the modes your hardware will support, and the hdparm man pages for how to set them.

Save battery power

You are probably familiar with using hdparm for tuning a hard drive, but it can also save battery life on your laptop, or make life quieter for you by spinning down drives.

hdparm -y /dev/hdb
hdparm -Y /dev/hdb
hdparm -S 36 /dev/hdb

In order, these commands will: cause the drive to switch to Standby mode, switch to Sleep mode, and finally set the Automatic spindown timeout. This last includes a numeric variable, whose units are blocks of 5 seconds (for example, a value of 12 would equal one minute).

Finding the biggest files

A common problem with computers is when you have a number of large files (such as audio/video clips) that you may want to get rid of. You can find the biggest files in the current directory with:

ls -lSrh

The «r» causes the large files to be listed at the end and the «h» gives human readable output (MB and such). You could also search for the biggest MP3/MPEGs:

ls -lSrh *.mp*

You can also look for the largest directories with:

du -kx | egrep -v "./.+/" | sort -n