N25_第二周作业_leon

第二周博客作业_leon

1.       Linux上的文件管理类命令有哪些?其常用的使用方法及其相关示例演示

常用文件管理类命令有:

mkdirtouchrmdircprmmv

 

mkdir—-创建目录

-m  直接设置权限(非默认权限)

-v   显示创建目录的过程

-p   自动按需创建父目录

[root@localhost ~]# cd /tmp

[root@localhost tmp]# ls

myfirst.sh  mytmp.WBjMq  test.xl2cYj

[root@localhost tmp]# mkdir 123

[root@localhost tmp]# ls

123  myfirst.sh  mytmp.WBjMq  test.xl2cYj

 

-p自动按需创建父目录

[root@localhost tmp]# ls

[root@localhost tmp]# mkdir 123/234/345

mkdir: cannot create directory `123/234/345': No such file or directory

[root@localhost tmp]# mkdir -p 123/234/345

[root@localhost tmp]# ls

123

[root@localhost tmp]# tree /tmp

/tmp

└── 123

    └── 234

        └── 345

3 directories, 0 files

 

-m直接设置权限(非默认权限)

[root@localhost tmp]# ls

[root@localhost tmp]# umask

0022

[root@localhost tmp]# mkdir 123

[root@localhost tmp]# ls -ld 123/

drwxr-xr-x. 2 root root 4096 Dec 13 09:07 123/

[root@localhost tmp]# mkdir -m 666 234

[root@localhost tmp]# ls

123  234

[root@localhost tmp]# ls -ld 234/

drw-rw-rw-. 2 root root 4096 Dec 13 09:08 234/

 

 

-v显示创建目录的过程

[root@localhost tmp]# ls

myfirst.sh  mytmp.WBjMq  test.xl2cYj

[root@localhost tmp]# mkdir -v 1 2 3 4

mkdir: created directory ‘1’

mkdir: created directory ‘2’

mkdir: created directory ‘3’

mkdir: created directory ‘4’

[root@localhost tmp]# ls

1  2  3  4  myfirst.sh  mytmp.WBjMq  test.xl2cYj

 

touch—-更改文件的时间戳 也可用于创建空文件

[root@localhost tmp]# ls

test2

[root@localhost tmp]# touch aa

[root@localhost tmp]# ls

aa  test2

[root@localhost tmp]# ls -la aa

-rw-r–r–. 1 root root 0 12 24 03:10 aa

 

rmdir—-删除空目录

[root@localhost tmp]# ls

123  2  myfirst.sh  mytmp.WBjMq  test.xl2cYj

[root@localhost tmp]# rmdir 123

[root@localhost tmp]# ls

2  myfirst.sh  mytmp.WBjMq  test.xl2cYj

[root@localhost tmp]# rmdir 2

[root@localhost tmp]# ls

myfirst.sh  mytmp.WBjMq  test.xl2cYj

 

cp—-文件拷贝

-r 拷贝目录

-p 保留文件属性及权限

-P 不拷贝链接文件的原文件

-f 目标文件存在时,强制覆盖

[root@localhost tmp]# ls

aa  test2

[root@localhost tmp]# cp /etc/passwd passwd.bak

[root@localhost tmp]# ls

aa  passwd.bak  test2

 

 

 

 

-r 拷贝目录

[root@localhost tmp]# ls

aa  passwd.bak  test2

[root@localhost tmp]# cp test2/ test2.bak

cp: 略过目录"test2/"

[root@localhost tmp]# cp -r test2/ test2.bak

[root@localhost tmp]# ls

aa  passwd.bak  test2  test2.bak

 

-p 保留文件属性及权限

[root@localhost tmp]# ls -la /etc/inittab

-rw-r–r–. 1 root root 884 1  14 2016 /etc/inittab

[root@localhost tmp]# cp /etc/inittab inittab.bak

[root@localhost tmp]# ls -ls inittab.bak

4 -rw-r–r–. 1 root root 884 12 24 03:27 inittab.bak

[root@localhost tmp]# cp -p /etc/inittab inittab.bak2

[root@localhost tmp]# ls -la inittab.bak inittab.bak2

-rw-r–r–. 1 root root 884 12 24 03:27 inittab.bak

-rw-r–r–. 1 root root 884 1  14 2016 inittab.bak2

 

cp -P 不拷贝链接文件的原文件

[root@localhost ~]# cp /etc/system-release /tmp

[root@localhost ~]# ls -la /tmp/system-release

-rw-r–r–. 1 root root 27 12 24 03:33 /tmp/system-release

[root@localhost ~]# cat /tmp/system-release

CentOS release 6.3 (Final)

[root@localhost ~]# cp -P /etc/system-release /tmp/system-release.bak

[root@localhost ~]# ls -la /tmp/{system-release,system-release.bak}

-rw-r–r–. 1 root root 27 12 24 03:33 /tmp/system-release

lrwxrwxrwx. 1 root root 14 12 24 03:34 /tmp/system-release.bak -> centos-release

 

cp -f 目标文件存在时,强制覆盖

[root@localhost tmp]# ls

inittab.bak2  test2

[root@localhost tmp]# cp /etc/inittab inittab.bak2

cp:是否覆盖"inittab.bak2" y

[root@localhost tmp]# ls

inittab.bak2  test2

[root@localhost tmp]# cp -f /etc/inittab inittab.bak2

[root@localhost tmp]# ls

inittab.bak2  test2

 

 

 

rm—-删除文件或目录

-f 强制删除

-i 删除之前提示

-r递归删除

[root@localhost tmp]# ls

inittab.bak2  test2

[root@localhost tmp]# rm inittab.bak2

rm:是否删除普通文件 "inittab.bak2"y

[root@localhost tmp]# ls

test2

 

rm -f 强制删除

[root@localhost tmp]# cp /etc/inittab ./

[root@localhost tmp]# ls

inittab  test2

[root@localhost tmp]# rm -f inittab

[root@localhost tmp]# ls

test2

 

rm -r递归删除

[root@localhost tmp]# ls

123  test2

[root@localhost tmp]# tree

.

├── 123

   └── 234

       └── 345

└── test2

    ├── test1

    ├── test3

    └── test4

 

4 directories, 3 files

[root@localhost tmp]# rm 123

rm: 无法删除"123": 是一个目录

[root@localhost tmp]# rm -r 123/

rm:是否进入目录"123"? y

rm:是否进入目录"123/234"? y

rm:是否删除目录 "123/234/345"^C

[root@localhost tmp]# rm -rf 123/

[root@localhost tmp]# ls

test2

 

 

mv—-移动(或改名)文件

-f 强制覆盖,不提示

-I 覆盖前提示

[root@localhost tmp]# ls

test2

[root@localhost tmp]# touch aa

[root@localhost tmp]# ls

aa  test2

[root@localhost tmp]# touch bb

[root@localhost tmp]# ls

aa  bb  test2

[root@localhost tmp]# mv aa bb

mv:是否覆盖"bb" y

[root@localhost tmp]# ls

bb  test2

[root@localhost tmp]# touch aa

[root@localhost tmp]# mv -f aa bb

[root@localhost tmp]# ls

bb  test2

 

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

bash的工作特性之命令执行状态返回值:

bash通过状态返回值来输出命令运行结果:成功 0 ;失败 1-255

命令执行完成之后,其状态返回值保存于bash的特殊变量$?

[root@localhost tmp]# ls

bb  test2

[root@localhost tmp]# cp 123 ./234

cp: 无法获取"123" 的文件状态(stat): 没有那个文件或目录

[root@localhost tmp]# echo $?

1

 

[root@localhost tmp]# ls

bb  test2

[root@localhost tmp]# cp bb ./234

[root@localhost tmp]# echo $?

0

 

bash的工作特性之命令行展开:

~:自动展开为用户的家目录,或指定的用户的家目录

[root@localhost tmp]# cd ~

[root@localhost ~]# pwd

/root

[root@localhost ~]# whoami

Root

{}:可承载一个以逗号分隔的路径列表,并能够将其展开为多个路径:

[root@localhost tmp]# ls

test2

[root@localhost tmp]# mkdir 22

[root@localhost tmp]# cp /etc/{inittab,issue} ./22

[root@localhost tmp]# ls -la 22

总用量 16

drwxr-xr-x. 2 root root 4096 12 24 05:38 .

drwxrwxrwt. 5 root root 4096 12 24 05:38 ..

-rw-r–r–. 1 root root  884 12 24 05:38 inittab

-rw-r–r–. 1 root root   47 12 24 05:38 issue

 

[root@localhost tmp]# mkdir -v {a,b}+{c,d}

mkdir: 已创建目录 "a+c"

mkdir: 已创建目录 "a+d"

mkdir: 已创建目录 "b+c"

mkdir: 已创建目录 "b+d"

[root@localhost tmp]# ls

22  a+c  a+d  b+c  b+d  test2

 

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

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

[root@localhost tmp]# mkdir -v {a,c}_{b,d}

mkdir: 已创建目录 "a_b"

mkdir: 已创建目录 "a_d"

mkdir: 已创建目录 "c_b"

mkdir: 已创建目录 "c_d"

[root@localhost tmp]# ls

a_b  a_d  c_b  c_d  test2

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

[root@localhost tmp]# ls

test2

[root@localhost tmp]#mkdir -pv /tmp/mylinux/{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}}

mkdir: 已创建目录 "/tmp/mylinux"

mkdir: 已创建目录 "/tmp/mylinux/bin"

mkdir: 已创建目录 "/tmp/mylinux/boot"

mkdir: 已创建目录 "/tmp/mylinux/boot/grub"

mkdir: 已创建目录 "/tmp/mylinux/dev"

mkdir: 已创建目录 "/tmp/mylinux/etc"

mkdir: 已创建目录 "/tmp/mylinux/etc/rc.d"

mkdir: 已创建目录 "/tmp/mylinux/etc/rc.d/init.d"

mkdir: 已创建目录 "/tmp/mylinux/etc/sysconfig"

mkdir: 已创建目录 "/tmp/mylinux/etc/sysconfig/network-scripts"

mkdir: 已创建目录 "/tmp/mylinux/lib"

mkdir: 已创建目录 "/tmp/mylinux/lib/modules"

mkdir: 已创建目录 "/tmp/mylinux/lib64"

mkdir: 已创建目录 "/tmp/mylinux/proc"

mkdir: 已创建目录 "/tmp/mylinux/sbin"

mkdir: 已创建目录 "/tmp/mylinux/sys"

mkdir: 已创建目录 "/tmp/mylinux/tmp"

mkdir: 已创建目录 "/tmp/mylinux/usr"

mkdir: 已创建目录 "/tmp/mylinux/usr/local"

mkdir: 已创建目录 "/tmp/mylinux/usr/local/bin"

mkdir: 已创建目录 "/tmp/mylinux/usr/sbin"

mkdir: 已创建目录 "/tmp/mylinux/var"

mkdir: 已创建目录 "/tmp/mylinux/var/lock"

mkdir: 已创建目录 "/tmp/mylinux/var/log"

mkdir: 已创建目录 "/tmp/mylinux/var/run"

[root@localhost tmp]# tree mylinux/

mylinux/

├── 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

 

24 directories, 0 files

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

文件的元数据有:

access:文件的访问时间

modify:文件的修改时间

change:文件的改变时间

 

使用stat命令查看

[root@localhost tmp]# ls

mylinux  test2

[root@localhost tmp]# touch ss

[root@localhost tmp]# ls

mylinux  ss  test2

[root@localhost tmp]# stat ss

  File: "ss"

  Size: 0          Blocks: 0          IO Block: 4096   普通空文件

Device: 803h/2051d Inode: 791845      Links: 1

Access: (0644/-rw-r–r–)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2016-12-24 06:03:43.885419559 +0800

Modify: 2016-12-24 06:03:43.885419559 +0800

Change: 2016-12-24 06:03:43.885419559 +0800

 

修改文件的访问时间:

touch -a

[root@localhost tmp]#

[root@localhost tmp]# stat ss

  File: "ss"

  Size: 0          Blocks: 0          IO Block: 4096   普通空文件

Device: 803h/2051d Inode: 791845      Links: 1

Access: (0644/-rw-r–r–)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2016-12-24 06:05:39.063719506 +0800

Modify: 2016-12-24 06:05:39.063719506 +0800

Change: 2016-12-24 06:05:39.063719506 +0800

[root@localhost tmp]# touch -a ss

[root@localhost tmp]# stat ss

  File: "ss"

  Size: 0          Blocks: 0          IO Block: 4096   普通空文件

Device: 803h/2051d Inode: 791845      Links: 1

Access: (0644/-rw-r–r–)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2016-12-24 06:08:05.122793372 +0800

Modify: 2016-12-24 06:05:39.063719506 +0800

Change: 2016-12-24 06:08:05.122793372 +0800

 

 

 

touch -m 修改文件的修改时间

[root@localhost tmp]# touch -m ss

[root@localhost tmp]# stat ss

  File: "ss"

  Size: 0          Blocks: 0          IO Block: 4096   普通空文件

Device: 803h/2051d Inode: 791845      Links: 1

Access: (0644/-rw-r–r–)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2016-12-24 06:08:05.122793372 +0800

Modify: 2016-12-24 06:09:53.767543500 +0800

Change: 2016-12-24 06:09:53.767543500 +0800

 

[root@localhost tmp]# touch -t 202011111111.11 ss

[root@localhost tmp]# stat ss

  File: "ss"

  Size: 0          Blocks: 0          IO Block: 4096   普通空文件

Device: 803h/2051d Inode: 791845      Links: 1

Access: (0644/-rw-r–r–)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2020-11-11 11:11:11.000000000 +0800

Modify: 2020-11-11 11:11:11.000000000 +0800

Change: 2016-12-24 06:13:15.432338068 +0800

 

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

alias命令定义一个命令的别名

[root@localhost tmp]# alias

alias l.='ls -d .* –color=auto'

alias ll='ls -l –color=auto'

alias ls='ls –color=auto'

alias mv='mv -i'

alias rm='rm -i'

alias which='alias | /usr/bin/which –tty-only –read-alias –show-dot –show-tilde'

[root@localhost tmp]# alias cp='cp -i'

[root@localhost tmp]# alias

alias cp='cp -i'

alias l.='ls -d .* –color=auto'

alias ll='ls -l –color=auto'

alias ls='ls –color=auto'

alias mv='mv -i'

alias rm='rm -i'

alias which='alias | /usr/bin/which –tty-only –read-alias –show-dot –show-tilde'

[root@localhost tmp]# ls

mylinux  ss  test2

[root@localhost tmp]# cp ss dd

[root@localhost tmp]# cp ss dd

cp:是否覆盖"dd" y

[root@localhost tmp]# ls

dd  mylinux  ss  test2

 

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

       [root@localhost tmp]# cp /etc/issue issue.bak

[root@localhost tmp]# ls

dd  issue.bak  mylinux  ss  test2

[root@localhost tmp]# cat issue.bak

CentOS release 6.3 (Final)

Kernel \r on an \m

[root@localhost tmp]# cat issue.bak | tr 'a-z' 'A-Z'

CENTOS RELEASE 6.3 (FINAL)

KERNEL \R ON AN \M

 

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

[root@localhost tmp]# ls -d /var/1*[0-9]*[[:lower:]]

ls: 无法访问/var/l*[0-9]*[[:lower:]]: 没有那个文件或目录

[root@localhost tmp]# touch /var/1A4???b

[root@localhost tmp]# ls -d /var/1*[0-9]*[[:lower:]]

/var/1A4???b

 

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

   [root@localhost tmp]# ls -d /etc/[0-9]*[^0-9]

ls: 无法访问/etc/[0-9]*[^0-9]: 没有那个文件或目录

[root@localhost tmp]# mkdir /etc/5iu

[root@localhost tmp]# ls -d /etc/[0-9]*[^0-9]

/etc/5iu

 

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

[root@localhost tmp]# ls -d /etc/[^a-z][^[:alpha:]]*

ls: 无法访问/etc/[^a-z][^[:alpha:]]*: 没有那个文件或目录

[root@localhost tmp]# touch /etc/46jaojg

[root@localhost tmp]# ls -d /etc/[^a-z][^[:alpha:]]*

/etc/46jaojg

 

9.       /tmp目录下创建以tfile开头,后跟当前日期和时间的文件,文件名形如:tfile-2016-05-27-09-32-22

[root@localhost tmp]# touch tfile-$(date +%F-%H-%M-%S)

[root@localhost tmp]# ls

dd  issue.bak  mylinux  ss  test2  tfile-2016-12-24-06-59-46

 

 

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

[root@localhost tmp]# mkdir mytest1

[root@localhost tmp]# ls

mytest1  test2

[root@localhost tmp]# cp -r /etc/p*[^0-9] ./mytest1/

[root@localhost tmp]# ls

mytest1  test2 [root@localhost tmp]# ls -la mytest1/

总用量 72

drwxr-xr-x. 12 root root 4096 12 24 07:04 .

drwxrwxrwt.  5 root root 4096 12 24 07:03 ..

drwxr-xr-x.  2 root root 4096 12 24 07:04 pam.d

drwxr-xr-x.  3 root root 4096 12 24 07:04 pango

-rw-r–r–.  1 root root 1018 12 24 07:04 passwd

-rw-r–r–.  1 root root 1055 12 24 07:04 passwd-

drwxr-xr-x.  8 root root 4096 12 24 07:04 pki

 

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

[root@localhost tmp]# ls

mytest1  test2

[root@localhost tmp]# mkdir mytest2

[root@localhost tmp]# ls

mytest1  mytest2  test2

[root@localhost tmp]# cp -r /etc/*.d ./mytest2

[root@localhost tmp]# ls -la mytest2

总用量 88

drwxr-xr-x. 22 root root 4096 12 24 07:07 .

drwxrwxrwt.  6 root root 4096 12 24 07:07 ..

drwxr-xr-x.  2 root root 4096 12 24 07:07 bash_completion.d

drwxr-xr-x.  2 root root 4096 12 24 07:07 chkconfig.d

drwxr-xr-x.  2 root root 4096 12 24 07:07 cron.d

drwxr-xr-x.  2 root root 4096 12 24 07:07 depmod.d

drwxr-xr-x.  2 root root 4096 12 24 07:07 dracut.conf.d

drwxr-xr-x.  2 root root 4096 12 24 07:07 event.d

lrwxrwxrwx.  1 root root   11 12 24 07:07 init.d -> rc.d/init.d

drwxr-xr-x.  2 root root 4096 12 24 07:07 ld.so.conf.d

drwxr-xr-x.  2 root root 4096 12 24 07:07 logrotate.d

drwxr-xr-x.  2 root root 4096 12 24 07:07 makedev.d

drwxr-xr-x.  2 root root 4096 12 24 07:07 modprobe.d

drwxr-xr-x.  2 root root 4096 12 24 07:07 pam.d

drwxr-xr-x.  2 root root 4096 12 24 07:07 popt.d

drwxr-xr-x.  2 root root 4096 12 24 07:07 profile.d

 

12.   复制/etc目录下所有以1mn开头,以.com结尾的文件至/tmp/mytest3目录中

[root@localhost tmp]# touch /etc/l2534.com

[root@localhost tmp]# cp /etc/[l,m,n]*.com ./mytest3

[root@localhost tmp]# ls mytest3

l2534.com

[root@localhost tmp]# ls -la mytest3

总用量 8

drwxr-xr-x. 2 root root 4096 12 24 07:13 .

drwxrwxrwt. 7 root root 4096 12 24 07:13 ..

-rw-r–r–. 1 root root    0 12 24 07:13 l2534.com

原创文章,作者:leon,如若转载,请注明出处:http://www.178linux.com/64587

(0)
上一篇 2016-12-23 21:24
下一篇 2016-12-24 11:17

相关推荐

  • 计算机运维基础知识 Linux版

    计算机系统由硬件系统和软件系统组成 第一代计算机   电子管时代 第二代计算机   晶体管时代 第三代计算机   集成电路时代 第四代计算机   大规模集成电路时代 1964年,世界第一代计算机ENICA。用来计算弹道轨迹。 冯诺依曼  计算机之父 运算器,控制器,存储器,输入设备,输出设备。 巨型计算机,大…

    Linux干货 2016-08-04
  • Linux第三周

    一.正则表达式和扩展正则表达式 1.首先有各种工具来对文本进行的查看,分析,统计   cat 命令:查看文本的工具    cat主要有三大功能:1.一次显示整个文件。$ cat filename2.从键盘创建一个文件。$ cat > filename     只能创建新文件,不能编辑已有文件….

    2017-06-04
  • OPENSSL&DNS

      1、详细描述一次加密通讯的过程,结合图示最佳。 Client 从互联网下载CA的公钥,用于验证Server身份 Server 通过加密算法生成一对密钥,将公钥发给CA认证机构,做数字证书 CA 通过自己的私钥加密 Server 公钥并加上自己的数字签名后,将生成的数字证书发给Server Client 与 Server 通TCP的三次握手建立连…

    Linux干货 2016-12-05
  • M20用户和组练习题和作业

    1、创建用户gentoo ,附加组为bin 和root ,默认shell为/bin/csh ,注释信息为"Gentoo Distribution"  useradd gentoo -G bin,boot -s /bin/csh -c "Gentoo Distribution" 2 、创建下面的用户、组和组成员…

    Linux干货 2016-08-03
  • tcp socket文件句柄泄漏

    今天发现有台redis机器上出现socket个数告警,这是很奇怪的现象。因为一台redis服务器上就部署了几个redis实例,打开的端口应该是有限。 1、netstat显示的tcp连接数正常 netstat -n | awk '/^tcp/ {++state[$NF]} END …

    Linux干货 2016-04-13
  • 初识正则表达式

    正则表达式是个什么东东? 正则表达式,又称正规表示法、常规表示法(英语:Regular Expression,在代码中常简写为regex、regexp或RE)。在很多文本编辑器里、命令中,通常要使用检索、替换、放行和拒绝那些符合某个模式的文本。而正则表达式就是用于描述这些规则的工具。换句话说,正则表达式就是记录文本规则的代码。  摘自《正则表达式之…

    Linux干货 2015-03-27