日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux shell 脚本攻略学习11--mkdir和touch命令详解

發(fā)布時(shí)間:2023/12/20 linux 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux shell 脚本攻略学习11--mkdir和touch命令详解 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、創(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)建一個(gè)目錄,非常常用的一個(gè)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: 對(duì)新建目錄設(shè)置存取權(quán)限,也可以用chmod命令設(shè)置;

????-p: 可以是一個(gè)路徑名稱。此時(shí)若路徑中的某些目錄尚不存在,加上此選項(xiàng)后,系統(tǒng)將自動(dòng)建立好那些尚不存在的目錄,即一次可以建立多個(gè)目錄;

? ? -v:表示打印每一個(gè)創(chuàng)建的目錄的信息。

? ? -z:從語義來看,是為每個(gè)ctx創(chuàng)建目錄時(shí)設(shè)置SELinux級(jí)安全上下文。

? ? -help,-version一個(gè)是顯示幫助信息,一個(gè)是顯示版本號(hào)

下面就來舉例說明參數(shù)內(nèi)容:

實(shí)例:

如何創(chuàng)建多級(jí)目錄?如何在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命令主要用來修改文件時(shí)間戳,或者新建一個(gè)不存在的文件

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 改變檔案的讀取時(shí)間記錄。-m 改變檔案的修改時(shí)間記錄。-c 假如目的檔案不存在,不會(huì)建立新的檔案。與 --no-create 的效果一樣。
-h ,不干擾引用 影響每個(gè)符號(hào)鏈接,而不是所有參考文件(只適用于系統(tǒng)的改變一個(gè)符號(hào),時(shí)間戳)-f 不會(huì)執(zhí)行實(shí)際操作,是為了與其他 unix 系統(tǒng)的相容性而保留。-r 使用參考檔的時(shí)間記錄,與
--file 的效果一樣。-d 設(shè)定時(shí)間與日期,可以使用各種不同的格式。-t 設(shè)定檔案的時(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í)數(shù)(幾點(diǎn)),mm為分鐘數(shù),SS為秒數(shù).此處秒的設(shè)定范圍是0--61,這樣可以處理閏秒.這些數(shù)字組成的時(shí)間是環(huán)境變量TZ指定的時(shí)區(qū)中的一個(gè)時(shí)間.由于系統(tǒng)的限制,早于1970年1月1日的時(shí)間是錯(cuò)誤的.--no-create 不會(huì)建立新檔案。--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修改時(shí)間記錄(-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:

指定時(shí)間來創(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 的時(shí)間記錄改變成與 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命令详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。