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

Get out from Recovery Mode Loop on iPhone

Download Here the following files for Mac and Windows users (iRecovery for Windows & Mac and LibUsb-Win32). Windows users should also download this version of  iRecovery from here.

Follow these steps:

1. Install libusb (Windows users only). Windows Vista users follow the extra steps below to install Libusb-Win32.

  • Right click on the file. Select Properties | Compatibility Click the “Run this program in compatibility mode” box and select  “Windows XP (Service Pack2)” from the drop down list.
  • Right click again and select “Run as “Administrator”. Follow the installation instructions. Running the test program list the usb devices plugged into my computer – but not my new device.

2) Start iRecovery in Terminal / Console by typing: irecovery -s

3) Execute the following commands in the provided shell:

	setenv auto-boot true
	saveenv
	/exit

4) Reboot your iPhone by holding home and sleep button for 10 seconds.

View hidden files and folders in Finder.

OSX Finder is hiding the standard Unix files and folders. You can «Go To Folder…» and type in the name of the «known» folder such as /var/log, but again you will not be able to see the «dot» files. Of course you can use terminal for viewing those files… but come one it is MacOSX!!!

Type at Terminal: defaults write com.apple.finder AppleShowAllFiles -bool true

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

Replacing same text in multiple files

If you have text you want to replace in multiple locations, there are several ways to do this. To replace the text Windows with Linux in all files in current directory called test[something] you can run this:

perl -i -pe 's/Windows/Linux/;' test*

To replace the text Windows with Linux in all text files in current directory and down you can run this:

find . -name '*.txt' -print | xargs perl -pi -e's/Windows/Linux/ig' *.txt

Or if you prefer this will also work, but only on regular files:

find -type f -name '*.txt' -print0 | xargs --null perl -pi -e 's/Windows/Linux/'

Recover root password

Step 1: For Redhat based distros:

Boot from your first install CD and as the very first screen comes up hit F2 and type:

rescue ( For RedHat «linux rescue» ) and the computer will boot in rescue mode.

It will show a few alternatives, select : ¨mount the existing partitions¨ and go to the shell/console prompt.

( Booting Mandrake in «failsafe» from the Lilo menu does the same )

Step 1: For SUSE:

Boot from your first install CD and press F1 at the first screen, then choose «Rescue System» from the menu and at the prompt type «root» ( you do not need a password )

Step 1: For other distro’s

Boot from the first install CD ( or any Live CD like Knoppix ) and at the bootprompt type:

linux single

(For Knoppix: knoppix single )

Alternative for step 1

Sure with most Live and Rescue CDs you can also just boot in the live version, mount the partition and, as root, make changes to the needed files

Step 2

# cd /etc

( if you boot from knoppix first cd to the partition your lost-password-distro is on )

We need to change two files; ¨passwd¨ and ¨shadow¨:

# vi passwd

< i > ( puts vi in insertmode )

This is the first line:

root:x:0:0:root:/root:/bin/bash

Make it:

root::0:0:root:/root:/bin/bash

So just get rid of the ¨x¨ and leave the ¨:¨ ( colons )

Save the file:

Press Esc,

write –> :x,

Press enter

Second file to be changed:

# vi shadow

< i > ( insert mode )

The first line is a long scrambled line of characters, just make it:

root:::: ( four : )

Press Esc

Write –> 😡

Press enter

Now you can reboot your computer. Log in as normal user, open a console and type:

$ su

# passwd

Now set the new root password !

Log out as root:

Ctrl+d

How to mount ext2fs from FreeBSD

To mount ext2fs filesystems under FreeBSD, you first have to build a new kernel with ext2fs support. Put the line

options “EXT2FS”

in your kernel configuration file for the new kernel and compile.
Read the FreeBSD handbook to learn how to do that.

or

Do the following steps to enable ext2fs support in the kernel:

# cd /usr/src/sys/modules/ext2fs
# make
# make install

You can use ‘kldload‘ to load the ext2fs module in to the kernel.

# kldload ext2fs
Then you will be able to mount your linux partitions by giving a command like:

# mount -t ext2fs /dev/ad1s1 /mnt

to unload module use

# kldunload ext2fs

To load the module automatically on system startup

add the following line in to /boot/loader.conf

ext2fs_load=”YES”