linux常用命令總結


一、find命令

作用:查找文件

[root@server ~]# find / -name elasticsearch
/var/log/elasticsearch
/var/run/elasticsearch
/var/lock/subsys/elasticsearch
/var/lib/elasticsearch
/var/lib/elasticsearch/elasticsearch
/usr/share/elasticsearch
/usr/share/elasticsearch/bin/elasticsearch
/etc/rc.d/init.d/elasticsearch
/etc/elasticsearch
/etc/sysconfig/elasticsearch
/opt/kibana/src/plugins/elasticsearch
/opt/kibana/node_modules/elasticsearch

 

二、tar 打包壓縮與解壓命令

[root@template ~]# cd /tmp

#進入目錄打包文件 [root@template tmp]# tar czvf passwd.tar.gz passwd passwd
#指定解壓目錄 [root@template tmp]# tar xzvf passwd.tar.gz -C /tmp passwd [root@template tmp]# ll total 8 -rw-r--r--. 1 root root 1138 Apr 16 04:12 passwd -rw-r--r--. 1 root root 618 Apr 16 04:13 passwd.tar.gz

 

用腳本對文件進行打包

[root@template tmp]# ll
總用量 8
drwxr-xr-x 2 root root 4096 4月 28 18:07 nulige
-rw-r--r-- 1 root root 75 4月 28 18:24 tar_backup.sh


[root@template tmp]# sh tar_backup.sh
ok


[root@template tmp]# ll
總用量 12
drwxr-xr-x 2 root root 4096 4月 28 18:07 nulige
-rw-r--r-- 1 root root 190 4月 28 18:24 nulige.tar.gz
-rw-r--r-- 1 root root 75 4月 28 18:24 tar_backup.sh


[root@template tmp]# cat tar_backup.sh
#!/bin/sbin
cd /tmp
tar zcf nulige.tar.gz ./nulige
[ $? -eq 0 ] && echo ok

 

三、grep命令

示例:過濾出帶有/sbin/nologin的內容

[root@template tmp]# grep /sbin/nologin /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:998:996:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:997:995:User for polkitd:/:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin

 

四、stat命令

作用:用於查看文件的具體存儲信息和時間

[root@template tmp]# stat passwd
File: ‘passwd’
Size: 1138 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 102426465 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2017-04-16 04:14:47.442943634 +0800
Modify: 2017-04-16 04:12:48.000000000 +0800
Change: 2017-04-16 04:14:47.442943634 +0800
Birth: -

 

五、cut命令

作用:用來按列提取文本字條

參數:

-f 參數:用來設置要看的列數

-d參數:設置間隔符號

示例:提取出passwd文件中的用戶名

[root@template tmp]# cut -d: -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
avahi-autoipd
systemd-bus-proxy
systemd-network
dbus
polkitd
abrt
tss
postfix
sshd
mysql

 

六、head命令

作用:用來查看文本文檔

示例:顯示前10行

[root@template tmp]# head -n 10 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

 

七、history

作用:查看歷史記錄命令

[root@template tmp]# history
    1  ip a
    2  shutdown -h now
    3  ip addr
    4  systemctl restart network
    5  ip addr

清空歷史記錄

[root@template tmp]# history -c

 

八、sosreport

  sosreport命令用於收集系統配置並診斷信息后輸出結論文檔,格式為:“sosreport”。
當咱們的Linux系統出現故障需要聯系紅帽廠商或其他技術支持時,大多數時候都要先使用這個SOS功能來簡單收集計算機的狀態和配置信息,以便讓技術支持公司能夠通過遠程就解決了一些小問題,又或者讓他們能對復雜問題能提前有些了解:

[root@template tmp]# sosreport

sosreport (version 3.2)

This command will collect diagnostic and configuration information from
this CentOS Linux system and installed applications.

An archive containing the collected information will be generated in
/var/tmp and may be provided to a CentOS support representative.

Any information provided to CentOS will be treated in accordance with
the published support policies at:

  https://www.centos.org/

The generated archive may contain data considered sensitive and its
content should be reviewed by the originating organization before being
passed to any third party.

No changes will be made to system configuration.

Press ENTER to continue, or CTRL-C to quit.  #此處敲擊回車來確認收集信息

Please enter your first initial and last name [template]:    #此處敲擊回車,來確認主機名稱                                   
Please enter the case id that you are generating this report for []:  #此處敲擊回車,生成報告

 Setting up archive ...
 Setting up plugins ...
 Running plugins. Please wait ...

  Running 77/77: yum...                      
Creating compressed archive...


Your sosreport has been generated and saved in:
  /var/tmp/sosreport-template-20170416043251.tar.xz

The checksum is: 9f5d08f6373e35985213ab8001f7a078

Please send this file to your support representative.

#此壓縮包文件和這段校驗值就是要發送給對方的內容。

#查看生成的報告
[root@template tmp]# ll /var/tmp/
-rw-------. 1 root root 5543652 Apr 16 04:34 sosreport-template-20170416043251.tar.xz

 

九、free命令

作用:顯示當前系統中內存的使用量信息

[root@linux~]# free -h
  總計內存量 已用量 可用量 進程共享的內存量 磁盤緩存的內存量 緩存的內存量
  total used free shared buffers cached
Mem: 1.8G 1.3G 542M 9.8M 1.6M 413M
-/+ buffers/cache:   869M 957M      
Swap: 2.0G 0B 2.0G      

 

十、w和who命令

作用:查看當前登入主機的用戶信息

[root@template tmp]# w
 04:40:12 up  1:04,  2 users,  load average: 0.00, 0.12, 0.12
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      03:36   55:00   0.15s  0.15s -bash
root     pts/0    192.168.30.1     03:46    4.00s  0.67s  0.02s w
[root@template tmp]# who
root     tty1         2017-04-16 03:36
root     pts/0        2017-04-16 03:46 (192.168.30.1)

 

十一、uptime命令

作用:用於查看系統的負載信息,

格式為:“uptime”。

這個命令真的很棒,它可以為您顯示當前系統時間、系統已運行時間、當前在線用戶以及平均負載值等信息數據。平均負載值指的是最近1分鍾、5分鍾、15分鍾的系統壓力情況,負載值越低越好,盡量不要長期超過1。另外您還可以結合搭配用"watch -n 1 uptime"命令來每秒刷新一次來獲得當前的系統負載情況。

[root@linux~]# uptime
22:49:55 up 10 min, 2 users, load average: 0.01, 0.19, 0.18

 

十二、查看系統詳細版本和內核版本

[root@template tmp]# uname -a
Linux template 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

[root@template tmp]# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 

 

十三、pidof命令

作用:查看指定服務的進程pid值

[root@template tmp]# pidof sshd
2956 1538

 

十四、top命令

作用:用於動態的監視進程活動與系統負載等信息

功能類似於windows系統中的任務管理器

前面的五行為系統整體的統計信息,下面咱們來逐行的講解:

1行:系統時間,運行時間,登錄用戶數,系統負載(分別為1分鍾、5分鍾、15分鍾的平均值)。第 2行:進程總數,運行中的,睡眠中的,停止的,僵死的。第 3行:用戶占用資源,系統內核占用資源,改變過優先級的進程,空閑的資源,等待輸入輸出的時間。此行數據均為CPU數據並以百分比格式顯示,例如"99.2 id"意味着有99.2%的CPU資源正在空閑中。第 4行:物理內存總量,使用量,空閑量,作為內核緩存的內存量。第 5行:虛擬內存總量,使用量,空閑量,已被提前加載的內存數據。

 

十五、ps命令

作用:用於查看系統中的進程狀態

參數 作用
-a 顯示所有的進程(包括其他用戶的)
-u 用戶以及其他詳細信息
-x 顯示沒有控制終端的進程

 

Linux系統中時刻運行着許許多多的進程,如果能夠合理的管理它們,絕對有益於系統的性能優化,Linux系統中進程最常見的5種不同的狀態是運行、中斷、不可中斷、僵死與停止,它們的特性分別是:

R(運行):正在運行或在運行隊列中等待。 S(中斷):休眠中, 在等待某個條件的形成或接收到信號。 D(不可中斷):收到信號不喚醒和不可運行, 進程必須等待直到有中斷發生。 Z:(僵死):進程已終止, 但進程描述符存在, 直到父進程調用wait4()系統調用后釋放。 T:(停止):進程收到SIGSTOP, SIGSTP, SIGTIN, SIGTOU信號后停止運行。

當執行"ps aux"命令后通常會看到下面格式的進程狀態,表格中只是列舉了部分輸出值,而且正常的輸出值中不包括中文注釋部分:

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
進程的所有者 進程ID號 運算器占用率 內存占用率 虛擬內存使用量(單位是KB) 占用的固定內存量(單位是KB) 所在終端 進程狀態 被啟動的時間 實際使用CPU的時間 命令名稱與參數
root 1 0.0 0.4 53684 7628 ? Ss 07:22 0:02 /usr/lib/systemd/systemd
root 2 0.0 0.0 0 0 ? S 07:22 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 07:22 0:00 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< 07:22 0:00 [kworker/0:0H]
root 7 0.0 0.0 0 0 ? S 07:22 0:00 [migration/0]

 

十六、date命令

date命令用於顯示及設置系統的時間或日期,格式為:"date [選項] [+指定的格式]"。

強大的date命令只需鍵入以"+"號開頭的參數即可按照指定格式來輸出系統的時間或日期,這樣日常工作時咱們便可以將打包數據的備份命令與指定格式輸出的時間信息結合到一起,使得咱們可以更加便捷的區分每個文件的備份時間啦,date命令常見的格式如下:

參數 作用
%t 跳格[TAB鍵]
%H 小時(00-23)
%I 小時(00-12)
%M 分鍾(00-59)
%S 秒(00-59)
%X 相當於%H:%M:%S
%Z 顯示時區
%p 顯示本地AM或PM
%A 星期幾 (Sunday-Saturday)
%a 星期幾 (Sun-Sat)
%B 完整月份 (January-December)
%b 縮寫月份 (Jan-Dec)
%d 日(01-31)
%j 一年中的第幾天(001-366)
%m 月份(01-12)
%Y 完整的年份


按照默認的格式查看當前的系統時間:

[root@linux ~]# date
Mon Aug 24 16:11:23 CST 2016

按照"年-月-日 小時:分鍾:秒"的格式查看當前的系統時間:

[root@linux ~]# date "+%Y-%m-%d %H:%M:%S"
2016-08-24 16:29:12

設置當前的系統時間為2016年9月1日8點30分:

[root@linux ~]# date -s "20160901 8:30:00"
Tue Sep 1 08:30:00 CST 2016

再次按照默認的格式查看當前的系統時間:

[root@linux ~]# date
Tue Sep 1 08:30:01 CST 2016

查看今天是一年中的第幾天:

[root@linux ~]# date "+%j"
245

 

十七、dd命令

作用:用於指定大小的拷貝文件或指定轉換文件

dd命令:是個比較重要且具有特色的一個命令,它能夠讓用戶指定數據塊的大小和個數來復制一個文件的內容,當然如果您願意的話還可以在復制過程中轉換其中的數據。Linux系統中有一個叫做/dev/zero的設備文件,每次講課解釋起來都感覺有點哲學理論的色彩,因為它不會占用您的系統存儲空間,但里面卻可以保存有無窮無盡的數據,一般用來搭配dd命令來生成出來一個指定大小的文件是再好不過的了。

參數 作用
if 輸入的文件名稱。
of 輸出的文件名稱。
bs 設置每個“塊”的大小。
count 設置要拷貝“塊”的個數。

 

[root@linux ~]# dd if=/dev/zero of=560_file count=1 bs=560M
1+0 records in
1+0 records out
587202560 bytes (587 MB) copied, 27.1755 s, 21.6 MB/s
#從光驅中復制iso鏡像文件到服務器指定目錄(注意:光驅插入Centos7.2.iso文件)
#從/dev/cdrom 中復制鏡像文件到/opt目錄中

[root@template opt]# dd if=/dev/cdrom of=/opt/CentOS-7.2.iso
記錄了8456192+0 的讀入
記錄了8456192+0 的寫出
4329570304字節(4.3 GB)已復制,155.866 秒,27.8 MB/秒

 

十八、生產常用命令

1.刪除0字節文件
find -type f -size 0 -exec rm -rf {} \;

2.查看進程
按內存從大到小排列
ps -e -o “%C : %p : %z : %a”|sort -k5 -nr

3.按cpu利用率從大到小排列
ps -e -o “%C : %p : %z : %a”|sort -nr

4.打印說cache里的URL
grep -r -a jpg /data/cache/* | strings | grep “http:” | awk -F’http:’ ‘{print “http:”$2;}’

5.查看http的並發請求數及其TCP連接狀態:
netstat -n | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}’

6.
sed -i ‘/Root/s/no/yes/’ /etc/ssh/sshd_config sed在這個文里Root的一行,匹配Root一行,將no替換成yes.

7.1.如何殺掉mysql進程:
ps aux|grep mysql|grep -v grep|awk ‘{print $2}’|xargs kill -9

(從中了解到awk的用途)
pgrep mysql |xargs kill -9

killall -TERM mysqld

kill -9 `cat /usr/local/apache2/logs/httpd.pid`

試試查殺進程PID

8.顯示運行3級別開啟的服務:
ls /etc/rc3.d/S* |cut -c 15-

(從中了解到cut的用途,截取數據)

9.如何在編寫SHELL顯示多個信息,用EOF
cat << EOF +--------------------------------------------------------------+ | === Welcome to Tunoff services === | +--------------------------------------------------------------+ EOF 
10. for 的巧用(如給mysql建軟鏈接) 
cd /usr/local/mysql/bin for i in * do ln /usr/local/mysql/bin/$i /usr/bin/$i done 
11. 取IP地址: 
ifconfig eth0|sed -n '2p'|awk '{print $2}'|cut -c 6-30 或者: ifconfig eth0 |grep "inet addr:" |awk '{print $2}'|cut -c 6- 或者 ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}' 或者: ifconfig eth0 | sed -n '/inet /{s/.*addr://;s/ .*//;p}' 
Perl實現獲取IP的方法: 
ifconfig -a | perl -ne 'if ( m/^\s*inet (?:addr:)?([\d.]+).*?cast/ ) { print qq($1\n); exit 0; }' 
12.內存的大小: free -m |grep "Mem" | awk '{print $2}' 
13. netstat -an -t | grep ":80" | grep ESTABLISHED | awk '{printf "%s %s\n",$5,$6}' | sort 
14.查看Apache的並發請求數及其TCP連接狀態: netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' 
15.因為同事要統計一下服務器下面所有的jpg的文件的大小,寫了個shell給他來統計.原來用xargs實現,但他一次處理一部分,搞的有多個總和....,下面的命令就能解決啦. 
find / -name *.jpg -exec wc -c {} \;|awk '{print $1}'|awk '{a+=$1}END{print a}'
CPU的數量(多核算多個CPU, cat /proc/cpuinfo |grep -c processor )越多,系統負載越低,每秒能處理的請求數也越多。 
-------------------------------------------------------------------------------------------------------------------- 
16. CPU負載 # cat /proc/loadavg 檢查前三個輸出值是否超過了系統邏輯CPU的4倍。 
18. CPU負載 #mpstat 1 1 檢查%idle是否過低(比如小於5%) 
19. 內存空間 # free 檢查free值是否過低 也可以用 # cat /proc/meminfo 
20. swap空間 # free 檢查swap used值是否過高 如果swap used值過高,進一步檢查swap動作是否頻繁: # vmstat 1 5 觀察si和so值是否較大 
21. 磁盤空間 # df -h 檢查是否有分區使用率(Use%)過高(比如超過90%) 如發現某個分區空間接近用盡,可以進入該分區的掛載點,用以下命令找出占用空間最多的文件或目錄: # du -cks * | sort -rn | head -n 10 
22. 磁盤I/O負載 # iostat -x 1 2 檢查I/O使用率(%util)是否超過100% 
23. 網絡負載 # sar -n DEV 檢查網絡流量(rxbyt/s, txbyt/s)是否過高 
24. 網絡錯誤 # netstat -i 檢查是否有網絡錯誤(drop fifo colls carrier) 也可以用命令:# cat /proc/net/dev 
25. 網絡連接數目 # netstat -an | grep -E “^(tcp)” | cut -c 68- | sort | uniq -c | sort -n 
26. 進程總數 # ps aux | wc -l 檢查進程個數是否正常 (比如超過250) 
27. 可運行進程數目 # vmwtat 1 5 列給出的是可運行進程的數目,檢查其是否超過系統邏輯CPU的4倍 
28. 進程 # top -id 1 觀察是否有異常進程出現 
29. 網絡狀態 檢查DNS, 網關等是否可以正常連通
ping traceroute nslookup dig 
30. 用戶 # who | wc -l 檢查登錄用戶是否過多 (比如超過50個) 也可以用命令:# uptime 
31. 系統日志 # cat /var/log/rflogview/*errors 
檢查是否有異常錯誤記錄 也可以搜尋一些異常關鍵字,例如: 
# grep -i error /var/log/messages # grep -i fail /var/log/messages 
# egrep -i 'error|warn' /var/log/messages 查看系統異常 32 核心日志 
# dmesg 檢查是否有異常錯誤記錄 
33. 系統時間 # date 檢查系統時間是否正確
當前時間: date +"%Y-%m-%d %H:%M:%S" 
34. 打開文件數目 # lsof | wc -l 檢查打開文件總數是否過多 
35. 日志 # logwatch ?print 配置/etc/log.d/logwatch.conf,將 Mailto 設置為自己的email 地址,啟動mail服務 (sendmail或者postfix),這樣就可以每天收到日志報告了。 
缺省logwatch只報告昨天的日志,可以用# logwatch ?print ?range all 獲得所有的日志分析結果。 
可以用# logwatch ?print ?detail high 獲得更具體的日志分析結果(而不僅僅是出錯日志)。 
36.殺掉80端口相關的進程 lsof -i :80|grep -v "PID"|awk '{print "kill -9",$2}'|sh 
37.清除僵死進程。 ps -eal | awk '{ if ($2 == "Z") {print $4}}' | kill -9 
38.tcpdump 抓包 ,用來防止80端口被人攻擊時可以分析數據 
# tcpdump -c 10000 -i eth0 -n dst port 80 > /root/pkts

39.然后檢查IP的重復數 並從小到大排序 注意 “-t\ +0″ 中間是兩個空格
# less pkts | awk {'printf $3"\n"'} | cut -d. -f 1-4 | sort | uniq -c | awk {'printf $1" "$2"\n"'} | sort -n -t\ +0

40.查看有多少個活動的php-cgi進程
netstat -anp | grep php-cgi | grep ^tcp | wc -l

41.利用iptables對應簡單攻擊
netstat -an | grep -v LISTEN | awk ‘{print $5}’ |grep -v 127.0.0.1|grep -v 本機ip|sed “s/::ffff://g”|awk ‘BEGIN { FS=”:” } { Num[$1]++ } END { for(i in Num) if(Num>8) { print i} }’ |grep ‘[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}’| xargs -i[] iptables -I INPUT -s [] -j DROP

Num>8部分設定值為閥值,這條句子會自動將netstat -an 中查到的來自同一IP的超過一定量的連接的列入禁止范圍。 基中本機ip改成你的服務器的ip地址

42. 怎樣知道某個進程在哪個CPU上運行?
# ps -eo pid,args,psr

43. 查看硬件制造商
dmidecode -s system-product-name

44.perl如何編譯成字節碼,這樣在處理復雜項目的時候會更快一點?
perlcc -B -o webseek webseek.pl

45. 統計var目錄下文件以M為大小,以列表形式列出來。
find /var -type f | xargs ls -s | sort -rn | awk ‘{size=$1/1024; printf(“%dMb %s\n”, size,$2);}’ | head
查找var目錄下文件大於100M的文件,並統計文件的個數
find /var -size +100M -type f | tee file_list | wc -l

46. sed 查找並替換內容
sed -i “s/varnish/LTCache/g” `grep “Via” -rl /usr/local/src/varnish-2.0.4`

sed -i “s/X-Varnish/X-LTCache/g” `grep “X-Varnish” -rl /usr/local/src/varnish-2.0.4`

47. 查看服務器制造商
dmidecode -s system-product-name

48. wget 模擬user-agent抓取網頁
wget -m -e robots=off -U “Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6” http://www.example.com/

50. 統計目錄下文件的大小(按M打印顯示)
du $1 –max-depth=1 | sort -n|awk ‘{printf “%7.2fM —-> %s\n”,$1/1024,$2}’|sed ‘s:/.*/\([^/]\{1,\}\)$:\1:g’

51.關於CND實施幾個相關的統計
統計一個目錄中的目錄個數
ls -l | awk ‘/^d/’ | wc -l

統計一個目錄中的文件個數
ls -l | awk ‘/^-/’ | wc -l

統計一個目錄中的全部文件數
find ./ -type f -print | wc -l

統計一個目錄中的全部子目錄數
find ./ -type d -print | wc -l

統計某類文件的大小:
find ./ -name “*.jpg” -exec wc -c {} \;|awk ‘{print $1}’|awk ‘{a+=$1}END{print a}’

53. 查找占用磁盤IO最多的進程
wget -c http://linux.web.psi.ch/dist/scientific/5/gfa/all/dstat-0.6.7-1.rf.noarch.rpm
dstat -M topio -d -M topbio

54. 去掉第一列(如行號代碼)
awk ‘{for(i=2;i<=NF;i++) if(i!=NF){printf $i" "}else{print $i} }' list 
55.輸出256中色彩 
for i in {0..255}; do echo -e "\e[38;05;${i}m${i}"; done | column -c 80 -s ' '; echo -e "\e[m" 
56.查看機器支持內存 機器插內存情況: dmidecode |grep -P "Maximum\s+Capacity" 
機器最大支持內存: dmidecode |grep -P "Maximum\s+Capacity" 
57.查看PHP-CGI占用的內存總數: 
total=0; for i in `ps -C php-cgi -o rss=`; do total=$(($total+$i)); done; echo "PHP-CGI Memory usage: $total kb"

 


1、find用法

[root@template tmp]# find / -type f -name "text.txt"
/tmp/text.txt
/root/text.txt

#找到文件后,交給管道刪除
[root@template tmp]# find / -type f -name "text.txt" | xargs rm -f

面試題:刪除一個目錄下的所有文件,但保留一個指定文件(保留file10)

[root@template tmp]# touch file{1..10}
[root@template tmp]# ll
總用量 0
-rw-r--r-- 1 root root 0 5月  20 10:15 file1 -rw-r--r-- 1 root root 0 5月 20 10:11 file10 -rw-r--r-- 1 root root 0 5月 20 10:15 file2 -rw-r--r-- 1 root root 0 5月 20 10:15 file3 -rw-r--r-- 1 root root 0 5月 20 10:15 file4 -rw-r--r-- 1 root root 0 5月 20 10:15 file5 -rw-r--r-- 1 root root 0 5月 20 10:15 file6 -rw-r--r-- 1 root root 0 5月 20 10:15 file7 -rw-r--r-- 1 root root 0 5月 20 10:15 file8 -rw-r--r-- 1 root root 0 5月 20 10:15 file9 #方法一: [root@template tmp]# find /tmp -type f ! -name "file10"|xargs rm -f 方法二: [root@template tmp]# find /tmp -type f ! -name "file10" -exec rm -f {} \;

#find找到的內容,-exec是參數,{}:要查找的目標,一般可以不寫 \ :反斜杠轉義字符
!作用就是:取反

 2、rm  remove  刪除文件或者目錄 

    -f 強制

   -r 刪除目錄

 注意:生產場景盡量不要使用rm,如果非要用,一定要先cp備份

[root@template tmp]# ll
總用量 0
-rw-r--r-- 1 root root 0 5月  20 10:06 text.txt [root@template tmp]# rm -f text.txt [root@template tmp]# ll 總用量 0

 3、echo用法

   打印輸出內容,配合“>或>>” 可以為文件覆蓋及追加內容

   ">" 意思是重定向,會清除文件里所有以前數據

   ">>" 為追加內容

4、cat 查看文件內容

  特殊用法:增加多行內容

[root@template tmp]# cat >>/tmp/nulige.txt <<EOF
> I am studying linux. > EOF [root@template tmp]# ll 總用量 4 -rw-r--r-- 1 root root 0 5月 20 10:11 file10 -rw-r--r-- 1 root root 21 5月 20 10:33 nulige.txt [root@template tmp]# cat nulige.txt I am studying linux.

cat高級用法示例:

1、過濾出除liya的字母

[root@template tmp]# cat text.txt 
test
limen
liya

方法一:
[root@template tmp]# cat text.txt|grep -v "liya" text.txt test limen 方法二: [root@template tmp]# head -2 text.txt test limen

6、sed命令

  作用:過濾:sed -n '/過濾的內容/處理的命令' 文件 

     -n  取消sed 的默認輸出

     -p  print打印

     -d  delete刪除

     -i   改變文件內容

1、過濾出除liya的字母

[root@template tmp]# cat text.txt 
test
limen
liya
方法一:
[root@template tmp]# grep -v "liya" text.txt test limen

方法二:
[root@template tmp]# sed -e /^tmp/d text.txt 
test
limen
liya

方法三:
[root@template tmp]# sed -e /^liya/d text.txt 
test
limen

方法四:
[root@template tmp]# sed /liya/d text.txt 
test
limen

5、cp 命令

語法:

cp 源文件  目錄文件(cp 的得要參數apr)

參數:

-a :相當於-pdr

-d :若源文件為鏈接文件(link file),則復制鏈接文件屬性而非檔案本身

-f :強制,右目錄檔案已經存在且無法開啟,則移除后再嘗試

-i: 若目標文件已經存在時,覆蓋時會先詢問

-p:連同檔案的屬性一起復制過去,而非使用默認屬性

-r:遞歸,用於復制目錄

-u:若目標文件存在,則目標文件比源文件舊時才復制

提示:如果源文件是多個,那么目標文件在最后,且是目錄

 

6、date----顯示、修改系統時間

date [options][+format][date]
$ date -s 06/09/2004 修改日期(按月日年格式)
$ date -s 13:56:00 修改時間(按時分秒格式)
$ date -r test 顯示test文件最后一次的修改時間
$ date +'%Y-%m-%d' 以yyyy-mm-dd格式顯示日期,其它格式請參考幫助
$ clock -r 查詢BIOS時間
$ clock -w 把修改后的時間寫回BIOS


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM