最常見,最頻繁使用的基礎命令如下:
系統經典語錄:
1、命令操作完沒有任何消息信息, 就是最好的消息
2、系統一切從根開始
3、系統中數據一切皆文件
皮一下,這都是乾貨偶,大佬輕噴
一、linux關機命令:
1.shutdown命令安全地將系統關機(推薦)參數說明:
- [-r] 重啟計算器。
- [-h] 關機後關閉電源〔halt〕。
- [-c] cancel current process取消目前正在執行的關機程序。
- [-time] 設定關機〔shutdown〕前的時間。
- shutdown -h now =立刻關機
- shutdown -h 時間 = 時間關機
- shutdown -r now =立即重啟
- shutdown -h 10 =十分鐘後關機
2.簡提一下 halt 也可單獨使用,也可達到關機的效果,但halt命令是其實halt就是調用shutdown -h。halt執行時,殺死應用進程,執行sync系統調用,內核停止,可能導致linux系統的死機,需要重啟。
3.poweroff 會發送一個 ACPI 信號來通知系統關機。(別人告訴的)
4. init 進程一共分為7個級別, 0和6分別代表關閉和重啟
二、linux重啟命令:
reboot 執行重啟命令,其他的我也不知道還能說些什麼。
三、linux查詢所在位置路徑:pwd
- [root@www network-scripts]# pwd
- /etc/sysconfig/network-scripts
四、linux切換目錄:cd
- [root@www network-scripts]# cd -
- /root
- [root@www ~]#
- [root@www network-scripts]# cd ..
- [root@www sysconfig]#
- [root@www sysconfig]# cd
- [root@www ~]#
五、linux創建目錄文件:mkdir
參數:-p 遞歸創建
- [root@www /]# mkdir 123
- [root@www /]# mkdir -p /123/123
六、linux以樹形結構展示目錄結構:tree
參數:-L :指定層數 -d:只顯示目錄
- [root@lizhiming ~]# tree -d /boot
- /boot
- ├── efi
- │?? └── EFI
- │?? └── centos
- ├── grub
- └── grub2
- ├── fonts
- ├── i386-pc
- └── locale
七、linux查看命令:ls
參數:-l :長格式顯示 -a :顯示所有文件 -d :顯示目錄
- [root@www /]# ls -l 123
- total 0
- drwxr-xr-x. 2 root root 6 Oct 16 18:31 123
- [root@www /]# ls -d 123
- 123
- [root@www /]# ls -a
- . 123 backup boot dev home lib64 mnt proc run server sys usr
- .. application bin data etc lib media opt root sbin srv tmp var
八、linux複製命令:cp
註釋:111是目錄文件,222是文本
參數:- r 遞歸 -i 是否覆蓋確認 -a 相當於dpr -p保持文件或目錄樹形
- [root@www 123]# ls
- 111 222
- [root@www 123]# cp 222 /456/999
- [root@www 123]# cd /456
- [root@www 456]# ls
- 999
- [root@www 456]# cp -r /123/111 /456/888
- [root@www 456]# ls
- 888 999
九、linux刪除命令:rm
參數:- r 遞歸 - f 強制 兩個一起用你可以刪掉世界(很暴力很血腥,危險的命令)
- [root@www /]# rm -rf /123 /456
十、linux更改命令別名:alias
刪除別名:unalias
- [root@www /]# alias ls='echo 看個錘子啊,笨蛋不配看內容'
- [root@www /]# ls
- 看個錘子啊,笨蛋不配看內容
- [root@www /]# unalias ls
- [root@www ~]# ls
- anaconda-ks.cfg
十一、linux移動命令:mv
參數:- t 把所用源參數移動到目錄中
在相同路徑目錄中使用相當於改名,在不同路徑中相當於移動
- [root@www 123]# ls
- 888
- [root@www 123]# mv /123/888 777
- [root@www 123]# ls
- 777
- [root@www 123]# mv /123/888 777
- [root@www 123]# ls
- 777
- [root@www 123]# mv /123/777 /456/777
- [root@www 123]# ls
- [root@www 123]# cd /456
- [root@www 456]# ls
- 777
十二、linux打印輸出命令:echo
參數:-h 不換行 - e 支持轉義 \\t 代表top \\n 代表回車
- [root@www /]# echo 8
- 8
十三、linux創建文件或更新文件時間戳:touch
- [root@yu yuxi]# touch 123
- [root@yu yuxi]# ll
- total 0
- -rw-r--r--. 1 root root 0 Oct 21 19:18 123
- [root@yu yuxi]# touch 123
- [root@yu yuxi]# ll
- total 0
- -rw-r--r--. 1 root root 0 Oct 21 19:19 123
十四、linux創建查看文件內容:cat
參數: -n 顯示行號
- [root@yu yuxi]# cat 123
- 123
- 123
- 123
- [root@yu yuxi]# cat -n 123
- 1123
- 2123
- 3123
十五、linux輸出頭部 / 尾部部分文件:head / tail
參數:-n 行數
- [root@yu yuxi]# head -n 4 123
- 1
- 2
- 3
- 4
- [root@yu yuxi]# tail -n 4 123
- 27
- 28
- 29
- 30
十六、linux替換或刪除字符:tr
注意:只是把文件內容輸出出來,而不是改變文件內容
- [root@yu yuxi]# cat 123
- 999999888888
- [root@yu yuxi]# tr '9' '1' < 123
- 111111888888
- [root@yu yuxi]# cat 123
- 999999888888
十七、linux查找文件裡符合條件的字符串:grep
linux中常用的文本(awk,sed,grep)處理工具之一
首先談一下grep命令的常用格式為:grep [選項] ”模式“ [文件]
grep家族總共有三個:grep,egrep,fgrep
參數:
參數 用途
- - color = auto過濾的內容加顏色
- v取反
- i不區分大小寫
- n顯示行號
- w按單詞位單位過濾
- o只輸出匹配的內容
- E相當於egrep(過濾多個參數)
- A顯示過濾字符串和它之後多少行
- B顯示過濾字符串和它之前多少行
-C顯示過濾字符串和它之前之後多少行
簡單應用:
- [root@yu yuxi]# grep -A 2 '15' 123
- 15
- 16
- 17
- [root@yu yuxi]# grep -C 2 '15' 123
- 13
- 14
- 15
- 16
- 17
- [root@yu yuxi]# grep -B 2 '15' 123
- 13
- 14
- 15
- [root@yu yuxi]# grep -n '15' 123
- 15:15
- [root@yu yuxi]# grep '15' 123
- 15
- [root@yu yuxi]# grep '1' 123
- 1
- 10
- 11
- [root@yu yuxi]# grep -o '1' 123
- 1
- 1
- 1
- [root@yu yuxi]# grep -w '1' 123
- 1
- [root@yu yuxi]# egrep -v "^[1-9]$|[1-2][0-9]" 123
- 30
十八、linux查看文件類型:file
- [root@yu yuxi]# file /usr/bin/cp
- /usr/bin/cp: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=d5e29bd19107fc7c0a75cffa392da6ac299add6d, stripped
十九、linux:創建創建軟硬鏈接:ln
參數:- s 創建軟連接
- [root@yu shangke]# ln -s /yuxi/shangke/123.txt /yuxi/xuexi/ruanlianjie.txt
- [root@yu xuexi]# ll -i
- 352 lrwxrwxrwx. 1 root root 21 Oct 21 21:12 ruanlianjie.txt -> /yuxi/shangke/123.txt
- [root@yu xuexi]# ln /yuxi/shangke/123.txt /yuxi/xuexi/lianjie.txt
- [root@yu xuexi]# ll -i
- 16814069 -rw-r--r--. 2 root root 4 Oct 21 21:06 lianjie.txt
- 16814069 -rw-r--r--. 2 root root 4 Oct 21 21:06 123.txt
二十、linux:查命令所在路徑:which
- [root@yu xuexi]# which cp
- alias cp='cp -i'
- /usr/bin/cp
二十一、查找目錄下文件:find
參數:
參數用途
- name按文件名查找
- type按文件類型查找(後面接文件類型參數,例如:目錄 d 文件 f
- exec對搜索結果在處理
- mtime按修改時間查找
簡單應用:
- [root@yu xuexi]# find / -name cp
- /usr/bin/cp
- [root@yu xuexi]# find /yuxi/ -type f
- /yuxi/xuexi/ruanlianjie.txt
- /yuxi/shangke/123.txt
二十二、從標準輸入執行命令:xargs
這只是最基礎參考,命令的九牛一毛,詳解百度搜索xargs命令
參數:
參數用途
- n數字,把幾個數字一組
- d指定分隔符,默認空格
- i把{}當作前面查找的結果
- [root@yu shangke]# cat 123.txt |xargs -n 3
- 1 2 3
- 4 5 6
- 7 8 9
二十三、查看用戶身份uid/gid:id
- [root@yu shangke]# id
- uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
二十四、查看當前用戶/添加普通用戶:whoami / uesradd
創建用戶就不演示了
- [root@yu shangke]# whoami
- root
二十五、查看文件屬性:stat
- [root@yu shangke]# stat /etc
- File: ‘/etc’
- Size: 8192 Blocks: 24 IO Block: 4096 directory
- Device: 803h/2051dInode: 16777281 Links: 79
- Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
- Context: system_u:object_r:etc_t:s0
- Access: 2019-10-21 19:43:39.253795426 +0800
- Modify: 2019-10-21 18:28:20.106983252 +0800
- Change: 2019-10-21 18:28:20.106983252 +0800
- Birth: -
二十六、顯示系統時間和日期:date
參數:- s 修改時間 - d 只能過去或未來格式
[root@yu shangke]# date
Mon Oct 21 21:51:25 CST 2019
二十七、查看運行等級:runlevel
- [root@yu shangke]# runlevel
- N 3
二十八、切換運行級別:init
- [root@yu ~]# init 5
- [root@yu ~]# runlevel
- 3 5
- [root@yu shangke]# init 0
- [root@yu shangke]# init 6
二十九、修改主機名:hostname
- [root@yu ~]# hostname yu
- [root@yu ~]# hostnamectl set-hostname yu
三十、壓縮打包:tar
參數用途
- z壓縮
- c創建
- v輸出打包過程
- f文件
- t查看文件
- C指定解壓路徑
- x解壓
- h跟隨軟連接
- exclude排除不打包文件
- X從文件中排除不打包的文件
三十一、查看文件系統:df
參數:- i inode 信息 - h 查看block信息
- [root@yu ~]# df -h
- Filesystem Size Used Avail Use% Mounted on
- /dev/sda3 18G 1.6G 17G 9% /
- devtmpfs 476M 0 476M 0% /dev
- tmpfs 487M 0 487M 0% /dev/shm
- tmpfs 487M 7.6M 479M 2% /run
- tmpfs 487M 0 487M 0% /sys/fs/cgroup
- /dev/sda1 1014M 127M 888M 13% /boot
- /dev/sr0 4.3G 4.3G 0 100% /yuxi/guazai
- tmpfs 98M 0 98M 0% /run/user/0
- [root@yu ~]# df -i
- Filesystem Inodes IUsed IFree IUse% Mounted on
- /dev/sda3 9436672 59319 9377353 1% /
- devtmpfs 121762 373 121389 1% /dev
- tmpfs 124487 1 124486 1% /dev/shm
- tmpfs 124487 702 123785 1% /run
- tmpfs 124487 16 124471 1% /sys/fs/cgroup
- /dev/sda1 524288 326 523962 1% /boot
- /dev/sr0 0 0 0 - /yuxi/guazai
- tmpfs 124487 1 124486 1% /run/user/0
三十二、點:source
source命令是bash shell的內置命令,點命令,就是個點符號,是source的另一名稱
當前腳本中配置的變量也將作為腳本的環境,source(或點)命令通常用於重新執行剛修改的初始化文檔,比如 .bash_profile 和 .profile 等等
三十三、查看磁盤文件UUID信息:blkid
- [root@yu ~]# blkid
- /dev/sr0: UUID="2018-11-25-23-54-16-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
- /dev/sda1: UUID="cc698e40-163f-4464-826e-a80ab50d682a" TYPE="xfs"
- /dev/sda2: UUID="e103cc3e-541d-4a08-ac0e-8d9d88f3050f" TYPE="swap"
- /dev/sda3: UUID="320e8964-efc4-4d25-96c6-4696a91f96bb" TYPE="xfs"
三十四、指定某個網卡激活啟動/關閉:ifdown/ifup
- [root@yu ~]# ifdown ens33 && ifup ens33
- Device 'ens33' successfully disconnected.
- Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/2)
三十五、查看uuid號:blkid
- [root@yu ~]# blkid
- /dev/sr0: UUID="2018-11-25-23-54-16-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
- /dev/sda1: UUID="cc698e40-163f-4464-826e-a80ab50d682a" TYPE="xfs"
- /dev/sda2: UUID="e103cc3e-541d-4a08-ac0e-8d9d88f3050f" TYPE="swap"
- /dev/sda3: UUID="320e8964-efc4-4d25-96c6-4696a91f96bb" TYPE="xfs"
三十六、查看服務是否開啟:telnet
telnet命令通常用來遠程登錄,但也可以確定遠程服務的狀態,比如確定遠程服務器的某個端口是否能訪問。
- [root@yu ~]# telnet 10.0.0.200 22
- Trying 10.0.0.200...
- Connected to 10.0.0.200.
三十七、檢查及刪除文本文件中重複出現的行列 / 文本文件內容加以排序:uniq / sort
sort幾個常用參數:
注意uniq命令只能篩選兩行在一起的數據,分開無法篩選,篩選前先排序
參數用途
uniq
- c在每列旁邊顯示該行重複出現的次數
- d僅顯示重複出現的行列
- u僅顯示出一次的行列
sort
- b忽略每行前面開始出的空格字符
- r以相反的順序來排序
- n依照數值的大小排序
- [root@yu xuexi]# cat 1
- 1
- haha
- 2
- haha
- 3
- haha
- 4
- haha
- [root@yu xuexi]# sort -n 1 |uniq -c
- 4 haha
- 1 1
- 1 2
- 1 3
- 1 4
三十八、外國人在廁所學統計:wc(統計,用於計算數字)
參數: - l 只顯示行數
- [root@yu xuexi]# wc -l 1.txt
- 8 1.txt
三十九、查看硬件信息大禮包
命令用途
lscpu查看cpu使用情況
free查看內存使用情況
w查看負載使用情況
top查看負載使用情況
uptime查看負載使用情況
四十、刪除執行中的程序:kill
強行殺死進程(很暴力很血腥,危險的命令)
- [root@yu xuexi]# kill -KILL pts/1
四十一、顯示目錄或文件的大小:du
參數:- h 人類能看懂的形式顯示出來
注:顯示指定的目錄或文件所佔用的磁盤空間
- [root@yu xuexi]# du -h /yuxi/xuexi/
- 8.0K/yuxi/xuexi/
四十二、顯示當前進程 (process) 的狀態:ps
- [root@yu ~]# ps
- PID TTY TIME CMD
- 37758 pts/0 00:00:00 bash
- 37780 pts/0 00:00:00 ps
怎麼樣?這些Linux命令都掌握了嘛?沒有掌握的話,小編這裡準備好了Linux從安裝到運用,到怎麼去做開發,大家來獲取一波吧!
獲取方式:轉發關注小編,私信小編“學習”來拿走吧~~
閱讀更多 程序員高級工程師 的文章