Useful Linux Commands

Linux Useful Commands

1
du -a | cut -d/ -f2 | sort | uniq -c | sort -nr
1
find . -type f | sed 's%/[^/]*$%%' | sort | uniq -c

1. tree

show directories

1
tree

show trees with friendly size show. print the size of each file but in a more human readable way, e.g. appending a size letter for kilobytes (K), megabytes (M), gigabytes (G), terabytes (T), petabytes (P) and exabytes (E).

1
$ tree -h

only show directory

1
$ tree -d

show folder by specify level

1
$ tree -L 1 (only show ./ files)

2. tail

show file tail contents Dynamically

1
$ sudo tail -f file.txt

3. w

Show who is logged on and what they are doing.

1
$ w

4. last

show listing of last logged in users

1
$ last

search for files in a directory hierarchy, find files end with swp :

5. find

find files in directories

1
$ find ./ -name '*.swp'

6. grep

find strings in file

1
$ grep -n abc -r ./

7. df

show system disk space usage

1
$ df -h

8. du

show directory space usage, recursively for directories

1
2
du -hd 1
du -sh

9. compress Commands

bzip2

1
2
3
$ bzip github.png  // --> github.png.gz   -v => show compress rate
$ bunzip gihub.png.gz // --> github.png
$ bzip -d github.png.gz // --> github.png

gzip

1
2
3
$ gzip github.png  // --> github.png.gz   -v => show compress rate
$ gunzip gihub.png.gz // --> github.png
$ gzip -d github.png.gz // --> github.png

zip

1
2
$ zip github.zip github.png // --> github.zip
$ unzip github.zip

  • compress rates: bzip2 > gzip > zip

10. show ubuntu version

1
2
3
4
5
6
7
8
$ lsb_release -a
------------ Result ---------------
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.1 LTS
Release: 16.04
Codename: xenial
-----------------------------------

11. wget & install deb file

1
2
3
4
5
# wget
wget url

# install from deb file
sudo dpkg -i atom-amd64.deb

12. watch

execute a program periodically, showing output fullscreen.

1
2
3
4
5
6
7
8
# every 10 sec execute backup.sh one time
watch -n 10 sh backup.sh

# when file outputput changed, exit watch
watch -n 10 -g sh backup.sh

# execute command directoly in watch command
watch -n 10 -x echo hello word!

13. which / type

show Commands path

1
2
3
4
5
6
$ which java
/home/oo/Library/jdk1.8.0_111/bin/java
$ type java
java is /home/oo/Library/jdk1.8.0_111/bin/java
$ type cd
cd is buildin

14. whereis / locate

search files use linux db, faster than find command

1
2
3
4
5
$ whereis file_name
$ locate file_name

$ rm -i `locate *.swp`
$ rm -i $(locate *.swp)

15. ps

1
2
3
4
5
6
7
8
$ ps -l
show olny user self's process

$ ps aux
show all process in memory

$ps aux | egrep '(java)'
find process use java

16. top

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
$ top

? :显示在 top 当中可以输入的按键命令
P :以 CPU 的使用资源排序显示
M(upper M) :以 Memory 的使用资源排序显示
m(lower m) :change memory show style
N :以 PID 来排序
T(upper t):由该 Process 使用的 CPU 时间累积 (TIME+) 排序
t(lower t) :change cpu show style
k :给予某个 PID 一个讯号 (signal)
r :给予某个 PID 重新制订一个 nice 值
q :离开 top 软件的按键
z :change color
Z :set & change color
d :delay time
1 :change cpu show style(line 3)
c :toggle CMD Name/CMD Line
i :Idle-process toggle. When this toggle is Off, tasks that have not used any CPU since the last update will not be displayed.

$ top -d 1
every 1s, update top results

$ top -b -n 2 > /tmp/top.txt
run 2 times top and write results into top.txt file.

$ top -ci
show only process that using cpu with command line.

$ top -p 3214
show process by PID.

top cpus:
us, user: time running un-niced user processes
sy, system : time running kernel processes
ni, nice : time running niced user processes
id, idle : time spent in the kernel idle handler
wa, IO-wait : time waiting for I/O completion
hi : time spent servicing hardware interrupts
si : time spent servicing software interrupts
st : time stolen from this vm by the hypervisor

17. kill / killall

send a signal to a process

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ kill -9 PID
stop process by PID.
9 is signal.
use kill -l show all avilable signal.
1: SIGHUP: 启动被终止的程序,可让该 PID 重新读取自己的配置档,类似重新启动
9: SIGKILL: 代表强制中断一个程序的进行,如果该程序进行到一半, 那么尚未完成的部分可能会有『半产品』产生,类似 vim会有 .filename.swp 保留下来。
15: SINGTERM: 以正常的结束程序来终止该程序。由於是正常的终止, 所以后续的动作会将他完成。不过,如果该程序已经发生问题,就是无法使用正常的方法终止时, 输入这个 signal 也是没有用的。

$ killall -i java
killall [-iIe] [command name]
-i :interactive 的意思,互动式的,若需要删除时,会出现提示字节给使用者;
-e :exact 的意思,表示『后面接的 command name 要一致』,但整个完整的命令,不能超过 15 个字节。
-I :命令名称(可能含参数)忽略大小写。
kill by command name.

18. nice

priority & nice: PRI(new) = PRI(old) + nice
run a program with modified scheduling priority

1
$ nice -n -5 vi test.txt

19. renice

alter priority of running processes

1
2
$ renice 10 18625
renice [number] PID

20. free

Display amount of free and used memory in the system

1
2
3
4
5
6
7
8
$ free -h -t 
free [-b|-k|-m|-g] [-t]
h: humanity
b: show in bytes(default)
k: show in kb
m: show in mb
g: show in Gb
t: show total

21. uname

print system information

1
2
3
4
5
6
7
uname -a
-a :所有系统相关的资讯,包括底下的数据都会被列出来;
-s :系统核心名称
-r :核心的版本
-m :本系统的硬件名称,例如 i686 或 x86_64 等;
-p :CPU 的类型,与 -m 类似,只是显示的是 CPU 的类型!
-i :硬件的平台 (ix86)

22. uptime

Tell how long the system has been running.

1
$ uptime

23. netstat

Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships.

1
2
3
4
5
6
7
8
9
10
$ netstat -t
$ netstat -tnp
$ netstat -tlnp
-l :列出目前正在网络监听 (listen) 的服务: useful
-t :列出 tcp 网络封包的数据: useful
-u :列出 udp 网络封包的数据: useful

-a :将目前系统上所有的连线、监听、Socket 数据都列出来
-n :不以程序的服务名称,以埠号 (port number) 来显示
-p :列出该网络服务的程序 PID

24. dmesg

print or control the kernel ring buffer
分析核心产生的信息

1
2
$ dmesg | more
$ dmesg | grep -i hd

25. vmstat

Report virtual memory statistics
侦测系统资源变化

1
2
3
4
5
6
7
8
9
范例一:统计目前主机 CPU 状态,每秒一次,共计三次!
[root@www ~]# vmstat 1 3

-a :使用 inactive/active(活跃与否) 取代 buffer/cache 的内存输出资讯;
-f :启动到目前为止,系统复制 (fork) 的程序数;
-s :将一些事件 (启动至目前为止) 导致的内存变化情况列表说明;
-S :后面可以接单位,让显示的数据有单位。例如 K/M 取代 bytes 的容量;
-d :列出磁碟的读写总量统计表
-p :后面列出分割槽,可显示该分割槽的读写总量统计表

26. /proc folder

所谓的程序都是在内存当中!而内存当中的数据又都是写入到 /proc/ 这个目录下的.
基本上,目前主机上面的各个程序的 PID 都是以目录的型态存在於 /proc/ 当中。

  • cmdline:这个程序被启动的命令串;
  • environ:这个程序的环境变量内容。
档名 文件内容
/proc/cmdline 加载 kernel 时所下达的相关参数!查阅此文件,可了解系统是如何启动的!
/proc/cpuinfo 本机的 CPU 的相关资讯,包含时脉、类型与运算功能等
/proc/devices 这个文件记录了系统各个主要装置的主要装置代号,与 mknod 有关呢!
/proc/filesystems 目前系统已经加载的文件系统罗!
/proc/interrupts 目前系统上面的 IRQ 分配状态。
/proc/ioports 目前系统上面各个装置所配置的 I/O 位址。
/proc/kcore 这个就是内存的大小啦!好大对吧!但是不要读他啦!
/proc/loadavg 还记得 top 以及 uptime 吧?没错!上头的三个平均数值就是记录在此!
/proc/meminfo 使用 free 列出的内存资讯,嘿嘿!在这里也能够查阅到!
/proc/modules 目前我们的 Linux 已经加载的模块列表,也可以想成是驱动程序啦!
/proc/mounts 系统已经挂载的数据,就是用 mount 这个命令呼叫出来的数据啦!
/proc/swaps 到底系统挂加载的内存在哪里?呵呵!使用掉的 partition 就记录在此啦!
/proc/partitions 使用 fdisk -l 会出现目前所有的 partition 吧?在这个文件当中也有纪录喔!
/proc/pci 在 PCI 汇流排上面,每个装置的详细情况!可用 lspci 来查阅!
/proc/uptime 就是用 uptime 的时候,会出现的资讯啦!
/proc/version 核心的版本,就是用 uname -a 显示的内容啦!
/proc/bus/ 一些汇流排的装置,还有 U盘 的装置也记录在此喔!

27. fuser

藉由文件(或文件系统)找出正在使用该文件的程序
identify processes using files or sockets

1
2
$ fuser -uv ./
$ fuser [-umv] [-k [i] [-signal]] file/dir

选项与参数:

  • -u :除了程序的 PID 之外,同时列出该程序的拥有者;
  • -m :后面接的那个档名会主动的上提到该文件系统的最顶层,对 umount 不成功很有效!
  • -v :可以列出每个文件与程序还有命令的完整相关性!
  • -k :找出使用该文件/目录的 PID ,并试图以 SIGKILL 这个讯号给予该 PID;
  • -i :必须与 -k 配合,在删除 PID 之前会先询问使用者意愿!
  • -signal:例如 -1 -15 等等,若不加的话,默认是 SIGKILL (-9) 罗!

28. lsof

list open files
列出被程序所开启的文件档名

1
2
$ lsof +d ~/Desktop
$ lsof -u root | grep java

选项与参数:

  • -a :多项数据需要『同时成立』才显示出结果时!
  • -U :仅列出 Unix like 系统的 socket 文件类型;
  • -u :后面接 username,列出该使用者相关程序所开启的文件;
  • +d :后面接目录,亦即找出某个目录底下已经被开启的文件!

29. pidof

找出某支正在运行的程序的 PID

1
2
$ pidof java
$ pidof init java

[root@www ~]# pidof [-sx] program_name
选项与参数:

  • -s :仅列出一个 PID 而不列出所有的 PID
  • -x :同时列出该 program name 可能的 PPID 那个程序的 PID

30. show all wlan ip address

use arp-scan tool

1
2
3
4
5
6
7
8
# install arp-scan
sudo apt-get install arp-scan

# And to give further detail:
sudo arp-scan --interface=eth0 --localnet

# Where eth0 is your device. You can find your device with:
ifconfig

31. check linux net speed

1
2
3
4
5
$ dmesg | grep -i duplex

or

$ ethtool eth1

32. Count all the lines of code in a directory recursively

1
2
3
find . -name '*.scala' | xargs wc -l

find . -name '*.scala' | xargs wc -l | grep total

33. show linux ports usage

1
2
3
4
sudo lsof -i
sudo netstat -lptu
sudo netstat -tulpn
fuser 9000/tcp