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
目录
相关文章
|
Linux 应用服务中间件 Shell
二、Linux文本处理与文件操作核心命令
熟悉了Linux的基本“行走”后,就该拿起真正的“工具”干活了。用grep这个“放大镜”在文件里搜索内容,用find这个“探测器”在系统中寻找文件,再用tar把东西打包带走。最关键的是要学会使用管道符|,它像一条流水线,能把这些命令串联起来,让简单工具组合出强大的功能,比如 ps -ef | grep 'nginx' 就能快速找出nginx进程。
1235 1
二、Linux文本处理与文件操作核心命令
|
安全 Ubuntu Unix
一、初识 Linux 与基本命令
玩转Linux命令行,就像探索一座新城市。首先要熟悉它的“地图”,也就是/根目录下/etc(放配置)、/home(住家)这些核心区域。然后掌握几个“生存口令”:用ls看周围,cd去别处,mkdir建新房,cp/mv搬东西,再用cat或tail看文件内容。最后,别忘了随时按Tab键,它能帮你自动补全命令和路径,是提高效率的第一神器。
1955 58
|
存储 安全 Linux
Linux卡在emergency mode怎么办?xfs_repair 命令轻松解决
Linux虚拟机遇紧急模式?别慌!多因磁盘挂载失败。本文教你通过日志定位问题,用`xfs_repair`等工具修复文件系统,三步快速恢复。掌握查日志、修磁盘、验重启,轻松应对紧急模式,保障系统稳定运行。
1893 2
|
缓存 监控 Linux
Linux内存问题排查命令详解
Linux服务器卡顿?可能是内存问题。掌握free、vmstat、sar三大命令,快速排查内存使用情况。free查看实时内存,vmstat诊断系统整体性能瓶颈,sar实现长期监控,三者结合,高效定位并解决内存问题。
1279 0
Linux内存问题排查命令详解
|
Linux 数据安全/隐私保护 Windows
初学[Linux]基础命令“touch, cat, more, cp, rm, vm“
初学[Linux]基础命令“touch, cat, more, cp, rm, vm“
518 1
|
Linux Shell
Linux命令(86)之touch
Linux命令(86)之touch
425 0
|
SQL Linux
linux(十一)文件和目录相关命令touch和mkdir(2)
3:创建目录的时候,同时指定权限 语法:mkdir -m 755 xxx sql 复制代码 [root@iZuf60ynur81p6k0ysvtneZ opt]# mkdir -m 700 auth [root@iZuf60ynur81p6k0ysvtneZ opt]# ll -a total 24 drwxr-xr-x. 6 root root 4096 Aug 27 10:49 . dr-xr-xr-x. 18 root root 4096 Aug 10 19:00 .. drwx------ 2 root root 4096 Aug 27 10:49 auth -rw-r--r--
270 0
|
Ubuntu Linux Shell
linux第三课:目录文档操作命令(内含绝对/相对路径+1.pwd+2.cd+3.mkdir(创建目录)+4. rmdir(删除目录)+5. ls+6. cp+7.rm+8cat+9touch命令)
linux第三课:目录文档操作命令(内含绝对/相对路径+1.pwd+2.cd+3.mkdir(创建目录)+4. rmdir(删除目录)+5. ls+6. cp+7.rm+8cat+9touch命令)
708 0
linux第三课:目录文档操作命令(内含绝对/相对路径+1.pwd+2.cd+3.mkdir(创建目录)+4. rmdir(删除目录)+5. ls+6. cp+7.rm+8cat+9touch命令)