Centos常用命令之:文件操作

在centos中,常用的文件操作命令有:

touch:建置新文件或者修改文件时间
cat:从第一行开始显示文件内容
tac:从最后一行开始显示文件内容,和cat相反
nl:显示的时候,顺道输出行号
more:一页一页的显示文件内容
less:与more类似,但是可以向前翻页
head:只看头几行
tail:只看尾巴几行
od:以二进制的方式读取文件内容

详细使用:

touch

这个命令主要用来创建一个空白的文件 。

使用方式:touch [选项]  文件

常用选项:

-a:仅修订access time

-c:仅修改文件的时间,若该文件不存在则不创建新文件

-d:后面接欲修订的日期,而不用当前日期。也可以使用-date="日期或时间"

-m:仅修改mtime

-t:后面可以接欲修订的时间而不用目前的时间。格式为:[YYYYMMDDhhmm]

我们知道,当我们用ls -l 命令查看文件的时候,会有一个最新修改时间显示出来。

实际上,在linux中,文件的时间分为三个:

  ·modification time(mtime):当文件的内容变更时,会更新这个时间(不包括文件的属性和权限)。默认显示。

  ·status time(ctime):当文件的状态改变的时候,会更新这个时间。(文件的属性和权限之类)使用ls -l --time=ctime  xxx 来查看。

  ·access time(atime):当文件的内容被读取的时候,会更新这个时间。使用ls -l --time=atime xxx来查看。

举例:

[fuwh@localhost stu]$ touch test.txt
[fuwh@localhost stu]$ ll
总用量 0
-rw-rw-r--. 1 fuwh fuwh 0 7月  24 16:40 test.txt
[fuwh@localhost stu]$ ll --time=ctime test.txt
-rw-rw-r--. 1 fuwh fuwh 0 7月  24 16:40 test.txt
[fuwh@localhost stu]$ ll --time=atime test.txt
-rw-rw-r--. 1 fuwh fuwh 0 7月  24 16:40 test.txt
[fuwh@localhost stu]$ vi test.txt
[fuwh@localhost stu]$ ll
总用量 4
-rw-rw-r--. 1 fuwh fuwh 5 7月  24 16:41 test.txt
[fuwh@localhost stu]$ date
2017年 07月 24日 星期一 16:41:57 JST
[fuwh@localhost stu]$ ll --time=atime test.txt
-rw-rw-r--. 1 fuwh fuwh 5 7月  24 16:41 test.txt
[fuwh@localhost stu]$ date
2017年 07月 24日 星期一 16:42:16 JST
[fuwh@localhost stu]$ cat test.txt
name
[fuwh@localhost stu]$ ll --time=atime test.txt
-rw-rw-r--. 1 fuwh fuwh 5 7月  24 16:42 test.txt
[fuwh@localhost stu]$

cat和tac

cat主要用来查看文件内容,使用方式:

cat [选项] 文件

常用选项:

-b:列出行号(空白行不做行号显示)

-n:列出行号,空白行也会有行号

-E:将结尾的断行符$显示出来

-T:将[tab]按键以^I显示出来

-v:列出一些看不出的字符。

tac:跟cat相反

例子:

[fuwh@localhost stu]$ cat test.txt
name
aba
cc

d
[fuwh@localhost stu]$ tac test.txt
d

cc
aba
name
[fuwh@localhost stu]$ cat -b test.txt
     1  name
     2  aba
     3  cc

     4  d
[fuwh@localhost stu]$ cat -n test.txt
     1  name
     2  aba
     3  cc
     4
     5  d
[fuwh@localhost stu]$ cat -T test.txt
name^I^I
aba
cc

d
[fuwh@localhost stu]$ cat -E test.txt
name            $
aba$
cc$
$
d$
[fuwh@localhost stu]$ cat -v test.txt
name
aba
cc

d
[fuwh@localhost stu]$

nl

使用方式:nl [选项] 文件

选项与参数:
-b  :指定行号指定的方式,主要有两种:
      -b a :表示不论是否为空行,也同样列出行号(类似 cat -n);
      -b t :如果有空行,空的那一行不要列出行号(默认值);
-n  :列出行号表示的方法,主要有三种:
      -n ln :行号在萤幕的最左方显示;
      -n rn :行号在自己栏位的最右方显示,且不加 0 ;
      -n rz :行号在自己栏位的最右方显示,且加 0 ;
-w  :行号栏位的占用的位数。

more与less

more:表示每次显示一页内容在屏幕上,如果内容大于一页的话,会在屏幕底端显示如下内容。退出之间按q就可以了。

--More--(36%):红色字体代表目前显示了整个文档的百分比。

在more查看的时候,支持以下几个按键来查看内容。

  • 空白键 (space):代表向下翻一页;
  • Enter         :代表向下翻『一行』;
  • /字串         :代表在这个显示的内容当中,向下搜寻『字串』这个关键字;
  • :f            :立刻显示出档名以及目前显示的行数;
  • q             :代表立刻离开 more ,不再显示该文件内容。
  • b 或 [ctrl]-b :代表往回翻页,不过这动作只对文件有用,对管线无用。

less:使用起来和more差不多,支持以下几个操作:

  • 空白键    :向下翻动一页;
  • [pagedown]:向下翻动一页;
  • [pageup]  :向上翻动一页;
  • /字串     :向下搜寻『字串』的功能;
  • ?字串     :向上搜寻『字串』的功能;
  • n         :重复前一个搜寻 (与 / 或 ? 有关!)
  • N         :反向的重复前一个搜寻 (与 / 或 ? 有关!)
  • q         :离开 less 这个程序;

head与tail

head用来显示前面几行,默认全部显示,使用方式:

head [-n 数字] 文件

-n:后面接数字,代表显示几行的意思。

[fuwh@localhost stu]$ head test
1
2
3
4
5
6
7
8
9
0
[fuwh@localhost stu]$ head -n3 test
1
2
3
[fuwh@localhost stu]$

tail:和head相反,用来取出后面的几行

使用方式:tail [-n 数字] 文件

[fuwh@localhost stu]$ tail test
1
2
3
4
5
6
7
8
9
0
[fuwh@localhost stu]$ tail -n4 test
7
8
9
0
[fuwh@localhost stu]$

od

当我们用普通的方式读取一些二进制文件的时候会出现乱码。这时候我们就需要用od命令来读取这些二进制文件了。

用法:od [-t TYPE] 文件

-t  :后面可以接各种『类型 (TYPE)』的输出,例如:
      a       :利用默认的字节来输出;
      c       :使用 ASCII 字节来输出
      d[size] :利用十进位(decimal)来输出数据,每个整数占用 size bytes ;
      f[size] :利用浮点数值(floating)来输出数据,每个数占用 size bytes ;
      o[size] :利用八进位(octal)来输出数据,每个整数占用 size bytes ;
      x[size] :利用十六进位(hexadecimal)来输出数据,每个整数占用 size bytes ;
原文地址:https://www.cnblogs.com/zerotomax/p/7225312.html