Linux Commands - Overview and Examples
The command line is one of the most powerful features of Linux. There exists a sea of Linux command line tools, allowing you to do almost everything you can think of doing on your Linux PC. However, this usually creates a problem: with so many commands available to use, you don't know where and how to start learning them, especially when you are beginner.
Author
本文的特点是非常简洁,将繁杂的Linux命令行筛选出100条左右,非常适合入门学习。
此外,将领域知识以“条目+示例”的方式来整理,类似编字典一样,在编辑的过程中可以促进学习者加深认识,也方便日后持续改进(增加注解、参考文献、索引等),是一种不错的学习方法。
最后,整理这些命令行的时候,我体会到操作系统最重要的工作实际就是对文件的管理,创建、移动、查看、编辑、销毁、检索,都是围绕文件的操作,事实上也是实际工作中使用最频繁的需求。对开发者来说,以Linux命令行为模版,命名风格、人机交互、小而美的实现方式,促进自己在其它领域的应用、提高大有裨益。
Adduser/Addgroup
The adduser and addgroup commands lets you add a new user and group to a system, respectively. Here's an example for adduser:
$ sudo adduser testuser
Adding user `testuser' ...
Adding new group `testuser' (1003) ...
Adding new user `testuser' (1003) with group `testuser' ...
Creating home directory `/home/testuser' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Arch
The arch command is used to print the machine's architecture. For example:
$ arch
i686
Not sure what 'i686' means? Head here.
Cal/Ncal
The cal and ncal commands display a calendar in the output.
$ cal
March 2017
Su Mo Tu We Th Fr Sa
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
$ ncal
March 2017
Su 5 12 19 26
Mo 6 13 20 27
Tu 7 14 21 28
We 1 8 15 22 29
Th 2 9 16 23 30
Fr 3 10 17 24 31
Sa 4 11 18 25
Cat
$ cat test.txt
Hello...how are you?
Cd
$ cd /home/himanshu/
Chgrp
$ chgrp howtoforge test.txt
Chmod
chmod +x helloWorld
Chown
chown root:root test.txt
Cksum
$ cksum test.txt
3741370333 20 test.txt
Not sure what checksum is? Head here.
Clear
$ clear
Cmp
$ cmp file1 file2
file1 file2 differ: byte 1, line 1
Comm
$ comm file1 file2
支持选项:
-1:不显示在第一个文件出现的内容;
-2:不显示在第二个文件中出现的内容;
-3:不显示同时在两个文件中都出现的内容。
Cp
$ cp test.txt /home//himanshu/Desktop/
Csplit
$ csplit file1 [n]
The two parts are saved as files with names 'xx00' and 'xx01', respectively.
Date
$ date
Tue Feb 28 17:14:57 IST 2017
Dd
dd if=/dev/sda of=/tmp/dev-sda-part.img
Df
$ df /dev/sda1
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 74985616 48138832 23014620 68% /
Diff
$ diff file1 file2
Diff3
diff3 file1 file2 file3
Dir
$ dir
test1 test2 test.7z test.zip
Dirname
$ dirname /home/himanshu/file1
/home/himanshu
Dmidecode
The dmidecode command prints a system's DMI (aka SMBIOS) table contents in a human-readable format.
$ sudo dmidecode
# dmidecode 2.12
SMBIOS 2.6 present.
50 structures occupying 2056 bytes.
Table at 0x000FCCA0.
Handle 0x0000, DMI type 0, 24 bytes
BIOS Information
Vendor: American Megatrends Inc.
Version: 080015
Release Date: 08/22/2011
...
...
...
DMI (Desktop Management Interface, DMI)就是帮助收集电脑系统信息的管理系统,DMI信息的收集必须在严格遵照SMBIOS规范的前提下进行。 SMBIOS(System Management BIOS)是主板或系统制造者以标准格式显示产品管理信息所需遵循的统一规范。SMBIOS和DMI是由行业指导机构Desktop Management Task Force (DMTF)起草的开放性的技术标准,其中DMI设计适用于任何的平台和操作系统。
Du
$ du /home/himanshu/Desktop/
92 /home/himanshu/Desktop/Downloads/meld/meld/ui
88 /home/himanshu/Desktop/Downloads/meld/meld/vc
56 /home/himanshu/Desktop/Downloads/meld/meld/matchers
12 /home/himanshu/Desktop/Downloads/meld/meld/__pycache__
688 /home/himanshu/Desktop/Downloads/meld/meld
16 /home/himanshu/Desktop/Downloads/meld/bin
328 /home/himanshu/Desktop/Downloads/meld/data/ui
52 /home/himanshu/Desktop/Downloads/meld/data/icons/svg
Echo
The echo command displays whatever input text is given to it.
$ echo hello hi
hello hi
Ed
$ ed
单行纯文本编辑器,它有命令模式(command mode)和输入模式(input mode)两种工作模式。
支持选项:
A:切换到输入模式,在文件的最后一行之后输入新的内容;
C:切换到输入模式,用输入的内容替换掉最后一行的内容;
i:切换到输入模式,在当前行之前加入一个新的空行来输入内容;
d:用于删除最后一行文本内容;
n:用于显示最后一行的行号和内容;
w:<文件名>:一给定的文件名保存当前正在编辑的文件;
q:退出ed编辑器。
Eject
$ eject
Env
$ env
Exit
$ exit
Expand
$ expand file1
Expr
$ expr 1 + 2
3
Factor
$ factor 135
135: 3 3 3 5
Fgrep
The fgrep command is equivalent to the grep command when executed with the -F command line option. The tool is also known as fixed or fast grep as it doesn't treat regular expression metacharacters as special, processing the information as simple string instead.
For example, if you want to search for dot (.) in a file, and don't want grep to interpret it as a wildcard character, use fgrep in the following way:
$ fgrep "." [file-name]
Find
$ find test*
test
test1
test2
test.7z
test.c
test.txt
More examples for the Linux Find command:
* 14 Practical Examples of Linux Find Command for Beginners
* Searching For Files And Folders With The find Command
* Finding Files On The Command Line
Fmt
$ fmt file1
Fold
The fold command wraps each input line to fit in specified width.
$ fold -w 10
Hi my name is himanshu Arora
Hi my name
is himans
hu Arora
Free
$ free
total used free shared buffers cached
Mem: 1800032 1355288 444744 79440 9068 216236
-/+ buffers/cache: 1129984 670048
Swap: 1832956 995076 837880
Grep
$ grep Hello test.txt
Hello...how are you?
More tutorials and examples for the Linux Grep command:
* How to use grep to search for strings in files on the shell
* How to perform pattern search in files using Grep
Groups
$ groups himanshu
himanshu : himanshu adm cdrom sudo dip plugdev lpadmin sambashare
Gzip
$ gzip file1
Gunzip
$ gunzip file1.gz
Head
$ head CHANGELOG.txt
BEEBEEP (Secure Lan Messanger)
BeeBEEP
2.0.4
- Some GUI improvements (new icons, file sharing tree load faster)
- Always Beep on new message arrived (option)
- Favorite users (right click on user and enable star button) is on top of the list
- improved group usability
- Offline users can be removed from list (right click on an offline user in list and then remove)
- Clear all files shared (option)
- Load minimized at startup (option)
Hostname
$ hostname
himanshu-desktop
Id
$ id himanshu
uid=1000(himanshu) gid=1000(himanshu) groups=1000(himanshu),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),108(lpadmin),124(sambashare)
Kill
$ kill [process-id]
Killall
$ killall nautilus
Last
$ last
himanshu pts/11 :0 Thu Mar 2 09:46 still logged in
himanshu pts/1 :0 Thu Mar 2 09:46 still logged in
himanshu :0 :0 Thu Mar 2 09:42 still logged in
reboot system boot 4.4.0-62-generic Thu Mar 2 09:41 - 10:36 (00:54)
himanshu pts/14 :0 Wed Mar 1 15:17 - 15:52 (00:35)
himanshu pts/13 :0 Wed Mar 1 14:40 - down (08:06)
Ldd
$ ldd /lib/i386-linux-gnu/libcrypt-2.19.so
linux-gate.so.1 => (0xb77df000)
libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb75da000)
/lib/ld-linux.so.2 (0x80088000)
Ln
$ ln test.txt lnk
Locate
$ locate [file-name]
Logname
$ logname
himanshu
Ls
$ ls progress
capture.png hlist.o progress progress.h sizes.c
hlist.c LICENSE progress.1 progress.o sizes.h
hlist.h Makefile progress.c README.md sizes.o
Lshw
$ sudo lshw
[sudo] password for himanshu:
himanshu-desktop
description: Desktop Computer
product: To Be Filled By O.E.M. (To Be Filled By O.E.M.)
vendor: To Be Filled By O.E.M.
version: To Be Filled By O.E.M.
serial: To Be Filled By O.E.M.
width: 32 bits
capabilities: smbios-2.6 dmi-2.6 smp-1.4 smp
...
...
..
Lscpu
$ lscpu
Architecture: i686
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 1
On-line CPU(s) list: 0
Thread(s) per core: 1
Core(s) per socket: 1
Socket(s): 1
Vendor ID: AuthenticAMD
CPU family: 16
Model: 6
Stepping: 3
CPU MHz: 2800.234
BogoMIPS: 5600.46
Virtualization: AMD-V
L1d cache: 64K
L1i cache: 64K
L2 cache: 1024K
Man
$ man ls
Md5sum
$ md5sum test.txt
ac34b1f34803a6691ff8b732bb97fbba test.txt
Mkdir
$ mkdir [dir-name]
Mkfifo
$ mkfifo [pipe-name]
More
$ cat [large-file] | more
Mv
$ mv test.txt /home/himanshu/Desktop/
Nice
$ nice -n[niceness-value] [program]
$ nice -n15 vim
Nl
$ nl file1
1 Hi
2 How are you
3 Bye
Nm
$ nm test
0804a020 B __bss_start
0804841d T compare
0804a020 b completed.6591
0804a018 D __data_start
0804a018 W data_start
08048360 t deregister_tm_clones
080483d0 t __do_global_dtors_aux
08049f0c t __do_global_dtors_aux_fini_array_entry
0804a01c D __dso_handle
08049f14 d _DYNAMIC
0804a020 D _edata
0804a024 B _end
080484e4 T _fini
080484f8 R _fp_hw
080483f0 t frame_dummy
...
...
...
Nproc
$ nproc
1
Od
$ od /bin/ls
0000000 042577 043114 000401 000001 000000 000000 000000 000000
0000020 000002 000003 000001 000000 140101 004004 000064 000000
0000040 122104 000001 000000 000000 000064 000040 000011 000050
0000060 000034 000033 000006 000000 000064 000000 100064 004004
0000100 100064 004004 000440 000000 000440 000000 000005 000000
0000120 000004 000000 000003 000000 000524 000000 100524 004004
...
...
...
Passwd
$ passwd himanshu
Changing password for himanshu.
(current) UNIX password:
Paste
$ cat file1
Hi
My name is
Himanshu
Arora
I
Am
a
Linux researcher
and tutorial
writer
Then the following 'paste' command will join all the lines of the file:
$ paste -s file1
Hi My name is Himanshu Arora I Am a Linux researcher and tutorial writer
Pidof
$ pidof nautilus
2714
Ping
$ ping
PING (104.24.0.68) 56(84) bytes of data.
64 bytes from 104.24.0.68: icmp_seq=1 ttl=58 time=47.3 ms
64 bytes from 104.24.0.68: icmp_seq=2 ttl=58 time=51.9 ms
64 bytes from 104.24.0.68: icmp_seq=3 ttl=58 time=57.4 ms
Ps
$ ps
PID TTY TIME CMD
4537 pts/1 00:00:00 bash
20592 pts/1 00:00:00 ps
Pstree
$ pstree
init???ModemManager???2*[{ModemManager}]
??NetworkManager???dhclient
? ??dnsmasq
? ??3*[{NetworkManager}]
??accounts-daemon???2*[{accounts-daemon}]
??acpid
??atop
Pwd
The pwd command displays the name of current/working directory.
$ pwd
/home/himanshu
Rm
$ rm [file-name]
Rmdir
$ rmdir [dir-name]
Scp
$ scp [name-and-path-of-file-to-transfer] [user]@[host]:[dest-path]
Sdiff
$ sdiff file1 file2
Sed
$ echo "Welcome to Howtoforge" | sed -e 's/Howtoforge/HowtoForge/g'
Welcome to HowtoForge
Seq
$ seq 1 2 10
1
3
5
7
9
Sha1sum
$ sha1sum test.txt
955e48dfc9256866b3e5138fcea5ea0406105e68 test.txt
Shutdown
The shutdown command lets user shut the system in a safe way.
$ shutdown
Size
$ size test
text data bss dec hex filename
1204 280 4 1488 5d0 test
Sleep
The sleep command lets user specify delay for a specified amount of time. You can use it to delay an operation like:
$ sleep 10; shutdown
Sort
$ cat file2
zeus
kyan
sam
adam
Then running the sort command produces the following output:
$ sort file2
adam
kyan
sam
zeus
Split
$ split [file-name]
Ssh
ssh is basically OpenSSH SSH client. It provides secure encrypted communication between two untrusted hosts over an insecure network.
$ ssh [user-name]@[remote-server]
Stat
$ stat test.txt
File: ‘test.txt’
Size: 20 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 284762 Links: 2
Access: (0664/-rw-rw-r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2017-03-03 12:41:27.791206947 +0530
Modify: 2017-02-28 16:05:15.952472926 +0530
Change: 2017-03-02 11:10:00.028548636 +0530
Birth: -
Strings
$ strings test
/lib/ld-linux.so.2
libc.so.6
_IO_stdin_used
puts
__libc_start_main
__gmon_start__
GLIBC_2.0
PTRh
QVhI
[^_]
EQUAL
;*2$"
GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
....
....
....
Su
$ su [user-name]
Sudo
$ sudo [command]
Sum
$ sum readme.txt
45252 5
Tac
$ cat file2
zeus
kyan
sam
adam
$ tac file2
adam
sam
kyan
zeus
Tail
$ tail [file-name]
Talk
$ talk [user-name]
Tar
$ tar -cf archive.tar foo bar
More...
Tee
$ uname | tee file2
Linux
$ cat file2
Linux
Test
$ test 7 -gt 5 && echo "true"
true
Time
$ time ping
PING (216.58.220.206) 56(84) bytes of data.
64 bytes from (216.58.220.206): icmp_seq=1 ttl=52 time=44.2 ms
^C
--- ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 44.288/44.288/44.288/0.000 ms
real 0m0.676s
user 0m0.000s
sys 0m0.000s
Top
$ top
Touch
$ touch [file-name]
Tr
$ echo 'howtoforge' | tr "[:lower:]" "[:upper:]"
HOWTOFORGE
Tty
$ tty
/dev/pts/10
Uname
$ uname -a
Linux himanshu-desktop 4.4.0-62-generic #83~14.04.1-Ubuntu SMP Wed Jan 18 18:10:26 UTC 2017 i686 athlon i686 GNU/Linux
Uniq
$ cat file2
Welcome to HowtoForge
Welcome to HowtoForge
A Linux tutorial website
Thanks
Then you can use the uniq command to omit the repeated line.
$ uniq file2
Welcome to HowtoForge
A Linux tutorial website
Thanks
Unexpand
$ unexpand file1
Uptime
$ uptime
15:59:59 up 6:20, 4 users, load average: 0.81, 0.92, 0.82
Users
$ users
himanshu himanshu himanshu himanshu
Vdir
$ vdir
total 1088
-rw-rw-r-- 1 himanshu himanshu 4850 May 20 2015 test_backup.pdf
-rw-rw-r-- 1 himanshu himanshu 2082 May 28 2015 test-filled.pdf
-rw-rw-r-- 1 himanshu himanshu 7101 May 28 2015 test.pdf
Vim
$ vim [file-name]
W
$ w
16:18:07 up 6:39, 4 users, load average: 0.07, 0.32, 0.53
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
himanshu :0 :0 09:39 ?xdm? 1:08m 0.25s init --user
himanshu pts/0 :0 09:41 6:36m 0.84s 7.84s gnome-terminal
himanshu pts/10 :0 14:51 0.00s 0.16s 0.00s w
himanshu pts/11 :0 15:41 35:19 0.05s 0.05s bash
Wall
$ wall [your-message]
Watch
$ watch date
Wc
$ wc test.txt
0 3 20 test.txt
Whatis
$ whatis mkdir
mkdir (1) - make directories
mkdir (2) - create a directory
mkdir (1posix) - make directories
Which
$ which date
/bin/date
Who
$ who
himanshu :0 2017-03-03 09:39 (:0)
himanshu pts/0 2017-03-03 09:41 (:0)
himanshu pts/10 2017-03-03 14:51 (:0)
himanshu pts/11 2017-03-03 15:41 (:0)
Whereis
$ whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1posix.gz /usr/share/man/man1/ls.1.gz
Whoami
$ whoami
himanshu
Xargs
$ xargs grep "Linux"
file1
file2
file3
file1:Linux researcher
file2:A Linux tutorial website
file3:Linux is opensource
More...
Yes
$ yes [string]