03Linu文件管理之时间、日志、复制拷贝20210228
16进制的41转为十进制之后就是65,65对应ASCII码就是大A,同理97就是小a;
[root@localhost ~]# nano test.txt [root@localhost ~]# cat test.txt Aa [root@localhost ~]# hexdump -C test.txt 00000000 41 61 0a |Aa.| 00000003 [root@localhost ~]# echo "ibase=16;41"|bc 65 [root@localhost ~]# echo "ibase=16;61"|bc 97 [root@localhost ~]#
History的用法之一
1.利用date +%F来直接创建以日期开头的文件日志
2.利用date +%T来直接创建以时间开头的文件日志
[root@localhost ~]# date +%F 2021-02-28 [root@localhost ~]# date +%T 10:47:49
3.利用history的功能来判断日期+时间+操作人
[root@localhost ~]# HISTTIMEFORMAT="%F %T `whoami` "
[root@localhost ~]# history
1 2021-02-28 10:47:41 root date +%F
2 2021-02-28 10:47:49 root date +%T
3 2021-02-28 10:48:50 root HISTTIMEFORMAT="%F %T `whoami` "
4 2021-02-28 10:49:00 root history
1 [root@localhost ~]# HISTTIMEFORMAT="%F %T `whoami` " 2 [root@localhost ~]# history 3 1 2021-02-28 10:47:41 root date +%F 4 2 2021-02-28 10:47:49 root date +%T 5 3 2021-02-28 10:48:50 root HISTTIMEFORMAT="%F %T `whoami` " 6 4 2021-02-28 10:49:00 root history
换行回车:
man ascii 可以查看帮助,
rpm -q lrzsz 检查是否安装了lrzsz,
没有安装的话,使用yum -y install lrzsz安装;
“0D”是把光标移到同一行的顶头——回车(CR);
“0A”是把光标移到下一行——换行(LF)。
换行
回车
时间 date + %F
[15:28:14 root@centos8 ~]#date +%F 2021-03-01 [15:30:55 root@centos8 ~]#date +%Y%m%d 20210301
举例1:
1 [15:42:51 root@centos8 ~]#touch $(date +%F).log(#以日期命名的文件日志) 2 [15:43:01 root@centos8 ~]#ls 3 2021-03-01_15-42-24.log 2021-03-01.log anaconda-ks.cfg initial-setup-ks.cfg 4 [15:43:02 root@centos8 ~]#ll 5 total 8 6 -rw-r--r--. 1 root root 0 Mar 1 15:42 2021-03-01_15-42-24.log 7 -rw-r--r--. 1 root root 0 Mar 1 15:43 2021-03-01.log 8 -rw-------. 1 root root 1489 Jan 11 16:41 anaconda-ks.cfg 9 -rw-r--r--. 1 root root 1778 Jan 11 17:12 initial-setup-ks.cfg
举例2:
1 [15:26:47 root@centos8 ~]#date -d "yesterday" +%Y%m%d 2 20210228 3 [15:27:04 root@centos8 ~]#date -d "-1 day" +%Y%m%d (#昨天) 4 20210228 5 [15:27:14 root@centos8 ~]#date -d "-2 day" +%Y%m%d (#前天) 6 20210227 7 [15:27:18 root@centos8 ~]#date -d "-3 day" +%Y%m%d 8 20210226 9 [15:27:21 root@centos8 ~]#date -d "-4 day" +%Y%m%d 10 20210225 11 [15:27:23 root@centos8 ~]#date -d "yesterday" +%F 12 2021-02-28 13 [15:27:37 root@centos8 ~]#date -d "-1 day" +%F 14 2021-02-28 15 [15:27:47 root@centos8 ~]#date -d "-2 day" +%F 16 2021-02-27 17 [15:27:56 root@centos8 ~]#date -d "-3 day" +%F 18 2021-02-26 19 [15:28:03 root@centos8 ~]#date -d "-4 day" +%F 20 2021-02-25
举例3:
一个月以后的表示
[15:45:39 root@centos8 ~]#date -d "+1 month" +%F 2021-04-01
复制文件和目录 :
利用 cp(copy)命令可以实现文件或目录的复制
-i 如果目标已存在,覆盖前提示是否覆盖 -n 不覆盖,注意两者顺序 -r, -R 递归复制目录及内部的所有内容 -a 归档,相当于-dR --preserv=all,常用于备份功能 -d --no-dereference --preserv=links 不复制原文件,只复制链接名 --preserv[=ATTR_LIST] mode: 权限 ownership: 属主属组 timestamp: links xattr context all -p 等同--preserv=mode,ownership,timestamp -v --verbose -f --force -u --update 只复制源比目标更新文件或目标不存在的文件 -b 目标存在,覆盖前先备份,默认形式为 `filename~`,只保留最近的一个备份 --backup=numbered 目标存在,覆盖前先备份加数字后缀,形式为`filename.~#~`,可以保留多个版本
[15:53:51 root@centos8 ~]#cp /etc/sysconfig/ /data/ cp: -r not specified; omitting directory '/etc/sysconfig/' [15:54:20 root@centos8 ~]#cp -r /etc/sysconfig/ /data/ [15:54:34 root@centos8 ~]#ls 2021-03-01.log anaconda-ks.cfg initial-setup-ks.cfg [15:54:36 root@centos8 ~]#cd /data [15:54:42 root@centos8 data]#ls sysconfig [15:54:43 root@centos8 data]#cd ~ [15:54:56 root@centos8 ~]#cp -a /etc/sysconfig/ /opt [15:55:10 root@centos8 ~]#cd /opt [15:55:18 root@centos8 opt]#ls sysconfig
拷贝举例:
1 [23:33:05 root@centos8 ~]#cp -av /etc/sysconfig/ /tmp (#加上v就显示了拷贝的过程) 2 '/etc/sysconfig/' -> '/tmp/sysconfig' 3 '/etc/sysconfig/nftables.conf' -> '/tmp/sysconfig/nftables.conf' 4 '/etc/sysconfig/ip6tables-config' -> '/tmp/sysconfig/ip6tables-config' 5 '/etc/sysconfig/iptables-config' -> '/tmp/sysconfig/iptables-config' 6 '/etc/sysconfig/ebtables-config' -> '/tmp/sysconfig/ebtables-config' 7 '/etc/sysconfig/kernel' -> '/tmp/sysconfig/kernel' 8 '/etc/sysconfig/anaconda' -> '/tmp/sysconfig/anaconda' 9 '/etc/sysconfig/sshd-permitrootlogin' -> '/tmp/sysconfig/sshd-permitrootlogin' 10 '/etc/sysconfig/network' -> '/tmp/sysconfig/network' 11 '/etc/sysconfig/selinux' -> '/tmp/sysconfig/selinux' 12 '/etc/sysconfig/chronyd' -> '/tmp/sysconfig/chronyd' 13 '/etc/sysconfig/samba' -> '/tmp/sysconfig/samba' 14 '/etc/sysconfig/rsyslog' -> '/tmp/sysconfig/rsyslog' 15 '/etc/sysconfig/run-parts' -> '/tmp/sysconfig/run-parts' 16 '/etc/sysconfig/crond' -> '/tmp/sysconfig/crond' 17 '/etc/sysconfig/modules' -> '/tmp/sysconfig/modules' 18 '/etc/sysconfig/ksm' -> '/tmp/sysconfig/ksm' 19 '/etc/sysconfig/rpcbind' -> '/tmp/sysconfig/rpcbind' 20 '/etc/sysconfig/saslauthd' -> '/tmp/sysconfig/saslauthd' 21 '/etc/sysconfig/raid-check' -> '/tmp/sysconfig/raid-check' 22 '/etc/sysconfig/wpa_supplicant' -> '/tmp/sysconfig/wpa_supplicant' 23 '/etc/sysconfig/radvd' -> '/tmp/sysconfig/radvd' 24 '/etc/sysconfig/grub' -> '/tmp/sysconfig/grub' 25 '/etc/sysconfig/libvirtd' -> '/tmp/sysconfig/libvirtd' 26 '/etc/sysconfig/virtlockd' -> '/tmp/sysconfig/virtlockd' 27 '/etc/sysconfig/virtlogd' -> '/tmp/sysconfig/virtlogd' 28 '/etc/sysconfig/kdump' -> '/tmp/sysconfig/kdump' 29 '/etc/sysconfig/cpupower' -> '/tmp/sysconfig/cpupower' 30 '/etc/sysconfig/firewalld' -> '/tmp/sysconfig/firewalld' 31 '/etc/sysconfig/sshd' -> '/tmp/sysconfig/sshd' 32 '/etc/sysconfig/atd' -> '/tmp/sysconfig/atd' 33 '/etc/sysconfig/smartmontools' -> '/tmp/sysconfig/smartmontools' 34 '/etc/sysconfig/qemu-ga' -> '/tmp/sysconfig/qemu-ga' 35 '/etc/sysconfig/irqbalance' -> '/tmp/sysconfig/irqbalance' 36 '/etc/sysconfig/man-db' -> '/tmp/sysconfig/man-db' 37 '/etc/sysconfig/network-scripts' -> '/tmp/sysconfig/network-scripts' 38 '/etc/sysconfig/network-scripts/ifcfg-ens33' -> '/tmp/sysconfig/network-scripts/ifcfg-ens33' 39 '/etc/sysconfig/cbq' -> '/tmp/sysconfig/cbq' 40 '/etc/sysconfig/cbq/avpkt' -> '/tmp/sysconfig/cbq/avpkt' 41 '/etc/sysconfig/cbq/cbq-0000.example' -> '/tmp/sysconfig/cbq/cbq-0000.example' 42 '/etc/sysconfig/console' -> '/tmp/sysconfig/console' 43 [23:35:30 root@centos8 ~]#
☆ 小练习:
1、每天将/etc/目录下所有文件,备份到/data独立的子目录下,并要求子目录格式为 backupYYYYmm-dd,备份过程可见
[root@centos8 ~]#cp -av /etc/ /data/backup`date +%F`
截取了部分代码:
查看结果,备份已存在:
[root@kevin306 ~]# cd /data/
[root@kevin306 data]# ls
backup2021-03-02
[root@kevin306 data]#
2、创建/data/rootdir目录,并复制/root下所有文件到该目录内,要求保留原有权限
[00:09:50 root@centos8 ~]#ls / bin data etc lib media opt root sbin sys usr boot dev home lib64 mnt proc run srv tmp var [00:09:53 root@centos8 ~]#cd /data [00:10:01 root@centos8 data]#mkdir rootdir [00:10:21 root@centos8 data]#ls rootdir sysconfig [00:10:21 root@centos8 data]#cp -a /root /data/rootdir [00:10:41 root@centos8 data]#ls rootdir sysconfig [00:10:45 root@centos8 data]#cd /rootdir -bash: cd: /rootdir: No such file or directory [00:10:59 root@centos8 data]#cd /data/rootdir [00:11:10 root@centos8 rootdir]#ls root
20210823renew~