linux shell 脚本攻略学习11--mkdir和touch命令详解
一、創建目錄(mkdir命令詳解)
amosli@amosli-pc:~/learn$ mkdir dir amosli@amosli-pc:~/learn/dir$ mkdir folder amosli@amosli-pc:~/learn/dir$ ls folder上面的命令中用到了mkdir,即是創建一個目錄,非常常用的一個linux 命令。該命令創建指定的目錄名,要求創建目錄的用戶在當前目錄中具有寫權限,并且指定的目錄名不能是當前目錄中已有的目錄.
在命令行內輸入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...其中[參數]都是可選,非必選。
選項介紹:
????-m: 對新建目錄設置存取權限,也可以用chmod命令設置;
????-p: 可以是一個路徑名稱。此時若路徑中的某些目錄尚不存在,加上此選項后,系統將自動建立好那些尚不存在的目錄,即一次可以建立多個目錄;
? ? -v:表示打印每一個創建的目錄的信息。
? ? -z:從語義來看,是為每個ctx創建目錄時設置SELinux級安全上下文。
? ? -help,-version一個是顯示幫助信息,一個是顯示版本號
下面就來舉例說明參數內容:
實例:
如何創建多級目錄?如何在amosli@amosli-pc:~/learn/dir/folder$目錄下創建/par/child/grand
amosli@amosli-pc:~/learn/dir/folder$ mkdir par #可不可以直接創那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 #創建完成 amosli@amosli-pc:~/learn/dir/folder$ cd par/child/grand/ amosli@amosli-pc:~/learn/dir/folder/par/child/grand$有沒有更方便的方法?
答案是有的,先刪除剛才創建的目錄:
amosli@amosli-pc:~/learn/dir/folder$ ls par amosli@amosli-pc:~/learn/dir/folder$ rm -rf * amosli@amosli-pc:~/learn/dir/folder$ ls然后開始一次性創建目錄:
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參數即可。
實例2:
關于參數-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 是管理權限的,-v 是顯示創建信息的。
-Z參數看了半天沒搞明白到底怎么用,這里就跳過了。
?
二、創建文件(touch命令詳解)
創建文件的方式比較多,如上一篇講到的dd 命令,和之前的 echo "hello" > a.txt 類似的>創建文件,這里主要介紹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...其中,參數非必選,現在就來看下提供的參數有哪些,各有什么作用?
-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日的時間是錯誤的.--no-create 不會建立新檔案。--help 列出幫助信息--version 列出版本信息
實例1:
創建文件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實例2:
更改a.txt修改時間記錄(-m參數): 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實例3:
指定時間來創建文件(-t參數):
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實例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?
?
?
?
?
總結
以上是生活随笔為你收集整理的linux shell 脚本攻略学习11--mkdir和touch命令详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一个即将步入运维的菜虫内心
- 下一篇: 【转载】应广大群众的要求,今天开始连载《