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
目录
相关文章
|
5天前
|
存储 网络协议 Linux
【Linux】进程IO|系统调用|open|write|文件描述符fd|封装|理解一切皆文件
本文详细介绍了Linux中的进程IO与系统调用,包括 `open`、`write`、`read`和 `close`函数及其用法,解释了文件描述符(fd)的概念,并深入探讨了Linux中的“一切皆文件”思想。这种设计极大地简化了系统编程,使得处理不同类型的IO设备变得更加一致和简单。通过本文的学习,您应该能够更好地理解和应用Linux中的进程IO操作,提高系统编程的效率和能力。
53 34
|
8天前
|
Linux
Linux系统之whereis命令的基本使用
Linux系统之whereis命令的基本使用
50 23
Linux系统之whereis命令的基本使用
|
22天前
|
网络协议 Unix Linux
深入解析:Linux网络配置工具ifconfig与ip命令的全面对比
虽然 `ifconfig`作为一个经典的网络配置工具,简单易用,但其功能已经不能满足现代网络配置的需求。相比之下,`ip`命令不仅功能全面,而且提供了一致且简洁的语法,适用于各种网络配置场景。因此,在实际使用中,推荐逐步过渡到 `ip`命令,以更好地适应现代网络管理需求。
34 11
|
2月前
|
存储 NoSQL Linux
linux积累-core文件是干啥的
核心文件是Linux系统在程序崩溃时生成的重要调试文件,通过分析核心文件,开发者可以找到程序崩溃的原因并进行调试和修复。本文详细介绍了核心文件的生成、配置、查看和分析方法
159 6
|
2月前
|
Ubuntu Linux Go
golang编译成Linux可运行文件
本文介绍了如何在 Linux 上编译和运行 Golang 程序,涵盖了本地编译和交叉编译的步骤。通过这些步骤,您可以轻松地将 Golang 程序编译成适合 Linux 平台的可执行文件,并在目标服务器上运行。掌握这些技巧,可以提高开发和部署 Golang 应用的效率。
277 14
|
2月前
|
Linux
linux查看目录下的文件夹命令,find查找某个目录,但是不包括这个目录本身?
通过本文的介绍,您应该对如何在 Linux 系统中查看目录下的文件夹以及使用 `find` 命令查找特定目录内容并排除该目录本身有了清晰的理解。掌握这些命令和技巧,可以大大提高日常文件管理和查找操作的效率。 在实际应用中,灵活使用这些命令和参数,可以帮助您快速定位和管理文件和目录,满足各种复杂的文件系统操作需求。
143 8
|
8月前
|
存储 安全 Unix
探索Linux中的`mkdir`命令:创建目录的艺术
`mkdir`命令在Linux中用于创建目录,是文件管理的关键工具。它可以递归创建目录(-p选项),设置新目录权限(-m选项)并显示详细信息(-v选项)。例如,`mkdir -p dir1/dir2/dir3`会创建多级目录,而`mkdir -m 700 secret`创建一个具有特定权限的目录。在处理项目数据时,常通过脚本批量创建目录,如创建多个年份销售数据的子目录。使用时注意检查目录是否存在,设置适当权限,并避免在根目录下操作。查阅`man mkdir`获取更多帮助。
|
9月前
|
Unix Linux Shell
Linux系统之mkdir与rmdir命令的基本使用
Linux系统之mkdir与rmdir命令的基本使用
93 1
Linux系统之mkdir与rmdir命令的基本使用
|
Linux
Linux mkdir命令:创建目录(文件夹)
mkdir 命令,是 make directories 的缩写,用于创建新目录,此命令所有用户都可以使用。mkdir 命令的基本格式为: [root@localhost ~]# mkdir [-mp] 目录名 -m 选项用于手动配置所创建目录的权限,而不再使用默认权限。 -p 选项递归创建所有目录,以创建 /home/test/demo 为例,在默认情况下,你需要一层一层的创建各个目录,而使用 -p 选项,则系统会自动帮你创建 /home、/home/test 以及 /home/test/demo。 【例 1】建立目录。 [root@localhost ~]#mkdir cangls
691 0
|
安全 Linux 测试技术
Linux系统-【文件管理】mkdir命令 – 创建目录文件
mkdir命令来自于英文词组“make directories”的缩写,其功能是用来创建目录文件。使用简单,但需要注意若要创建的目标目录已经存在,则会提示已存在而不继续创建,不覆盖已有文件。而目录不存在,但具有嵌套的依赖关系,例如a/b/c/d/e/f,要想一次性创建则需要加入-p参数,进行递归操作。
159 0