Linux基礎命令總結
[root@node0 ~]# uname # 不帶任何參數默認加入-s參數
Linux
[root@node0 ~]# uname -s
Linux
[root@node0 ~]# uname -a # 顯示所有信息
Linux node0 2.6.32-573.el6.x86_64 #1 SMP Thu Jul 23 15:44:03 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
[root@node0 ~]#
NO.2 hostname 查看、設置主機名
[root@node0 ~]# hostname
node0
No.3 ifconfig 查看、設置IP網絡參數
[root@node0 ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:00:85:3A
inet addr:192.168.10.2 Bcast:192.168.10.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe00:853a/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:138 errors:0 dropped:0 overruns:0 frame:0
TX packets:99 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:15432 (15.0 KiB) TX bytes:15685 (15.3 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:1232 errors:0 dropped:0 overruns:0 frame:0
TX packets:1232 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:80160 (78.2 KiB) TX bytes:80160 (78.2 KiB)
No.4 查看CPU、內存信息/proc/cpuinfo
[root@node0 ~]# cat /proc/cpuinfo # 查看CPU信息
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 69
model name : Intel(R) Core(TM) i3-4030U CPU @ 1.90GHz
stepping : 1
microcode : 29
cpu MHz : 1900.000
cache size : 3072 KB
physical id : 0
siblings : 1
core id : 0
cpu cores : 1
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc up arch_perfmon xtopology tsc_reliable nonstop_tsc unfair_spinlock pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm arat xsaveopt tpr_shadow vnmi ept vpid fsgsbase bmi1 avx2 smep bmi2 invpcid
bogomips : 3800.00
clflush size : 64
cache_alignment : 64
address sizes : 43 bits physical, 48 bits virtual
power management:
[root@node0 ~]# cat /proc/meminfo # 查看內存信息
MemTotal: 1004768 kB
MemFree: 808816 kB
Buffers: 10384 kB
Cached: 70572 kB
SwapCached: 0 kB
Active: 34820 kB
Inactive: 64228 kB
Active(anon): 18104 kB
Inactive(anon): 140 kB
Active(file): 16716 kB
Inactive(file): 64088 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 1023996 kB
SwapFree: 1023996 kB
Dirty: 0 kB
Writeback: 0 kB
AnonPages: 18156 kB
Mapped: 17684 kB
Shmem: 156 kB
Slab: 67424 kB
SReclaimable: 10772 kB
SUnreclaim: 56652 kB
KernelStack: 1760 kB
PageTables: 1544 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 1526380 kB
Committed_AS: 127792 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 155212 kB
VmallocChunk: 34359569876 kB
HardwareCorrupted: 0 kB
AnonHugePages: 2048 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
DirectMap4k: 6144 kB
DirectMap2M: 1042432 kB
DirectMap1G: 0 kB
No.5 pwd 查看當前目錄
[root@node0 ~]# pwd
/root
No.6 cd 切換目錄
[root@node0 ~]# cd ~
No.7 ls 列出文件
[root@node0 /]# ls # 僅僅顯示文件和目錄
bin boot cgroup dev dvd etc home lib lib64 lost+found media mnt opt proc root sbin selinux srv sys tmp usr var vmtools
[root@node0 /]# ls -Alh # -h選項提供易讀的大小顯示
total 102K
-rw-r--r--. 1 root root 0 Nov 18 23:28 .autofsck
dr-xr-xr-x. 2 root root 4.0K Nov 13 03:42 bin
dr-xr-xr-x. 5 root root 1.0K Nov 13 03:43 boot
drwxr-xr-x. 2 root root 4.0K Jul 24 2015 cgroup
drwxr-xr-x. 16 root root 3.6K Nov 18 23:28 dev
dr-xr-xr-x. 7 root root 4.0K Aug 5 2015 dvd
drwxr-xr-x. 93 root root 4.0K Nov 18 23:28 etc
drwxr-xr-x. 2 root root 4.0K Sep 23 2011 home
dr-xr-xr-x. 13 root root 4.0K Nov 13 03:42 lib
dr-xr-xr-x. 9 root root 12K Nov 13 03:42 lib64
drwx------. 2 root root 16K Nov 13 03:35 lost+found
drwxr-xr-x. 2 root root 4.0K Sep 23 2011 media
drwxr-xr-x. 3 root root 4.0K Nov 13 03:48 mnt
drwxr-xr-x. 3 root root 4.0K Nov 13 03:42 opt
dr-xr-xr-x. 112 root root 0 Nov 18 23:28 proc
dr-xr-x---. 2 root root 4.0K Nov 13 03:59 root
dr-xr-xr-x. 2 root root 12K Nov 13 03:48 sbin
drwxr-xr-x. 7 root root 0 Nov 18 23:28 selinux
drwxr-xr-x. 2 root root 4.0K Sep 23 2011 srv
drwxr-xr-x. 13 root root 0 Nov 18 23:28 sys
drwxrwxrwt. 5 root root 4.0K Nov 18 23:28 tmp
drwxr-xr-x. 13 root root 4.0K Nov 13 03:36 usr
drwxr-xr-x. 20 root root 4.0K Nov 13 03:42 var
drwxr-xr-x. 3 root root 4.0K Nov 13 03:47 vmtools
[root@node0 /]# ls -Al # -l使用長格式顯示
total 102
-rw-r--r--. 1 root root 0 Nov 18 23:28 .autofsck
dr-xr-xr-x. 2 root root 4096 Nov 13 03:42 bin
dr-xr-xr-x. 5 root root 1024 Nov 13 03:43 boot
drwxr-xr-x. 2 root root 4096 Jul 24 2015 cgroup
drwxr-xr-x. 16 root root 3600 Nov 18 23:28 dev
dr-xr-xr-x. 7 root root 4096 Aug 5 2015 dvd
drwxr-xr-x. 93 root root 4096 Nov 18 23:28 etc
drwxr-xr-x. 2 root root 4096 Sep 23 2011 home
dr-xr-xr-x. 13 root root 4096 Nov 13 03:42 lib
dr-xr-xr-x. 9 root root 12288 Nov 13 03:42 lib64
drwx------. 2 root root 16384 Nov 13 03:35 lost+found
drwxr-xr-x. 2 root root 4096 Sep 23 2011 media
drwxr-xr-x. 3 root root 4096 Nov 13 03:48 mnt
drwxr-xr-x. 3 root root 4096 Nov 13 03:42 opt
dr-xr-xr-x. 112 root root 0 Nov 18 23:28 proc
dr-xr-x---. 2 root root 4096 Nov 13 03:59 root
dr-xr-xr-x. 2 root root 12288 Nov 13 03:48 sbin
drwxr-xr-x. 7 root root 0 Nov 18 23:28 selinux
drwxr-xr-x. 2 root root 4096 Sep 23 2011 srv
drwxr-xr-x. 13 root root 0 Nov 18 23:28 sys
drwxrwxrwt. 5 root root 4096 Nov 18 23:28 tmp
drwxr-xr-x. 13 root root 4096 Nov 13 03:36 usr
drwxr-xr-x. 20 root root 4096 Nov 13 03:42 var
drwxr-xr-x. 3 root root 4096 Nov 13 03:47 vmtools
No.8 關機和重啟
關機:
shutdown -h now # 立刻關機
shutdown -h +15 “Hello” # 延時關機
poweroff
init 0
重啟:
shutdown -r now
init 6
reboot
No.9 type 檢查命令類型
[root@node0 /]# type ls cd bash sh
ls is aliased to `ls --color=auto'
cd is a shell builtin
bash is /bin/bash
sh is /bin/sh
No.10 快速編輯命令行
單擊<Tab>是自動補全,雙擊<Tab>是內容提示,使用/表示強制換行。
<Alt>+<.>輸出上一個命令
<Ctrl>+<U>清空至行首
<Ctrl>+<K>清空至行尾
<Ctrl>+<L>清空整個屏幕
<Ctrl>+<C>廢棄當前編輯
No.11 獲取幫助信息
(1)命令內部幫助:
help 命令
[root@node0 /]# help help
help: help [-dms] [pattern ...]
Display information about builtin commands.
Displays brief summaries of builtin commands. If PATTERN is
specified, gives detailed help on all commands matching PATTERN,
otherwise the list of help topics is printed.
Options:
-d output short description for each topic
-m display usage in pseudo-manpage format
-s output only a short usage synopsis for each topic matching
PATTERN
Arguments:
PATTERN Pattern specifiying a help topic
Exit Status:
Returns success unless PATTERN is not found or an invalid option is given.
(2)命令外部幫助:
使用--help選項.
[root@node0 /]# df --help
Usage: df [OPTION]... [FILE]...
Show information about the file system on which each FILE resides,
or all file systems by default.
Mandatory arguments to long options are mandatory for short options too.
-a, --all include dummy file systems
-B, --block-size=SIZE use SIZE-byte blocks
--direct show statistics for a file instead of mount point
--total produce a grand total
-h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G)
-H, --si likewise, but use powers of 1000 not 1024
-i, --inodes list inode information instead of block usage
-k like --block-size=1K
-l, --local limit listing to local file systems
--no-sync do not invoke sync before getting usage info (default)
-P, --portability use the POSIX output format
--sync invoke sync before getting usage info
-t, --type=TYPE limit listing to file systems of type TYPE
-T, --print-type print file system type
-x, --exclude-type=TYPE limit listing to file systems not of type TYPE
-v (ignored)
--help display this help and exit
--version output version information and exit
Display values are in units of the first available SIZE from --block-size,
and the DF_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables.
Otherwise, units default to 1024 bytes (or 512 if POSIXLY_CORRECT is set).
SIZE may be (or may be an integer optionally followed by) one of following:
KB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.
Report df bugs to [email protected]
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils 'df invocation'
(3)使用man手冊獲取幫助:
1 用戶命令
8 系統調用
5 文件格式和約定
************************************************************************************************
du命令顯示的是實際上分配給單個文件的塊,df命令顯示的是整個文件系統中分配的塊,包括 inode 以及其他的 metadata。
************************************************************************************************
No.12 touch cp mv rm創建、復制、移動、重命名、刪除文件和目錄
[root@node0 ~]# touch test # 創建文件
[root@node0 ~]# ll
total 0
-rw-r--r--. 1 root root 0 Nov 19 00:29 test
[root@node0 ~]# mkdir mydir # 創建目錄,使用-p參數遞歸創建目錄
[root@node0 ~]# mv test mydir/ #移動文件
[root@node0 ~]# ll
total 4
drwxr-xr-x. 2 root root 4096 Nov 19 00:29 mydir
[root@node0 ~]# ll mydir/
total 0
-rw-r--r--. 1 root root 0 Nov 19 00:29 test
[root@node0 ~]# mv mydir/test mydir/testNew # 重命名
[root@node0 ~]# ll mydir/
total 0
-rw-r--r--. 1 root root 0 Nov 19 00:29 testNew
[root@node0 ~]# cp mydir/ ../ # 復制文件使用-r參數
cp: omitting directory `mydir/'
[root@node0 ~]# cp -r mydir/ ../ #使用-r參數復制目錄
[root@node0 ~]# ll /m
media/ mnt/ mydir/
[root@node0 ~]# ll /mydir/
total 0
-rw-r--r--. 1 root root 0 Nov 19 00:30 testNew
[root@node0 ~]# mv mydir/ mydirs
[root@node0 ~]# ll mydirs/
total 0
-rw-r--r--. 1 root root 0 Nov 19 00:29 testNew
[root@node0 ~]# rm -rf /mydir/ # 強制刪除
************************************************************************************************
在命令前邊插入\,可以取消命令別名的定義
************************************************************************************************
No.13 cat more less head tail查看文件內容
[root@node0 ~]# cat /etc/issue # -n參數顯示行號
CentOS release 6.7 (Final)
Kernel \r on an \m
[root@node0 ~]# more /etc/issue
CentOS release 6.7 (Final)
Kernel \r on an \m
[root@node0 ~]# less /etc/issue
CentOS release 6.7 (Final)
Kernel \r on an \m
/etc/issue (END) #按鍵盤<Q>鍵退出查看器
head是從頭部查看,不使用-n參數指定行數默認顯示10行,tail是從尾部查看,不使用-n參數指定行數默認顯示10行,tail使用-f參數可以動態顯示文件信息,有利於實時查看日誌變化。
No.14 wc 統計文本
[root@node0 ~]# wc -l /var/log/messages # 計算文本的總行數
7287 /var/log/messages
[root@node0 ~]# wc -w /var/log/messages # 計算單詞的個數
86235 /var/log/messages
[root@node0 ~]# wc -c /var/log/messages # 計算文本的字節數
576190 /var/log/messages
[root@node0 ~]# wc -m /var/log/messages # 計算文本的字符個數
576190 /var/log/messages
[root@node0 ~]# wc /var/log/messages # 不加任何參數,默認是-lwc參數
7287 86235 576190 /var/log/messages
[root@node0 ~]# ls /etc/*.conf | wc -l # 統計/etc/目錄下以.conf結尾文件的個數
39
No.15 grep egrep 文本過濾
grep 選項 ‘查找條件’ 目標文件
egrep 選項 ‘查找條件’ 目標文件
-i:忽略大小寫
-v:反轉查找
--color:高亮顯示
-c:計算行數
-E:支持正則
egrep相當於grep -E
^字符串:指定開頭
$字符串:指定結尾
[root@node0 ~]# grep -Ev '^#|^$' /etc/fstab # 過濾註釋行和空白行
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
/dev/sr0 /dvd iso9660 defaults 0 0
No.16 文件壓縮與解壓、歸檔
(1)zip、unzip保留源文件
*.zip壓縮:zip 壓縮包名稱 被壓縮名稱
-r:壓縮目錄
*.zip解壓:upzip 壓縮包名稱 -d 目標目錄
[root@node0 ~]# ll
total 547508
-rw-r--r--. 1 root root 560640197 Nov 13 00:41 90後小夥的hadoop工作經驗分享.mp4
[root@node0 ~]# zip video.zip 90後小夥的hadoop工作經驗分享.mp4 # 壓縮文件
adding: 90後小夥的hadoop工作經驗分享.mp4 (deflated 4%)
[root@node0 ~]# unzip video.zip # 解壓縮文件
Archive: video.zip
inflating: 90後小夥的hadoop工作經驗分享.mp4
[root@node0 ~]# ll
total 1.1G
-rw-r--r--. 1 root root 535M Nov 13 00:41 90後小夥的hadoop工作經驗分享.mp4
-rw-r--r--. 1 root root 516M Nov 19 00:58 video.zip
(2)gzip、gzip-d/gunzip 不保留源文件
*.gz壓縮:gzip 被壓縮文件
[root@node0 ~]# gzip 90後小夥的hadoop工作經驗分享.mp4 # 壓縮文件
[root@node0 ~]# ll
total 516M
-rw-r--r--. 1 root root 516M Nov 13 00:41 90後小夥的hadoop工作經驗分享.mp4.gz
*.gz解壓:gzip -d/gunzip
[root@node0 ~]# gzip -d 90後小夥的hadoop工作經驗分享.mp4.gz # 解壓文件
[root@node0 ~]# ll
total 535M
-rw-r--r--. 1 root root 535M Nov 13 00:41 90後小夥的hadoop工作經驗分享.mp4
(3)bzip2、bunzip2/bzip2 不保留源文件
*.bz2壓縮: bzip2 被壓縮文件
[root@node0 ~]# ll
total 535M
-rw-r--r--. 1 root root 535M Nov 13 00:41 90後小夥的hadoop工作經驗分享.mp4
*.bz2解壓:bunzip2/bzip2 壓縮文件
[root@node0 ~]# bzip2 90後小夥的hadoop工作經驗分享.mp4 # 壓縮文件
[root@node0 ~]# ll
total 300M
-rw-r--r--. 1 root root 300M Nov 13 00:41 90後小夥的hadoop工作經驗分享.mp4.bz2
*.bz2解壓:gunzip2/bzip -d 壓縮文件
[root@node0 ~]# bzip2 -d 90後小夥的hadoop工作經驗分享.mp4.bz2 # 解壓文件
[root@node0 ~]# ll
total 535M
-rw-r--r--. 1 root root 535M Nov 13 00:41 90後小夥的hadoop工作經驗分享.mp4
(4)xz、unxz/xz -d 不保留源文件
*.xz壓縮:xz 被壓縮文件
[root@node0 ~]# xz 90後小夥的hadoop工作經驗分享.mp4 [root@node0 ~]# ll
total 120M
-rw-r--r--. 1 root root 120M Nov 13 00:41 90後小夥的hadoop工作經驗分享.mp4.xz
*.xz解壓:unzx/xz -d壓縮文件
[root@node0 ~]# unxz 90後小夥的hadoop工作經驗分享.mp4.xz # 解壓文件
[root@node0 ~]# ll
total 535M
-rw-r--r--. 1 root root 535M Nov 13 00:41 90後小夥的hadoop工作經驗分享.mp4
(5)tar 歸檔工具
歸檔文件:tar 選項 歸檔文件名 源文件或目錄
釋放文件:tar 選項 歸檔文件 -C 目標目錄
-c:創建*.tar文件
-x:解包*.tar文件
-C:釋放到指定目錄
-f:指定*.tar文件,放到其他參數之後使用
-z/-j/J:壓縮文件為*.gz/*.bz2/*.xz文件
-v:輸出詳細信息
-t:查看歸檔包中的文件列表
tar命令會解包的時候自動解壓文件,並且會自動判斷壓縮文件的壓縮文件類型,在解壓的時候會強制覆蓋文件,所以建議使用-C參數的時候指定相對路徑。
No.17 which whereis whatis locate find查找命令
whereis 選項 參數
-b:只查找二進制文件位置
-m:只查找手冊頁部分
whatis 命令
在whatis庫中搜尋特定的命令
which 命令
查看可執行命令的路徑
locate 選項 命令
文件定位,依據/var/lib/mlocate/ mlocate.db
執行updatedb命令刷新數據庫信息。
[root@node0 ~]# updatedb
[root@node0 ~]# ll /var/lib/mlocate/
total 1.8M
-rw-r-----. 1 root slocate 1.8M Nov 19 02:25 mlocate.db
find 查找範圍 查找條件
-type:按文件類型進行查找,f表示普通文件,d表示目錄,l表示連接文件
-name:使用雙引號,按照文件名查找,支持通配符
-size:按文件大小查找,+表示以上,-表示以下。單位:cwbkMG
-mtime:按文件的修改時間查找,+和-表示範圍,單位是天。
常用方式:
find 範圍 參數 -exec 處理命令 {} \;
[root@node0 ~]# find /boot/ -size +1M -exec cp {} . \;
[root@node0 ~]# ll
-rw-------. 1 root root 27M Nov 19 02:35 initramfs-2.6.32-573.el6.x86_64.img
-rw-r--r--. 1 root root 2.5M Nov 19 02:35 System.map-2.6.32-573.el6.x86_64
-rwxr-xr-x. 1 root root 4.1M Nov 19 02:35 vmlinuz-2.6.32-573.el6.x86_64
No.18 vim/vi基礎操作
鍵盤<o>鍵:另起一行插入
鍵盤<i>鍵:在光標左側插入
鍵盤<a>鍵:在光標右側插入
<1>+<Shift>+<g>鍵或雙擊<g>鍵:跳轉到行首
<Shift>+<g>鍵:跳轉到行尾
雙擊<y>鍵:復制當前行
<數字>雙擊<y>鍵:向下復制指定的行數
<p>鍵:在光標之後粘貼
<Shift>+<p>:在光標之前粘貼
<x>鍵或<Delete>鍵:刪除光標處字符
雙擊<d>鍵:刪除當前行
<數字>雙擊<d>鍵:向下刪除指定行
<d>+<^>鍵:刪除光標處至行首
<d>+<$>鍵:刪除光標處到行尾
/字符串:向後查找
?字符串:向前查找
<n>鍵:跳轉到下一個結果
<Shift>+<n>鍵:跳轉到上一個結果
<u>鍵:撤銷最近一次操作
<Shift>+<u>鍵:撤銷當前行的修改
<Ctrl>+<r>鍵:恢復上一次撤銷
<Shift>雙擊<z>鍵:保存退出
:w:保存
:x:保存
:wq:保存並退出
:q!:不保存退出
:w 文件路徑:另存為
:r 文件路徑:讀入文件內容
************************************************************************************************
在vim中:x與:w的區別:
:wq:寫入並退出,即使文件沒有被修改也強制寫入,並更新文件的修改時間。
:x:寫入並退出,僅當文件被修改時才寫入,並更新文件修改時間;否則不會更新文件修改時間。
************************************************************************************************
行內替換:
:s /源字符串/目標字符串 :替換當前行的第一個源字符串。
:s /源字符串/目標字符串/g:替換當前行的所有源字符串。
區域內替換:
:n,ms /源字符串/目標字符串/g :替換n至m行的源字符串。
:%s /源字符串/目標字符串/g:替換所有的源字符串。
:%s /源字符串/目標字符串/gc:替換時加上c,每次替換需要用戶確認替換操作。
:set nu|nonu:顯示|不顯示行號
:set ai|noai:啟用|不啟用自動縮進
vim配置文件 .vimrc:
set nu # 設置顯示行號
set ai # 設置自動縮進
set tabstop=4 # 設置<Tab>鍵等於4個<Space>鍵大小
vim交換文件 .文件名稱.swp
No.19 mount、umount掛載和卸載以及autofs觸發掛載
mount 設備目錄 掛載目錄
umount 設備目錄或掛載目錄
mount:查看設備
[root@node0 ~]# mount
/dev/sda3 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
/dev/sr0 on /dvd type iso9660 (ro)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
訪問觸發掛載點:
cd /misc/cd
訪問後直接掛載,5分鐘後如果沒有任何操作將會取消掛載。
對應服務:autofs
[root@node0 ~]# yum install autofs -y
[root@node0 misc]# /etc/init.d/autofs start
Loading autofs4: [ OK ]
Starting automount: [ OK ]
Linux基礎命令總結