Linux常用命令(2)

Linux常用命令(2)

1.1 file

在Windows中,可以通过某文件的图标或文件后缀,来判断这是一个什么类型的文件,如果是可执行文件,还是一个文本文件。但在Linux中,很多文件类型往往和后缀没有关系,所以我们要判断一个文件是什么类型的文件可以用file来判断。

file的用法如下

file /path/file

例如,判断/etc/hosts/是什么类型的文件,命令如下

[root@rh1 ~]# file /etc/hosts 
/etc/hosts: ASCII text

这里显示/etc/hosts 是一个文本文件

判断/boot/initramfs-4.18.0-348.el8.x86_64.img的文件类型,命令如下。

[root@rh1 ~]# file /boot/initramfs-4.18.0-348.el8.x86_64.img 
/boot/initramfs-4.18.0-348.el8.x86_64.img: ASCII cpio archive (SVR4 with no CRC)

这里显示/boot/initramfs-4.18.0-348.el8.x86_64.img是一个CPIO归档文件。

2. wc

wc用于统计文件的行数,单词数、字符数,先查看/etc/hosts中的内容

[root@rh1 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

第一个数字2表示 /etc/hosts 有两行。第二个数字10表示/etc/hosts有10个单词,这里单词指的是以空格、【TAB】、逗号隔开的字符。第三个数字158表示/etc/hosts一共有158个字符,这里统计字符数包括了空格及行末我们看不到的换行符。

以上几个信息也可以单独查看,wc -l可以查看文件的行数。

[root@rh1 ~]# wc -l /etc/hosts 
2 /etc/hosts

wc -w 可以查看文件的单词数

[root@rh1 ~]# wc -w /etc/hosts
10 /etc/hosts

wc -c 可以查看文件的字符数

[root@rh1 ~]# wc -c /etc/hosts
158 /etc/hosts

3. touch

touch 用于创建文件或者更新一个文件的时间,用法如下。

touch /path/file

如果 /path/file 不存在,则会把这个文件创建出来;如果存在,则会更新这个文件的时间。

查看/opt/ 目录中的内容

[root@rh1 ~]# ls /opt/
11  yy

如果文件不存在则会直接创建出来

[root@rh1 ~]# touch /opt/aa1.txt
[root@rh1 ~]# ls /opt/
11  aa1.txt  yy

查看/etc/hosts 文件的信息

[root@rh1 ~]# ls -l /etc/hosts
-rw-r--r--. 1 root root 158 9月  10 2018 /etc/hosts

然后对整个文件进行touch操作

[root@rh1 ~]# touch /etc/hosts
[root@rh1 ~]# ls -l /etc/hosts
-rw-r--r--. 1 root root 158 12月  1 11:28 /etc/hosts

可以看到,这个文件的时间更新了,这里并不会覆盖这个文件的内容,仅仅是更新了时间而已

4. rm

rm用于删除一个文件和目录

[root@rh1 ~]# rm /opt/aa1.txt
rm:是否删除普通空文件 '/opt/aa1.txt'?y

这里必须输入“ y ”,如果什么都不输入直接按【Enter】键,等同于输入“ n ”并按【Enter】键,即不删除的意思。

因为/opt/11是一个目录,所以可以在rm后加上 -rf 选项,-r 表示递归的意思,-f 表示强制的意思。

[root@rh1 ~]# rm -rf /opt/11/
[root@rh1 ~]# ls /opt/
yy

5. ln

ln 用于创建软链接,所谓软链接,就是Windows中的快捷方式

ln的用法如下。

ln -s 源文件 快捷方式

例如,给/opt/yy创建一个快捷方式/opt/zz,命令如下。

[root@rh1 ~]# ln -s /opt/yy /opt/zz
[root@rh1 ~]# ls -ls /opt/zz
0 lrwxrwxrwx. 1 root root 7 12月  1 11:47 /opt/zz -> /opt/yy

查看/opt/zz的属性,可以看到/opt/zz是指向/opt/yy的。删除/opt/zz,命令如下。

[root@rh1 ~]# rm -rf /opt/zz

6. alias

alias用于创建别名,对于一个复杂的命令,我们可以创建别名,以后执行别名即可。

alias 的用法如下。

alias 别名='命令'

为ifconfig ens160设置一个别名xx。

[root@rh1 ~]# alias xx='ifconfig ens160'
[root@rh1 ~]# xx

ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.25.170  netmask 255.255.255.0  broadcast 192.168.25.255
        inet6 fe80::20c:29ff:fe67:6b7f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:67:6b:7f  txqueuelen 1000  (Ethernet)
        RX packets 3122  bytes 236239 (230.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 941  bytes 87766 (85.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

可以看到,当执行xx命令时,实际上执行的是ifconfig ens160命令。

取消别名的语法如下

unalias 别名

取消xx这个别名,命令如下。

[root@rh1 ~]# unalias xx

7. cat

cat 用于查看比较小的(文本)文件

cat 用法如下

cat /path/file

查看/etc/hosts 中的内容,命令如下。

[root@rh1 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

8. more

但是对于比较大的文件 cat 就不太合适了,因为cat命令会很快地将文件的内容像翻书一样“翻过去”,直接翻到最后,我们可以用less或more命令。

more的用法如下

more /path/file

例如查看/etc/services 中的内容

[root@rh1 ~]# cat /etc/hosts

在这里插入图片描述

此时左下角显示有“更多”,说明还有更多的内容,此时按【Enter】键会一行一行地往下显示,按空格键会一页一页地往下显示(这里终端的大小就是一页的大小),按【q】键退出。

9. less

比more更灵活的命令是less,用法与more类似,

less /path/file

按【Enter】键会一行一行地往下显示,按空格键会一页一页地往下显示。不过 less支持按【PgUp】键往前翻页和按【PgDn】键往后翻页,也支持按【Home】键跳到开头和按【End】键跳到结束。

10. head

head默认查看文件的前10行,如果想查看文件前几行,有两种方法,命令如下。

head -n N /path/file

head ‐N /path/file

查看/etc/passwd 的前两行,命令如下

[root@rh1 ~]# head -n 2 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

或者

[root@rh1 ~]# head -2 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

11.tail

tail默认查看文件的后10行,如果想查看文件后几行,有两种方法,命令如下。

tail ‐n N /path/file

或者

 tail ‐N /path/file

查看/etc/passwd 的最后两行

[root@rh1 ~]# tail -n 2 /etc/passwd
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
bdqn:x:1000:1000:bdqn:/home/bdqn:/bin/bash

或者

[root@rh1 ~]# tail -2 /etc/passwd
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
bdqn:x:1000:1000:bdqn:/home/bdqn:/bin/bash
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值