Linux shell常用的73條指令總結

高洛峰
發布: 2023-03-05 14:50:02
原創
1688 人瀏覽過

前言

使用Linux shell是一些程式設計師每天的基本工作,但我們常常忘記一些有用的shell指令和技巧。當然,命令我能記住,但我不敢說我能記得如何用它執行某個特定任務。要注意一點的是,有些用法需要在你的Linux系統裡安裝額外的軟體。下面話不多說了,來看看詳細的內容吧。

檢查遠程端口是否對bash開放:

echo >/dev/tcp/8.8.8.8/53 && echo "open"

讓進程轉入後台:

Ctrl + z

將進程轉到前台:

fg

產生隨機的十六進位數,其中n是字元數:

openssl rand -hex n

在目前shell/home/ user/file.name


截取前5個字元:

${variable:0:5}


SSH debug 模式:

ssh -vvvv userip_address user@ip_address -i key.pem


用wget抓取完整的網站目錄結構,存放到本地目錄中:

wget -r --no-parent --reject "index.html*" http:// hostname/ -P /home/user/dirs


一次建立多個目錄:

mkdir -p /home/user/{test,test1,test2}


列出包含子進程的進程樹:

ps axwef

建立war 檔案:

jar -cvf name.war file

測試硬碟寫入速度:

dd if=/dev/zero of=/tmp/output.imcountg bs=8k ; rm -rf /tmp/output.img

測試硬碟讀取速度:

hdparm -Tt /dev/sda

取得文字的md5 hash:

echom -檢查xml格式:


xmllint --noout file.xml

將tar.gz提取到新目錄:


tar zxvf package.tar.gz -C new_dir

curl -I http://www.example.com

修改檔案或目錄的時間戳(YYMMDDhhmm):

touch -t 0712250000 file

用wget指令執行//username:password@hostname

產生隨機密碼(例子裡是16個字元長):

LANG=c

快速備份一個檔案:

cp some_file_name{,.bkp}

存取Windows共用目錄:

smbclient -U "MAIN
存取Windows共用目錄:

test/dir

執行歷史記錄裡的命令(這裡是第100行):

!100

解壓縮:

unzip package_name.zip -d dir_namedndone ):

cat > test.txt


建立空檔案或清空一個現有檔案:

> test.txt


與Ubuntu NTP server同步時間:

netstat顯示所有tcp4監聽埠:

netstat -lnt4 | awk '{print $4}' | cut -f2 -d: | grep -o '[0-9]*'

qcow2鏡像檔轉換:

-img convert -f qcow2 -O raw precise-server-cloudimg-amd64-disk1.img precise-server-cloudimg-amd64-disk1.raw


重複運行文件,顯示其輸出(缺省是2秒一次):

watch ps -ef


所有使用者清單:

getent passwd


Mount root in read/write mode:

mount -o remount,rw /

情況):


mount --bind /source /destination

動態更新DNS server:


nsupdate

列出前10個最大的文件:

lsof / | awk '{ if($7 > 1048576) print $7/1048576 "MB "$9 }' | sort -n$n }' u | tail

顯示剩餘記憶體(MB):

free -m | grep cache | awk '/[0-9]/{ print $4" MB" }'

打開Vim並跳到檔案結束:

vim + some_file_name

Git 克隆指定分支(master):

git clone git@github.com:name/app.git -b master

develop


Git 刪除分支(myfeature):

git branch -d myfeature


Git 刪除遠端分支

git push origin :branchName塞origin mynewfeature


印刷歷史記錄中最後一次cat指令:

!cat:p


運行歷史記錄裡最後一次cat指令:

!cat


/home:/

find /home/user -maxdepth 1 -type d -empty


取得test.txt檔案中第50-60行內容:


一個指令(如果最後一個指令是mkdir /root/test, 下面將會運行: sudo mkdir /root/test):

sudo !!


建立臨時RAM檔案系統– ramdisk (先建立/tmpram目錄):

mount -t tmpfs tmpfs /tmpram -o size=512m


Grep whole words:

grep -w "name" test.txt


Append text to a file when privileges need to be elevated:

echo "some text" | sudo tee -a /path/file

List all kill Signal parameter:

kill -l

Disable recording of the last session in bash history:

kill -9 $$

Scan the network for open ports:

nmap -p 8081 172.20.0.0/16

Set git email:

git config --global user. email "me@example.com"

To sync with master if you have unpublished commits:

git pull --rebase origin master

will All files containing "txt" in their file names are moved into the /home/user directory:

find -iname "*txt*" -exec mv -v {} /home/user \;

Display files side by side:

paste test.txt test1.txt

Progress bar in shell:

pv data.log

Use netcat to send data to Graphite server:

echo "hosts.sampleHost 10 `date +%s`" | nc 192.168.200.2 3000

Convert tabs to spaces:

expand test.txt > test1.txt

Skip bash history:

< space >cmd

Go to the previous working directory:

cd -

Split the large tar.gz files (100MB each) and merge them back:

split –b 100m /path/to/large/archive /path/to/output/files cat files* > archive

Use curl to get HTTP status code:

curl -sL -w "%{http_code}\\n" www.example.com -o /dev/null

Set root password to strengthen MySQL security installation:

/usr/bin/mysql_secure_installation

When Ctrl + c does not work:

Ctrl + \

Get the file owner:

stat -c %U file.txt

block device list:

lsblk -f

Find files with spaces at the end of their file names:

find . -type f -exec egrep -l " +$" {} \;

Find the files whose file names have tab indentation

find . -type f -exec egrep -l $'\t' {} \;

Use "=" to print out horizontal lines: select all and copy them into notes

printf '%100s\n' | tr ' ' =

Summary

The above is the entire content of this article. I hope the content of this article can bring some help to everyone's study or work. And convenient, if you have any questions, you can leave a message to communicate.

For more related articles summarizing the 73 commonly used commands in Linux shell, please pay attention to the PHP Chinese website!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板