linux shell 脚本攻略学习11--mkdir和touch命令详解
一、創(chuàng)建目錄(mkdir命令詳解)
amosli@amosli-pc:~/learn$ mkdir dir amosli@amosli-pc:~/learn/dir$ mkdir folder amosli@amosli-pc:~/learn/dir$ ls folder上面的命令中用到了mkdir,即是創(chuàng)建一個目錄,非常常用的一個linux 命令。該命令創(chuàng)建指定的目錄名,要求創(chuàng)建目錄的用戶在當(dāng)前目錄中具有寫權(quán)限,并且指定的目錄名不能是當(dāng)前目錄中已有的目錄.
在命令行內(nèi)輸入mkdir --help查看幫助信息.
amosli@amosli-pc:~/learn/dir$ mkdir --help Usage: mkdir [OPTION]... DIRECTORY... Create the DIRECTORY(ies), if they do not already exist.Mandatory arguments to long options are mandatory for short options too.-m, --mode=MODE set file mode (as in chmod), not a=rwx - umask-p, --parents no error if existing, make parent directories as needed-v, --verbose print a message for each created directory-Z, --context=CTX set the SELinux security context of each createddirectory to CTX--help display this help and exit--version output version information and exitReport mkdir bugs to bug-coreutils@gnu.org GNU coreutils home page: <http://www.gnu.org/software/coreutils/> General help using GNU software: <http://www.gnu.org/gethelp/> For complete documentation, run: info coreutils 'mkdir invocation'由上面提示可以得知mkdir命令語法為:
mkdir [OPTION]... DIRECTORY...其中[參數(shù)]都是可選,非必選。
選項(xiàng)介紹:
????-m: 對新建目錄設(shè)置存取權(quán)限,也可以用chmod命令設(shè)置;
????-p: 可以是一個路徑名稱。此時若路徑中的某些目錄尚不存在,加上此選項(xiàng)后,系統(tǒng)將自動建立好那些尚不存在的目錄,即一次可以建立多個目錄;
? ? -v:表示打印每一個創(chuàng)建的目錄的信息。
? ? -z:從語義來看,是為每個ctx創(chuàng)建目錄時設(shè)置SELinux級安全上下文。
? ? -help,-version一個是顯示幫助信息,一個是顯示版本號
下面就來舉例說明參數(shù)內(nèi)容:
實(shí)例:
如何創(chuàng)建多級目錄?如何在amosli@amosli-pc:~/learn/dir/folder$目錄下創(chuàng)建/par/child/grand
amosli@amosli-pc:~/learn/dir/folder$ mkdir par #可不可以直接創(chuàng)那child/grand/目錄? amosli@amosli-pc:~/learn/dir/folder$ mkdir par/child/grand mkdir: cannot create directory `par/child/grand': No such file or directory #答案是不可以 amosli@amosli-pc:~/learn/dir/folder$ mkdir par/child/ amosli@amosli-pc:~/learn/dir/folder$ mkdir par/child/grand #創(chuàng)建完成 amosli@amosli-pc:~/learn/dir/folder$ cd par/child/grand/ amosli@amosli-pc:~/learn/dir/folder/par/child/grand$有沒有更方便的方法?
答案是有的,先刪除剛才創(chuàng)建的目錄:
amosli@amosli-pc:~/learn/dir/folder$ ls par amosli@amosli-pc:~/learn/dir/folder$ rm -rf * amosli@amosli-pc:~/learn/dir/folder$ ls然后開始一次性創(chuàng)建目錄:
amosli@amosli-pc:~/learn/dir/folder$ mkdir -p par/child/grand amosli@amosli-pc:~/learn/dir/folder$ cd par/child/grand/ amosli@amosli-pc:~/learn/dir/folder/par/child/grand$加上-p參數(shù)即可。
實(shí)例2:
關(guān)于參數(shù)-v -m的使用:
amosli@amosli-pc:~/learn/dir/folder$ rm -rf * #刪除目錄 amosli@amosli-pc:~/learn/dir/folder$ mkdir -v -m 775 par mkdir: created directory `par' amosli@amosli-pc:~/learn/dir/folder$ ll total 12 drwxrwxr-x 3 amosli amosli 4096 12月 26 22:57 ./ drwxrwxr-x 3 amosli amosli 4096 12月 26 22:33 ../ drwxrwxr-x 2 amosli amosli 4096 12月 26 22:57 par/ amosli@amosli-pc:~/learn/dir/folder$由上面的例子可以看出-m 是管理權(quán)限的,-v 是顯示創(chuàng)建信息的。
-Z參數(shù)看了半天沒搞明白到底怎么用,這里就跳過了。
?
二、創(chuàng)建文件(touch命令詳解)
創(chuàng)建文件的方式比較多,如上一篇講到的dd 命令,和之前的 echo "hello" > a.txt 類似的>創(chuàng)建文件,這里主要介紹touch命令
touch命令主要用來修改文件時間戳,或者新建一個不存在的文件
touch --help來看一下幫助信息:
amosli@amosli-pc:~/learn/dir/folder/par$ touch --help Usage: touch [OPTION]... FILE... Update the access and modification times of each FILE to the current time.A FILE argument that does not exist is created empty, unless -c or -h is supplied.A FILE argument string of - is handled specially and causes touch to change the times of the file associated with standard output.Mandatory arguments to long options are mandatory for short options too.-a change only the access time-c, --no-create do not create any files-d, --date=STRING parse STRING and use it instead of current time-f (ignored)-h, --no-dereference affect each symbolic link instead of any referencedfile (useful only on systems that can change thetimestamps of a symlink)-m change only the modification time-r, --reference=FILE use this file's times instead of current time-t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time--time=WORD change the specified time:WORD is access, atime, or use: equivalent to -aWORD is modify or mtime: equivalent to -m--help display this help and exit--version output version information and exitNote that the -d and -t options accept different time-date formats.Report touch bugs to bug-coreutils@gnu.org GNU coreutils home page: <http://www.gnu.org/software/coreutils/> General help using GNU software: <http://www.gnu.org/gethelp/> For complete documentation, run: info coreutils 'touch invocation'從中可以看出來與mkdir 很類似,touch 的命令語法如下:
touch [OPTION]... FILE...其中,參數(shù)非必選,現(xiàn)在就來看下提供的參數(shù)有哪些,各有什么作用?
-a 改變檔案的讀取時間記錄。-m 改變檔案的修改時間記錄。-c 假如目的檔案不存在,不會建立新的檔案。與 --no-create 的效果一樣。-h ,不干擾引用 影響每個符號鏈接,而不是所有參考文件(只適用于系統(tǒng)的改變一個符號,時間戳)-f 不會執(zhí)行實(shí)際操作,是為了與其他 unix 系統(tǒng)的相容性而保留。-r 使用參考檔的時間記錄,與 --file 的效果一樣。-d 設(shè)定時間與日期,可以使用各種不同的格式。-t 設(shè)定檔案的時間記錄,格式與 date 指令相同。[[CC]YY]MMDDhhmm[.SS],CC為年數(shù)中的前兩位,即”世紀(jì)數(shù)”;YY為年數(shù)的后兩位,即某世紀(jì)中的年數(shù).如果不給出CC的值,則linux中touch命令參數(shù)將把年數(shù)CCYY限定在1969--2068之內(nèi).MM為月數(shù),DD為天將把年數(shù)CCYY限定在1969--2068之內(nèi).MM為月數(shù),DD為天數(shù),hh 為小時數(shù)(幾點(diǎn)),mm為分鐘數(shù),SS為秒數(shù).此處秒的設(shè)定范圍是0--61,這樣可以處理閏秒.這些數(shù)字組成的時間是環(huán)境變量TZ指定的時區(qū)中的一個時間.由于系統(tǒng)的限制,早于1970年1月1日的時間是錯誤的.--no-create 不會建立新檔案。--help 列出幫助信息--version 列出版本信息
實(shí)例1:
創(chuàng)建文件a.txt
amosli@amosli-pc:~/learn/dir/folder/par$ touch a.txt amosli@amosli-pc:~/learn/dir/folder/par$ ls -l total 0 -rw-rw-r-- 1 amosli amosli 0 12月 26 23:07 a.txt實(shí)例2:
更改a.txt修改時間記錄(-m參數(shù)): amosli@amosli-pc:~/learn/dir/folder/par$ touch -m a.txt amosli@amosli-pc:~/learn/dir/folder/par$ ls -l total 0 -rw-rw-r-- 1 amosli amosli 0 12月 26 23:09 a.txt實(shí)例3:
指定時間來創(chuàng)建文件(-t參數(shù)):
amosli@amosli-pc:~/learn/dir/folder/par$ touch -t 201812262315.34 b.txt amosli@amosli-pc:~/learn/dir/folder/par$ ll total 8 drwxrwxr-x 2 amosli amosli 4096 12月 26 23:24 ./ drwxrwxr-x 3 amosli amosli 4096 12月 26 22:57 ../ -rw-rw-r-- 1 amosli amosli 0 12月 26 23:19 a.txt -rw-rw-r-- 1 amosli amosli 0 12月 26 2018 b.txt實(shí)例4:
?
#將 file 的時間記錄改變成與 referencefile 一樣。 touch -r referencefile file?
amosli@amosli-pc:~/learn/dir/folder/par$ touch -r b.txt a.txt amosli@amosli-pc:~/learn/dir/folder/par$ ls -l total 0 -rw-rw-r-- 1 amosli amosli 0 12月 26 2018 a.txt -rw-rw-r-- 1 amosli amosli 0 12月 26 2018 b.txt?
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的linux shell 脚本攻略学习11--mkdir和touch命令详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一个即将步入运维的菜虫内心
- 下一篇: 理解并实施:VRRP(200-120新增