一、创建目录(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命令详解相关推荐

  1. Linux Shell 脚本攻略学习--四

    linux中(chattr)创建不可修改文件的方法 在常见的linux扩展文件系统中(如ext2.ext3.ext4等),可以将文件设置为不可修改(immutable).某些文件属性可帮助我们将文件设 ...

  2. Linux Shell脚本攻略学习总结:一

    终端打印 终端打印的常用命令有两个:echo和print 首先,我先介绍echo 1.echo echo这个命令接受三种形式的参数,实例如下: echo "Hello World" ...

  3. Linux Shell脚本攻略学习总结:三

    根据扩展名切分文件名 首先,我们先来看两个例子: file_jpg="sample.jgp" name=${file_jpg%.*} echo File name is : $na ...

  4. Linux Shell脚本攻略学习总结:二

    比较与测试 程序中的流程控制是由比较和测试语句来处理的. 我们可以用if,if else 以及逻辑运算符来执行测试,而用一些比较运算符来比较数据项.另外,有一个test 命令也可以用来进行测试.让我们 ...

  5. linux shell 脚本攻略学习10--生成任意大小的文件和文本文件的交集与差集详解

    一.生成任意大小的文件(dd命令): 举例: amosli@amosli-pc:~/learn/example$ dd if=/dev/zero of=test.zip bs=2M count=1; ...

  6. LINUX SHELL脚本攻略笔记[速查]

    LINUX SHELL脚本攻略笔记[速查] Linux Shell脚本攻略笔记[速查] 资源 shell script run shell script echo printf 环境变量和变量 pgr ...

  7. Linux Shell脚本攻略 读书笔记

    内容目录: 嗨,Echo一下 给终端来点颜色 shell的控制结构 算术比较 目录操作 网站下载 tar 归档工具 rsync 备份系统快照 ftp自动传输 磁盘管理 故障排查 使用syslog记录日 ...

  8. 《Linux Shell脚本攻略》学习笔记-第一章

    1.1 简介 计算机可以从文本文件(称为shell脚本)中读取并执行命令. sehll脚本不仅节省了时间,而且清楚明白地表明了所执行的操作. bash shell变成了UNIX和Linux中既成事实的 ...

  9. linux shell脚本攻略_(python)Linux下shell脚本监控Tomcat的状态并实现自动启动步骤...

    今天为大家带来的内容是:(python)Linux下shell脚本监控Tomcat的状态并实现自动启动步骤 本文内容主要介绍了Linux下shell脚本监控Tomcat的状态并实现自动启动的步骤,文章 ...

最新文章

  1. flash h5录音_博乐h5网站建设案例-AI优网络
  2. 数据科学干货分享来了!
  3. 弹窗页面PHP代码不执行,PHP代码没有被执行,而是代码显示在页面上
  4. altium designer2020中文版
  5. 数据库连接报错:Listener refused the connection with the following error: ORA-12505 的解决方法
  6. FPGA组合逻辑部件LUT的基本原理
  7. 单机搭建Android开发环境(五)
  8. 周二直播丨数据库上云趋势下,如何面对海量数据迁移及落地实践
  9. 3d模型多怎么优化_3D打印人像模型是怎么制作出来的呢?
  10. wps怎么统一修改标点符号_毕业论文的参考文献怎么加入??脚注如何降重?
  11. koa2+html模板,lenneth -- 基于koa2 的web极简框架
  12. zoom怎么解除静音_如何召开一场Zoom视频会议
  13. 在条件类型中使用 infer 关键字
  14. Windows Moible, Wince 使用.NET Compact Framework进行蓝牙(Bluetooth)开发 之 32feet.NET
  15. 谷歌插件如何下载到本地
  16. tcpreplay发包工具的简单介绍
  17. flutter友盟统计
  18. uc浏览器的navigator.userAgent
  19. vcs -fgp 仿真加速功能
  20. HTML期末作业-图书商城书店

热门文章

  1. 大学计算机基础毕业论文操作步骤,大学计算机基础教学论文
  2. php 科研,科研之路2
  3. [UWP]针对UWP程序多语言支持的总结,含RTL
  4. 生成某一文件夹内文件清单(批量处理)
  5. 双链集合添加删除算法
  6. AirPrint: 无交互的后台打印实现(Print without UI,iOS8+)
  7. ipconfig /flushdns 解释
  8. MyBatis中如何通过继承SqlSessionDaoSupport来编写DAO(二)
  9. CImageList类Create函数参数解析
  10. 给TreeView控件绑定数据