shell--常用命令

用戶相關

踢除登錄用戶

pkill -kill -t pts/1

殺掉某個用戶所有進程

如殺掉test用戶所有進程

pkill -kill -u test

修改用戶密碼
方式一:

echo "password" | passwd testuser --stdin >/dev/null

方式二:

echo testuser:password | chpasswd

系統設置

釋放緩存

sync; echo 3 > /proc/sys/vm/drop_caches

設置提示符

export PS1='\u@\h \w \$ '

測試方法

創建指定大小的文件

dd if=/dev/zero of=test bs=1M count=1000

修改文件的時間

如把文件時間改爲3天前

touch -d "3 days ago" test_file

文本處理

顯示前k行內容

head -n k test

顯示末尾k行內容

tail -n k test

顯示除最後k行內容

head -n -k test

從第k行開始顯示(去掉前k-1行)

tail -n +k test

sed命令

在第一行前添加字符串AAA

sed -i '1i AAA' file_path

在最後一行添加字符串AAA

sed -i '$a AAA' file_path

刪除匹配行
如刪除文件中以#號開始的行

sed -i '/^#/d' file_path

grep命令

grep匹配多個

grep -iE "error|fialed"

查找包含某個字符串的所有文件

grep -R some_str

find命令

查找包含特定內容的文件

find . -type f | xargs grep some_str

find結合sed進行批量操作
批量修改,把當前目錄下所有文件中的A替換成B

find . -type f | xargs sed -i "s/A/B/g"

批量刪除

find . -type f | xargs sed -i "s/some_str/d"

字符串截取

舉例變量file="modify_suffix.sh.tar.gz"

從右側開始匹配".",保留左側到第一次匹配部分

echo "${file%.*}"

結果爲:modify_suffix.sh.tar

從右側開始匹配".",保留左側到最後一次匹配部分

echo "${file%%.*}"

結果爲:modify_suffix

從左側開始匹配".",保留右側到第一次匹配部分

echo "${file#*.}"

結果爲:sh.tar.gz

從左側開始匹配".",保留右側到最後匹配部分

echo "${file##*.}"

結果爲:gz

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