再來一發,常用Linux的9個命令,隨用隨查,日常積累

df 查詢文件系統掛載點信息

df -hT

df -hi

  • -h 使容量的大小顯示更人性化 -T 顯示文件系統類型

  • -i 顯示inode容量

再來一發,常用Linux的9個命令,隨用隨查,日常積累

```

[root@test ~]# df -hT

Filesystem Type Size Used Avail Use% Mounted on

/dev/xvda2 ext4 39G 1.4G 36G 4% /

devtmpfs devtmpfs 478M 0 478M 0% /dev

tmpfs tmpfs 486M 0 486M 0% /dev/shm

tmpfs tmpfs 486M 49M 437M 11% /run

tmpfs tmpfs 486M 0 486M 0% /sys/fs/cgroup

/dev/xvda1 ext4 477M 148M 300M 34% /boot

tmpfs tmpfs 98M 0 98M 0% /run/user/0

[root@test ~]# df -hi

Filesystem Inodes IUsed IFree IUse% Mounted on

/dev/xvda2 2.5M 43K 2.5M 2% /

devtmpfs 120K 331 120K 1% /dev

tmpfs 122K 1 122K 1% /dev/shm

tmpfs 122K 389 122K 1% /run

tmpfs 122K 13 122K 1% /sys/fs/cgroup

/dev/xvda1 126K 341 125K 1% /boot

tmpfs 122K 1 122K 1% /run/user/0

[root@test ~]#

```

du 命令

單位是KB 如下獲取/tmp下所有文件的大小, 並按降序排列

du -xs /tmp/* | sort -rn -k1

再來一發,常用Linux的9個命令,隨用隨查,日常積累

```

[root@test ~]# du -xs /var/log/*

12/var/log/boot.log

5584/var/log/btmp

240/var/log/cloud-init.log

136/var/log/cron

40/var/log/dmesg

40/var/log/dmesg.old

320/var/log/maillog

424/var/log/messages

4676/var/log/secure

12/var/log/tuned

32/var/log/wtmp

4/var/log/yum.log

[root@test ~]# du -xs /var/log/* | sort -rn -k1

5584/var/log/btmp

4676/var/log/secure

424/var/log/messages

320/var/log/maillog

240/var/log/cloud-init.log

136/var/log/cron

40/var/log/dmesg.old

40/var/log/dmesg

32/var/log/wtmp

12/var/log/tuned

12/var/log/boot.log

4/var/log/yum.log

```

find刪除查找到的文件

find命令經常會用到, -type f 表示只返回文件, -exec 可將已找到的結果作為標準輸入執行其他命令.

```

find -type f -exec rm {} \; # 刪除查找到的文件, 將 path 改為實際要查找的目錄

find [PATH] [option] [action]

-mtime n : n為數字,意思為在n天之前的“一天內”被更改過的文件;

-mtime +n : 列出在n天之前(不含n天本身)被更改過的文件名;

-mtime -n : 列出在n天之內(含n天本身)被更改過的文件名;

-newer file : 列出比file還要新的文件名

例如:

find /root -mtime 0 # 在當前目錄下查找今天之內有改動的文件

與文件權限及名稱有關的參數:

-name filename :找出文件名為filename的文件

-size [+-]SIZE :找出比SIZE還要大(+)或小(-)的文件

-tpye TYPE :查找文件的類型為TYPE的文件,TYPE的值主要有:一般文件(f)、設備文件(b、c)、

目錄(d)、連接文件(l)、socket(s)、FIFO管道文件(p);

-perm mode :查找文件權限剛好等於mode的文件,mode用數字表示,如0755;

-perm -mode :查找文件權限必須要全部包括mode權限的文件,mode用數字表示

-perm +mode :查找文件權限包含任一mode的權限的文件,mode用數字表示

例如:

find / -name passwd # 查找文件名為passwd的文件

find . -perm 0755 # 查找當前目錄中文件權限的0755的文件

find . -size +12k # 查找當前目錄中大於12KB的文件,注意c表示byte

```

xargs 命令

該命令可以將一個命令的輸出作為參數傳遞給另一個命令。

區別於管道符 | 是將將輸出作為另一個命令的標準輸入傳遞.

find /tmp -name *.png -type f | xargs tar -cvzf images.tar.gz

默認是將內容放到參數的最後面, 如果要放到指定位置,需要使用 -i 和 {}

如下所示,將第一個命令的輸出放到 {} 出現的位置

再來一發,常用Linux的9個命令,隨用隨查,日常積累

ls /etc/*.conf | xargs -i cp {} /home/likegeeks/Desktop/out

```

[root@test ~]# find /var/log/ -name *.log -type f | xargs tar -cvzf logs.tar.gz

tar: Removing leading `/' from member names

/var/log/yum.log

/var/log/tuned/tuned.log

/var/log/boot.log

/var/log/cloud-init.log

[root@test ~]# ls logs.tar.gz

logs.tar.gz

[root@test ~]#

[root@test ~]# ls /etc/*.conf | xargs -i cp {} /tmp/xargs.files

[root@test ~]# ls /tmp/xargs.files

/tmp/xargs.files

[root@test ~]#

```

grep查詢文本

在文件中查找字符串(不區分大小寫)

grep -i "the" /etc/hosts

輸出成功匹配的行,以及該行之後的三行. -B 表示前三行. -C 指前後三行

grep -A 3 "localhost" /etc/hosts

在一個文件夾中遞歸查詢包含指定字符串的文件

grep -r "abc" /etc

查找不包含 127 的所有行

grep -v "127" /etc/hosts

再來一發,常用Linux的9個命令,隨用隨查,日常積累

```

[root@test ~]# grep -i "root" /etc/passwd

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

[root@test ~]# grep -A 3 "localhost" /etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

[root@test ~]# grep -r "abc" /etc

/etc/services:abcvoice-port 3781/tcp # ABCvoice server port

/etc/services:abcvoice-port 3781/udp # ABCvoice server port

/etc/services:abcsoftware 3996/tcp # abcsoftware-01

/etc/services:abcsoftware 3996/udp # abcsoftware-01

/etc/services:omabcastltkm 4359/tcp # OMA BCAST Long-Term Key Messages

/etc/services:omabcastltkm 4359/udp # OMA BCAST Long-Term Key Messages

Binary file /etc/udev/hwdb.bin matches

Binary file /etc/selinux/targeted/policy/policy.30 matches

Binary file /etc/selinux/targeted/contexts/files/file_contexts.homedirs.bin matches

/etc/selinux/targeted/contexts/files/file_contexts.homedirs:/home/[^/]+/abc--unconfined_u:object_r:mozilla_home_t:s0

Binary file /etc/selinux/targeted/active/policy.kern matches

Binary file /etc/selinux/targeted/active/modules/100/zabbix/cil matches

/etc/selinux/targeted/active/homedir_template:HOME_DIR/abc--system_u:object_r:mozilla_home_t:s0

Binary file /etc/selinux/targeted/active/policy.linked matches

[root@test ~]# grep -v "127" /etc/hosts

::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

[root@test ~]#

```

tail 查看文件指定行信息

顯示最後3行記錄

tail -3 /var/log/yum.log-20180101

從第770行開始顯示

tail -n +770 /var/log/yum.log-20180101

再來一發,常用Linux的9個命令,隨用隨查,日常積累

```

[root@test ~]# tail -3 /var/log/messages

Jan 23 22:01:01 test systemd: Starting Session 740 of user root.

Jan 23 22:10:01 test systemd: Started Session 741 of user root.

Jan 23 22:10:01 test systemd: Starting Session 741 of user root.

[root@test ~]#

[root@test ~]# tail -n +770 /var/log/messages

Jan 18 06:00:01 test systemd: Stopping User Slice of root.

Jan 18 06:01:01 test systemd: Created slice User Slice of root.

Jan 18 06:01:01 test systemd: Starting User Slice of root.

Jan 18 06:01:01 test systemd: Started Session 94 of user root.

Jan 18 06:01:01 test systemd: Starting Session 94 of user root.

Jan 18 06:01:01 test systemd: Removed slice User Slice of root.

Jan 18 06:01:01 test systemd: Stopping User Slice of root.

Jan 18 06:10:01 test systemd: Created slice User Slice of root.

Jan 18 06:10:01 test systemd: Starting User Slice of root.

Jan 18 06:10:01 test systemd: Started Session 95 of user root.

```

批量創建目錄

mkdir -p new_folder/{folder_1,folder_2,folder_3,folder_4,folder_5}

再來一發,常用Linux的9個命令,隨用隨查,日常積累

```

[root@test ~]# mkdir -p new_folder/{folder_1,folder_2,folder_3,folder_4,folder_5}

[root@test ~]# ls new_folder/

folder_1 folder_2 folder_3 folder_4 folder_5

[root@test ~]#

```

echo 相關命令

顯示當前使用哪個shell

echo $0

-bash

顯示最近一個命令執行的結果碼

echo $?

0

再來一發,常用Linux的9個命令,隨用隨查,日常積累

```

[root@test ~]# echo $0

-bash

[root@test ~]# echo $?

0

[root@test ~]#

```

nohup與標準輸出,標準錯誤輸出

nohup 配和 & 可以讓進程在後臺運行, 如果沒有顯示指定, 默認將標準輸出和錯誤輸出重定向到 nohup.out

1>/dev/null 首先表示標準輸出重定向到空設備文件,也就是不輸出任何信息到終端,不顯示任何信息

2>&1 表示標準錯誤輸出重定向等同於標準輸出,因為之前標準輸出已經重定向到了空設備文件,所以標準錯誤輸出

也重定向到空設備文件

常用使用方式為:

nohup COMMAND >output.log 2>&1 &

再來一發,常用Linux的9個命令,隨用隨查,日常積累

```

[root@test ~]# nohup yum -y install mysql-server > output.log 2>&1 &

[1] 31228

[root@test ~]# ls output.log

output.log

[root@test ~]# cat output.log

nohup: ignoring input

Loaded plugins: fastestmirror, langpacks

Loading mirror speeds from cached hostfile

* base: mirrors.tuna.tsinghua.edu.cn

* epel: mirrors.sohu.com

* extras: mirrors.aliyun.com

* updates: mirrors.tuna.tsinghua.edu.cn

No package mysql-server available.

Error: Nothing to do

[1]+ Exit 1 nohup yum -y install mysql-server > output.log 2>&1

[root@test ~]#


分享到:


相關文章: