第三章Linux文件管理和IO重定向(1)-文件操作命令

文件操作命令

1、列出文件内容-ls

ls [-alrtAFR] [name...]

选项:

  • -a 显示所有文件及目录 (. 开头的隐藏文件也会列出)
  • -l 除文件名称外,亦将文件型态、权限、拥有者、文件大小等资讯详细列出
  • -r 将文件以相反次序显示(原定依英文字母次序)
  • -t 将文件mtime之先后次序列出
  • -A 同 -a ,但不列出 "." (目前目录) 及 ".." (父目录)
  • -R 若目录下有文件,则以下之文件亦皆依序列出
  • -u配合-t选项,显示并按atime排序

显示 / 下面文件,并显示额外信息

[root@localhost ~]# ll / total 20 lrwxrwxrwx. 1 root root 7 May 10 2019 bin -> usr/bin dr-xr-xr-x. 6 root root 4096 Dec 6 10:53 boot drwxr-xr-x. 19 root root 3200 Dec 22 08:54 dev drwxr-xr-x. 78 root root 8192 Dec 19 07:37 etc drwxr-xr-x. 4 root root 48 Dec 14 11:55 home lrwxrwxrwx. 1 root root 7 May 10 2019 lib -> usr/lib lrwxrwxrwx. 1 root root 9 May 10 2019 lib64 -> usr/lib64

将 /bin 目录以下所有目录及文件详细资料列出 :

[root@localhost ~]# ls -lR /bin lrwxrwxrwx. 1 root root 7 May 10 2019 /bin -> usr/bin

列出目前工作目录下所有名称是 s 开头的文件,越新的排越后面

ls -ltr s*

2、查看文件状态stat

文件的数据分两种:

  • 一种元数据,既属性数据:metadata ()

  • 一种就是数据本身:data

文件时间戳

atime:访问时间(access time),指的是文件最后被读取的时间,可以使用touch命令更改为当前时间;

ctime:变更时间(change time),指的是文件本身最后被变更的时间,变更动作可以使chmod、chgrp、mv等等;

mtime:修改时间(modify time),指的是文件内容最后被修改的时间,修改动作可以使echo重定向、vi等等;

[root@localhost ~]# stat bb.txt File: bb.txt Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 802h/2050d Inode: 19461 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Context: unconfined_u:object_r:admin_home_t:s0 Access: 2020-12-23 11:16:10.167490395 -0500 Modify: 2020-12-23 11:16:10.167490395 -0500 Change: 2020-12-23 11:16:10.167490395 -0500 Birth: -

文件的元数据包含了文件的大小、块数量、IO块大小、文件类型、Inode号、硬链接数、访问权限、属主、属组、SELinux安全上下文和时间戳信息等内容。

3、创建空文件和刷新时间-touch

touch命令用于修改文件或者目录的时间属性,包括存取时间和更改时间。若文件不存在,系统会建立一个新的文件。

touch [-acfm] [-d<日期时间>][-r<参考文件或目录>] [-t<日期时间>] file

  • -a 改变atime和ctime时间
  • -m 改变mtime和的ctime时间
  • -t 设定档案的时间记录,格式与 date 指令相同;改变aime和mtime时间
  • -c文件不存在,不创建

4、复制文件和目录-cp

   cp [options] source dest    cp [options] source... directory

  • -a:此选项通常在复制目录时使用,它保留链接、文件属性,并复制目录下的所有内容。其作用等于dpR参数组合。
  • -d:复制时保留链接。这里所说的链接相当于Windows系统中的快捷方式。
  • -f:覆盖已经存在的目标文件而不给出提示。
  • -i:与-f选项相反,在覆盖目标文件之前给出提示,要求用户确认是否覆盖,回答"y"时目标文件将被覆盖。
  • -p:除复制文件的内容外,还把修改时间和访问权限也复制到新文件中。
  • -r:若给出的源文件是一个目录文件,此时将复制该目录下所有的子目录和文件。
  • -l:不复制文件,只是生成链接文件

将当前目录下的所有文件复制到新目录 /home/newtest 下

[root@MiWiFi-R4CM-srv test]# cp -r . /home/newtest

将文件复制到新目录/home/newtest

[root@MiWiFi-R4CM-srv test]# cp litao laowang /home/newtest/

创建/testdir/rootdir目录并复制/root下所有文件到该目录内,要求保留原有权限。

cp -rp /root/* testdir/rootdir/

5、移动和重命名-mv

mv [options] source dest mv [options] source... directory

  • -b: 当目标文件或目录存在时,在执行覆盖前,会为其创建一个备份。
  • -i: 如果指定移动的源目录或文件与目标的目录或文件同名,则会先询问是否覆盖旧文件,输入 y 表示直接覆盖,输入 n 表示取消该操作。
  • -f: 如果指定移动的源目录或文件与目标的目录或文件同名,不会询问,直接覆盖旧文件。
  • -n: 不要覆盖任何已存在的文件或目录。
  • -u:当源文件比目标文件新或者目标文件不存在时,才执行移动操作。

将当前文件及目录移动到文件夹ingo中

[root@localhost litao]# ls passwd_bak passwd_link passwd_link2 passwd_link4 shadowlink testdir [root@localhost litao]# mkdir ingo [root@localhost litao]# mv passwd_* shadowlink testdir/ ingo/ [root@localhost litao]# ls ingo/ passwd_bak passwd_link passwd_link2 passwd_link4 shadowlink testdir

将 ingo 目录放入 logs 目录中。注意,如果 logs 目录不存在,则该命令将 info 改名为 logs。

[root@localhost litao]# mv ingo/ logs

6、删除文件-rm

rm [options] name...

  • -i 删除前逐一询问确认。
  • -f 即使原档案属性设为唯读,亦直接删除,无需逐一确认。
  • -r 将目录及以下之档案亦逐一删除。

删除文件可以直接使用rm命令,若删除目录则必须配合选项"-r",例如:

[root@localhost logs]# rm -f ingo/ rm: cannot remove 'ingo/': Is a directory [root@localhost logs]# rm -rf ingo/

7、查看文件类型 file 查看ASCII格式  dos\unix格式转换

[root@localhost ~]# file anaconda-ks.cfg
anaconda-ks.cfg: ASCII text
[root@localhost ~]# file test
test: directory
[root@localhost ~]# file win.txt
win.txt: ASCII text, with no line terminators
[root@localhost ~]# hexdump win.txt  //查看ASCII码格式
0000000 6977 646e 736f 6620 6c69 0065
000000b

[root@localhost ~]# dos2unix  win.txt
dos2unix: converting file win.txt to Unix format...
[root@localhost ~]# file win.txt
win.txt: ASCII text, with no line terminators 

8、文件通配符

[root@localhost ~]# ls file?  //匹配file 1个任意字符
file0  file1  file2  file3  file4  file5  file6  file7  file8  file9
[root@localhost ~]# ls file?? //匹配file文件2个任意字符
file10  file16  file22  file28  file34  file40  file46  file52  file58  file64  file70  file76  file82  file88  file94
[root@localhost ~]# ls .*  //查看隐藏文件

 创建一个昨天的文件

posted @ 2020-12-24 01:37  湘北10#  阅读(183)  评论(0编辑  收藏  举报