一、文件内容显示
1、文件内容查看:
(1)查看/etc/passwd文件的第6行
[root@server ~]# cat /etc/passwd | head -6 | tail -1
sync:x:5:0:sync:/sbin:/bin/sync
[root@server ~]# head -6 /etc/passwd | tail -1
sync:x:5:0:sync:/sbin:/bin/sync
(2)查看/etc/selinux/config 以 SELINUX开头的行
[root@server ~]# cat /etc/selinux/config | grep ^SELINUX
SELINUX=enforcing
SELINUXTYPE=targeted
[root@server ~]# grep ^SELINUX /etc/selinux/config
SELINUX=enforcing
SELINUXTYPE=targeted
(3)查找/etc/ssh/sshd_config 以no结尾的行
[root@server ~]# cat /etc/ssh/sshd_config | grep no$
#HostbasedAuthentication no
#IgnoreUserKnownHosts no
#PermitEmptyPasswords no
ChallengeResponseAuthentication no
#KerberosAuthentication no
#KerberosGetAFSToken no
GSSAPICleanupCredentials no
#GSSAPIKeyExchange no
#GSSAPIEnablek5users no
#GatewayPorts no
PrintMotd no
#PermitUserEnvironment no
#UseDNS no
#PermitTunnel no
# X11Forwarding no
# AllowTcpForwarding no
# PermitTTY no
[root@server ~]# grep no$ /etc/ssh/sshd_config
#HostbasedAuthentication no
#IgnoreUserKnownHosts no
#PermitEmptyPasswords no
ChallengeResponseAuthentication no
#KerberosAuthentication no
#KerberosGetAFSToken no
GSSAPICleanupCredentials no
#GSSAPIKeyExchange no
#GSSAPIEnablek5users no
#GatewayPorts no
PrintMotd no
#PermitUserEnvironment no
#UseDNS no
#PermitTunnel no
# X11Forwarding no
# AllowTcpForwarding no
# PermitTTY no
(4)过滤/etc/ssh/sshd_config 包含数字的行
[root@server ~]# cat /etc/ssh/sshd_config | grep [0-9]
# $OpenBSD: sshd_config,v 1.103 2018/04/09 20:41:22 tj Exp $
# sshd_config(5) for more information.
#Port 22
#ListenAddress 0.0.0.0
HostKey /etc/ssh/ssh_host_ed25519_key
# Please, check manual pages for update-crypto-policies(8) and sshd_config(5).
#LoginGraceTime 2m
#MaxAuthTries 6
#MaxSessions 10
# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
#GSSAPIEnablek5users no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#ClientAliveInterval 0
#ClientAliveCountMax 3
#MaxStartups 10:30:100
# X11Forwarding no
[root@server ~]# grep [0-9] /etc/ssh/sshd_config
# $OpenBSD: sshd_config,v 1.103 2018/04/09 20:41:22 tj Exp $
# sshd_config(5) for more information.
#Port 22
#ListenAddress 0.0.0.0
HostKey /etc/ssh/ssh_host_ed25519_key
# Please, check manual pages for update-crypto-policies(8) and sshd_config(5).
#LoginGraceTime 2m
#MaxAuthTries 6
#MaxSessions 10
# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
#GSSAPIEnablek5users no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#ClientAliveInterval 0
#ClientAliveCountMax 3
#MaxStartups 10:30:100
# X11Forwarding no
2、文本处理命令:
(1)查看/etc/passwd文件以 : 为分隔符的第一列内容,并按字母逆序排序
[root@server ~]# cat /etc/passwd | cut -d : -f 1 | sort -r
xue
usbmuxd
unbound
tss
tcpdump
systemd-resolve
systemd-coredump
sync
sssd
sshd
shutdown
setroubleshoot
saslauth
rtkit
rpcuser
rpc
root
radvd
qemu
pulse
polkitd
pipewire
operator
nobody
mail
lp
libstoragemgmt
halt
gnome-initial-setup
gluster
geoclue
gdm
games
ftp
flatpak
dnsmasq
dbus
daemon
colord
cockpit-wsinstance
cockpit-ws
clevis
chrony
bin
avahi
adm
[root@server ~]# cut -d : -f 1 /etc/passwd | sort -r
xue
usbmuxd
unbound
tss
tcpdump
systemd-resolve
systemd-coredump
sync
sssd
sshd
shutdown
setroubleshoot
saslauth
rtkit
rpcuser
rpc
root
radvd
qemu
pulse
polkitd
pipewire
operator
nobody
mail
lp
libstoragemgmt
halt
gnome-initial-setup
gluster
geoclue
gdm
games
ftp
flatpak
dnsmasq
dbus
daemon
colord
cockpit-wsinstance
cockpit-ws
clevis
chrony
bin
avahi
adm
(2)使用cut命令将当前主机的ip地址切割显示
[root@server ~]# ip a | grep ens160 | tail -1 |tr -s ' '| cut -d ' ' -f 3 | cut -d / -f 1
192.168.100.128
3、总结:
二、文件的其他操作命令
1、复制、移动
(1)在/test目录下创建一个子目录dir,将/etc/passwd复制到该目录
[root@server test]# mkdir /test/dir
[root@server test]# ll -d /test/dir
drwxr-xr-x. 2 root root 6 Oct 23 14:24 /test/dir
(2)将/etc/ssh/sshd_config文件复制到/test目录
[root@server test]# cp /etc/ssh/ssh_config /test/dir/
[root@server test]# ll /test/dir/
total 4
-rw-r--r--. 1 root root 1770 Oct 23 14:25 ssh_config
(3)将/etc/yum.repos.d/目录复制到/test目录
[root@server test]# ll /test/
total 40
-rw-r--r--. 1 root root 162 Oct 21 16:16 bashrc
drwxr-xr-x. 2 root root 24 Oct 23 14:25 dir
-rw-r--r--. 1 root root 137 Oct 21 11:37 file
-rw-r--r--. 1 root root 73 Oct 21 16:15 group
-rw-r--r--. 2 root root 10 Oct 22 10:57 motd.hard
lrwxrwxrwx. 1 root root 9 Oct 21 11:18 motd.soft -> /etc/motd
-rw-r--r--. 1 root root 2613 Oct 21 16:12 passwd
-rw-r--r--. 1 root root 68 Oct 21 16:22 profile
-rw-r--r--. 1 root root 4309 Oct 21 15:46 sshd.conf
-rw-r--r--. 1 root root 4343 Oct 21 16:33 sshd_config
drwxr-xr-x. 2 root root 25 Oct 23 14:27 yum.repos.d
(4)将/etc/hosts文件复制到/test目录
[root@server test]# cp /etc/hosts /test/
[root@server test]# ll /test/hosts
-rw-r--r--. 1 root root 158 Oct 23 14:28 /test/hosts
(5)将/etc/hostname文件复制到/test目录
[root@server test]# cp /etc/hostname /test
[root@server test]# ll /test/hostname
-rw-r--r--. 1 root root 13 Oct 23 14:28 /test/hostname
(6)将/test/sshd_config文件移动到/test/dir目录下并改名为sshd.conf
[root@server test]# mv /test/sshd_config /test/dir/sshd.conf
[root@server test]# ll /test/sshd_config
ls: cannot access '/test/sshd_config': No such file or directory
[root@server test]# ll /test/dir/sshd.conf
-rw-r--r--. 1 root root 4343 Oct 21 16:33 /test/dir/sshd.conf
2、文件查找
(1)在$HOME目录及其子目录中,查找2天前被更改过的文件
[root@server test]# find $HOME -mtime +2 -type f
/root/.bash_logout
/root/.bash_profile
/root/.cshrc
/root/.tcshrc
/root/anaconda-ks.cfg
/root/.cache/dconf/user
/root/.cache/tracker/db-version.txt
/root/.cache/tracker/db-locale.txt
/root/.cache/tracker/ontologies.gvdb
/root/.cache/tracker/parser-version.txt
/root/.cache/tracker/last-crawl.txt
/root/.cache/tracker/first-index.txt
/root/.cache/event-sound-cache.tdb.2315632e618d4d57a12c97c3cee1a435.x86_64-redhat-linux-gnu
/root/.cache/gnome-software/appstream/components
(2)在/etc/目录下寻找以host开头的文件
[root@server test]# find /etc -name "host*" -print
/etc/host.conf
/etc/hosts
/etc/avahi/hosts
/etc/hostname
/etc/nvme/hostnqn
/etc/nvme/hostid
(3)在/test/下面查找目录文件
[root@server test]# find /test -type d -print
/test
/test/dir
/test/yum.repos.d
(4)在/test目录及子目录中,查找超过2KB的文件
[root@server test]# find /test -size +2k
/test/sshd.conf
/test/passwd
/test/dir/sshd.conf
3、打包压缩
(1)将/test目录下的所有文件和文件夹全部压缩成myfile.zip文件
[root@server test]# zip myfile.zip /test
adding: test/ (stored 0%)
(2)把myfile.zip文件解压到 /opt
[root@server test]# unzip myfile.zip -d /opt/
Archive: myfile.zip
creating: /opt/test/
[root@server test]# ll -d /opt/test/
drwxr-xr-x. 2 root root 6 Oct 23 14:45 /opt/test/
(3)将/opt目录下的文件全部打包并用gzip压缩成/test/newfile.tar.gz
[root@server opt]# tar czf newfile.tar.gz /opt
tar: Removing leading `/' from member names
(5)查看/test/newfile.tar.gz文件中有哪些文件?
[root@server opt]# tar tf newfile.tar.gz
opt/
opt/test/
(6)将newfile.tar.gz下载至windows客户端主机
[C:\~]$ sftp root@192.168.100.128
Connecting to 192.168.100.128:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Your current local directory is
C:\Users\win10\Documents\NetSarang Computer\6\Xshell\Sessions
Type `help' to browse available commnands.
sftp:/root> cd /opt/
sftp:/opt> ls
drwxr-xr-x 3 root root 40 Oct 23 14:50 .
dr-xr-xr-x 18 root root 257 Oct 21 11:07 ..
drwxr-xr-x 2 root root 6 Oct 23 14:45 test
-rw-r--r-- 1 root root 127 Oct 23 14:50 newfile.tar.gz
sftp:/opt> get newfile.tar.gz
Fetching /opt/newfile.tar.gz to newfile.tar.gz
sftp: received 127 字节 in 0.00 seconds
查验
(7)在/test目录内,备份/etc下的所有文件并保留其权限
[root@server opt]# cp -ar /etc /test
[root@server opt]# ll /test/
total 56
-rw-r--r--. 1 root root 162 Oct 21 16:16 bashrc
drwxr-xr-x. 2 root root 41 Oct 23 14:29 dir
drwxr-xr-x. 145 root root 8192 Oct 22 10:57 etc
-rw-r--r--. 1 root root 137 Oct 21 11:37 file
-rw-r--r--. 1 root root 73 Oct 21 16:15 group
-rw-r--r--. 1 root root 13 Oct 23 14:28 hostname
-rw-r--r--. 1 root root 158 Oct 23 14:28 hosts
-rw-r--r--. 2 root root 10 Oct 22 10:57 motd.hard
lrwxrwxrwx. 1 root root 9 Oct 21 11:18 motd.soft -> /etc/motd
-rw-r--r--. 1 root root 160 Oct 23 14:45 myfile.zip
-rw-r--r--. 1 root root 2613 Oct 21 16:12 passwd
-rw-r--r--. 1 root root 68 Oct 21 16:22 profile
-rw-r--r--. 1 root root 4309 Oct 21 15:46 sshd.conf
drwxr-xr-x. 2 root root 25 Oct 23 14:27 yum.repos.d