Linux文件管理命令(1)-增加文件
目录
前言
今天要学的命令是mkdir、touch,这两个命令是用来创建文件以及目录的,在工作上也是较为常用的命令
一、mkdir命令介绍
mkdir命令来自于英文词组"make directories"的缩写,主要功能是用来创建目录文件,使用下来还是比较简单的,需要注意的前面有创建过相同的目录,系统会提示文件已存在而不继续创建不覆盖已有的文件。当然,想一次性创建嵌套的目录文件,这里也有参数可以做到,例如:a/b/c/d/e/f,用-p参数即可进行递归创建。
二、语法以及常用参数
这里可以用mkdir --help 或者使用man mkdir来查看命令的详细信息
[root@localhost ~]# mkdir --help
用法:mkdir [选项]... 目录...
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 set SELinux security context of each created directory
to the default type
--context[=CTX] like -Z, or if CTX is specified then set the SELinux
or SMACK security context to CTX
--help 显示此帮助信息并退出
--version 显示版本信息并退出
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
请向<http://translationproject.org/team/zh_CN.html> 报告mkdir 的翻译错误
要获取完整文档,请运行:info coreutils 'mkdir invocation'
可以看到的是这个命令的语法是mkdir 【选项】 目录
常用参数有:
-m:可直接给目录自定义权限,就像命令chmod一样,例 a=rwx ,770等
[root@localhost ~]# mkdir -m a=rx testdir
[root@localhost ~]# ll -d testdir/
dr-xr-xr-x 2 root root 6 2月 3 14:42 testdir/
-p:递归创建目录
[root@VM-12-17-centos ~]# mkdir -p testdir/a/b/c/d
[root@VM-12-17-centos ~]# tree testdir/
testdir/
`-- a
`-- b
`-- c
`-- d
4 directories, 0 files
--verbose:打印输出创建文件的信息
[root@localhost ~]# mkdir --verbose abc
mkdir: 已创建目录 "abc"
[root@localhost ~]#
-Z:SELinux默认类型
[root@localhost ~]# mkdir -Z testdir1
[root@localhost ~]# ll -dZ testdir1
drwxr-xr-x. root root root:object_r:admin_home_t:s0 testdir1
小贴士:这里引出了SELinux的知识,SELinux这个是美国国家安全局在Linux开源社区的帮助下开发的一个强制访问控制(MAC,Mandatory Access Control)的安全子系统。Linux系统使用SELinux技术的目的是为了让各个服务进程都受到约束,使其仅获取到本应获取的资源。而SELinux分为SELinux安全上下文和SELinux域,分别作用是SELinux域限制可以确保服务程序做不了出格的事情,对文件资源的访问进行限制;SELinux安全上下文确保文件资源只能被其所属的服务程序进行访问
三、touch命令介绍
touch命令可以用来创建文件,并且可以结合正则表达式中的{}符号,可以一次性创建多个文件
四、语法以及常用参数
通过help参数可以看到语法格式是 touch 【选项】 文件
[root@localhost ~]# touch --help
用法:touch [选项]... 文件...
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 只更改访问时间
-c, --no-create 不创建任何文件
-d, --date=字符串 使用指定字符串表示时间而非当前时间
-f (忽略)
-h, --no-dereference 会影响符号链接本身,而非符号链接所指示的目的地
(当系统支持更改符号链接的所有者时,此选项才有用)
-m 只更改修改时间
-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 -a
WORD is modify or mtime: equivalent to -m
--help 显示此帮助信息并退出
--version 显示版本信息并退出
参数:
-a | 改变档案的读取时间记录 |
-m | 改变档案的修改时间记录 |
-r | 使用参考档的时间记录,与 --file 的效果一样 |
-c | 不创建新文件 |
-d | 设定时间与日期,可以使用各种不同的格式 |
-t | 设定档案的时间记录,格式与 date 命令相同 |
--no-create | 不创建新文件 |
案例:
一次性创建多个文件
[root@localhost data]# touch test{1..5}.txt
[root@localhost data]# ll
总用量 0
-rw-r--r--. 1 root root 0 2月 3 15:27 test1.txt
-rw-r--r--. 1 root root 0 2月 3 15:27 test2.txt
-rw-r--r--. 1 root root 0 2月 3 15:27 test3.txt
-rw-r--r--. 1 root root 0 2月 3 15:27 test4.txt
-rw-r--r--. 1 root root 0 2月 3 15:27 test5.txt
[root@localhost data]#
使用-a参数,修改了文件的访问时间
[root@localhost data]# touch -a test1.txt
[root@localhost data]# stat test1.txt
文件:"test1.txt"
大小:0 块:0 IO 块:4096 普通空文件
设备:fd00h/64768d Inode:34850080 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
环境:root:object_r:unlabeled_t:s0
最近访问:2023-02-03 15:31:03.049191404 +0800 #访问时间有变化
最近更改:2023-02-03 15:27:31.043170706 +0800
最近改动:2023-02-03 15:31:03.049191404 +0800 #改动时间有变化
创建时间:-
[root@localhost data]#
那么什么情况下会修改访问时间呢
可以看到用vim打开文件后,atime有变动,但是并没有在往文件中写入内容;如果一写入内容,atime、mtime、ctime都会有所改变
而用echo命令,atime并不会有所改变,修改的是mtime和ctime
使用mv进行文件重命名时,则会变动ctime的时间
总结
mkdir和touch都是新增文件的命令,使用起来也比较简单,记住语法的格式和常需要用到的参数,能很快掌握下来!
相关文章
- Linux备份ifcfg-eth0文件导致的网络故障问题
- linux 从百度网盘下载文件的方法
- 重新点亮linux 命令树————文件权限和目录权限[九]
- Linux统计某文件夹下文件、文件夹的个数
- linux 系统tar文件压缩打包命令
- Linux之Ubuntu:Ubuntu中常使用的快捷键命令(sudo/dpkg等文件处理相关/系统硬件相关/文件管理/文档编辑/磁盘管理/系统管理/备份压缩)、常用基础案例(图文教程)之详细攻略
- linux c 文件打开并创建代码分析
- linux服务器上运行python文件
- Linux文件时间属性
- Linux下通过md5sum生成MD5文件&校验MD5
- L39.linux命令每日一练 -- 第六章 文件备份与压缩命令 -- scp和rsync
- L37.linux命令每日一练 -- 第六章 文件备份与压缩命令 -- tar和gzip
- L24.linux命令每日一练 -- 第三章 文件过滤及内容编辑处理命令 -- diff和vimdiff命令
- L8.linux命令每日一练 -- 第二章 文件和目录操作命令 -- rm和rmdir命令
- L7.linux命令每日一练 -- 第二章 文件和目录操作命令 -- cp和mv命令
- Linux基础命令-whereis查找命令及相关文件
- Linux基础命令-lsof查看进程打开的文件
- Linux基础命令-chattr更改文件隐藏属性
- Linux基础命令-ls显示目录和文件的属性信息
- Linux中打开文件
- 转 linux查看文件前几行和后几行的命令