系统相关
首页 > 系统相关> > Linux练习(Part2:文件管理和IO重定向)

Linux练习(Part2:文件管理和IO重定向)

作者:互联网

目录

Part2:文件管理和IO重定向

练习1:

1、显示/etc目录下所有以l开头,以一个小写字母结尾,且中间出现至少一位数字的文件或目录列表

[16:08:05 root@Centos8 /data]#ls /etc/l*[0-9]*[[:lower:]]
/etc/l57haha

2、显示/etc目录下以任意一位数字开头,且以非数字结尾的文件或目录列表

[16:11:25 root@Centos8 /data]#ls -d /etc/[[:digit:]]*[^[:digit:]]
/etc/57.haha
[16:13:35 root@Centos8 /data]#ls /etc/[0-9]*[^0-9]
/etc/57.haha

3、显示/etc/目录下以非字母开头,后面跟了一个字母及其它任意长度任意字符的文件或目录列表

[16:15:49 root@Centos8 /data]#ls  /etc/[^a-Z][a-Z]*
/etc/7a.haha

4、显示/etc/目录下所有以rc开头,并后面是0-6之间的数字,其它为任意字符的文件或目录列表

[16:15:51 root@Centos8 /data]#ls /etc/rc[0-6]*
/etc/rc0.d:

/etc/rc1.d:

/etc/rc2.d:

/etc/rc3.d:

/etc/rc4.d:

/etc/rc5.d:

/etc/rc6.d:

5、显示/etc目录下,所有.conf结尾,且以m,n,r,p开头的文件或目录列表

[16:17:36 root@Centos8 /data]#ls /etc/[mnrp]*.conf
/etc/man_db.conf  /etc/nfs.conf       /etc/pbm2ppa.conf  /etc/request-key.conf
/etc/mke2fs.conf  /etc/nfsmount.conf  /etc/pnm2ppa.conf  /etc/resolv.conf
/etc/mtools.conf  /etc/nsswitch.conf  /etc/radvd.conf    /etc/rsyslog.conf

6、只显示/root下的隐藏文件和目录列表

[16:22:21 root@Centos8 ~]#ls -a --ignore=[^.]*  /root
.   .bash_history  .bash_profile  .cache   .cshrc  .esd_auth  .viminfo
..  .bash_logout   .bashrc        .config  .dbus   .tcshrc    .Xauthority
[16:22:29 root@Centos8 ~]# ls -a -I "[^.]*"  /root
.   .bash_history  .bash_profile  .cache   .cshrc  .esd_auth  .viminfo
..  .bash_logout   .bashrc        .config  .dbus   .tcshrc    .Xauthority
[16:22:42 root@Centos8 ~]#ls -d .*  /root
.   .bash_history  .bash_profile  .cache   .cshrc  .esd_auth  .tcshrc   .Xauthority
..  .bash_logout   .bashrc        .config  .dbus   /root      .viminfo

7、只显示/etc下的非隐藏目录列表

[16:23:00 root@Centos8 ~]#ls -d /etc/*/
/etc/alsa/               /etc/fonts/           /etc/ndctl/           /etc/rwtab.d/
/etc/alternatives/       /etc/fwupd/           /etc/NetworkManager/  /etc/samba/
/etc/audit/              /etc/gconf/           /etc/nftables/        /etc/sane.d/
/etc/authselect/         /etc/gcrypt/          /etc/oddjob/          /etc/sasl2/
/etc/avahi/              /etc/gdm/             /etc/oddjobd.conf.d/  /etc/security/
/etc/bash_completion.d/  /etc/geoclue/         /etc/openldap/        /etc/selinux/
/etc/binfmt.d/           /etc/glvnd/           /etc/opt/             /etc/setroubleshoot/
/etc/bluetooth/          /etc/gnupg/           /etc/ostree/          /etc/sgml/
/etc/brltty/             /etc/groff/           /etc/PackageKit/      /etc/skel/
/etc/chkconfig.d/        /etc/grub.d/          /etc/pam.d/           /etc/smartmontools/
/etc/chromium/           /etc/gss/             /etc/pipewire/        /etc/speech-dispatcher/
/etc/cifs-utils/         /etc/gssproxy/        /etc/pkcs11/          /etc/ssh/
/etc/cni/                /etc/hp/              /etc/pki/             /etc/ssl/
/etc/cockpit/            /etc/init.d/          /etc/plymouth/        /etc/sssd/
/etc/containers/         /etc/iproute2/        /etc/pm/              /etc/sudoers.d/
/etc/cron.d/             /etc/iscsi/           /etc/polkit-1/        /etc/sysconfig/
/etc/cron.daily/         /etc/issue.d/         /etc/popt.d/          /etc/sysctl.d/
/etc/cron.hourly/        /etc/kernel/          /etc/prelink.conf.d/  /etc/systemd/
/etc/cron.monthly/       /etc/krb5.conf.d/     /etc/profile.d/       /etc/terminfo/
/etc/cron.weekly/        /etc/ld.so.conf.d/    /etc/pulse/           /etc/tmpfiles.d/
/etc/crypto-policies/    /etc/libblockdev/     /etc/qemu-ga/         /etc/tuned/
/etc/cups/               /etc/libibverbs.d/    /etc/qemu-kvm/        /etc/udev/
/etc/cupshelpers/        /etc/libnl/           /etc/ras/             /etc/udisks2/
/etc/dbus-1/             /etc/libpaper.d/      /etc/rc0.d/           /etc/unbound/
/etc/dconf/              /etc/libreport/       /etc/rc1.d/           /etc/UPower/
/etc/default/            /etc/libssh/          /etc/rc2.d/           /etc/vmware-tools/
/etc/depmod.d/           /etc/libvirt/         /etc/rc3.d/           /etc/wpa_supplicant/
/etc/dhcp/               /etc/logrotate.d/     /etc/rc4.d/           /etc/X11/
/etc/dnf/                /etc/lsm/             /etc/rc5.d/           /etc/xdg/
/etc/dnsmasq.d/          /etc/lvm/             /etc/rc6.d/           /etc/xinetd.d/
/etc/dracut.conf.d/      /etc/mcelog/          /etc/rc.d/            /etc/xml/
/etc/egl/                /etc/microcode_ctl/   /etc/rdma/            /etc/yum/
/etc/exports.d/          /etc/modprobe.d/      /etc/request-key.d/   /etc/yum.repos.d/
/etc/firefox/            /etc/modules-load.d/  /etc/rhsm/
/etc/firewalld/          /etc/motd.d/          /etc/rpm/
/etc/flatpak/            /etc/multipath/       /etc/rsyslog.d/

练习2

1、每天将/etc/目录下所有文件,备份到/data独立的子目录下,并要求子目录格式为backupYYYY-mm-dd,备份过程可见

[18:00:53 root@Centos8 /data]#cp -av /etc/ /data/backp`date +%F`

[18:00:53 root@Centos8 /data]#ll
total 12
drwxr-xr-x. 135 root root 8192 Dec 22 16:15 backp2020-12-22

2、创建/data/rootdir目录,并复制/root下所有文件到该目录内,要求保留原有权限

[18:05:28 root@Centos8 /data]#cp -a /root /data/rootdir

[18:05:54 root@Centos8 /data]#ll /root/
total 16
-rw-------. 1 root root 1494 Dec 17 19:30 anaconda-ks.cfg
-rw-r--r--. 1 root root 1604 Dec 17 20:27 initial-setup-ks.cfg
-rw-r--r--. 1 root root    6 Dec 22 13:51 linux.txt
-rw-r--r--. 1 root root    7 Dec 22 13:59 win.txt
[18:05:57 root@Centos8 /data]#ll rootdir/
total 16
-rw-------. 1 root root 1494 Dec 17 19:30 anaconda-ks.cfg
-rw-r--r--. 1 root root 1604 Dec 17 20:27 initial-setup-ks.cfg
-rw-r--r--. 1 root root    6 Dec 22 13:51 linux.txt
-rw-r--r--. 1 root root    7 Dec 22 13:59 win.txt

练习3

(1)如何创建/testdir/dir1/x, /testdir/dir1/y, /testdir/dir1/x/a, /testdir/dir1/x/b, /testdir/dir1/y/a,/testdir/dir1/y/b

mkdir -p testdir/dir1/{x,y}/{a,b}
[09:58:11 root@Centos8 /data]#mkdir -pv testdir/dir1/{x,y}/{a,b}
mkdir: created directory 'testdir'
mkdir: created directory 'testdir/dir1'
mkdir: created directory 'testdir/dir1/x'
mkdir: created directory 'testdir/dir1/x/a'
mkdir: created directory 'testdir/dir1/x/b'
mkdir: created directory 'testdir/dir1/y'
mkdir: created directory 'testdir/dir1/y/a'
mkdir: created directory 'testdir/dir1/y/b'
[09:58:20 root@Centos8 /data]#tree
.
└── testdir
    └── dir1
        ├── x
        │   ├── a
        │   └── b
        └── y
            ├── a
            └── b

8 directories, 0 files

(2)如何创建/testdir/dir2/x, /testdir/dir2/y, /testdir/dir2/x/a, /testdir/dir2/x/b

mkdir -p testdir/dir1/{x/{a,b},y}
[10:03:15 root@Centos8 /data]#mkdir -pv testdir/dir1/{x/{a,b},y}
mkdir: created directory 'testdir'
mkdir: created directory 'testdir/dir1'
mkdir: created directory 'testdir/dir1/x'
mkdir: created directory 'testdir/dir1/x/a'
mkdir: created directory 'testdir/dir1/x/b'
mkdir: created directory 'testdir/dir1/y'
[10:03:21 root@Centos8 /data]#tree
.
└── testdir
    └── dir1
        ├── x
        │   ├── a
        │   └── b
        └── y

6 directories, 0 files

(3)如何创建/testdir/dir3,/testdir/dir4, /testdir/dir5, /testdir/dir5/dir6, /testdir/dir5/dir7

mkdir -p testdir/dir{3,4,5/dir{6,7}}
[10:00:41 root@Centos8 /data]#mkdir -pv testdir/dir{3,4,5/dir{6,7}}
mkdir: created directory 'testdir'
mkdir: created directory 'testdir/dir3'
mkdir: created directory 'testdir/dir4'
mkdir: created directory 'testdir/dir5'
mkdir: created directory 'testdir/dir5/dir6'
mkdir: created directory 'testdir/dir5/dir7'
[10:01:03 root@Centos8 /data]#tree
.
└── testdir
    ├── dir3
    ├── dir4
    └── dir5
        ├── dir6
        └── dir7

6 directories, 0 files

练习4

1、将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中

[10:27:11 root@Centos8 /data]#cat /etc/issue
\S
Kernel \r on an \m Hostname:\n TTY:\l TIME:\t

[10:27:51 root@Centos8 /data]#tr 'a-z' 'A-Z'</etc/issue
\S
KERNEL \R ON AN \M HOSTNAME:\N TTY:\L TIME:\T

[10:27:56 root@Centos8 /data]#tr 'a-z' 'A-Z'</etc/issue >/tmp/issue.out
[10:28:23 root@Centos8 /data]#cat /tmp/issue.out
\S
KERNEL \R ON AN \M HOSTNAME:\N TTY:\L TIME:\T

2、将当前系统登录用户的信息转换为大写后保存至/tmp/who.out文件中

[10:29:17 root@Centos8 /data]#who
root     pts/0        2020-12-23 09:00 (100.0.0.2)
[10:30:09 root@Centos8 /data]#who |tr 'a-z' 'A-Z' >/tmp/who.out
[10:31:19 root@Centos8 /data]#cat /tmp/who.out
ROOT     PTS/0        2020-12-23 09:00 (100.0.0.2)

3、一个linux用户给root发邮件,需求邮件标题为“help”,邮件正文如下:

Hello,I am 用户名,The system version is here,please help me ro check it,thanks!
操作系统版本信息

[10:43:02 huawei@Centos8 ~]$mail -s help root <<EOF
Hello,I am `whoami`,The system version is here,pleace help me check it,thanks!
`cat /etc/redhat-release`
EOF

[10:46:56 root@Centos8 ~]#mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 3 messages 1 new
    1 huawei@Centos8.local  Wed Dec 23 10:39  19/611   "help"
    2 huawei@Centos8.local  Wed Dec 23 10:43  20/712   "help"
>N  3 huawei@Centos8.local  Wed Dec 23 10:47  19/701   "help"
& 3
Message  3:
From huawei@Centos8.localdomain  Wed Dec 23 10:47:07 2020
Return-Path: <huawei@Centos8.localdomain>
X-Original-To: root
Delivered-To: root@Centos8.localdomain
Date: Wed, 23 Dec 2020 10:47:07 +0800
To: root@Centos8.localdomain
Subject: help
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: huawei@Centos8.localdomain
Status: R

Hello,I am huawei,The system version is here,pleace help me check it,thanks!
CentOS Linux release 8.2.2004 (Core)

&

4、将/root下文件列表,显示成一行,并文件名之间用空格隔开

[11:01:07 root@Centos8 ~]#ls /root/
anaconda-ks.cfg  initial-setup-ks.cfg  linux.txt  win.txt
[11:01:28 root@Centos8 ~]#ls -1 /root/
anaconda-ks.cfg
initial-setup-ks.cfg
linux.txt
win.txt
[11:01:34 root@Centos8 ~]#ls -1 /root/ |tr '\n' ' '
anaconda-ks.cfg initial-setup-ks.cfg linux.txt win.txt [11:01:38 root@Centos8 ~]#

5、计算1+2+3+...+99+100的总和

[11:03:30 root@Centos8 ~]#echo {1..100}
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
[11:03:43 root@Centos8 ~]#echo {1..100} |tr ' ' '+'
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+30+31+32+33+34+35+36+37+38+39+40+41+42+43+44+45+46+47+48+49+50+51+52+53+54+55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70+71+72+73+74+75+76+77+78+79+80+81+82+83+84+85+86+87+88+89+90+91+92+93+94+95+96+97+98+99+100
[11:03:57 root@Centos8 ~]#echo {1..100} |tr ' ' '+'|bc
5050

[11:06:23 root@Centos8 ~]#seq -s + 1 100|bc
5050

6、删除Windows文本文件中的回车字符,即“\r”

[11:11:10 root@Centos8 ~]#file win.txt
win.txt: ASCII text, with CRLF line terminators
[11:11:27 root@Centos8 ~]#file linux.txt
linux.txt: ASCII text
[11:11:31 root@Centos8 ~]#hexdump -c win.txt
0000000   a  \r  \n   b  \r  \n   c
0000007
[11:11:36 root@Centos8 ~]#hexdump -c linux.txt
0000000   a  \n   b  \n   c  \n
0000006
[11:11:40 root@Centos8 ~]#tr -d '\r'<win.txt>win2.txt
[11:11:48 root@Centos8 ~]#hexdump -c win2.txt
0000000   a  \n   b  \n   c
0000005
[11:11:53 root@Centos8 ~]#file win2.txt
win2.txt: ASCII text

7、处理字符串“xt.,l 1 jr#!$mn 2 c*/fe 3 uz 4”,只保留其中的数字和空格

[11:24:43 root@Centos8 ~]#echo "xt.,| 1 jr#mn 2 c*/fe 3 uz 4" |tr -dc ' 0-9'
 1  2  3  4

[11:26:04 root@Centos8 ~]#echo "xt.,| 1 jr#mn 2 c*/fe 3 uz 4" |tr -d '[[:alpha:]][[:punct:]]'
 1  2  3  4

8、将PATH变量每个目录显示在独立的一行

[11:29:51 root@Centos8 ~]#echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[11:30:52 root@Centos8 ~]#echo $PATH |tr ':' '\n'
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/root/bin

9、将指定文件中0-9分别替代称a-j

[11:34:09 root@Centos8 /data]#cat test.txt
123154646784512!!!!???
[11:34:24 root@Centos8 /data]#cat test.txt |tr '0-9' 'a-j'
bcdbfegeghiefbc!!!!???

10、将文件/etc/centos-release中每个单词(由字母组成)显示在独立一行,并无空行

[11:35:24 root@Centos8 /data]#cat /etc/redhat-release |tr -s ' ' '\n'
CentOS
Linux
release
8.2.2004
(Core)

标签:11,data,etc,Part2,IO,Linux,root,testdir,Centos8
来源: https://www.cnblogs.com/chenchunyuBEyOND/p/14188341.html