Shell中的一些常用指令整理彙總(1)
nohup 命令:不中斷地運行指令
man nohup
查看一下幫助文檔:
NOHUP(1) User Commands NOHUP(1)
NAME
nohup - run a command immune to hangups, with output to a non-tty
SYNOPSIS
nohup COMMAND [ARG]...
nohup OPTION
DESCRIPTION
Run COMMAND, ignoring hangup signals.
--help display this help and exit
--version
output version information and exit
If standard input is a terminal, redirect it from /dev/null. If stan-
dard output is a terminal, append output to ‘nohup.out’ if possible,
‘$HOME/nohup.out’ otherwise. If standard error is a terminal, redirect
it to standard output. To save output to FILE, use ‘nohup COMMAND >
FILE’.
NOTE: your shell may have its own version of nohup, which usually
supersedes the version described here. Please refer to your shell’s
documentation for details about the options it supports.
AUTHOR
Written by Jim Meyering.
可以看出,nohup的作用是,讓後面執行的cmd不受終端關閉或者賬戶退出的影響。如果不指定輸出,那麼默認會把標準輸出爲terminal的部分內容重定向到一個nohup.out
文件,也可以用>
對保存輸出文件進行指定。如果時遠程執行shell指令,且擔心關閉xshell等終端後程序跟着自動停止的,可以實用nohup指令避免這種情況。
& 指令:後臺運行程序
對於不需要交互的且時間較長的程序,可以放在後臺運行,從而空出終端的交互界面。
如:
$ ls &
[1] 30048
anaconda3 anaconda-ks.cfg bash install.log install.log.syslog myinit
[1]+ Done ls --color=auto
對於ls這個指令,後臺運行,給了一個pid號,然後運行結束。
head指令:查看文件前面幾行
首先創建一個test.txt文件:
vim test.txt
寫入如下內容:
123
124
125
通過head命令查看前面2行
head -2 test.txt
123
124
wc : 統計文檔字數/行數/字節數/字符數
仍然是對test.txt,我們利用wc查看文檔的字符數統計信息:
WC(1) User Commands WC(1)
NAME
wc - print newline, word, and byte counts for each file
SYNOPSIS
wc [OPTION]... [FILE]...
wc [OPTION]... --files0-from=F
DESCRIPTION
Print newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified. With
no FILE, or when FILE is -, read standard input.
-c, --bytes
print the byte counts
-m, --chars
print the character counts
-l, --lines
print the newline counts
--files0-from=F
read input from the files specified by NUL-terminated names in file F; If F is - then read names from
standard input
-L, --max-line-length
print the length of the longest line
-w, --words
print the word counts
--help display this help and exit
--version
output version information and exit
實驗一下:
$ wc -lmcw test.txt
3 3 12 12 test.txt
2019-08-01 10:57:20
cat 命令:連接和輸入
cat可以用於將兩個文件concat起來,也可以用於在鍵盤創建一個文件
CAT(1) User Commands CAT(1)
NAME
cat - concatenate files and print on the standard output
SYNOPSIS
cat [OPTION]... [FILE]...
DESCRIPTION
Concatenate FILE(s), or standard input, to standard output.
-A, --show-all
equivalent to -vET
-b, --number-nonblank
number nonempty output lines
-e equivalent to -vE
-E, --show-ends
display $ at end of each line
-n, --number
number all output lines
-s, --squeeze-blank
suppress repeated empty output lines
-t equivalent to -vT
-T, --show-tabs
display TAB characters as ^I
-u (ignored)
-v, --show-nonprinting
use ^ and M- notation, except for LFD and TAB
創建方法如下:
cat > filename.txt
然後鍵盤接入內容,完成後ctrl+d退出。
比如我們創建了test2.txt:
$ head test2.txt
123
124
125
126
127
133
666
-n 標記行號
-s 壓縮多行空行爲一行
-b 只對non-blank非空行編號
$ cat -n test2.txt
1 123
2 124
3 125
4 126
5
6
7 127
8
9 133
10 666
$ cat -b test2.txt
1 123
2 124
3 125
4 126
5 127
6 133
7 666
$ cat -s test2.txt
123
124
125
126
127
133
666
2019-08-01 11:15:26