Linux从0到1④文件管理(创建复制移动删除查看)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sunshine1_0/article/details/82770889

在这里插入图片描述


创建

1.文件(touch)

  • touch命令用于创建空白文件或者设置文件时间
  • 格式:touch [选项] [文件]
[root@tong ~]#touch file1.txt   //无则创建,有则修改时间
[root@tong ~]#touch file2 file3
[root@tong ~]#touch /home/file4.txt
[root@tong ~]#touch /home/{file5,file6}
[root@tong ~]#touch file{7..15}         //创建文件file7,file8...file15
[root@tong ~]#touch file{a..d}       //创建文件filea,fileb,filec,filed
[root@tong ~]#touch file{e,f,g}

2. 目录(mkdir)

  • 用于创建空白的的目录
  • 格式:mkdir [选项] 目录
  • 相关参数:-p 递归创建父目录
[root@tong ~]#mkdir dir1  //创建目录dir1
[root@tong ~]#mkdir /home/dir2  /home/dir3
[root@tong ~]#mkdir /home{dir4,dir5}
[root@tong ~]#mkdir -v /home/dir8/tong
[root@tong ~]#mkdir -pv /home/dir8/tong/dir9

复制

  • 用于复制文件或目录
  • 格式:cp [选项] 源文件 目标文件
  • 相关参数:-p 保留原始文件的属性
    -d 对象为“链接文件”,则保留该“链接文件”的属性
    -r 递归持续复制(用于目录)
    -i 若目标文件存在则询问是否覆盖
    -v 显示具体操作过程
    -a = -pdr

三种情况:
1)目标文件是目录,则会把源文件复制到该目录中

[root@tong ~]# cp -v anaconda-ks.cfg /home/
‘anaconda-ks.cfg’ -> ‘/home/anaconda-ks.cfg’
[root@tong ~]# ls /home/
alice  anaconda-ks.cfg 

2)目标文件也是普通文件,则会询问是否要覆盖它

[root@tong ~]# cp -v file1.txt /home/file04.txt 
cp: overwrite ‘/home/file04.txt’? y
‘file1.txt’ -> ‘/home/file04.txt’

3)目标文件不存在,则正常复制文件


移动(mv)

  • 用于剪切文件(会把源文件删除掉,只保留剪切后的文件)或者重命名
    (在同一个目录中对一个文件进行剪切=重命名)
  • 格式:mv [选项] 源文件 [目标文件|目标文件名]
[root@tong ~]# mv file1.txt /day4/file01.txt  //将file1.txt移到/day4目录下并重命名file01.txt
[root@tong ~]# ls /day4/
file01.txt
[root@tong ~]# mv /day4/file01.txt /day4/file02.txt  //重命名
[root@tong ~]# ls /day4/
file02.txt

删除(rm)

  • 用于删除文件或目录
  • 格式:rm [选项] 文件
  • 相关参数:
    -r 递归
    -f 强制删除
    -v 显示删除的详细过程

手动删除:

[root@tong ~]# rm -rfv /day4/    
removed ‘/day4/file02.txt’
removed directory: ‘/day4/’
[root@tong ~]# type -a cp    //cp默认是cp -i
cp is aliased to `cp -i'
cp is /usr/bin/cp

脚本删除


查看文件内容

  • cat:正序直接显示文件内容
[root@tong ~]# cat file1.txt   //显示文件内容
hello
world
[root@tong ~]# cat -n file1.txt     //-n显示行号
     1	hello
     2	world
[root@tong ~]# cat -A file1.txt  //-A显示包括控制字符(换行符,制表符)
hello$
world$
  • tac:逆序显示文件内容
[root@tong ~]# tac file1.txt 
world
hello
  • less more head
[root@tong ~]# head -2 /etc/passwd    //显示文件的前两行
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

  • tail
[root@tong ~]# tail -f /var/log/secure  //-f 动态查看文件的尾部
  • grep:可根据文件内容进行过滤
[root@tong ~]# grep 'root' /etc/passwd  //过滤文件中root的行
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@tong ~]# grep '^operator' /etc/passwd    //过滤以operator开头的行
operator:x:11:0:operator:/root:/sbin/nologin
[root@tong ~]# grep 'bash$' /etc/passwd      //过滤文件中以bash结尾的行
root:x:0:0:root:/root:/bin/bash
alice:x:1000:1000::/home/alice:/bin/bash

文件时间

访问时间:atime 即查看内容的时间,RHEL以后会延后修改时间
修改时间:mtime 即修改内容的时间
改变时间:ctime 即改变文件属性的时间,例如权限等
删除时间:dtime 即文件被删除的时间

[root@tong ~]# stat /etc/hostname    //查看文件详细属性
..........                           //省略
Access: 2018-09-29 14:14:58.119000683 +0800
Modify: 2018-09-22 23:48:41.144019526 +0800
Change: 2018-09-22 23:48:41.144019526 +0800
 Birth: -


查看文件类型

  • 方法一:
    ls -l 文件名 //查看第一个字符
    - 普通文件
    d 目录文件(蓝色)
    b 设备文件(块设备)存储设备硬盘,U盘
    c 设备文件(字符设备)打印机,终端
    s 套接字文件
    p 管道文件
    l 链接文件
[root@tong ~]# ls -l file1.txt     //ls -l 相当于 ll
-rw-r--r--. 1 root root 12 Sep 29 22:49 file1.txt
[root@tong ~]# ll anaconda-ks.cfg 
-rw-------. 1 root root 1243 Aug  4 16:32 anaconda-ks.cfg
[root@tong ~]# ll /dev/sda
brw-rw----. 1 root disk 8, 0 Sep 29 22:38 /dev/sda
  • 方法二:
    file 文件名
[root@tong ~]# file anaconda-ks.cfg 
anaconda-ks.cfg: ASCII text
[root@tong ~]# file /dev/sda
/dev/sda: block special

猜你喜欢

转载自blog.csdn.net/sunshine1_0/article/details/82770889
今日推荐