linux(十一)文件和目录相关命令touch和mkdir(1)

简介: 前边看了ls,cd,pwd这三个纯用来操作目录的命令。接下来,来看一下文件和目录都有的命令。

前边看了ls,cd,pwd这三个纯用来操作目录的命令。

接下来,来看一下文件和目录都有的命令。

 

首先,分别是创建文件命令touch,以及创建目录命令mkdir

一:touch命令

1:创建一个空文件

csharp

复制代码

[root@iZuf60ynur81p6k0ysvtneZ opt]# touch ceshi.txt
[root@iZuf60ynur81p6k0ysvtneZ opt]# ll -a
total 8
drwxr-xr-x.  2 root root 4096 Aug 27 09:20 .
dr-xr-xr-x. 18 root root 4096 Aug 10 19:00 ..
-rw-r--r--   1 root root    0 Aug 27 09:20 ceshi.txt
[root@iZuf60ynur81p6k0ysvtneZ opt]#

 

2:使用touch命令一次创建多个文件

sql

复制代码

[root@iZuf60ynur81p6k0ysvtneZ opt]# touch ceshi1.txt ceshi2.txt ceshi3.txt
[root@iZuf60ynur81p6k0ysvtneZ opt]# ll -a
total 8
drwxr-xr-x.  2 root root 4096 Aug 27 09:22 .
dr-xr-xr-x. 18 root root 4096 Aug 10 19:00 ..
-rw-r--r--   1 root root    0 Aug 27 09:22 ceshi1.txt
-rw-r--r--   1 root root    0 Aug 27 09:22 ceshi2.txt
-rw-r--r--   1 root root    0 Aug 27 09:22 ceshi3.txt
-rw-r--r--   1 root root    0 Aug 27 09:20 ceshi.txt

 

3:强制避免使用touch命令创建新文件

有时,如果新文件不存在,则需要避免创建新文件。 在这种情况下,您可以使用touch命令使用'-c'选项,

sql

复制代码

[root@iZuf60ynur81p6k0ysvtneZ opt]# touch -c test.h
[root@iZuf60ynur81p6k0ysvtneZ opt]# ll -a
total 8
drwxr-xr-x.  2 root root 4096 Aug 27 09:22 .
dr-xr-xr-x. 18 root root 4096 Aug 10 19:00 ..
-rw-r--r--   1 root root    0 Aug 27 09:22 ceshi1.txt
-rw-r--r--   1 root root    0 Aug 27 09:22 ceshi2.txt
-rw-r--r--   1 root root    0 Aug 27 09:22 ceshi3.txt
-rw-r--r--   1 root root    0 Aug 27 09:20 ceshi.txt

 

这个命令具体在哪各场景应用,我也是没搞清楚希望有大神看到了,能给我解释一下

 4:同时修改文件访问时间及修改时间

makefile

复制代码

[root@iZuf60ynur81p6k0ysvtneZ opt]# touch ceshi.txt
[root@iZuf60ynur81p6k0ysvtneZ opt]# stat ceshi.txt
  File: ‘ceshi.txt’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d   Inode: 1179659     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-08-27 10:27:46.209966525 +0800
Modify: 2020-08-27 10:27:46.209966525 +0800
Change: 2020-08-27 10:27:46.209966525 +0800
 Birth: -

 

结合上边的代码,我们可以看到文件的访问和修改时间都改成了10:27:46

 5:单独更改访问时间/单独更改修改时间

(1):修改访问时间,使用-a

makefile

复制代码

[root@iZuf60ynur81p6k0ysvtneZ opt]# touch -a ceshi.txt
[root@iZuf60ynur81p6k0ysvtneZ opt]# stat ceshi.txt
  File: ‘ceshi.txt’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d   Inode: 1179659     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-08-27 10:30:06.105001882 +0800
Modify: 2020-08-27 10:27:46.209966525 +0800
Change: 2020-08-27 10:30:06.105001882 +0800
 Birth: -

我们看到,修改版时间发生了改变

(2):更改修改时间,使用-m

makefile

复制代码

[root@iZuf60ynur81p6k0ysvtneZ opt]# touch -m ceshi.txt
[root@iZuf60ynur81p6k0ysvtneZ opt]# stat ceshi.txt
  File: ‘ceshi.txt’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d   Inode: 1179659     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-08-27 10:30:06.105001882 +0800
Modify: 2020-08-27 10:31:06.882886472 +0800
Change: 2020-08-27 10:31:06.882886472 +0800
 Birth: -

 

6:将访问和修改时间从一个文件复制到另一个文件

我们将ceshi1.txt文件的修改访问时间,复制到ceshi.txt上,使用-r参数

makefile

复制代码

[root@iZuf60ynur81p6k0ysvtneZ opt]# touch ceshi.txt -r ceshi1.txt
[root@iZuf60ynur81p6k0ysvtneZ opt]# stat ceshi1.txt
  File: ‘ceshi1.txt’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d   Inode: 1179660     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-08-27 09:22:12.361072160 +0800
Modify: 2020-08-27 09:22:12.361072160 +0800
Change: 2020-08-27 09:22:12.361072160 +0800
 Birth: -

 7:使用指定时间戳创建文件

语法:touch -t YYMMDDHHMM.SS “filename”

makefile

复制代码

[root@iZuf60ynur81p6k0ysvtneZ opt]# touch -t 2006151230.30 linuxidc
[root@iZuf60ynur81p6k0ysvtneZ opt]# stat linuxidc
  File: ‘linuxidc’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d   Inode: 1179663     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-06-15 12:30:30.000000000 +0800
Modify: 2020-06-15 12:30:30.000000000 +0800
Change: 2020-08-27 10:43:34.588771917 +0800
 Birth: -

 8:将文件的时间戳更改为其他时间

语法:touch -c -t YYMMDDHHMM.SS “filename”

makefile

复制代码

[root@iZuf60ynur81p6k0ysvtneZ opt]# touch -c -t 2008191130.30 linuxidc
[root@iZuf60ynur81p6k0ysvtneZ opt]# stat linuxidc
  File: ‘linuxidc’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d   Inode: 1179663     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-08-19 11:30:30.000000000 +0800
Modify: 2020-08-19 11:30:30.000000000 +0800
Change: 2020-08-27 10:44:57.244973180 +0800
 Birth: -

二:创建目录mkdir

1:创建一个空目录

sql

复制代码

[root@iZuf60ynur81p6k0ysvtneZ opt]# mkdir first
[root@iZuf60ynur81p6k0ysvtneZ opt]# ll -a
total 12
drwxr-xr-x.  3 root root 4096 Aug 27 10:47 .
dr-xr-xr-x. 18 root root 4096 Aug 10 19:00 ..
-rw-r--r--   1 root root    0 Aug 27 09:22 ceshi1.txt
-rw-r--r--   1 root root    0 Aug 27 09:22 ceshi2.txt
-rw-r--r--   1 root root    0 Aug 27 09:22 ceshi3.txt
-rw-r--r--   1 root root    0 Aug 27 09:22 ceshi.txt
drwxr-xr-x   2 root root 4096 Aug 27 10:47 first
-rw-r--r--   1 root root    0 Aug 19 11:30 linuxidc

 2:同时创建多个目录

sql

复制代码

[root@iZuf60ynur81p6k0ysvtneZ opt]# mkdir second third
[root@iZuf60ynur81p6k0ysvtneZ opt]# ll -a
total 20
drwxr-xr-x.  5 root root 4096 Aug 27 10:48 .
dr-xr-xr-x. 18 root root 4096 Aug 10 19:00 ..
-rw-r--r--   1 root root    0 Aug 27 09:22 ceshi1.txt
-rw-r--r--   1 root root    0 Aug 27 09:22 ceshi2.txt
-rw-r--r--   1 root root    0 Aug 27 09:22 ceshi3.txt
-rw-r--r--   1 root root    0 Aug 27 09:22 ceshi.txt
drwxr-xr-x   2 root root 4096 Aug 27 10:47 first
-rw-r--r--   1 root root    0 Aug 19 11:30 linuxidc
drwxr-xr-x   2 root root 4096 Aug 27 10:48 second
drwxr-xr-x   2 root root 4096 Aug 27 10:48 third
目录
相关文章
|
1天前
|
缓存 监控 Linux
Linux系统之smem命令的基本使用
【7月更文挑战第1天】Linux系统之smem命令的基本使用
16 2
|
1天前
|
存储 监控 Linux
深入解析Linux命令:rootfs-expand
`rootfs-expand`是假设的Linux工具,用于扩展嵌入式设备或特定发行版的根文件系统。它检查当前大小,确定可扩展空间,并调整分区。命令参数包括 `-s` 设置扩展大小,`-f` 强制扩展,`-v` 显示详细信息和 `-h` 显示帮助。扩展前务必备份数据,谨慎使用强制选项,并确保有足够未分配空间。示例命令:`sudo rootfs-expand -s 4G [-v]`。虽然具体实现会有所不同,但这个概念可以帮助理解根文件系统扩展的一般流程。
|
1天前
|
数据采集 Linux 数据处理
深入了解Linux命令:rev
`rev`命令在Linux中反转文本字符顺序,用于数据预处理和分析。它可以改变每行字符的排列,例如`echo "Hello, World!" | rev`输出`!dlroW ,olleH`。常用参数包括显示版本信息的`-V`。结合其他命令如`cat`,可处理文件内容。注意行格式和性能影响,适用于小到中型数据处理。
|
1天前
|
监控 网络协议 安全
Linux基本指令之网络通信命令
Linux基本指令之网络通信命令
|
1天前
|
网络协议 Linux 网络安全
linux基本命令之系统管理命令
linux基本命令之系统管理命令
|
2月前
|
Linux
快速上手linux | 一文秒懂Linux各种常用目录命令(上)
快速上手linux | 一文秒懂Linux各种常用目录命令(上)
25 0
|
2月前
|
Ubuntu Linux
linux(三十六)文件和目录相关命令tree
linux(三十六)文件和目录相关命令tree
32 0
|
2月前
|
Linux Shell PHP
linux(十五)文件和目录相关命令-控制台输出命令echo和重定向
linux(十五)文件和目录相关命令-控制台输出命令echo和重定向
90 0
|
2月前
|
Linux
linux(十四)文件和目录相关命令cat和more
linux(十四)文件和目录相关命令cat和more
46 0
|
2月前
|
Linux
linux(十三)文件和目录相关命令rm
linux(十三)文件和目录相关命令rm
42 0