Linux Shell學習筆記(1):必須掌握的七大類命令

文章目錄

一、執行幫助文檔命令(man)

常見執行Linux命令的格式是這樣的:

命令名稱 [命令參數] [命令對象]

比如:

find /home -name *.txt 

命令意思很簡單,查找home下文件後綴名是txt的文件

1. man(查看幫助文檔)

main命令用於查看命令的幫助文檔

man ls

運行命令在shell終端就會輸出 ls 命令的文檔信息,包括介紹以及參數等等。

LS(1)                                                                  User Commands                                                                 LS(1)

NAME
       ls - list directory contents

SYNOPSIS
       ls [OPTION]... [FILE]...

DESCRIPTION
       List information about the FILEs (the current directory by default).  Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

       Mandatory arguments to long options are mandatory for short options too.

       -a, --all
              do not ignore entries starting with .

       -A, --almost-all
              do not list implied . and ..

       --author
              with -l, print the author of each file

       -b, --escape
              print C-style escapes for nongraphic characters

       --block-size=SIZE
              scale sizes by SIZE before printing them; e.g., '--block-size=M' prints sizes in units of 1,048,576 bytes; see SIZE format below

       -B, --ignore-backups
              do not list implied entries ending with ~

       -c     with  -lt: sort by, and show, ctime (time of last modification of file status information); with -l: show ctime and sort by name; otherwise:
              sort by ctime, newest first

       -C     list entries by columns

       --color[=WHEN]
              colorize the output; WHEN can be 'always' (default if omitted), 'auto', or 'never'; more info below

       -d, --directory
              list directories themselves, not their contents

輸出內容應該很長,可以使用鼠標滾輪向下滑動,或者是按空格鍵。按q退出幫助文檔
當然,你也可以使用命令 man man查看man命令自己的幫助文檔,這也是可以的。
輸出的幫助文檔有很多,也許不知道從何看起,如上輸出ls命令的部分幫助文檔中,有NAME、SYNOPSIS、DESCRIPTION等大寫英文單詞,具體含義參照下表(包含主要結構名稱,部分命令可能含有結構名稱):

結構名稱 代表含義
NAME 命令名稱
SYNOPSOS 參數的大致使用方法(格式)
DESCRIPTION 介紹
EXAMPLES 演示(例子)
OVERVIEM 默認功能
DEFAULTS 具體的可用選項
OPTIONS 具體可用選項
ENVIRONMENT 環境變量
FILES 用到的文件
SEE ALSO 相關資料
HISTORY 維護歷史與聯繫方式

具體的內容對照相應的輸出會更加形象,你可以在你的虛擬機、linux系統的PC或者是雲端Linux服務器中運行一下查看一下。

二、 常用系統工作命令(echo、date、reboot、poweroff、wget、ps 、top、pidof、kill、killall)

1. echo(輸出命令)

用於在終端輸出字符串或者是變量提取後的值echo [字符串|$變量]

2. date(設置系統時間或者格式)

顯示或者設置系統的時間,格式:date [-選項] [+指定的格式]
具體的選項可以使用man命令進行查看

3. reboot(重啓)

重啓計算機(系統),格式:reboot

4. poweroff(關機)

關閉系統(計算機),格式:poweroff

reboot和poweroff命令都涉及了計算機硬件資源的管理權限,所以必須擁有root權限才能運行這兩個命令

5. wget (從網站中獲取)

個人感覺這個命令算是作用比較大了,wget命令的作用是在終端中下載網絡文件。格式:wget [參數] 網址
|參數|作用 |
|–|--|
| -b | 後臺下載模式 |
| -P| 下載到指定目錄 |
| -t|最大嘗試次數 |
| -c| 斷點續傳 |
| -p|下載頁面所有資源,包括圖片、視頻等 |
| -r | 遞歸下載 |
這些參數不用死記硬背,要麼就是用的多了,自然就記住了,要麼就找點法子,其實參數都是英文單詞的縮寫,後臺下載(background -b),最大嘗試次數(-t times)斷點續傳(-c continue),下載到指定目錄(-P emmmm,我沒想到),遞歸下載(-r recursion)

6. ps(查看系統進程)

查看系統中進程的狀態,格式:ps [參數]
一般ps命令和管道符配合使用效果很好,配合管道符,可以抓取某個指定服務進程相對應的PID碼

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

Linux系統中有五種常見的進程狀態,運行(R)、終端(S)、不可中斷(D)、僵死(Z)、停止(T)

7. top (動態監視負載)

用於動態監視進程活動與負載等信息,格式:top
top命令很強大,能夠動態查看運維狀態,應該算是運維工程師最常用的命令了。
運行效果圖下圖

top - 22:11:24 up 115 days,  1:30,  1 user,  load average: 0.00, 0.00, 0.00
Tasks: 141 total,   1 running, 110 sleeping,   1 stopped,   0 zombie
%Cpu(s):  0.7 us,  0.3 sy,  0.0 ni, 99.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  2041216 total,   247352 free,   516484 used,  1277380 buff/cache
KiB Swap:   969964 total,   969964 free,        0 used.  1330336 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
 1400 gdm       20   0  684840  42492   6292 S  1.0  2.1 693:43.38 gsd-color
 7908 root      10 -10  139708  19616  15160 S  0.7  1.0  65:17.09 AliYunDun
 2324 root      20   0  553632  34128      0 S  0.3  1.7 127:08.69 dockerd
 2371 root      20   0  569724  19356    312 S  0.3  0.9 249:59.16 containerd
26270 root      20   0   41776   3580   2976 R  0.3  0.2   0:00.04 top
    1 root      20   0  160116   7480   4740 S  0.0  0.4   0:58.49 systemd
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.43 kthreadd
    4 root       0 -20       0      0      0 I  0.0  0.0   0:00.00 kworker/0:0H
    6 root       0 -20       0      0      0 I  0.0  0.0   0:00.00 mm_percpu_wq
    7 root      20   0       0      0      0 S  0.0  0.0   0:18.68 ksoftirqd/0
    8 root      20   0       0      0      0 I  0.0  0.0   9:30.99 rcu_sched
    9 root      20   0       0      0      0 I  0.0  0.0   0:00.00 rcu_bh
   10 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0
   11 root      rt   0       0      0      0 S  0.0  0.0   0:16.18 watchdog/0
   12 root      20   0       0      0      0 S  0.0  0.0   0:00.00 cpuhp/0
   13 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kdevtmpfs
   14 root       0 -20       0      0      0 I  0.0  0.0   0:00.00 netns
   15 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_tasks_kthre
   16 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kauditd
   17 root      20   0       0      0      0 S  0.0  0.0   0:03.22 khungtaskd
   18 root      20   0       0      0      0 S  0.0  0.0   0:00.00 oom_reaper
   19 root       0 -20       0      0      0 I  0.0  0.0   0:00.00 writeback
   20 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kcompactd0
   21 root      25   5       0      0      0 S  0.0  0.0   0:00.00 ksmd
   22 root      39  19       0      0      0 S  0.0  0.0   0:09.50 khugepaged
   23 root       0 -20       0      0      0 I  0.0  0.0   0:00.00 crypto
   24 root       0 -20       0      0      0 I  0.0  0.0   0:00.00 kintegrityd
   25 root       0 -20       0      0      0 I  0.0  0.0   0:00.00 kblockd
   26 root       0 -20       0      0      0 I  0.0  0.0   0:00.00 ata_sff
   27 root       0 -20       0      0      0 I  0.0  0.0   0:00.00 md
   28 root       0 -20       0      0      0 I  0.0  0.0   0:00.00 edac-poller
   29 root       0 -20       0      0      0 I  0.0  0.0   0:00.00 devfreq_wq
   30 root       0 -20       0      0      0 I  0.0  0.0   0:00.00 watchdogd
   34 root      20   0       0      0      0 S  0.0  0.0   0:03.24 kswapd0
   35 root       0 -20       0      0      0 I  0.0  0.0   0:00.00 kworker/u3:0

第一行 ,系統時間、運行時間、登錄終端數、系統負載
第二行,進程總數、運行中的進程數,睡眠中的進程數、停止的進程數、僵死的進程數等
第三行,用戶佔用資源白分比、系統內核佔用資源百分比、改變過優先級的進程資源百分比、空閒的資源的百分比

8. pidof(查詢服務的PID)

查詢某個指定服務進程的PID值,格式爲pidof [參數] [服務名稱]

9. kill (終止進程)

終止進程(殺死進程),格式:kill [參數] [進程PID]
但是進程狀態爲D(不可終止)的進程用kill命令也是殺不死的。

10. killall (終止指定文件名稱的服務)

用於終止某個指定名稱的服務所對應的全部進程,格式 killall [參數] [進程名稱]

kill和killall 可以理解爲 emmmm,重點狙擊和火箭覆蓋。

三、系統狀態檢測命令(ifconfig、unname、uptime、free、who、last、history、sosreport)

1. ifconfig(獲取網卡配置與網絡狀態信息)

ifconfig命令用於獲取網卡配置與網絡狀態信息,格式爲:ifconfig 【網絡設備】 [參數]

2. uname(查看系統信息)

查看系統內核和系統版本等信息,具體的參數可以藉助man命令查看。

3. uptime(查看系統負載)

查看系統負載信息,格式:uptime

4. free (顯示當前系統內存信息)

顯示當前系統中內存的使用信息
格式:free [-h]

這個命令可以時刻查看系統的內存使用情況,注意觀察內存的使用,避免服務器因爲內存耗盡而出現宕機狀態

5. who(查看登錄用戶信息)

查看當前登入主機的用戶端信息
格式:who

6. last(查看系統登錄記錄)

查看所有系統的登錄記錄last [參數]

如果是多用戶的話,這個命令可以輸出登錄系統的用戶信息,包括用戶名和用戶登錄事件等信息

root     pts/0        140.206.249.253  Tue Apr 28 19:41   still logged in
root     pts/0        140.206.249.253  Mon Apr 27 22:13 - 01:37  (03:24)
root     pts/0        140.206.249.253  Sun Apr 26 20:12 - 00:52  (04:40)
root     pts/0        140.206.249.253  Fri Apr 24 22:17 - 01:30  (03:12)
root     pts/0        223.167.213.117  Sun Apr 19 12:49 - 22:02  (09:12)
root     pts/0        223.167.213.117  Sun Apr 12 15:15 - 16:05  (00:50)
gw       pts/0        223.167.213.117  Sun Apr 12 12:51 - 15:14  (02:22)
root     pts/0        223.167.213.117  Sun Apr 12 12:24 - 12:48  (00:23)
root     pts/1        223.167.213.117  Sat Apr 11 14:49 - 20:01  (05:11)
gw       pts/1        140.206.248.74   Sat Apr 11 11:45 - 11:46  (00:00)
root     pts/0        140.206.248.74   Sat Apr 11 11:41 - 14:52  (03:11)

7.history命令(歷史執行命令集合)

顯示歷史執行過的命令。
history [-c]

歷史命令會被保存到用戶家目錄中的。bash_history文件中。Linux系統中以(.)開頭的文件代表隱藏文件(ls -a 可以查看隱藏文件),這些文件大多是爲系統服務文件

8. sosreport命令(手機系統配置及架構並輸出診斷文檔)

用於收集系統配置及架構信息並輸出診斷文檔,格式爲:sosreport

這個命令算是比較有用的了,如果你的服務器出現了問題或者故障,需要技術人員遠程維修的話,一般是先執行這個命令,輸出診斷信息,輸出的信息會保存到linux下的對應目錄中,同時輸出報告的位置、名稱、解壓密碼,然後你就可以發給維修人員或者自己看看到底是哪裏出了問題。(其實命令很好記,sos+report 求救報告。。。)

如果你運行了 sosreport 命令。而你的服務器顯示

Command 'sosreport' not found

那就是你還沒有安裝sosreport,使用命令apt install sosreport

root@我的服務器地址怎麼能告訴你們呢:~# sosreport

sosreport (version 3.9)

This command will collect system configuration and diagnostic
information from this Ubuntu system.

For more information on Canonical visit:

  https://www.ubuntu.com/

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 the case id that you are generating this report for []: test1

 Setting up archive ...
 Setting up plugins ...
[plugin:networking] skipped command 'nft list ruleset': required kmods missing: nf_tables.
[plugin:networking] skipped command 'ip -s macsec show': required kmods missing: macsec. Use '--allow-system-changes' to enable collection.
[plugin:networking] skipped command 'ss -peaonmi': required kmods missing: udp_diag, netlink_diag, tcp_diag, unix_diag, inet_diag, af_packet_diag. Use '--allow-system-changes' to enable collection.
[plugin:sar] sar: could not list /var/log/sysstat/*
 Running plugins. Please wait ...

  Starting 1/71  acpid           [Running: acpid]                                 Starting 2/71  anacron         [Running: anacron]                               Starting 4/71  apport          [Running: apport]                                Starting 3/71  apparmor        [Running: apport apparmor]                       Starting 6/71  ata             [Running: apport apparmor ata]                   Starting 5/71  apt             [Running: apport apparmor apt]                   Starting 7/71  block           [Running: apport apparmor apt block]             Starting 8/71  boot            [Running: apparmor apt block boot]               Starting 9/71  ceph            [Running: apt block boot ceph]                   Starting 10/71 cgroups         [Running: apt block ceph cgroups]                Starting 11/71 chrony          [Running: apt ceph cgroups chrony]               Starting 12/71 cloud_init      [Running: apt ceph chrony cloud_init]            Starting 13/71 cron            [Running: apt ceph chrony cron]                  Starting 14/71 crypto          [Running: apt ceph chrony crypto]                Starting 15/71 cups            [Running: apt ceph chrony cups]                  Starting 16/71 date            [Running: ceph chrony cups date]                 Starting 17/71 dbus            [Running: ceph chrony cups dbus]                 Starting 18/71 devicemapper    [Running: ceph chrony cups devicemapper]         Starting 19/71 devices         [Running: ceph chrony cups devices]              Starting 20/71 dpkg            [Running: ceph cups devices dpkg]                Starting 21/71 ebpf            [Running: ceph cups dpkg ebpf]                   Starting 22/71 filesys         [Running: ceph cups ebpf filesys]                Starting 23/71 fwupd           [Running: ceph cups filesys fwupd]               Starting 24/71 gdm             [Running: ceph cups fwupd gdm]                   Starting 25/71 grub2           [Running: ceph fwupd gdm grub2]                  Starting 26/71 hardware        [Running: ceph gdm grub2 hardware]               Starting 27/71 host            [Running: ceph gdm hardware host]                Starting 28/71 i18n            [Running: ceph gdm host i18n]                    Starting 29/71 kernel          [Running: ceph gdm host kernel]                  Starting 30/71 libraries       [Running: ceph gdm kernel libraries]             Starting 31/71 libvirt         [Running: ceph gdm kernel libvirt]               Starting 32/71 login           [Running: ceph gdm kernel login]                 Starting 33/71 logrotate       [Running: ceph kernel login logrotate]           Starting 34/71 logs            [Running: ceph kernel login logs]                Starting 35/71 lvm2            [Running: ceph kernel logs lvm2]                 Starting 36/71 md              [Running: ceph kernel logs md]                   Starting 37/71 memory          [Running: ceph kernel logs memory]               Starting 38/71 multipath       [Running: ceph kernel logs multipath]            Starting 39/71 mysql           [Running: ceph kernel logs mysql]                Starting 40/71 networking      [Running: ceph kernel logs networking]           Starting 41/71 networkmanager  [Running: kernel logs networking networkmanager  Starting 42/71 nginx           [Running: logs networking networkmanager nginx]  Starting 43/71 nscd            [Running: logs networking nginx nscd]            Starting 44/71 ntp             [Running: logs networking nginx ntp]             Starting 45/71 openssl         [Running: logs networking nginx openssl]         Starting 46/71 pam             [Running: logs networking nginx pam]             Starting 47/71 pci             [Running: logs networking nginx pci]             Starting 48/71 perl            [Running: logs networking nginx perl]            Starting 49/71 ppp             [Running: logs networking nginx ppp]             Starting 50/71 procenv         [Running: logs networking nginx procenv]         Starting 51/71 process         [Running: logs networking nginx process]         Starting 52/71 processor       [Running: logs networking process processor]     Starting 53/71 python          [Running: logs networking process python]        Starting 54/71 release         [Running: logs networking python release]        Starting 55/71 sar             [Running: logs networking python sar]            Starting 56/71 scsi            [Running: logs networking python scsi]           Starting 57/71 services        [Running: logs networking python services]       Starting 58/71 snappy          [Running: logs networking python snappy]         Starting 59/71 soundcard       [Running: logs python snappy soundcard]          Starting 60/71 ssh             [Running: logs snappy soundcard ssh]             Starting 61/71 sudo            [Running: logs snappy soundcard sudo]            Starting 62/71 system          [Running: logs snappy soundcard system]          Starting 63/71 systemd         [Running: logs snappy soundcard systemd]         Starting 64/71 systemtap       [Running: logs snappy systemd systemtap]         Starting 65/71 sysvipc         [Running: logs snappy systemd sysvipc]           Starting 66/71 ubuntu          [Running: logs snappy systemd ubuntu]            Starting 67/71 udev            [Running: logs systemd ubuntu udev]              Starting 68/71 usb             [Running: logs systemd ubuntu usb]               Starting 69/71 wireless        [Running: logs systemd ubuntu wireless]          Starting 70/71 x11             [Running: logs systemd ubuntu x11]               Finishing plugins              [Running: systemd ubuntu x11]                    Starting 71/71 xfs             [Running: systemd ubuntu x11 xfs]                Finishing plugins              [Running: systemd ubuntu x11]                    Finishing plugins              [Running: ubuntu x11]                            Finishing plugins              [Running: x11]                                 
  Finished running plugins                                                      
Creating compressed archive...

Your sosreport has been generated and saved in:
  /tmp/sosreport-我的服務器地址怎麼能告訴你們呢-test1-2020-04-28-zsupnaz.tar.xz

 Size   3.89MiB
 Owner  root
 md5    f13ddac3ebb349456162a71b3f9e36d3

Please send this file to your support representative.

最後幾行就是 生成的報告的位置和名稱等。這個命令可以說非常好用了。

四、工作目錄切換命令(pwd、cd、ls)

工作目錄: 用戶當前在系統中的位置。
這一部分的命令是使用頻率最高的命令,幾乎你進入shell終端你救護用到。

1. pwd(顯示當前工作目錄)

顯示用戶當前所處的工作目錄 格式pwd 【參數】

使用man 命令查看,可以看到pwd 命令的詳細信息

PWD(1)                           User Commands                          PWD(1)

NAME
       pwd - print name of current/working directory

SYNOPSIS
       pwd [OPTION]...

DESCRIPTION
       Print the full filename of the current working directory.

       -L, --logical
              use PWD from environment, even if it contains symlinks

       -P, --physical
              avoid all symlinks

       --help display this help and exit

       --version
              output version information and exit

       If no option is specified, -P is assumed.

       NOTE:  your shell may have its own version of pwd, which usually super‐
       sedes the version described here.  Please refer to your  shell's  docu‐
       mentation for details about the options it supports.

AUTHOR
       Written by Jim Meyering.

REPORTING BUGS
       GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
       Report pwd translation bugs to <http://translationproject.org/team/>

COPYRIGHT
       Copyright  ©  2017  Free Software Foundation, Inc.  License GPLv3+: GNU
       GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
       This is free software: you are free  to  change  and  redistribute  it.
       There is NO WARRANTY, to the extent permitted by law.

2. cd(切換工作目錄)

這個不用說了,切換工作路徑

3. ls(顯示目錄中的問價信息)

顯示目錄中的文件信息
格式ls [參數]
這個命令最常用的參數有a(詳細,顯示文家大小和隱藏文件)、l(list顯示) 等

具體的詳細信息,老樣子,自己動手運行一下man ls瞅一眼~~

五、文本文件編輯命令(cat、more、head、tail、tr wc、stat、cut、diff、)

1. cat (輸出純文本文檔內容【文檔較小】)

查看純文本文件,當然是內筒較少的文件,不然就會出現屏幕一閃而過,而你啥都沒看到的狀況
格式:cat 選項 文件名
一般使用這個命令我會加上一個-n的參數用於顯示行號

2. more(輸出純文本文檔內容【文檔較大】)

cat 命令查看純文本文件(內容較多的),格式爲more [選項] 文件

閱讀配置文件的時候一般使用more命令,當然,你要是閒得DT來度長篇小說,more命令也適合你。它會在屏幕下端給你顯示一個百分比的文檔閱讀進度

3. head(查看文檔前n行)

查看純文本文檔的前n行,格式爲head [選項] [文件]

head -n 20 文件

這個命令就是查看文件的前20行內容。

4. tail(查看文檔後n行)

tail 命令用於查看純文本文檔的後N行或持續更新的內容,格式tail [選項] [文件]

tail -n 20 文件

查看文件的後20行內容

tail -f 文件名
tail -f /var/message

實時查看最新日誌文件

5. tr (替換目標文件中的字符)

替換目標文件中的字符,
格式 tr [原始字符] [目標字符]

cat a.txt | tr [a-z] [A-Z]

上面命令 的意思是打開a.txt 文件,替換其中的小寫字母爲大寫字母,( | )是Linux中非常重要的一個符號**“管道符”** 可以理解非一根管道,左邊輸出的內筒通過管道符輸入到右邊,作爲右邊命令的執行參數

6. wc(統計文件)

統計文本的行數、字數、字節數
格式:wc [參數] 文本

我一般把這個命令和find命令組合使用,用來查看文件夾中相應文件的數量

find [文件夾路徑] -name [模糊匹配的文件名,如:*.txt] | wc -l

運行命令會輸出一個數字,就是你要找的這個類型的文件在目標文件夾中的數量

7. stat(查看文件具體信息)

查看文件的具體信息存儲信息,格式爲stat 文件名稱

root@~~~~~~:/home/test# stat a.txt
  File: a.txt
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fc01h/64513d    Inode: 820406      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-04-28 22:12:46.252720683 +0800
Modify: 2020-04-28 22:12:46.252720683 +0800
Change: 2020-04-28 22:12:46.252720683 +0800
 Birth: -

8. cut(提取文本字符)

cut 命令用於按"列"提取文本字符
格式cut [參數] 文本

9. diff(比較多個文本文件的差異)

用於比較多個文本文件的差異
格式爲 diff [參數] 文件

可以使用–brief 參數來確認兩個文件是否相同,還可以使用-c參數來詳細比較多個文件的差異之處,
檢測文件是否被篡改的常用手段

diff --brief a.txt b.txt

如果兩份文件內容是一樣的,則不會輸出任何東西,如果不同,則如下輸出:

Files a.txt and b.txt differ

來試試參數-c

*** a.txt       2020-04-28 22:22:20.693747186 +0800
--- b.txt       2020-04-28 22:23:49.573906014 +0800
***************
*** 1 ****
! ▒▒▒▒һ▒▒▒▒▒▒ļ▒▒▒a
\ No newline at end of file
--- 1 ----
! ▒▒▒▒һ▒▒▒▒▒▒ļ▒▒▒b
\ No newline at end of file

上述命令可以通過管道符和其他命令結合使用,看你自己的了~~

六、文件目錄管理命令(touch、mkdir、cp、mv、rm、dd、file)

1. touch(創建、設置文件命令)

touch 命令用於創建空白文件或者設置文件的時間
格式:touch [選項] [文件]

參數 作用
-a 修改讀取時間
-m 修改“修改時間”
-d 同時修改atime和mtime
ls -l a.txt
-rw-r--r-- 1 root root 19 Apr 28 22:22 a.txt

然後向文件中輸入一行字符串(修改時間)

echo "change mtime " > a.txt

再次查看時間發現文件的修改時間已經改變。

 ls -l a.txt
 -rw-r--r-- 1 root root 14 Apr 29 23:30 a.txt

但是黑客不想讓別人知道他在這個時候修改了這個文件,然後黑客可以運行touch命令,變更文件修改時間。
把文件的修改時間偷偷修改成原來的時間, 事了拂衣去,深藏功與名~~~

touch -d "2020-4-28 22:22" a.txt
 ls -l a.txt
-rw-r--r-- 1 root root 14 Apr 28 22:22 a.txt

2. mkdir(創建文件夾)

用於創建空白的目錄。
格式:mkdir [選項] 目錄


mkdir -p a/b/c

-p參數可以創建具有嵌套疊層關係的目錄

3. cp(拷貝)

用於拷貝文件,也是一個比較常用的命令
格式:cp [參數] 源文件 目標文件

  • 目標文件是目錄(文件夾),怎將源文件拷貝到目標文件夾下
  • 目標文件是普通文件,詢問是否要覆蓋目標文件
  • 目標文件不存在,正常拷貝
參數 作用
-p 保留原始文件的屬性
-d 如果目標文件時鏈接文件,則保留連接文件的屬性
-r 遞歸持續複製(用於目錄)
-i 若目標文件存在,則詢問是否覆蓋
-a -pdr

4. mv (移動文件)

用於剪切 複製 文件
格式mv 【目標問路徑|目標文件名】
同一個目錄對同一個文件使用mv命令相當於對文件進行重命名

5. rm(刪庫跑路~必備命令)

這個命令用於刪除文件。
格式:rm [參數] [文件]

linux系統刪除文件時候,會詢問是否刪除。如果不想老是詢問了,可以使用參數 -f 強制刪除,如果要刪除一個目錄 則使用 -r

rm -rf *

這個命令就厲害了,成員的刪庫跑的梗就是這麼來的。意思是刪除工作目錄想啊任何文件、文件夾
所以使用這個命令要慎重小心點,不然你都能把“系統刪除”嘍

6. dd(按指定數據塊,複製\轉換 文件)

用戶按照指定大小和個數的數據塊複製文件或轉換文件
格式:dd [參數]

這個命令有意思了。一般運行這個命令首先必須知道Linux中一個名爲/dev/zero 的設備文件**(不佔系統存儲空間,但是可以提供無窮無盡的數據)**因此可以使用它作爲dd命令的輸入文件

參數 作用
if 輸入文件的名稱
of 輸出文件的名稱
bs 設置每個塊的大小
count 設置要複製的塊的個數
dd if=/dev/zero of=file_100 count=1 bs=100M

命令的含義是從/dev/zero 中取出一個大小爲100M的數據塊,然後保存爲file_100文件

 dd if=/dev/zero of=10_file count=1 bs=10M
 
1+0 records in
1+0 records out
10485760 bytes (10 MB, 10MiB) copied, 0.0122317 s, 857 MB/s
total 10256
drwxr-xr-x 2 root root     4096 Apr 30 00:20 ./
drwxr-xr-x 4 root root     4096 Apr 24 22:19 ../
-rw-r--r-- 1 root root 10485760 Apr 30 00:20 10_file
-rw-r--r-- 1 root root       14 Apr 28 22:22 a.txt
-rw-r--r-- 1 root root       19 Apr 28 22:23 b.txt
-rw-r--r-- 1 root root        0 Apr 29 23:22 test.py
ls -lh 10_file
-rw-r--r-- 1 root root 10M Apr 30 00:20 10_file

7. file(查看文件類型)

用於查看文件類型
格式file 文件名

file 10_file
10_file: data

七、打包壓縮與搜索命令(tar、grep、find)

1. tar(解壓縮)

tar命令用於對文件進行打包壓縮或者解壓
格式:tar [選項] [文件]

參數很多,用man命令就可以查看具體參數和相關信息

2. grep(搜索)

在文本中執行關鍵字搜索,平顯示匹配的結果
格式:grep [選項] [文件]
|參數| 含義 |
|–|--|
|-b | 將可執行文件(binary)當做文本文件來搜索 |
| -c | 僅顯示找到的行數|
| -i | 忽略大小寫|
| -n| 顯示行號|
| -v| 反向選擇——僅列出沒有“關鍵詞的行”|

3. find(查找文件或文件夾)

用於按照指定條件來查找文件,格式爲“find [查找路徑] 尋找條件 操作”

find /home -name *.txt | wc -l

查找home目錄下一共有多少txt文件

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章