Linux系统命令从入门到完全忘记(一)

3,044次阅读
没有评论

共计 17066 个字符,预计需要花费 43 分钟才能阅读完成。

Linux系统命令从入门到完全忘记(一)

线上查询及帮助命令

man

概要:
查看命令或配置文件的帮助手册

语法格式:
man [Command]…
man [FILE]…

使用示例:

  • 查看命令的帮助
1
[root@ansheng ~]# man passwd
  • 查看配置文件的帮助信息
1
[root@ansheng ~]# man /etc/inittab

目录操作命令

ls

简介:
显示指定目录下的内容

语法格式:
ls [OPTION]… [FILE]…

参数:

参数 说明
-a 显示所有文件,包括隐藏文件
-A 不显示目录下面的”.”和”..”
-r 倒序显示文件
-t 以文件修改时间显示
-d 显示目录
-h 人类可读形式
-l 以长格式显示文件信息
-i 查看文件的i节点号
-F/-p 给不同文件结尾添加符号
–color 不同文件显示颜色
–time-style= [long-iso\full-iso\iso]时间的格式

使用示例:

  • 列出当前目录下所有包含”ansheng”的目录或者文件
1
2
3
4
[root@ansheng ~]# ls
ansheng.me  sadan32shengwe.me  sadansheng.me  sadanshengwe.me
[root@ansheng ~]# ls *ansheng*    
ansheng.me  sadansheng.me  sadanshengwe.me
  • 显示当前文件所在目录所有文件的详细信息,包括隐藏文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@ansheng ~]# ls -laA
total 36
dr-xr-x---.  2 root root 4096 Apr 21 09:33 .
dr-xr-xr-x. 22 root root 4096 Apr 21 09:13 ..
-rw-r--r--   1 root root    0 Apr 21 09:33 ansheng.me
-rw-------.  1 root root   28 Apr  9 18:29 .bash_history
-rw-r--r--.  1 root root   18 May 20  2009 .bash_logout
-rw-r--r--.  1 root root  176 May 20  2009 .bash_profile
-rw-r--r--.  1 root root  176 Sep 23  2004 .bashrc
-rw-r--r--.  1 root root  100 Sep 23  2004 .cshrc
-rw-r--r--   1 root root    0 Apr 21 09:33 sadan32shengwe.me
-rw-r--r--   1 root root    0 Apr 21 09:33 sadansheng.me
-rw-r--r--   1 root root    0 Apr 21 09:33 sadanshengwe.me
-rw-r--r--.  1 root root  129 Dec  4  2004 .tcshrc
-rw-------.  1 root root  741 Apr  9 18:08 .viminfo
  • 当文件过多时,ls要显示好几屏文件,如何显示最近修改的文件或目录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@ansheng ~]# cd /var/
[root@ansheng var]# ls -lrt
total 76
drwxr-xr-x.  2 root root 4096 Sep 23  2011 yp
drwxr-xr-x.  2 root root 4096 Sep 23  2011 preserve
drwxr-xr-x.  2 root root 4096 Sep 23  2011 opt
drwxr-xr-x.  2 root root 4096 Sep 23  2011 nis
drwxr-xr-x.  2 root root 4096 Sep 23  2011 local
drwxr-xr-x.  2 root root 4096 Sep 23  2011 games
drwxr-xr-x.  2 root root 4096 Nov 22  2013 cvs
drwxr-xr-x.  2 root root 4096 Mar 23 00:12 crash
drwxr-xr-x.  6 root root 4096 Mar 23 03:04 www
lrwxrwxrwx.  1 root root   10 Apr  9 17:58 mail -> spool/mail
drwxr-xr-x.  3 root root 4096 Apr  9 18:00 db
drwxr-xr-x.  3 root root 4096 Apr  9 18:03 empty
drwxr-xr-x. 11 root root 4096 Apr  9 18:03 spool
drwxr-xr-x.  2 root root 4096 Apr  9 18:03 account
drwxr-xr-x. 10 root root 4096 Apr  9 18:03 cache
drwxr-xr-x. 25 root root 4096 Apr  9 18:27 lib
drwxrwxrwt.  2 root root 4096 Apr  9 18:28 tmp
drwxrwxr-x.  5 root lock 4096 Apr  9 18:28 lock
drwxr-xr-x. 10 root root 4096 Apr 21 09:13 log
drwxr-xr-x. 19 root root 4096 Apr 21 09:13 run
  • 如何过滤出当前目录下中的所有一级目录?

当前目录下有用以下文件及目录

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@ansheng ~]# ll
total 28
drwxr-xr-x 2 root root 4096 Apr 21 09:40 ad
drwxr-xr-x 2 root root 4096 Apr 21 09:40 ansheng
-rw-r--r-- 1 root root    0 Apr 21 09:33 ansheng.me
drwxr-xr-x 2 root root 4096 Apr 21 09:40 d
drwxr-xr-x 2 root root 4096 Apr 21 09:40 gsdf
drwxr-xr-x 2 root root 4096 Apr 21 09:40 hf
drwxr-xr-x 2 root root 4096 Apr 21 09:40 r
-rw-r--r-- 1 root root    0 Apr 21 09:33 sadan32shengwe.me
-rw-r--r-- 1 root root    0 Apr 21 09:33 sadansheng.me
-rw-r--r-- 1 root root    0 Apr 21 09:33 sadanshengwe.me
drwxr-xr-x 2 root root 4096 Apr 21 09:40 wdg

第一种方法:

1
2
3
4
5
6
7
8
[root@ansheng ~]# ll -F|grep "/$"
drwxr-xr-x 2 root root 4096 Apr 21 09:40 ad/
drwxr-xr-x 2 root root 4096 Apr 21 09:40 ansheng/
drwxr-xr-x 2 root root 4096 Apr 21 09:40 d/
drwxr-xr-x 2 root root 4096 Apr 21 09:40 gsdf/
drwxr-xr-x 2 root root 4096 Apr 21 09:40 hf/
drwxr-xr-x 2 root root 4096 Apr 21 09:40 r/
drwxr-xr-x 2 root root 4096 Apr 21 09:40 wdg/

第二种方法:

1
2
3
4
5
6
7
8
[root@ansheng ~]# ll -F|grep "^d"
drwxr-xr-x 2 root root 4096 Apr 21 09:40 ad/
drwxr-xr-x 2 root root 4096 Apr 21 09:40 ansheng/
drwxr-xr-x 2 root root 4096 Apr 21 09:40 d/
drwxr-xr-x 2 root root 4096 Apr 21 09:40 gsdf/
drwxr-xr-x 2 root root 4096 Apr 21 09:40 hf/
drwxr-xr-x 2 root root 4096 Apr 21 09:40 r/
drwxr-xr-x 2 root root 4096 Apr 21 09:40 wdg/

小结:可以用ls –l或者ll显示文件的属性信息,然后通过-F参数给目录后面加个标示符,过滤出以“/”结尾的即可,或者过滤出以”d”开头的

tree

概要:
把目录以树状的格式列出,如果系统没有这个命令请使用以下指令进行安装:

1
[root@ansheng ~]# yum -y install tree

语法格式:
tree [OPTION]… [PATH]…

参数:

参数 说明
-a 把所有文件以目录树的格式显示
-d 把所有的目录以树状形式显示
-g 列出文件或目录的所属群组名称,没有对应的名称时,则显示群组识别码
-L 指定目录层次
-l 当显示的目录或文件问链接是,则同时显示源文件
-p 显示文件或目录的权限
-s 列出文件或目录大小
-u 显示文件或目录的所属主

使用示例:

  • 以树状的形式显示/usr目录下的所有一级目录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@ansheng ~]# tree -L 1 /usr
/usr
├── bin
├── etc
├── games
├── include
├── lib
├── lib64
├── libexec
├── local
├── sbin
├── share
├── src
└── tmp -> ../var/tmp

12 directories, 0 files
  • 以树状形式列出/root目录的所有文件并且显示用户组合用户主
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@ansheng ~]# tree -agu ./
./
├── [root     root    ]  ad
├── [root     root    ]  ansheng
├── [root     root    ]  ansheng.me
├── [root     root    ]  .bash_history
├── [root     root    ]  .bash_logout
├── [root     root    ]  .bash_profile
├── [root     root    ]  .bashrc
├── [root     root    ]  .cshrc
├── [root     root    ]  d
├── [root     root    ]  gsdf
├── [root     root    ]  hf
├── [root     root    ]  r
├── [root     root    ]  sadan32shengwe.me
├── [root     root    ]  sadansheng.me
├── [root     root    ]  sadanshengwe.me
├── [root     root    ]  .tcshrc
├── [root     root    ]  .viminfo
└── [root     root    ]  wdg

7 directories, 11 files
  • 以树状形式列出/etc/yum.repos.d目录,并且显示文件权限以及大小
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@ansheng ~]# cd /etc/yum.repos.d/
[root@ansheng yum.repos.d]# tree -sp ./
./
├── [-rw-r--r--        2572]  CentOS-Base.repo
├── [-rw-r--r--        1083]  epel.repo
└── [drwxr-xr-x        4096]  repo
    ├── [-rw-r--r--        1991]  CentOS-Base.repo
    ├── [-rw-r--r--         647]  CentOS-Debuginfo.repo
    ├── [-rw-r--r--         289]  CentOS-fasttrack.repo
    ├── [-rw-r--r--         630]  CentOS-Media.repo
    └── [-rw-r--r--        6259]  CentOS-Vault.repo

1 directory, 7 files

pwd

概要:
打印现在所在目录名

语法格式:
pwd [OPTION]…

参数:

参数 说明
-P 当所在目录为链接目录是,显示源目录

使用示例:

  • 显示当前所在目录
1
2
[root@ansheng ~]# pwd
/root
  • 当所在目录为链接目录时,要求显示源目录
1
2
3
4
5
6
7
8
[root@ansheng ~]# ln -s /etc/sysconfig ./
[root@ansheng ~]# ls -d sysconfig
sysconfig
[root@ansheng ~]# cd sysconfig/
[root@ansheng sysconfig]# pwd
/root/sysconfig
[root@ansheng sysconfig]# pwd -P
/etc/sysconfig

mkdir

使用示例:
创建一个或多个目录

语法格式:
mkdir [OPTION]… DIRECTORY…

参数:

参数 说明
-m 创建目录时同时设置目录权限
-p 递归创建目录
-v 显示目录创建过程

使用示例:

  • 请用一条命令在当前目录下创建名为ansheng3,ansheng6的目录,其中ansheng3中含有子目录ansheng6,ansheng5,ansheng4中含有子目录test,并用一条命令验证输出结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@ansheng ~]# mkdir -pv ./oldboy3/oldboy{4/test,5} ./oldboy6 && tree
mkdir: created directory `./oldboy3'
mkdir: created directory `./oldboy3/oldboy4'
mkdir: created directory `./oldboy3/oldboy4/test'
mkdir: created directory `./oldboy3/oldboy5'
mkdir: created directory `./oldboy6'
.
├── oldboy3
│   ├── oldboy4
│   │   └── test
│   └── oldboy5
└── oldboy6

5 directories, 0 files
  • 请用mkdir命令在/root目录下创建目录test,并且设置权限为700
1
2
[root@ansheng ~]# mkdir -m 700 test && ll -d test
drwx------ 2 root root 4096 Apr 21 09:58 test
  • 请用一条命令在当前下分别创建d1 d2 d3这三个同级目录
1
2
3
4
5
6
7
[root@ansheng ~]#  mkdir ./d1 ./d2 ./d3 && tree
.
├── d1
├── d2
└── d3

3 directories, 0 files

rmdir

概要:
删除目录,删除的这个目录必须是空目录

语法格式:
rmdir [OPTION]… DIRECTORY…

参数:

参数 说明
-p 递归删除空目录,删除子目录后当父目录为空目录时也一同删除
-v 显示命令执行过程

使用示例:

  • 删除linux/china这个目录,并且显示执行过程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@ansheng ~]# tree ./
./
├── gun
└── linux
    └── china

3 directories, 0 files
[root@ansheng ~]# rmdir -p -v linux/china && tree
rmdir: removing directory, `linux/china'
rmdir: removing directory, `linux'
.
└── gun

1 directory, 0 files

cd

概要:
更改当前所在目录,是系统内置命令

语法格式:
cd [OPTION]…
cd [directory]…

参数:

参数 说明
~ 用户家目录
返回上次所在目录
.. 返回所在目录的上一层目录

使用示例:

  • 当前目录在/tnp下,请用一条命令返回家目录(不可写家目录的绝对路径)
1
2
3
4
5
[root@ansheng tmp]# pwd
/tmp
[root@ansheng tmp]# cd ~
[root@ansheng ~]# pwd
/root
  • 假如我当前在/etc/yum.repos.d/下,然后又进入到了/etc/init.d/目录下,现在我希望快速返回上一次进入的目录,即/etc/yum.repos.d/目录,该如何做呢
1
2
3
4
5
6
7
8
9
[root@ansheng yum.repos.d]# pwd
/etc/yum.repos.d
[root@ansheng yum.repos.d]# cd /etc/init.d/
[root@ansheng init.d]# pwd
/etc/init.d
[root@ansheng init.d]# cd -
/etc/yum.repos.d
[root@ansheng yum.repos.d]# pwd
/etc/yum.repos.d
  • 当前所在目录/etc/yum.repos.d/,如何返回上一层目录,即/etc/
1
2
3
4
5
[root@ansheng yum.repos.d]# pwd
/etc/yum.repos.d
[root@ansheng yum.repos.d]# cd ../
[root@ansheng etc]# pwd
/etc

文件操作命令

touch

概要:
创建单个或多个文件,或者更改时间戳

语法格式:
touch [OPTION]… FILE…

使用示例:

  • 同时创建文件f1 f2 f3 f4 f5

第一种方法:

1
2
3
[root@ansheng ~]# touch f1 f2 f3 f4 f5
[root@ansheng ~]# ls
f1  f2  f3  f4  f5

第二种方法:

1
2
3
[root@ansheng ~]# touch f{1..5}
[root@ansheng ~]# ls
f1  f2  f3  f4  f5
  • 更改file文件的时间戳
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@ansheng ~]# stat /etc/issue
  File: `/etc/issue'
  Size: 47              Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d      Inode: 1054636     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-04-21 09:13:35.832310735 +0800
Modify: 2015-08-04 00:13:46.000000000 +0800
Change: 2016-04-09 18:01:45.370999965 +0800
[root@ansheng ~]# touch issue
[root@ansheng ~]# stat /etc/issue
  File: `/etc/issue'
  Size: 47              Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d      Inode: 1054636     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-04-21 09:13:35.832310735 +0800
Modify: 2015-08-04 00:13:46.000000000 +0800
Change: 2016-04-09 18:01:45.370999965 +0800

cp

概要:
复制文件或者目录

语法格式:
cp [OPTION]… SOURCE… DIRECTORY

参数:

参数 说明
-a 相当于同时执行”-dpR”参数
-b 备份目标文件,~表示备份的文件
-f 强行复制文件或目录, 不论目的文件或目录是否已经存在
-i 若源文件与目标文件重名则提示是否覆盖
-n 跳过已存在的文件或目录
-R/-r 复制源目录下面的所有文件包括目录
-p 保持源文件的属性
-s 为源文件创建连接

使用示例:

  • 复制/tmp/目录下的linux文件到/root目录下,已知/root目录下已有Linux这个文件,要求备份目标文件。
1
2
3
4
5
6
7
8
9
10
[root@ansheng ~]# echo "root" > linux
[root@ansheng ~]# echo "tmp" > /tmp/linux
[root@ansheng ~]# cp --backup /tmp/linux linux  
cp: overwrite `linux'? y
[root@ansheng ~]# ls
linux  linux~
[root@ansheng ~]# cat linux
tmp
[root@ansheng ~]# cat linux~ 
root
  • 复制当前目录下面的linux文件到/opt下,要求保持源文件属性
1
2
3
4
5
[root@ansheng ~]# ll linux 
-rw-r--r-- 1 root root 4 Apr 21 10:30 linux
[root@ansheng ~]# cp -p linux /opt/
[root@ansheng ~]# ll /opt/linux    
-rw-r--r-- 1 root root 4 Apr 21 10:30 /opt/linux
  • 为/etc/passwd文件创建一个链接文件到/root/下
1
2
3
[root@ansheng ~]# cp -s /etc/passwd /root/
[root@ansheng ~]# ls
passwd

mv

概要:
移动或更改文件/目录名

语法格式:

mv [参数] [源文件或目录] [目标文件或目录]

参数:

参数 说明
-b 如果源文件与目标文件同名时,则备份目标文件,用”~”区别
-f 如果目标文件已存在,则不提示直接覆盖
-i 若目标文件已存在,则提示是否覆盖
-u 若目标文件已经存在,且source比较新,才会更新(update)
-t 移动多个文件夹到一个目录

使用示例:

  • 将/root目录下面的test目录移动到/tmp目录下并且把目录名称改为“testlinux”
1
2
3
4
5
6
[root@ansheng ~]# ls
test
[root@ansheng ~]# mv test /tmp/testlinux
[root@ansheng ~]# ls
[root@ansheng ~]# ls -d /tmp/testlinux/
/tmp/testlinux/
  • 将/root目录下面的d1 d2 d3 d4目录移动到/tmp目录下面的d5目录中
1
2
3
[root@ansheng ~]# mv d1 d2 d3 d4 /tmp/d5
[root@ansheng ~]# ls /tmp/d5/
d1  d2  d3  d4
  • 已知/roo目录和/tmp目录都有linux这个文件,请将/root目录下面的linux移动到/tmp目录下,并且没有任何提示。
1
2
3
4
[root@ansheng ~]# mv -f /root/linux /tmp/
[root@ansheng ~]# ls /root/
[root@ansheng ~]# ls /tmp/
linux

rm

概要:
删除单个或多个文件和目录

语法格式:
rm [OPTION]… FILE…

参数:

参数 说明
-f 将目录下的文件及目录全部删除
-i 删除前询问
-r/-R 递归式的删除
-v 显示命令执行过程

使用示例:

  • 删除当前目录下的所有文件和目录,并且显示执行过程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@ansheng ~]# ls
asdasd  assgrf  d  df  dg  e  g  saf  sd  sdf
[root@ansheng ~]# rm -rfv ./*
removed `./asdasd'
removed directory: `./assgrf'
removed directory: `./d'
removed `./df'
removed `./dg'
removed directory: `./e'
removed `./g'
removed `./saf'
removed `./sd'
removed directory: `./sdf'
[root@ansheng ~]# ls

ln

概要:
创建文件或目录的链接

语法格式:
ln [参数] [源文件] [目标文件]

参数:

参数 说明
-s 创建软连接

使用示例:

  • 为/etc/passwd文件创建硬连接到当前目录下
1
2
3
4
[root@ansheng ~]# ln /etc/passwd ./
[root@ansheng ~]# ll
total 4
-rw-r--r--. 2 root root 1273 Apr  9 18:05 passwd
  • 为/boot目录创建软连接到当前目录
1
2
3
[root@ansheng ~]# ln -s /boot ./
[root@ansheng ~]# ls
boot

find

概要:
用于在文件树种查找文件,并作出相应的处理

语法格式:

find [目录] [option]

参数:

参数 说明
-name 按照文件名称查找文件
-user 按照文件属主查找文件
-group 按照文件属组查找文件
-mtime 按照文件的更改时间来查找文件,-n表示文件更改时间距现在n天以内,+n表示最近多少天
-type 按照文件类型查找文件
-follow 如果find命令遇到符号链接文件,就跟踪至链接所指向的文件。
-exec 执行命令,语法:-exec ‘command’ {} \;
! -name 排除某个文件或目录

使用示例:

  • 查找/var目录下面后缀为“.log”的所有文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@ansheng ~]# find /var/ -type f -name "*.log"
/var/log/anaconda.program.log
/var/log/dracut.log
/var/log/prelink/prelink.log
/var/log/audit/audit.log
/var/log/anaconda.log
/var/log/boot.log
/var/log/anaconda.yum.log
/var/log/anaconda.ifcfg.log
/var/log/yum.log
/var/log/anaconda.storage.log
``

- 查找/etc/sysconfig/network-scripts目录下包含eth0的所有文件,并且属组和主都是root,然后以长格式显示这个文件的信息

```bash
[root@ansheng ~]# find /etc/sysconfig/network-scripts/ -type f -name "*eth0*" -user root -group root -exec ls -l {} \;
-rw-r--r--. 1 root root 271 Apr  9 17:58 /etc/sysconfig/network-scripts/ifcfg-eth0
  • 已知 apache 服务的访问日志按天记录在服务器本地目录/app/logs 下,由于磁盘空间紧张,现在要求只能保留最近 7 天的访问日志!请问如何解决? 请给出解决办法或配置或处理命令

模拟日志脚本:

1
2
3
4
5
6
#!/bin/bash
for n in `seq 14`
do
        date -s "11/0$n/14"
        touch access_www_`(date +%F)`.log
done

创建的文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@ansheng logs]# ll
total 0
-rw-r--r--. 1 root root 0 Dec  1 00:00 access_www_2014-12-01.log
-rw-r--r--. 1 root root 0 Dec  2 00:00 access_www_2014-12-02.log
-rw-r--r--. 1 root root 0 Dec  3 00:00 access_www_2014-12-03.log
-rw-r--r--. 1 root root 0 Dec  4 00:00 access_www_2014-12-04.log
-rw-r--r--. 1 root root 0 Dec  5 00:00 access_www_2014-12-05.log
-rw-r--r--. 1 root root 0 Dec  6 00:00 access_www_2014-12-06.log
-rw-r--r--. 1 root root 0 Dec  7 00:00 access_www_2014-12-07.log
-rw-r--r--. 1 root root 0 Dec  8 00:00 access_www_2014-12-08.log
-rw-r--r--. 1 root root 0 Dec  9 00:00 access_www_2014-12-09.log
-rw-r--r--. 1 root root 0 Dec 10 00:00 access_www_2014-12-10.log
-rw-r--r--. 1 root root 0 Dec 11 00:00 access_www_2014-12-11.log
-rw-r--r--. 1 root root 0 Dec 12 00:00 access_www_2014-12-12.log
-rw-r--r--. 1 root root 0 Dec 13 00:00 access_www_2014-12-13.log
-rw-r--r--. 1 root root 0 Dec 14 00:00 access_www_2014-12-14.log

验证

1
2
3
4
5
6
7
8
9
10
11
[root@ansheng logs]# sh -c 'find /app/logs/ -type f -mtime +7 -name "*.log"|xargs rm -f' 
[root@ansheng logs]# ll
total 0
-rw-r--r--. 1 root root 0 Dec  7 00:00 access_www_2014-12-07.log
-rw-r--r--. 1 root root 0 Dec  8 00:00 access_www_2014-12-08.log
-rw-r--r--. 1 root root 0 Dec  9 00:00 access_www_2014-12-09.log
-rw-r--r--. 1 root root 0 Dec 10 00:00 access_www_2014-12-10.log
-rw-r--r--. 1 root root 0 Dec 11 00:00 access_www_2014-12-11.log
-rw-r--r--. 1 root root 0 Dec 12 00:00 access_www_2014-12-12.log
-rw-r--r--. 1 root root 0 Dec 13 00:00 access_www_2014-12-13.log
-rw-r--r--. 1 root root 0 Dec 14 00:00 access_www_2014-12-14.log

文件查看及处理命令

cat

概要:
三大功能:

  • 一次性输出整个文件的内容
  • 创建一个新的文件,只能创建新文件不能编辑已有的文件
  • 将多个文件合并为一个文件

语法格式:
cat [OPTION]… [FILE]…

参数:

参数 说明
-n 由1开始对输出内容的行数进行编号
-b 由1开始对输出内容的行进行编号,跳过空行不对其编号
-s 当遇到有连续两行以上的空白行,就代换为一行的空白行

使用示例:

  • 用cat命令将/etc/sysconfig/network-scripts/ifcfg-eth0的内容显示行号并且输入到testfile1里面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@ansheng ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0    
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=172.16.10.10
NETMASK=255.255.255.0
GATEWAY=172.16.10.2
DNS1=172.16.10.2
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME="System eth0"
[root@ansheng ~]# cat -n /etc/sysconfig/network-scripts/ifcfg-eth0 > textfile
[root@ansheng ~]# cat textfile
     1  DEVICE=eth0
     2  TYPE=Ethernet
     3  ONBOOT=yes
     4  NM_CONTROLLED=yes
     5  BOOTPROTO=static
     6  IPADDR=172.16.10.10
     7  NETMASK=255.255.255.0
     8  GATEWAY=172.16.10.2
     9  DNS1=172.16.10.2
    10  DEFROUTE=yes
    11  IPV4_FAILURE_FATAL=yes
    12  IPV6INIT=no
    13  NAME="System eth0"

高级应用:

  • 创建一个新的文件名为network并且输入内容”cat Advanced Applications”

方法1:

1
2
3
4
5
[root@ansheng ~]# cat > network
cat Advanced Applications
^C
[root@ansheng ~]# cat network 
cat Advanced Applications

方法2

1
2
3
4
5
[root@ansheng ~]# cat >>network<<EOF
> cat Advanced Applications
> EOF
[root@ansheng ~]# cat network 
cat Advanced Applications
  • 请将f1、f2、f3文件的内容追加到f4文件中
1
2
3
4
5
6
7
8
9
10
[root@ansheng ~]# cat f1 f2 f3
f1
f2
f3
[root@ansheng ~]# cat f4
[root@ansheng ~]# cat f1 f2 f3 >> f4
[root@ansheng ~]# cat f4
f1
f2
f3
  • 清空当前目录下面的networks文件内容
1
2
3
4
[root@ansheng ~]# cat network 
cat Advanced Applications
[root@ansheng ~]# cat /dev/null > network 
[root@ansheng ~]# cat network

概要:
输出文件头部内容,默认显示文件头部头10行

语法格式:
head [OPTION]… [FILE]…

参数:

参数 说明
-n 显示文件头几行内容,-n 5也可以写作-5,显示文件头5行内容

使用示例:

  • 输出文件/etc/inittab文件的头5行内容
1
2
3
4
5
6
7
8
9
10
11
12
[root@ansheng ~]# head -n 5 /etc/inittab  
# inittab is only used by upstart for the default runlevel.
#
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# System initialization is started by /etc/init/rcS.conf
[root@ansheng ~]# head -5 /etc/inittab    
# inittab is only used by upstart for the default runlevel.
#
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# System initialization is started by /etc/init/rcS.conf

tac

概要:
倒序的查看文件内容

语法格式:
tac file

使用示例:

  • 查看文件/etc/issue内容
1
2
3
4
5
6
7
8
[root@ansheng ~]# cat /etc/issue
CentOS release 6.7 (Final)
Kernel \r on an \m

[root@ansheng ~]# tac /etc/issue

Kernel \r on an \m
CentOS release 6.7 (Final)

tail

概要:
输出文件尾部内容,默认显示文件尾部10行

语法格式:
tail [OPTION]… [FILE]…

参数:

参数 说明
-n 显示文件尾部几行内容,-n 5也可以写作-5,显示文件尾部5行内容
-f 实时跟踪一个文件内容的变化
-F 实时监控文件状态,当文件被删掉时继续监控

使用示例:

  • 输出文件/etc/sysconfig/network-scripts/ifcfg-eth0文件的尾3行内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@ansheng ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=172.16.10.10
NETMASK=255.255.255.0
GATEWAY=172.16.10.2
DNS1=172.16.10.2
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME="System eth0"
[root@ansheng ~]# tail -n 3 /etc/sysconfig/network-scripts/ifcfg-eth0    
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME="System eth0"

cut

概要:
切割,取列,适合简单的内容选取

语法格式:
cut [-bn] [file]
cut [-c] [file]
cut [-df] [file]

参数:

参数 说明
-d 指定分隔符
-c 按照字符查找
-f 取列

使用示例:

  • 获取/etc/passwd文件中的用户名
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[root@ansheng ~]# cut -d ":" -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
uucp
operator
games
gopher
ftp
nobody
dbus
vcsa
abrt
haldaemon
ntp
apache
saslauth
postfix
sshd
stap-server
oprofile
tcpdump
  • 获取文件ansheng中5-9数字
1
2
3
4
[root@ansheng ~]# cat ansheng 
1 2 3 4 5 6 7 8 9 10
[root@ansheng ~]# cut -c 9-17 ansheng 
5 6 7 8 9

wc

概要:
统计指定文件中的字节数、字数、行数,并将统计结果显示输出。

语法格式:
wc [选项] 文件

参数:

参数 说明
-l 输出文件的行数
-c 显示一个文件的字节数
-m 显示一个文件的字符数
-L 显示一个文件中的最长行的长度
-w 显示一个文件的字数

使用示例:

  • 列出文件/etc/hosts的行数
1
2
[root@ansheng ~]# wc -l /etc/hosts
2 /etc/hosts

file

概要:
查看文件类型

语法格式:
file [文件]

使用示例:

  • 查看/etc/passwd文件类型
1
2
[root@ansheng ~]# file /etc/passwd
/etc/passwd: ASCII text

nl

概要:
显示文件内容的行号,默认空行不加行号。

语法格式:
nl file

使用示例:

  • 查看文件/etc/sysconfig/network-scripts/ifcfg-eth0的内容对应的行号
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@ansheng ~]# nl /etc/sysconfig/network-scripts/ifcfg-eth0 
     1  DEVICE=eth0
     2  TYPE=Ethernet
     3  ONBOOT=yes
     4  NM_CONTROLLED=yes
     5  BOOTPROTO=static
     6  IPADDR=172.16.10.10
     7  NETMASK=255.255.255.0
     8  GATEWAY=172.16.10.2
     9  DNS1=172.16.10.2
    10  DEFROUTE=yes
    11  IPV4_FAILURE_FATAL=yes
    12  IPV6INIT=no
    13  NAME="System eth0"

概要:
查看链接文件的源地址

语法格式:
Readlink linkdir/linkfile

使用示例:

  • 查看链接文件/etc/grub.conf文件的源地址
1
2
[root@ansheng ~]# readlink /etc/grub.conf 
../boot/grub/grub.conf

dos2unix

概要:
dos2unix命令用来将DOS格式的文本文件转换成UNIX格式的(DOS/MAC to UNIX text file format converter)。

1
[root@ansheng ~]# yum -y install dos2unix

语法格式:
dos2unix file1 file2 file3 …

使用示例:

  • 转换/etc/hosts的文件格式
1
2
[root@ansheng ~]# dos2unix /etc/hosts
dos2unix: converting file /etc/hosts to UNIX format ...

sort

概要:
sort命令是帮我们依据不同的数据类型进行排序

语法格式:

sort [-bcfMnrtk][源文件][-o 输出文件]

参数:

参数 说明
-b 忽略每行前面开始出的空格字符
-c 检查文件是否已经按照顺序排序
-f 排序时,忽略大小写字母
-M 将前面3个字母依照月份的缩写进行排序
-n 依照数值的大小排序
-o <输出文件> 将排序后的结果存入指定的文件
-r 以相反的顺序来排序
-t <分隔字符> 指定排序时所用的栏位分隔字符
-k 选择以哪个区间进行排序

使用示例:

  • sort将文件/文本的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@ansheng ~]# cat sort.txt 
aaa:10:1.1
ccc:30:3.3
ddd:40:4.4
bbb:20:2.2
eee:50:5.5
eee:50:5.5
[root@ansheng ~]# sort sort.txt 
aaa:10:1.1
bbb:20:2.2
ccc:30:3.3
ddd:40:4.4
eee:50:5.5
eee:50:5.5
  • 忽略相同行使用-u选项或者uniq
1
2
3
4
5
6
[root@ansheng ~]# sort -u sort.txt 
aaa:10:1.1
bbb:20:2.2
ccc:30:3.3
ddd:40:4.4
eee:50:5.5
  • sort的-n、-r、-k、-t选项的使用:
1
2
3
4
5
6
7
[root@ansheng ~]# sort -nk 2 -t: sort.txt 
aaa:10:1.1
bbb:20:2.2
ccc:30:3.3
ddd:40:4.4
eee:50:5.5
eee:50:5.5

-n是按照数字大小排序,-r是以相反顺序,-k是指定需要爱排序的栏位,-t指定栏位分隔符为冒号

uniq

概要:
uniq命令可以去除排序过的文件中的重复行,因此uniq经常和sort合用。也就是说,为了使uniq起作用,所有的重复行必须是相邻的。

语法格式:
uniq [OPTION]… [INPUT [OUTPUT]]

参数:

参数 说明
-i 忽略大小写字符的不同;
-c 进行计数
-u 只显示唯一的行
正文完
请博主喝杯咖啡吧!
post-qrcode
 
admin
版权声明:本站原创文章,由 admin 2016-07-12发表,共计17066字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码