马哥linux第二周作业

1linux上的文件管理命令有哪些,其常用的使用方法及其相关演示。

cp,rm,rmdir,mkdir,mv,mktmp,touch,more,tail,less,vi,grep,find,ls,cd,chmod,chown,chgrp

cp一个文件:

[root@node1 tmp]# cp /etc/inittab .
[root@node1 tmp]# ls
inittab
[root@node1 tmp]# 

cp多个文件:

[root@node1 tmp]# cp /etc/inittab /etc/hosts .
[root@node1 tmp]# ls
hosts  inittab
[root@node1 tmp]# 

cp一个目录:

[root@node1 tmp]# cp -r /etc/rc.d/ .
[root@node1 tmp]# ls
rc.d
[root@node1 tmp]# cd rc.d/
[root@node1 rc.d]# ls
init.d  rc0.d  rc1.d  rc2.d  rc3.d  rc4.d  rc5.d  rc6.d  rc.local

rm一个文件:

[root@node1 rc.d]# rm fstab 
rm: remove regular file ‘fstab’? y

rm一个目录:

[root@node1 tmp]# ls
rc.d
[root@node1 tmp]# rm -rf rc.d/
[root@node1 tmp]# ls
[root@node1 tmp]# 

rmdir空目录: 

[root@node1 tmp]# ls
test
[root@node1 tmp]# rmdir test
[root@node1 tmp]# ls
[root@node1 tmp]# 

创建多个目录:

[root@node1 tmp]# mkdir a b c d
[root@node1 tmp]# ls
a  b  c  d
[root@node1 tmp]# 

移动文件:

[root@node1 tmp]# mv a x
[root@node1 tmp]# ls
b  c  d  x
[root@node1 tmp]# 

创建临时文件:

[root@node1 tmp]# mktemp aa.XXXXX
aa.e8juU
[root@node1 tmp]# ls
aa.e8juU  aa.tkH
[root@node1 tmp]# 

修改文件时间戳:
[root@node1 tmp]# touch a
[root@node1 tmp]# ls -l
total 0
-rw-r--r--. 1 root root 0 Jun 30 10:41 a
[root@node1 tmp]# touch a
[root@node1 tmp]# ls -l
total 0
-rw-r--r--. 1 root root 0 Jun 30 10:42 a

more查看文件:
more functions

   less查看文件

   less functions

  tail查看文件:

  tail -100 functions

  tail -f functions

 编辑文件:

  vi functions

 grep 搜索字符串:

匹配正则表达式:

[root@node1 tmp]# echo "my name is mark" | grep m*
my name is mark
[root@node1 tmp]#

反向匹配:

[root@node1 tmp]# echo -e "my name is mark\n I like money" | grep -v m*
[root@node1 tmp]# 

忽略大小写:

[root@node1 tmp]# echo -e "my name is mark\n I like money" | grep -i M*
my name is mark
 I like money
[root@node1 tmp]# 

只获取匹配项:

[root@node1 tmp]# echo -e "my name is mark\n I like money" | grep -i -o M*
m
m
m
m
[root@node1 tmp]# 

多个正则表达式:

[root@node1 tmp]# echo -e "my name is mark\n I like money" | grep -i -o -e M* -e e
m
m
e
m
e
m
e
[root@node1 tmp]# 
[root@node1 tmp]# echo -e "my name is mark\n I like money" | grep -i -o -e M.* -e e
my name is mark
e
money
[root@node1 tmp]# 

显示上下文嘻嘻:

grep -A 3 aaa

grep -B 3 aaa

grep -C 3 aaaa


find查找:

查找文件名:

[root@node1 tmp]# find / -name hxptest
/tmp/hxptest
[root@node1 tmp]# 

按大小查找:

[root@node1 tmp]# find / -size +100M
/proc/kcore
find: ‘/proc/42269/task/42269/fd/6’: No such file or directory
find: ‘/proc/42269/task/42269/fdinfo/6’: No such file or directory
find: ‘/proc/42269/fd/6’: No such file or directory
find: ‘/proc/42269/fdinfo/6’: No such file or directory
/sys/devices/pci0000:00/0000:00:0f.0/resource1_wc
/sys/devices/pci0000:00/0000:00:0f.0/resource1
/var/lib/docker/overlay/7f3a3938e0a8b67d254fdd53a04d1d7b6b58a2ce90341076ecff4cb346c7c4a6/root/hyperkube
/var/lib/docker/overlay/35ae819e3bdce842257fa51cce019d53a521e8c3cb798849eda0dc48c035817d/merged/hyperkube
/var/lib/docker/overlay/58ba60c86c83822a9ecd01b39ca20bc89532d984fc812279c516eb0d4a5e865e/merged/hyperkube
/var/lib/docker/overlay/ae9e8ffb7bfba5e6953b6102d28d0bfc5188f1d4037228d2fd18ced074cb7535/merged/hyperkube
/var/lib/docker/overlay/60a7007a24b923a6ce605a3920624123fec540671e13ef6732c4fea9139e6085/merged/hyperkube
/var/log/messages-20180617
/var/log/messages-20180609

按属主查找:

[root@node1 tmp]# find / -user hxp
/home/hxp
/home/hxp/.bash_logout
/home/hxp/.bash_profile
/home/hxp/.bashrc
/home/hxp/.cache
/home/hxp/.cache/abrt
/home/hxp/.cache/abrt/lastnotification
/home/hxp/.config
/home/hxp/.config/abrt
/home/hxp/.bash_history

按属组查找:

[root@node1 tmp]# find / -group hxp
/home/hxp
/home/hxp/.bash_logout
/home/hxp/.bash_profile
/home/hxp/.bashrc
/home/hxp/.cache
/home/hxp/.cache/abrt
/home/hxp/.cache/abrt/lastnotification
/home/hxp/.config
/home/hxp/.config/abrt
/home/hxp/.bash_history

按权限查找:

[root@node1 tmp]# find /tmp -perm 777
/tmp/a
/tmp/functions
/tmp/test
/tmp/hxptest
[root@node1 tmp]# ls -l 
total 20
-rwxrwxrwx. 1 root root     0 Jun 30 10:42 a
-rwxrwxrwx. 1 root root 17500 Jun 30 10:42 functions
-rwxrwxrwx. 1 root root     0 Jun 30 11:43 hxptest
-rwxrwxrwx. 1 root root     0 Jun 30 11:42 test

ls查看文件列表

[root@node1 tmp]# ls -l
total 20
-rwxrwxrwx. 1 root root     0 Jun 30 10:42 a
-rwxrwxrwx. 1 root root 17500 Jun 30 10:42 functions
-rwxrwxrwx. 1 root root     0 Jun 30 11:43 hxptest
-rwxrwxrwx. 1 root root     0 Jun 30 11:42 test

查看目录:

[root@node1 tmp]# ls -ld aaa
drwxr-xr-x. 2 root root 6 Jun 30 11:53 aaa

glob风格查看:

[root@node1 tmp]# ls -ld a*
-rwxrwxrwx. 1 root root 0 Jun 30 10:42 a
drwxr-xr-x. 2 root root 6 Jun 30 11:53 aaa

跳转目录

[root@node1 tmp]# cd ..
[root@node1 /]# cd .
[root@node1 /]# cd 
[root@node1 ~]# cd /tmp
[root@node1 tmp]# cd -
/root
[root@node1 ~]# 

改变权限:

[root@node1 tmp]# ls -l
total 20
-rwxrwxrwx. 1 root root     0 Jun 30 10:42 a
drwxr-xr-x. 2 root root     6 Jun 30 11:53 aaa
-rwxrwxrwx. 1 root root 17500 Jun 30 10:42 functions
-rwxrwxrwx. 1 root root     0 Jun 30 11:43 hxptest
-rwxrwxrwx. 1 root root     0 Jun 30 11:42 test
[root@node1 tmp]# chmod a-x a
[root@node1 tmp]# ls -l
total 20
-rw-rw-rw-. 1 root root     0 Jun 30 10:42 a
drwxr-xr-x. 2 root root     6 Jun 30 11:53 aaa
-rwxrwxrwx. 1 root root 17500 Jun 30 10:42 functions
-rwxrwxrwx. 1 root root     0 Jun 30 11:43 hxptest
-rwxrwxrwx. 1 root root     0 Jun 30 11:42 test
[root@node1 tmp]# chmod u+x a
[root@node1 tmp]# ls -l
total 20
-rwxrw-rw-. 1 root root     0 Jun 30 10:42 a
drwxr-xr-x. 2 root root     6 Jun 30 11:53 aaa
-rwxrwxrwx. 1 root root 17500 Jun 30 10:42 functions
-rwxrwxrwx. 1 root root     0 Jun 30 11:43 hxptest
-rwxrwxrwx. 1 root root     0 Jun 30 11:42 test
[root@node1 tmp]# chmod 666 a
[root@node1 tmp]# ls -l
total 20
-rw-rw-rw-. 1 root root     0 Jun 30 10:42 a
drwxr-xr-x. 2 root root     6 Jun 30 11:53 aaa
-rwxrwxrwx. 1 root root 17500 Jun 30 10:42 functions
-rwxrwxrwx. 1 root root     0 Jun 30 11:43 hxptest
-rwxrwxrwx. 1 root root     0 Jun 30 11:42 test

改变属组属主:

[root@node1 tmp]# ls -l
total 20
-rw-rw-rw-. 1 root root     0 Jun 30 10:42 a
drwxr-xr-x. 2 root root     6 Jun 30 11:53 aaa
-rwxrwxrwx. 1 root root 17500 Jun 30 10:42 functions
-rwxrwxrwx. 1 root root     0 Jun 30 11:43 hxptest
-rwxrwxrwx. 1 root root     0 Jun 30 11:42 test
[root@node1 tmp]# chowner hxp:hxp a
-bash: chowner: command not found
[root@node1 tmp]# chown hxp:hxp a
[root@node1 tmp]# ls -l
total 20
-rw-rw-rw-. 1 hxp  hxp      0 Jun 30 10:42 a
drwxr-xr-x. 2 root root     6 Jun 30 11:53 aaa
-rwxrwxrwx. 1 root root 17500 Jun 30 10:42 functions
-rwxrwxrwx. 1 root root     0 Jun 30 11:43 hxptest
-rwxrwxrwx. 1 root root     0 Jun 30 11:42 test
[root@node1 tmp]# chown root a
[root@node1 tmp]# ls -l
total 20
-rw-rw-rw-. 1 root hxp      0 Jun 30 10:42 a
drwxr-xr-x. 2 root root     6 Jun 30 11:53 aaa
-rwxrwxrwx. 1 root root 17500 Jun 30 10:42 functions
-rwxrwxrwx. 1 root root     0 Jun 30 11:43 hxptest
-rwxrwxrwx. 1 root root     0 Jun 30 11:42 test

[root@node1 tmp]# ls -l
total 20
-rw-rw-rw-. 1 root hxp      0 Jun 30 10:42 a
drwxr-xr-x. 2 root root     6 Jun 30 11:53 aaa
-rwxrwxrwx. 1 root root 17500 Jun 30 10:42 functions
-rwxrwxrwx. 1 root root     0 Jun 30 11:43 hxptest
-rwxrwxrwx. 1 root root     0 Jun 30 11:42 test
[root@node1 tmp]# ^C
[root@node1 tmp]# chgrp root a
[root@node1 tmp]# ls -l
total 20
-rw-rw-rw-. 1 root root     0 Jun 30 10:42 a
drwxr-xr-x. 2 root root     6 Jun 30 11:53 aaa
-rwxrwxrwx. 1 root root 17500 Jun 30 10:42 functions
-rwxrwxrwx. 1 root root     0 Jun 30 11:43 hxptest
-rwxrwxrwx. 1 root root     0 Jun 30 11:42 test

2bash的工作特性之命令行状态返回值和命令行展开所涉及的内容及其示例演示

[root@node1 tmp]# ls -l
total 20
-rw-rw-rw-. 1 root root     0 Jun 30 10:42 a
drwxr-xr-x. 2 root root     6 Jun 30 11:53 aaa
-rwxrwxrwx. 1 root root 17500 Jun 30 10:42 functions
-rwxrwxrwx. 1 root root     0 Jun 30 11:43 hxptest
-rwxrwxrwx. 1 root root     0 Jun 30 11:42 test
[root@node1 tmp]# echo $?
0
[root@node1 tmp]# lls -l
-bash: lls: command not found
[root@node1 tmp]# echo $?
127
[root@node1 tmp]# 

命令行展开

[root@node1 tmp]# mkdir {a,b}-{c,d}
[root@node1 tmp]# ls -l
total 0
drwxr-xr-x. 2 root root 6 Jun 30 12:04 a-c
drwxr-xr-x. 2 root root 6 Jun 30 12:04 a-d
drwxr-xr-x. 2 root root 6 Jun 30 12:04 b-c
drwxr-xr-x. 2 root root 6 Jun 30 12:04 b-d

3请使用命令行展开功能完成以下练习

1)创建/tmp目录下的:a_c,a_d,b_c,b_d

2)创建/tmp/mylinux目录下的:

[root@node1 tmp]# mkdir {a,b}-{c,d}
[root@node1 tmp]# ls -l
total 0
drwxr-xr-x. 2 root root 6 Jun 30 12:04 a-c
drwxr-xr-x. 2 root root 6 Jun 30 12:04 a-d
drwxr-xr-x. 2 root root 6 Jun 30 12:04 b-c
drwxr-xr-x. 2 root root 6 Jun 30 12:04 b-d
[root@node1 mylinux]# mkdir -p bin boot/grub dev etc/{rc.d/init.d,sysconfig/network-scripts} lib/modules lib64 proc sbin sys tmp usr/local/{bin,sbin} var/{lock,log,run}
[root@node1 mylinux]# tree .
.
|-- bin
|-- boot
|   `-- grub
|-- dev
|-- etc
|   |-- rc.d
|   |   `-- init.d
|   `-- sysconfig
|       `-- network-scripts
|-- lib
|   `-- modules
|-- lib64
|-- proc
|-- sbin
|-- sys
|-- tmp
|-- usr
|   `-- local
|       |-- bin
|       `-- sbin
`-- var
    |-- lock
    |-- log
    `-- run

4文件的元数据信息有哪些,分别表示什么含义,如何查看?如何修改文件的时间戳信息。

文件的元数据信息有,文件类型,文件权限,数组属主,访问时间,修改时间,改变时间

查看方法ls -l和stat

读取文件可修改文件的访问时间

touch可修改三个时间

写入文件可修改修改时间

[root@node1 tmp]# stat a-c
  File: ‘a-c’
  Size: 6               Blocks: 0          IO Block: 4096   directory
Device: fd00h/64768d    Inode: 67407257    Links: 2
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2018-06-30 12:20:30.912194121 +0800
Modify: 2018-06-30 12:20:30.912194121 +0800
Change: 2018-06-30 12:20:30.912194121 +0800
 Birth: -
[root@node1 tmp]# touch a-c
[root@node1 tmp]# stat a-c
  File: ‘a-c’
  Size: 6               Blocks: 0          IO Block: 4096   directory
Device: fd00h/64768d    Inode: 67407257    Links: 2
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2018-06-30 12:21:18.256195180 +0800
Modify: 2018-06-30 12:21:18.256195180 +0800
Change: 2018-06-30 12:21:18.256195180 +0800
 Birth: -

5如何定义一个命令的别名,如何在命令中引用另一个命令的执行结果

定义别名临时:

alias  别名=命令

永久有效,定义在.bashrc中

用管道|在命令中引用另一个命令的执行结果

6显示/var目录下所有以1开头,以一个小写字母结尾,且中间至少出现一位数字(可以有其他字符)的文件

[root@node1 1ss133b]# ls -ld /tmp/1*[0-9]*[a-z]
drwxr-xr-x. 2 root root 6 Jun 30 12:30 /tmp/1ss133b

7显示/etc/目录下,以任意一个数字开头,且以非数据结尾的文件或目录

[root@node1 etc]# ls -ld /etc/[0-9]*[^0-9]

-rw-r--r--. 1 root root 0 Jun 30 12:44 /etc/1sss

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

[root@node1 etc]# ls -ld [^[:alpha:]]*[[:alpha:]]*
-rw-r--r--. 1 root root 0 Jun 30 12:44 1sss

9在tmp目录下创建以tfile开头,后面跟当前日期和时间的文件,文件名形如tfile-2016-05-07-09-22-01

[root@node1 tmp]# touch tfile-$(date +%Y-%M-%d-%H-%m-%S)
[root@node1 tmp]# ls

tfile-2018-53-30-12-06-36

10复制/etc/目录下所有以p开头,以非数字结尾的文件或目录到/tmp/mytest1目录汇总

[root@node1 tmp]# mkdir mytest1
[root@node1 tmp]# cp /etc/p*[^0-9] mytest1/
cp: omitting directory ‘/etc/pam.d’
cp: omitting directory ‘/etc/pcp’
cp: omitting directory ‘/etc/pki’
cp: omitting directory ‘/etc/plymouth’
cp: omitting directory ‘/etc/pm’
cp: omitting directory ‘/etc/popt.d’
cp: omitting directory ‘/etc/postfix’
cp: omitting directory ‘/etc/ppp’
cp: omitting directory ‘/etc/prelink.conf.d’
cp: omitting directory ‘/etc/profile.d’
cp: omitting directory ‘/etc/python’
[root@node1 tmp]# ls /mydata/
[root@node1 tmp]# ls -l
total 0
drwxr-xr-x. 2 root root 129 Jun 30 12:55 mytest1
-rw-r--r--. 1 root root   0 Jun 30 12:53 tfile-2018-53-30-12-06-36
[root@node1 tmp]# ls mytest1/
passwd  passwd-  pcp.conf  pcp.env  pinforc  printcap  profile  protocols

11复制/etc/目录下所有以.d结尾的文件或目录至/tmp/mytest2目录中

[root@node1 tmp]# mkdir mytest2
[root@node1 tmp]# cp -r /etc/*.d mytest2/
[root@node1 tmp]# ls mytest2/
auto.master.d      dracut.conf.d  ld.so.conf.d    pam.d           rc3.d          rwtab.d      xinetd.d
bash_completion.d  exports.d      libibverbs.d    popt.d          rc4.d          setuptool.d  yum.repos.d
binfmt.d           gdbinit.d      logrotate.d     prelink.conf.d  rc5.d          slp.reg.d
cgconfig.d         grub.d         modprobe.d      profile.d       rc6.d          statetab.d
chkconfig.d        init.d         modules-load.d  rc0.d           rc.d           sudoers.d
cron.d             krb5.conf.d    my.cnf.d        rc1.d           request-key.d  sysctl.d
depmod.d           latrace.d      oddjobd.conf.d  rc2.d           rsyslog.d      tmpfiles.d

12复制/etc目录下所有以1或m或n开头,以.conf结尾的文件到/tmp/mytest3目录中

[root@node1 tmp]# mkdir mytest3
[root@node1 tmp]# cp /etc/{l,m,n}*.conf mytest3
[root@node1 tmp]# ls mytest3
ld.so.conf     libuser.conf  logrotate.conf  mke2fs.conf  nfsmount.conf  ntp.conf
libaudit.conf  locale.conf   man_db.conf     nfs.conf     nsswitch.conf

猜你喜欢

转载自blog.csdn.net/hxpjava1/article/details/80864333