shell實戰命令全集

shell 實戰命令全集

cat:將文件或標準輸入轉化爲標準輸出

	1、一次顯示整個文件。     cat   filename
	2、創建一個文件,只能創建新文件不能編輯新文件。   cat > filename
	3、將幾個文件合併爲一個文件。     cat  file1  file2  > file
	退出cat    ctrl+c   或 ctrl+d        或者控制檯輸入 control -D        control -C

cd:切換目錄

	切換目錄    cd dirname
	cd   進入主目錄
	cd ~  進入用戶主目錄
	cd -  返回進入此目錄之前所在的目錄
	cd .. 返回上級
	cd ../.. 返回上兩級目錄
	cd !$  把上個命令的參數作爲cd參數使用

ls:顯示文件和目錄的信息

	顯示文件和目錄的信息
	ls 以默認方式顯示當前目錄文件列表
	ls -a 顯示所有文件包括隱藏文件
	ls -l    或 ll    顯示文件屬性,包括大小,日期,符號連接,是否可讀寫及是否可執行
	ls -lc    列出文件的最後更改時間
	ls -lu    列出文件的最後訪問時間
	ls -lh 顯示文件的大小,以容易理解的格式印出文件大小 (例如 1K 234M2G)
	ls -lt 顯示文件,按照修改時間排序

cp:拷貝文件

	拷貝文件
	cp source target 將文件source複製爲target
	cp /root/source . 將/root下的文件source複製到當前目錄
	cp –av soure_dir target_dir 將整個目錄複製,兩目錄完全一樣

rm:刪除文件或目錄

	刪除文件或目錄
	rm file 刪除某一個文件
	rm -f file 刪除時候不進行提示。可以於r參數配合使用
	rm -rf dir 刪除當前目錄下叫dir的整個目錄
	rm -rf !(file1|file2|file3) 刪除除file1 file2 file3 的所有文件

mv:將文件移走或改名

	將文件移走或改名
	mv source target 將文件source更名爲target
	mv text.txt text.log    改名
	mv text.txt  rodger/file   移走
	命令參數:
			-b :若需覆蓋文件,則覆蓋前先行備份。 
			-f :force 強制的意思,如果目標文件已經存在,不會詢問而直接覆蓋;
			-i :若目標文件 (destination) 已經存在時,就會詢問是否覆蓋!
			-u :若目標文件已經存在,且 source 比較新,纔會更新(update)
		    -t : --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY,即指定mv的目標目錄,該選項適用於移動多個源文件到一個目錄的情況,此時目標目錄在前,源文件在後。

sz:下載文件 將選定的文件發送到本地機器

	下載文件   將選定的文件發送到本地機器
	sz filename 

rz:上傳文件

	上傳文件	  運行該命令會彈出一個文件選擇窗口,從本地選擇文件上傳到linux服務器
	rz
	-a, –ascii  
	-b, –binary 用二進制的方式上傳下載,不解釋字符爲 ascii
	-e, –escape 強制escape 所有控制字符,比如 Ctrl+x,DEL 等
	-ary –o-sync
	-a 表示使用ascii碼格式傳輸文件,如果是Dos格式的文件,會轉換爲unix格式
	-r 使用 Crash recovery mode. 即文件傳輸中斷會重傳
	-y 表示文件已存在的時候會覆蓋
	–o-sync 採用同步寫模式,以處理從緩存寫到磁盤時中斷丟失的情況

chomd:改變用戶的權限

	改變用戶的權限
	chmod a+x file  將file文件設置爲可運行
	chmod 666 file 將文件file設置爲可讀寫
	chmod 750 file 將文件file設置爲,所有者爲完全權限,同組可以讀和執行,其他無權限

touch:修改文件時間戳或者新建一個不存在的文件

	修改文件時間戳或者新建一個不存在的文件
		-a 改變檔案的讀取時間記錄。
	   -m 改變檔案的修改時間記錄。
	   -c 假如目的檔案不存在,不會建立新的檔案。與 --no-create 的效果一樣。
	    -h ,不干擾引用 影響每個符號鏈接,而不是所有參考文件(只適用於系統的改變一個符號,時間戳)
	   -f 不會執行實際操作,是爲了與其他 unix 系統的相容性而保留。
	   -r 使用參考檔的時間記錄,與 --file 的效果一樣。
	   -d 設定時間與日期,可以使用各種不同的格式。
	   -t 設定檔案的時間記錄,格式與 date 指令相同。[[CC]YY]MMDDhhmm[.SS],CC爲年數中的前兩位,即”世紀數”;YY爲年數的後兩位,即某世紀中的年數.如果不給出CC的值,則linux中touch命令參數將把年數CCYY限定在1969--2068之內.MM爲月數,DD爲天將把年數CCYY限定在1969--2068之內.MM爲月數,DD爲天數,hh 爲小時數(幾點),mm爲分鐘數,SS爲秒數.此處秒的設定範圍是0--61,這樣可以處理閏秒.這些數字組成的時間是環境變量TZ指定的時區中的一個時間.由於系統的限制,早於1970年1月1日的時間是錯誤的.
	touch file    創建file文件
	touch -t 201812262315.34 b.txt 指定時間創建文件

vim:文本編輯器

	文本編輯器,有三種模式,相互切換如下
				命令模式——>編輯模式:“i a o I A O”
				編輯模式——>命令模式:“ESC”
				命令模式——>末行模式:“: /  ?”(:與/用於搜索
	命令模式:
			複製:yy——複製一行,nyy——複製n行
			剪切/刪除:dd——刪除/剪切一行,ndd——刪除/剪切n行
			刪除一個字符:x
			替換:r——替換一個字符(如r a表示把r替換爲a),R——持續替換
			粘貼:p
			撤銷一個操作:u
			移動光標到文件末尾:G
			移動光標到文件首行:gg
			移動光標到文件第n行:nG或:n
			不能同時在兩個終端用vim命令打開同一個文件,需將打開的一個vi命令文件關閉後才能打開另一個,否則另一個文件保存不了。

	編輯模式:
			i :在當前光標前面插入數據
			a :在當前光標之後插入數據
			I :在當前光標行頭開始插入數據
			A :在當前光標行末開始插入數據
			o :在當前光標所在下一行增加一個新行
			O :在當前光標所在上一行增加一個新行(空行)

	末尾模式:
			保存文件不退出vi :          ":w"
			保存文件並退出 :        “:wq”
			退出 :                    ":q"
			不保存退出:                “:q!"
			顯示行號:                   “:set nu”
			 取消行號:                  “:set nonu”
			跳轉到第n行:              “:n”

less :分屏查看文件內容

	分屏查看文件內容
	語法  less [參數] 文件
		參數:
				-b <緩衝區大小> 設置緩衝區的大小
				-e  當文件顯示結束後,自動離開
				-f  強迫打開特殊文件,例如外圍設備代號、目錄和二進制文件
				-g  只標誌最後搜索的關鍵詞
				-i  忽略搜索時的大小寫
				-m  顯示類似more命令的百分比
				-N  顯示每行的行號
				-o <文件名> 將less 輸出的內容在指定文件中保存起來
				-Q  不使用警告音
				-s  顯示連續空行爲一行
				-S  行過長時間將超出部分捨棄
				-x <數字> 將“tab”鍵顯示爲規定的數字空格
	
	退出:  q 或 Q  或 ZZ
	
	常用操作 :
		1、/字符串   :向下搜索"字符串"
		2、?字符串   :向上搜索"字符串"
		3、n        	:重複前一個搜索
		4、N         :向上重複前一個搜索
		5、b         :向後翻一頁
		6、空格       :滾動一頁
		7、回車       :滾動一行
		8、pageDown	:向下翻一頁
		9、pageUp	:向上翻一頁
		10、G		:移到最後一行
		11、g         :移到第一行

stat :顯示文件的詳細信息

	顯示文件的詳細信息,包括inode 、 atime 、mtime 、ctime
	stat  [參數]  文件或目錄
	參數 :  
		-f     顯示文件系統的信息
		-t     以簡潔的方式輸出
		Access	訪問時間
		Modify	修改時間
		Change  狀態修改時間

more:查看文件內容

	查看文件內容,一頁一頁的顯示
	參數 :
		-num 一次顯示的行數 
		-d 提示使用者,在畫面下方顯示 [Press space to continue, 'q' to quit.] ,如果使用者按錯鍵,則會顯示 [Press 'h' for instructions.] 而不是 '嗶' 聲 
		-l 取消遇見特殊字元 ^L(送紙字元)時會暫停的功能 
		-f 計算行數時,以實際上的行數,而非自動換行過後的行數(有些單行字數太長的會被擴展爲兩行或兩行以上) 
		-p 不以捲動的方式顯示每一頁,而是先清除螢幕後再顯示內容 
		-c 跟 -p 相似,不同的是先顯示內容再清除其他舊資料 
		-s 當遇到有連續兩行以上的空白行,就代換爲一行的空白行 
		-u 不顯示下引號 (根據環境變數 TERM 指定的 terminal 而有所不同) 
		+/ 在每個檔案顯示前搜尋該字串(pattern),然後從該字串之後開始顯示 
		+num 從第 num 行開始顯示 
	操作:
		空格下一頁
		b  上一頁
		G  最後一行
		h  顯示幫助

pwd:打印當前工作目錄名

	打印當前工作目錄名

read:用於從標準輸入讀取數值

	用於從標準輸入讀取數值
	語法 :read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
	-a 後跟一個變量,該變量會被認爲是個數組,然後給其賦值,默認是以空格爲分割符。
	-d 後面跟一個標誌符,其實只有其後的第一個字符有用,作爲結束的標誌。
	-p 後面跟提示信息,即在輸入前打印提示信息。
	-e 在輸入的時候可以使用命令補全功能。
	-n 後跟一個數字,定義輸入文本的長度,很實用。
	-r 屏蔽\,如果沒有該選項,則\作爲一個轉義字符,有的話 \就是個正常的字符了。
	-s 安靜模式,在輸入字符時不再屏幕上顯示,例如login時輸入密碼。
	-t 後面跟秒數,定義輸入字符的等待時間。
	-u 後面跟fd,從文件描述符中讀入,該文件描述符可以是exec新開啓的。

sed :一種流編輯器,它是文本處理中非常有用的工具,可以完美配合正則表達式使用。

	sed是一種流編輯器,它是文本處理中非常有用的工具,可以完美配合正則表達式使用。
	格式: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

		  -n, --quiet, --silent
		                 suppress automatic printing of pattern space   (不顯示自動輸出內容)
		  -e script, --expression=script
		                 add the script to the commands to be executed  (將腳本添加到要執行的命令中)
		  -f script-file, --file=script-file
		                 add the contents of script-file to the commands to be executed (添加腳本文件內容到要執行的命令中)
		  --follow-symlinks
		                 follow symlinks when processing in place       (處理到位時遵循符號鏈接)
		  -i[SUFFIX], --in-place[=SUFFIX]
		                 edit files in place (makes backup if SUFFIX supplied)     (就地編輯文件,如果提供後綴則進行備份)
		  -l N, --line-length=N
		                 specify the desired line-wrap length for the `l' command   (爲 'l' 命令指定所需的換行長度)
		  --posix
		                 disable all GNU extensions.   (禁用所有GNU拓展)
		  -r, --regexp-extended
		                 use extended regular expressions in the script.  (在腳本中使用拓展正則表達式)
		  -s, --separate
		                 consider files as separate rather than as a single continuous long stream.     (將文件視爲單獨的而不是一連串的長流)
		  -u, --unbuffered
		                 load minimal amounts of data from the input files and flush the output buffers more often    (從輸入文件加載最小數量的數據並刷新輸出緩衝區)
		  -z, --null-data
		                 separate lines by NUL characters      (按數字字符分隔行)
		  --help     display this help and exit
		  --version  output version information and exit
	If no -e, --expression, -f, or --file option is given, then the first                如果沒有指定 -e 或者 -f 選項,則第一個非選項參數被視作要解釋的seq腳本,其他剩餘參數作爲輸入文件的名字
	non-option argument is taken as the sed script to interpret.  all                    如果沒有指定輸入文件,會讀取標準輸入
	remaining arguments are names of input files; if no input files are
	specified, then the standard input is read.
	eg:
			1、組合多個表達式
				sed '表達式' | sed '表達式'
				等價於
				sed '表達式; 表達式'
			2、引用
				sed表達式可以使用單引號來引用,但是如果表達式內部包含變量字符串,就需要使用雙引號。
			3、替換字符串
				sed -i 's/需要替換的字符串/替代字符串/' filename         會替換所有匹配的字符串
			4、替換行
				sed -i '/匹配行/c 新內容' filename
				eg: sed -i '/aba/c  11111'  a.txt     將a.txt中包含aba字符的行替換爲 11111

file:確定文件類型

	 確定文件類型
	 Usage: file [OPTION...] [FILE...]
			Determine type of FILEs.
			      --help                 display this help and exit
			  
			  -v, --version              output version information and exit 							(輸出版本信息並退出)

			  -m, --magic-file LIST      use LIST as a colon-separated list of magic number files        (使用LIST給magic number 文件以冒號分隔)

			  -z, --uncompress           try to look inside compressed files 							 (嘗試查看壓縮文件)

			  -b, --brief                do not prepend filenames to output lines
			  
			  -c, --checking-printout    print the parsed form of the magic file, use in                (輸出magic文件的解析格式,用於和-m一起在安裝新的magic文件前先調試它)
			                               conjunction with -m to debug a new magic file
			                               before installing it
			  
			  -e, --exclude TEST         exclude TEST from the list of test to be                       (從要對文件執行的測試列表中排除測試
			                               performed for file. Valid tests are:							  有效的測試是:apptype,ascii,cdf,compress,elf,encoding,soft,tar,text,tokens)
			                               apptype, ascii, cdf, compress, elf, encoding,
			                               soft, tar, text, tokens
			  -f, --files-from FILE      read the filenames to be examined from FILE

			  -F, --separator STRING     use string as separator instead of `:'
			  
			  -i, --mime                 output MIME type strings (--mime-type and
			                               --mime-encoding)
			      --apple                output the Apple CREATOR/TYPE
			  
			      --mime-type            output the MIME type
			  
			      --mime-encoding        output the MIME encoding
			  
			  -k, --keep-going           don't stop at the first match                (不在第一次匹配就停下來)
			  
			  -l, --list                 list magic strength							(列出magic強度)
			  
			  -L, --dereference          follow symlinks (default if POSIXLY_CORRECT is set)          (遵循符號鏈接,未設置則默認爲POSIXLY_CORRECT)
			  
			  -h, --no-dereference       don't follow symlinks (default if POSIXLY_CORRECT is not set) 	(不遵循符號鏈接,未設置則默認爲POSIXLY_CORRECT)
			  
			  -n, --no-buffer            do not buffer output											(不緩衝輸出)
			  
			  -N, --no-pad               do not pad output												(不覆蓋輸出)
			  
			  -0, --print0               terminate filenames with ASCII NUL           					 (用 ASCII NUL 終止文件名)
			  
			  -p, --preserve-date        preserve access times on files
			  
			  -P, --parameter            set file engine parameter limits
			                               indir        15 recursion limit for indirection
			                               name         30 use limit for name/use magic
			                               elf_notes   256 max ELF notes processed
			                               elf_phnum   128 max ELF prog sections processed
			                               elf_shnum 32768 max ELF sections processed
			  
			  -r, --raw                  don't translate unprintable chars to \ooo             (不將無法打印的字符轉化爲\ooo)
			  
			  -s, --special-files        treat special (block/char devices) files as ordinary ones  (對特殊(塊字符設備)文件視爲一般文件)
			  
			  -C, --compile              compile file specified by -m                              (編譯由-m指定的文件)
			  
			  -d, --debug                print debugging messages                                  (打印調試信息)

head:查看文件開頭內容

	查看文件開頭內容
	Usage: head [OPTION]... [FILE]...
	Print the first 10 lines of each FILE to standard output.                                    打印每個文件的前十行到標準輸出
	With more than one FILE, precede each with a header giving the file name.					 超過一個文件,在每一個文件前加上文件名的頭
	With no FILE, or when FILE is -, read standard input.										 如果沒有文件,或者文件是-,讀入標準輸入

	Mandatory arguments to long options are mandatory for short options too.                     規定的長參數和短參數的是一樣的
	  -c, --bytes=[-]K         print the first K bytes of each file;						     打印每個文件開頭k字節的內容
	                             with the leading '-', print all but the last					 使用引導符-,打印除了最後k字節的所有內容
	                             K bytes of each file
	  -n, --lines=[-]K         print the first K lines instead of the first 10;                  打印每個文件開頭K行的內容
	                             with the leading '-', print all but the last					 使用引導符-,打印除了最後k行的所有內容
	                             K lines of each file
	  -q, --quiet, --silent    never print headers giving file names                             不打印文件名
	  -v, --verbose            always print headers giving file names                            打印文件名
	      --help     display this help and exit           										 顯示這個幫助並退出
	      --version  output version information and exit                                         輸出版本信息並退出
	K may have a multiplier suffix:
	b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,                                           k可以有多種後綴名
	GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.

tail:查看文件末尾內容

	查看文件末尾內容
	Usage: tail [OPTION]... [FILE]...
	Print the last 10 lines of each FILE to standard output.                                    打印每個文件的最後1-行到標準輸出
	With more than one FILE, precede each with a header giving the file name.					超過一個文件,在每一個文件前加上文件名的頭
	With no FILE, or when FILE is -, read standard input.										如果沒有文件,或者文件是-,讀入標準輸入

	Mandatory arguments to long options are mandatory for short options too.
	  -c, --bytes=K            output the last K bytes; alternatively, use -c +K                輸出最後k字節,
	                           to output bytes starting with the Kth of each file               使用-c +K 從每個文件的第K個開始輸出字節
	  -f, --follow[={name|descriptor}]
	                           output appended data as the file grows;                          隨着文件增長輸出附加數據
	                           -f, --follow, and --follow=descriptor are
	                           equivalent
	  -F                       same as --follow=name --retry                                    與 --follow=name --retry 相同                                    
	  -n, --lines=K            output the last K lines, instead of the last 10;                 輸出後 K行, 使用-n +K 輸出從第K開始的行
	                           or use -n +K to output lines starting with the Kth
	      --max-unchanged-stats=N
	                           with --follow=name, reopen a FILE which has not                  使用 --follow=name 在N次(默認5次)迭代後重新打開一個未更改大小的文件,查看它是否已被
	                           changed size after N (default 5) iterations						取消鏈接或重命名
	                           to see if it has been unlinked or renamed
	                           (this is the usual case of rotated log files).
	                           With inotify, this option is rarely useful.
	      --pid=PID            with -f, terminate after process ID, PID dies                    使用-f,在進程ID,PID結束後終止
	  -q, --quiet, --silent    never output headers giving file names                           不在輸出頭上打印文件名
	      --retry              keep trying to open a file even when it is or                    即使文件變得不可訪問或變得不可訪問也繼續嘗試打開該文件
	                             becomes inaccessible; useful when following by
	                             name, i.e., with --follow=name
	  -s, --sleep-interval=N   with -f, sleep for approximately N seconds                       使用-f在迭代之間睡眠大約N秒(默認爲1.0),使用inotify 和 --pid=P,每隔N秒至少檢查進程P一次
	                             (default 1.0) between iterations.
	                             With inotify and --pid=P, check process P at
	                             least once every N seconds.
	  -v, --verbose            always output headers giving file names                          輸出帶文件名
	      --help     display this help and exit
	      --version  output version information and exit

	If the first character of K (the number of bytes or lines) is a '+',
	print beginning with the Kth item from the start of each file, otherwise,
	print the last K items in the file.  K may have a multiplier suffix:
	b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,
	GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.

	With --follow (-f), tail defaults to following the file descriptor, which
	means that even if a tail'ed file is renamed, tail will continue to track
	its end.  This default behavior is not desirable when you really want to
	track the actual name of the file, not the file descriptor (e.g., log
	rotation).  Use --follow=name in that case.  That causes tail to track the
	named file in a way that accommodates renaming, removal and creation.

date: 顯示系統當前時間和日期

	顯示系統當前時間和日期

cal:顯示當前月份的日曆

	顯示當前月份的日曆

df:查看磁盤剩餘空間的數量

	查看磁盤剩餘空間的數量

free:顯示空閒內存的數量

	顯示空閒內存的數量

exit:結束終端會話

	結束終端會話

zip :

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