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

Αφήστε μια απάντηση

Η ηλ. διεύθυνσή σας δεν δημοσιεύεται. Τα υποχρεωτικά πεδία σημειώνονται με *