Linux操作文档——目录和文件管理


一、查看及检索文件

1、查看文件内容

1、 cat命令:显示并连接文件的内容

查看/etc/sysconfig/network-scripts/ifcfg-ens33配置文件中的内容,并了解第一块以太网卡的配置信息

[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33 
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
IPADDR=192.168.1.10
GATEWAY=192.168.1.1
NETMASK=255.255.255.0
DNS1=114.114.114.114
DNS2=223.5.5.5
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=be069d20-5065-4834-9f5e-3e756241c439
DEVICE=ens33
ONBOOT=yes

依次显示/etc/redhat-release(CnetOS系统的发行版本信息)、/proc/version(系统内核及开发环境、时间等信息)文件的内容

[root@localhost ~]# cat /etc/redhat-release /proc/version 
CentOS Linux release 7.8.2003 (Core)
Linux version 3.10.0-1127.8.2.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) ) #1 SMP Tue May 12 16:57:42 UTC 2020
2、more和less命令:分页查看文件内容

more:Enter键向下逐行滚动查看,按Space键可以向下翻一屏,按b键向上翻一屏,按q键退出并返回原来的命令环境
分屏查看/etc/yum.conf (yum配置文件)的内容

[root@localhost ~]# more /etc/yum.conf
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://b
ugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release


#  This is the default, if you make this bigger yum won't see if the metadata
# is newer on the remote and so you'll "gain" the bandwidth of not having to
# download the new metadata and "pay" for it by yum not having correct
# information.
#  It is esp. important, to have correct metadata, for distributions like
# Fedora which don't keep old packages around. If you don't like this checking
# interupting your command line usage, it's much better to have something
# manually check the metadata once an hour (yum-updatesd will do this).
--More--(89%)

分页查看/etc/目录下扩展名为".conf"的配置文件

-rw-r--r--. 1 root root   55 88 2019 /etc/asound.conf
-rw-r--r--. 1 root root 1.1K 88 2019 /etc/chrony.conf
-rw-r--r--. 1 root root 1.3K 930 2020 /etc/dracut.conf
-rw-r--r--. 1 root root  112 930 2020 /etc/e2fsck.conf
-rw-r--r--. 1 root root   38 1030 2018 /etc/fuse.conf
-rw-r--r--. 1 root root    9 67 2013 /etc/host.conf
-rw-r--r--. 1 root root 7.2K 59 20:51 /etc/kdump.conf
-rw-r--r--. 1 root root  646 930 2020 /etc/krb5.conf
-rw-r--r--. 1 root root   28 228 2013 /etc/ld.so.conf
-rw-r-----. 1 root root  191 32 2019 /etc/libaudit.conf
-rw-r--r--. 1 root root 2.4K 1013 2013 /etc/libuser.conf
-rw-r--r--. 1 root root   19 59 20:53 /etc/locale.conf
-rw-r--r--. 1 root root  662 731 2013 /etc/logrotate.conf
-rw-r--r--. 1 root root 5.1K 1031 2018 /etc/man_db.conf
-rw-r--r--. 1 root root 1.1K 930 2020 /etc/mke2fs.conf
-rw-r--r--. 1 root root 2.0K 59 20:50 /etc/nsswitch.conf
-rw-r--r--. 1 root root   78 59 20:54 /etc/resolv.conf
-rw-r--r--. 1 root root 3.2K 1013 2020 /etc/rsyslog.conf
-rw-r--r--. 1 root root  216 41 2020 /etc/sestatus.conf
-rw-r-----. 1 root root 1.8K 930 2020 /etc/sudo.conf
-rw-r-----. 1 root root 3.2K 930 2020 /etc/sudo-ldap.conf
-rw-r--r--. 1 root root  449 1013 2020 /etc/sysctl.conf
-rw-r--r--. 1 root root   37 59 20:53 /etc/vconsole.conf
--More--

less:结合管道符"|"分屏查看执行命令时输出的信息时,既可以向下翻页,也可以向上翻页

3、head用于显示文件开头的一部分内容,tail显示文件末尾的一部分内容

使用"n"选项(n为具体的行数)指定需要显示多少行的内容,若不指定行数,默认只显示十行
查看用户账号文件/etc/passwd开头第一行至第四行的部分内容

[root@localhost ~]# head -4 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin

查看系统公共日志文件/var/log/messages的最后十行内容,并在末尾跟踪显示该文件中新记录的内容(按Ctrl+C组合键终止)

May  9 21:10:34 localhost systemd: Starting Cleanup of Temporary Directories...
May  9 21:10:34 localhost systemd: Started Cleanup of Temporary Directories.
May  9 21:28:47 localhost yum[8298]: Installed: tree-1.6.0-10.el7.x86_64
May  9 21:28:55 localhost systemd: Got automount request for /proc/sys/fs/binfmt_misc, triggered by 8333 (tree)
May  9 21:28:55 localhost systemd: Mounting Arbitrary Executable File Formats File System...
May  9 21:28:56 localhost systemd: Mounted Arbitrary Executable File Formats File System.
May  9 21:50:26 localhost su: (to zeng) root on pts/0
May  9 22:01:01 localhost systemd: Started Session 4 of user root.
May  9 22:05:03 localhost yum[18030]: Installed: finger-0.17-52.el7.x86_64
May  9 22:10:43 localhost yum[18334]: Installed: ftp-0.17-67.el7.x86_64

2、统计文件内容

1、wc命令:统计文件内容中的单词数量、行数等信息
  -c:统计文件内容中的字节数。
  -l:统计文件内容中的行数。
  -w:统计文件内容中的单词个数(以空格或制表位作为分隔)

统计出当前linux操作系统中拥有的用户账号数量

[root@localhost ~]# wc -l /etc/passwd
23 /etc/passwd

使用不带任何选项的wc命令时,默认将统计指定文件的字节数、行数、单词个数
统计出/etc/hosts文件中共包含几行、几个单词、几字节的内容

[root@localhost ~]# wc /etc/hosts
  2  10 158 /etc/hosts

二、文本处理

1、grep命令

文本搜索工具,根据用户指定的“模式(过滤条件)”对目标文本逐行进行匹配检查;打印匹配到的行;
1、基本用法

  -I:查找内容时忽略大小写
  -v:反转查找,即输出与查我条件不相符的行

查找名为ftp的用户账号的信息

[root@localhost ~]# grep "ftp" /etc/passwd
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

显示/etc/yum.conf 文件中以开头的行和空行以外的内容

[root@localhost ~]# grep -v "^#" /etc/yum.conf | grep -v "^$"
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release

2、正则表达式

字符匹配
.匹配单个字符
[ ]匹配范围内的任意单个字符
匹配次数
\ ?匹配前面的字符0次或一次,意味着可有可无
*匹配前面的字符0次或任意次
.*匹配任意长度的任意字符
\ +匹配前面的字符1次或任意次,意味着至少一次
\ {n\ }匹配其前面的字符n次
\ {n,m\ }匹配其前面的字符至少n次,至多m次
\ {0,n\ }匹配前面的字符至多n次
\ {m,\ }至少m次
位置限定
^行首锚定;用于模式的最左侧
$行尾锚定;用于模式的最右侧
^PATTERN$用于PATTERN来匹配整行
^$空白行
\ < 或 \b词首锚定,用于单词模式的左侧
\ > 或 \b词尾锚定,用于单词模式的右侧
\ <PATTERN\ >匹配完整单词
其他及特殊的POSIX字符
\w匹配字母、数字和下划线等于[A-Za-z0-9]
\W匹配非字母、非数字和非下划线[^A-Za-z0-9]
\n匹配一个换行符
\r匹配一个回车符
\t匹配一个制表符
\s匹配任何空白字符
\S匹配任何非空白字符
\f匹配一个换页符
[[:alnum:]]文字数字字符(除去符号之外,数字、字母都可以包括中文)
[[:alpha:]]文字字符(字母,包括中文)
[[:digit:]]数字字符(阿拉伯数字)
[[:graph:]]非空字符(排除空格、tab键)
[[:print:]]非空字符(包括空格,空格认为非空,tab键不适用)
[[:lower:]]小写字符
[[:upper:]]大写字符
[[:cntrl:]]控制字符
[[:punct:]]标点,符号(包括常用符号)
[[:space:]]所有空白字符(新行,空格,制表符)
[[:xdigit:]]十六进制数(0-9,a-f,A-F)

2、sed命令

逐行进行处理数据

常用参数说明
-e多条件编辑
-r支持扩展正则表达式
-n只显示匹配出的行
-f指定sed脚本
-i直接修改源文件
=显示文件行号

二、文件搜索

1、find命令

1、全局性搜索文件

工作方式:沿着文件的层次结构依次向下搜索,找到符合条件的,打印或者是执行相应的操作
在/etc目录下,寻找名字为network的文件

[root@localhost ~]# find /etc/ -name network
/etc/rc.d/init.d/network
/etc/sysconfig/network
/etc/vmware-tools/scripts/vmware/network
2、按条件查找

(1)按照文件名搜索

-name:按名字查找 代表任意字符,? 代表单个字符)
-iname:忽略大小写

查找/etc目录下,所有以.conf结尾的文件

[root@localhost ~]# find /etc/ -name *.conf
/etc/resolv.conf
/etc/fonts/conf.d/61-urw-d050000l.conf
/etc/fonts/conf.d/31-cantarell.conf
/etc/fonts/conf.d/66-sil-nuosu.conf
/etc/fonts/conf.d/59-liberation-sans.conf
(略)

查找/etc/目录下,以.conf结尾,名称是5个字符的

[root@localhost ~]# find /etc/ -name ?????.conf
/etc/fonts/fonts.conf
/etc/libreport/plugins/mailx.conf
/etc/prelink.conf.d/grub2.conf
/etc/security/group.conf
/etc/rdma/modules/iwarp.conf
/etc/rdma/modules/iwpmd.conf
/etc/ld.so.conf
(略)
3、按照文件类型查找 -type

f:普通文件
查找系统中类型是套接字的文件

[root@localhost ~]# find / -type s

可以将查找中的错误提示重定向到“黑洞”(/dev/null)

[root@localhost ~]# find / -type s 2>/dev/null
3、按照时间查找

atime:访问时间
mtime:文件的内容发生变化的时间
ctime:文件的属性发生变化的时间
-atime n 以天为单位
-amin n 以分钟为单位(-cmin、-mmin)

搜索最近七天内被访问过的所有文件

[root@localhost ~]# find . -type f -atime -7

搜索恰好在七天前被访问过的所有文件

[root@localhost ~]# find . -type f -atime 7

搜索超过七天内被访问过的所有文件

[root@localhost ~]# find . -type f -atime +7

搜索访问时间超过10分钟的所有文件

[root@localhost ~]# find . -type f -amin +10
4、按照用户和组查找

-user 用户名
-group 组名
-uid uid
-gid gid
-nouser:孤儿文件 没有所有者的文件
-nogroup:没有所属组的文件

查找系统中所有者是quota2的文件

[root@localhost ~]# find / -user quota2 -type f

查找系统中的孤儿文件

[root@localhost ~]# find . -type f -nouser
5、按照权限查找 -perm

查找find目录下,小组权限为可写的文件

[root@localhost find]# ll `find ./ -perm -g=w -type f`
[root@localhost find]# ll `find ./ -perm /g=w -type f`

查找find目录下,用户可写and组可写and其他人可写的文件

[root@localhost find]# ll `find ./ -perm -222 -type f`

查找find目录下,用户可写or组可写or其他人可写

[root@localhost find]# ll `find ./ -perm /222 -type f`
6、按照文件大小查找 -size

查找目录下,文件大小小于3M大小的文件

[root@localhost find]# find . -type f -size -3M

查找目录下,文件大小等于3M大小的文件

[root@localhost find]# find . -type f -size 3M

查找目录下,文件大小大于3M大小的文件

[root@localhost find]# find . -type f -size +3M

三、备份与恢复文档

1、gzip 和 gunzip命令

当前目录下的mkfile文件进行 压缩,生成压缩文件mkfile.gz

[root@work01 ~]# ls -lh mkfile*
-rw-r--r-- 1 root root 0 May 10 09:45 mkfile
[root@work01 ~]# gzip mkfile
[root@work01 ~]# ls -lh mkfile*
-rw-r--r-- 1 root root 27 May 10 09:45 mkfile.gz

将压缩文件mkfile.gz进行解压缩

[root@work01 ~]# gzip -d mkfile.gz

或者

[root@work01 ~]# gunzip mkfile.gz

2、bzip2和bunzip2命令

对当前目录下的mkfile文件以较高压缩比进行压缩

[root@work01 ~]# bzip2 -9 mkfile
[root@work01 ~]# ls -lh mkfile*
-rw-r--r-- 1 root root 14 May 10 09:45 mkfile.bz2

将压缩文件mkfile.bz2进行解压缩

[root@work01 ~]# bzip2 -d mkfile.bz2

或者

[root@work01 ~]# bunzip2 mkfile.bz2

3、使用tar归档和释放工具

-c:创建.tar格式的包文件。
-C:解压时指定释放的目标文件夹。
-f:表示使用归档文件。
-j:调用bzip2程序进行压缩或解压。
-p:打包时保留文件及目录的权限。
-P:打包时保留文件及目录的绝对路径。
-t:列表查看包内的文件。
-v:输出详细信息。
-x:解开.tar格式的包文件。
-z:调用gzip程序进行压缩或解压。

1、制作归档备份文件

对/etc和/boot目录进行备份,在当前目录下生成名为sysfile.tar.gz的归档压缩包,执行过程中可以看到被归档的文件列表信息

[root@work01 /]# tar zcvf sysfile.tar.gz etc boot
etc/
etc/fstab
etc/crypttab
etc/mtab
etc/resolv.conf
etc/grub.d/
etc/grub.d/00_header
etc/grub.d/01_users
etc/grub.d/10_linux
etc/grub.d/20_linux_xen
etc/grub.d/20_ppc_terminfo
etc/grub.d/30_os-prober
etc/grub.d/40_custom
etc/grub.d/41_custom
etc/grub.d/README
etc/grub.d/00_tuned

对/etc/passwd、/etc/shadow文件进行备份,在/tmp目录下生成名为usershome. tar.bz2 的归档压缩包

[root@work01 /]# tar jcvf /tmp/usershome.tar.bz2 passwd shadow
2、从归档文件中恢复数据

从备份文件usershome.tar.bz2中恢复数据,释放到/etc目录下(将覆盖现有文件)

[root@work01 /]# tar jxf /tmp/usershome.tar.bz2 -C /etc

四、使用vi文本编辑器

1、命令模式中的基本操作

1、模式切换

  a:在当前光标位置之后插入内容。
  A:在光标所在行的末尾(行尾)插入内容。
  i:在当前光标位置之前插入内容。

I:在光标所在行的开头(行首)插入内容。
o:在光标所在行的后面插入一个新行。
0:在光标所在行的前面插入一个新行。

2、移动光标

  翻页移动。

♦ 按Page Down键或Ctrl+F组合键向下翻动一整页内容。
♦ 按Page Up键或Ctrl+B组合键向上翻动一整页内容。
行内快速跳转。
♦ 按Home键或^键、数字0键将光标快速跳转到本行的行首。
♦ 按End键或$键将光标快速跳转到本行的行尾。
行间快速跳转。
♦ 使用按键命令1G或者gg可跳转到文件内容的第1行。
♦ 使用按键命令G可跳转到文件的最后一行。
♦ 使用按键命令#G可跳转到文件中的第#行(其中"#"号用具体数字替换)。

“:set nu"命令即可显示行号,执行":set nonu"命令可以取消显示行号

  1 [main]
  2 cachedir=/var/cache/yum/$basearch/$releasever
  3 keepcache=0
  4 debuglevel=2
  5 logfile=/var/log/yum.log
  6 exactarch=1
  7 obsoletes=1
  8 gpgcheck=1
  9 plugins=1
 10 installonly_limit=5
 11 bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http    ://bugs.centos.org/bug_report_page.php?category=yum
 12 distroverpkg=centos-release
 13
 14
 15 #  This is the default, if you make this bigger yum won't see if the metadat    a
 16 # is newer on the remote and so you'll "gain" the bandwidth of not having to
 17 # download the new metadata and "pay" for it by yum not having correct
 18 # information.
 19 #  It is esp. important, to have correct metadata, for distributions like
 20 # Fedora which don't keep old packages around. If you don't like this checki    ng
:set number

3、复制、粘贴和删除

  删除操作。

♦ 按x键或Delete键删除光标处的单个字符。
♦ 使用按键命令dd删除当前光标所在行,使用#dd的形式还可以删除从光标处幵始的#行内容(其中#号用具体数字替换)。
♦ 使用按键命令d^删除当前光标之前到行首的所有字符。
♦ 使用按键命令d$删除当前光标处到行尾的所有字符。
复制操作。使用按键命令yy复制当前行整行的内容到剪贴板,使用#yy的形式还可以复制从光标处幵始的#行内容(其中#号用具体数字替换)。复制的内容需要粘贴后才能使用。
粘贴操作。在vi编辑器中,前一次被删除或复制的内容将会保存到剪切板缓冲区中,按p键即可将缓冲区中的内容粘贴到光标位置处之后,按P键则会粘贴到光标位置处之前。

4、查找文件内容
在命令模式中,按/键后可以输入指定的字符串,从当前光标处开始向后进行查找(按?键则向前查找),按n键可以移动到下一个查找结果

  1 [main]
  2 cachedir=/var/cache/yum/$basearch/$releasever
  3 keepcache=0
  4 debuglevel=2
  5 logfile=/var/log/yum.log
  6 exactarch=1
  7 obsoletes=1
  8 gpgcheck=1
  9 plugins=1
 10 installonly_limit=5
 11 bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http    ://bugs.centos.org/bug_report_page.php?category=yum
 12 distroverpkg=centos-release
 13
 14
 15 #  This is the default, if you make this bigger yum won't see if the metadat    a
 16 # is newer on the remote and so you'll "gain" the bandwidth of not having to
 17 # download the new metadata and "pay" for it by yum not having correct
 18 # information.
 19 #  It is esp. important, to have correct metadata, for distributions like
 20 # Fedora which don't keep old packages around. If you don't like this checki    ng
/yum                                    

5、撤销编辑及保存和退出
u键命令:取消最近一次的操作,并恢复操作结果,可以多次重复按u键恢复已进行的多步操作;
U键命令:取消对当前行所做的所有编辑。
ZZ键命令:保存当前的文件内容并退出vi编辑器

2、末行模式中的基本操作

1、保存文件及退出vi编辑器
当前编辑的文件另存到/root目录下,文件名为newfile

:w /root/newfile

强行退出(不保存即退出)

:q! 

保存并退出

:wq

:x

2、打开新文件或读入其他文件内容
打开当前用户宿主目录中的initial-setup-ks.cfg文件进行编辑

:e ~/initial-setup-ks.cfg

把系统文件/etc/f il esystems中的内容复制到当前文件中

:r /etc/filesystem

3、替换文件内容

  %:在整个文件内容中进行查找并替换。
  n,m:在指定行数范围内的文件内容中进行查找并替换

/g:表示对替换范围内每一行的所有匹配结果都进行替换,省略"/g"时将只替换每行中的第一个匹配结果

将文档中第5行~第15行中 的"initdefault"字符串替换为"DEFAULT"

:5,15 s /initdefault/DEFAULT/g

把当前文件中所有的"initdefault”字符串替换为"bootdefault”

:% s /initdefault/bootdefault/g
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值