设为首页 收藏本站
查看: 605|回复: 0

[经验分享] Linux目录、文件的创建与删除

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-12-8 12:46:07 | 显示全部楼层 |阅读模式
目录的创建:
mkdir - make directories
使用示例及说明:
1
2
[iyunv@localhost ~]# mkdir /root/x/y/z
mkdir: cannot create directory `/root/x/y/z': No such file or directory



如果直接创建层级目录,是不能创建的。
1
[iyunv@localhost ~]# mkdir -p /root/x/y/z



如果带上“-p”就可以了,“-p”的意思是如果父目录不存在则创建父目录。
1
2
3
4
5
6
7
8
9
10
[iyunv@localhost ~]# tree /root
/root
|-- Desktop
|-- anaconda-ks.cfg
|-- install.log
|-- install.log.syslog
`-- x
    `-- y
        `-- z
4 directories, 3 files



tree命令可以查看目录树状结构。
1
2
3
4
5
[iyunv@localhost ~]# mkdir -pv /root/test/{x/y,z}
mkdir: created directory `/root/test'
mkdir: created directory `/root/test/x'
mkdir: created directory `/root/test/x/y'
mkdir: created directory `/root/test/z'



说明:/root/test/{x/y,z}等于/root/test/x/y /root/test/z}
-v:当创建目录后显示信息。
1
2
[iyunv@localhost ~]# rmdir -p /root/x/y/z
rmdir: /root: Directory not empty



说明:rmdir用于删除空目录,如果目录非空则不能删除;
      -p:用于删除目录和它的父目录(如果父目录是空的),此为选项。

      可以从本例中看出,删除z目录时,x和y目录一并被删除,但是因为/root非空无法将其删除。

      可以通过tree命令查看:

1
2
3
4
5
6
7
8
9
10
11
[iyunv@localhost ~]# tree /root
/root
|-- Desktop
|-- anaconda-ks.cfg
|-- install.log
|-- install.log.syslog
`-- test
    |-- x
    |   `-- y
    `-- z
5 directories, 3 files



从本例可以看出/root非空。
***********************分割线*************************
文件的创建于删除:
1
2
3
[iyunv@localhost ~]# touch /root/test/hello
[iyunv@localhost ~]# ls -l /root/test/hello
-rw-r--r-- 1 root root 0 Dec  7 03:42 /root/test/hello



touch本来的用途是用来修改文件的时间戳,却同时也可以用来创建空文件,如上例所示:
用# touch /root/test/hello创建了hello空文件,用ls -l /root/test/hello可以查看hello文件信息;
同时也可以使用stat查看文件状态:
1
2
3
4
5
6
7
8
[iyunv@localhost ~]# stat /root/test/hello
  File: `/root/test/hello'
  Size: 0         Blocks: 8          IO Block: 4096   regular empty file
Device: fd00h/64768dInode: 29851728    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2014-12-07 03:42:22.000000000 +0800
Modify: 2014-12-07 03:42:22.000000000 +0800
Change: 2014-12-07 03:42:22.000000000 +0800



可以看到hello的的访问(access)、修改(modify)、change(改变)时间一样;
1
2
3
4
5
6
7
8
9
[iyunv@localhost test]# touch -a hello
[iyunv@localhost test]# stat hello
  File: `hello'
  Size: 0         Blocks: 8          IO Block: 4096   regular empty file
Device: fd00h/64768dInode: 29851728    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2014-12-07 03:51:22.000000000 +0800
Modify: 2013-12-07 12:50:30.000000000 +0800
Change: 2014-12-07 03:51:22.000000000 +0800



使用-a选项可以修改文件的访问时间,同时改变时间也一并修改。
(注意我已经使用cd命令切换到了/test目录,所以才能直接把参数输入为“hello”)
1
2
3
4
5
6
7
8
9
[iyunv@localhost test]# touch -m -t 201312071250.30 hello
[iyunv@localhost test]# stat hello
  File: `hello'
  Size: 0         Blocks: 8          IO Block: 4096   regular empty file
Device: fd00h/64768dInode: 29851728    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2012-12-07 12:50:30.000000000 +0800
Modify: 2013-12-07 12:50:30.000000000 +0800
Change: 2014-12-07 03:47:35.000000000 +0800



说明:-m用来修改文件的修改时间
      -t用来直接设置时间,如果没有则使用当前时间,格式:[[CC]YY]MMDDhhmm[.ss]

使用nano命令可以编辑文件,比较简单,大家可以自行尝试。

1
2
[iyunv@localhost test]# rm hello
rm: remove regular empty file `hello'? y



rm命令用来删除文件。
删除后找不到hello文件了:
1
2
3
4
5
6
7
8
9
10
[iyunv@localhost test]# tree
.
|-- a_c
|-- a_d
|-- b_c
|-- b_d
|-- x
|   `-- y
`-- z
7 directories, 0 files




[iyunv@localhost test]# rm -rf /root/test
选项-r递归地删除/test及其包含的所有文件和目录,
    -f强制删除不提示。

可以看到/test目录已经没有了:
1
2
3
4
5
6
7
[iyunv@localhost ~]# tree
.
|-- Desktop
|-- anaconda-ks.cfg
|-- install.log
`-- install.log.syslog
1 directory, 3 files



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-37579-1-1.html 上篇帖子: Linux grep命令用法以及正则表达式 下篇帖子: CentOS-6.4-x86_64_Minimal Desktop安装 Linux
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表