Useful Linux Commands
Linux |
Disk space
Free disk space- df -k Mounted disk list
- df -h - shows disk free info
- du -sh - shows information about utilisation
- du -sk * | sort -n - what files/directories are using the most storage
What is taking up disk space?
- find / -type f -size +100000k -exec ls -lh {} \; | awk '{print $5 ": " $NF}'
Find Folder size, sorted.
- du * -sh | sort –nr
Remove specific files recursively
- rm `find . -name '*.log'` -rf
Finding files
Finding files by filename
Finding files by content
Finding files by filename
- find / -name mystuff
Finding files by content
- grep -H -r “the text” /home/tom
- find ./ |xargs grep -i StringToFind
- fuser -m -u /var/log/logfile.log
Networking
What is blocking the local port?- netstat -tlnp | grep 50101
- lsof | grep 50101
- List Open Files
- lsof List all files opened by any process
- lsof-p 1000 List files opened by a process with PID 1000
- sof -p ^1000 ist files opened by any other process than PID 1000
- lsof-u <username> List files opened by specific user-owned processes
- sof -u^root List files opened by all non-root users
- sof <directory> Show what processes are using <directory>
- lsof <filename> List all processes that have opened <filename>
- lsof+D /tmp List all files that have been opened under /tmp
- [sof +L1 List files deleted but still held open by running processes
- lsof-c <command> List open files by processes executing <command>
- lsof-d mem List all memory-mapped files
- List Network Connections and Sockets
- lsof-i List all open network connections
- lsof-i:80 List open TCP/UDP connections with port 80
- lsof-i tcp:1-1024 List open TCP connections with a given port range
- lsof-i-n List open network connections with no reverse DNS lookup
- lsof-i-n -P List open network connections with no port name conversion
- lsof-i6 List open IPv6 network connections (-i4 for IPv4)
- Other Usages
- lsof-U List open unix domain sockets
- sof /dev/tty1 List commands/processes associated with /dev/tty-
- kill -9 $(lsof-t -u <username>) Kill all activities of a specific user
- 192
- 34 shares
- fuser -v -n tcp 80
- netstat –i – list all the interfaces (including bonded interfaces)
- netstat –s – summary statistics - dropped packets, retransmissions
- netstat –l – show listening sockets - who’s got what port open
- netstat –t – show active connections - who’s connected
- netstat –r – show the routing table used by the kernel - what routes are currently loaded
- ss -no state established '( dport = :8999 or sport = :8999 )' - who is connected to/from port 8999
- ss -pmn (processes, memory, numeric – don’t resolve) (su to pricing) - show the process and memory info of each socket
- ifconfig [devicename] [reqd. action] [options] :
- ifconfig eth0 192.168.1.1
- whois google.com
- ping 192.168.1.1
- netstat -a
- traceroute 192.168.1.1
- route -n
- telnet -l abc 192.168.1.1
- ssh -X abc@192.168.1.1
- scp file.txt abc@192.168.1.1:~\
- wget http://www.google.co.za
iptables [options] : This command requires root permission. It enables to carry out packet filtering and Network Address Translation.
-F : Flush set of rules
-n : Show rules without carrying out DNS lookups
-A : Appends one or more rules to the end of the selected chain.
-j : Specify the target of the rule
-s : specify source
-L : Current rules are listed
For example : To block packets from certain IP Address 192.168.1.1, can be done by
- iptables -A INPUT -s 192.168.1.1 -j DROP
-i ether0 to select the interface you what to monitor
- tcpdump -i ethe0
How To Find My Public IP Address From Command Line On a Linux
- dig +short myip.opendns.com @resolver1.opendns.com
Processes
- top : The top program provides a dynamic real-time view of a running system.
- mpstat : writes to standard output activities for each available processor. Repeat every second : "mpstat 1"
- ps : Display all running processes (eg : to list java processes = "ps -ef | grep java" )
- pgrep : Looks through the currently running processes and lists the process IDs which matches the selection criteria to screen
- pstree : shows running processes as a tree
- netstat : displays network connections (both incoming and outgoing), routing tables, and a number of network interface (network interface controller or software-defined network interface) and network protocol statistics (the very useful "-p" option is not allowed in PROD unless you're root)
- htop : Show all CPU running process (For mutilator CPU)
Find threads/handles for all users
- ps -eLf
Total counts on threads/handles for all users
- ps -eLf | wc -l
kill
- Never use kill -9 if you can help it
- You can send any signal you like (use kill -l to list them) SIGUSR1 and SIGUSR2 are user defined. Each signal has a default action, e.g. to temporarily interrupt or to terminate the program
strace - system trace
- strace -p 25292 -f : trace pid 25292 in real time
- strace -p 25292 -f -c : trace pid 25292 and report summary statistics on exit
How to use VI editor.
INSERT Mode
For adding text to a file ( press Esc and then below command) . The three most common ways to enter the Insert Mode are:
Letter | Action |
---|---|
i | Starts inserting in front of the current cursor position |
I | Starts adding at the front of the line |
a | Starts adding after the cursor |
A | Starts adding at the end of the line |
o | Starts opening a new line underneath the cursor |
O | Starts opening a line above the cursor. |
<Esc> | Gets out of Insert Mode |
- See more at: http://www.tutorial.tecktricks.com/vi-command-in-unix-with-example/#sthash.oNG2pKkx.dpuf
EDIT Mode
Generally for moving the cursor and deleting stuff.
In the Edit Mode, the keys do not type letters, but do other actions such as cursor movement, deletions, copying lines, etc.
Letter | Simple Cursor Movement | |
---|---|---|
h | Moves cursor left one space | Note: the Arrow keys do work locally, but sometimes mess up over a network. |
j | Moves cursor down one line | |
k | Moves cursor up one line | |
l | Moves cursor right one space | |
Fast Cursor Movement | ||
w | Moves the cursor a full word at a time to the right | |
b | Moves the cursor back to the left a word at a time | |
^ | Moves the cursor to the front of a line | |
$ | Moves the cursor to the end of a line | |
<ctrl>f | Moves the cursor forward a full page of text at a time | |
<ctrl>b | Moves the cursor backward a full page of text at a time | |
Modifying Text | ||
x | Deletes the character under the cursor | |
dd | Deletes the line where the cursor is located (type d twice!) | |
n dd | Delete n consecutive lines ( n is an integer) | |
r | Replaces the character under the cursor with the next thing typed | |
J | Joins current line with the one below (Capital J!) | |
u | Undoes the last edit operation | |
<ctrl> r | Redo (Undoes the last undo operation) | |
Cut and Paste Operations | ||
yy | Copies or yanks a line ( 5yy yanks 5 lines) | |
p | Puts the yanked text on the line below the cursor (lower case p) | |
P | Puts the yanked text above the current line (capital P) | |
Note: If vi is already in the input mode, text from that or another window may be highlited using the left mouse button, and copied into place by pressing the middle mouse button. |
- See more at: http://www.tutorial.tecktricks.com/vi-command-in-unix-with-example/#sthash.oNG2pKkx.dpuf
COMMAND Mode
For interacting with the operating system.
To enter the Command Mode, a colon “ : ” must precede the actual command.
Letter | Action |
---|---|
: r <file> | reads a file from disk into the vi editor |
: w <file> | writes current file to disk |
: wq | writes the file and quits vi |
: q! | quits without writing (useful if you’ve messed up!) |