马哥教育网络班20期+第2周课程练习

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

 文件管理类命令有ls,cat,touch,stat,cp,mv,rm等

 ls:查看文件,其使用方法以及常用选项有:

Usage: ls [OPTION]… [FILE]…

常用选项:

  -a:列出所有内容,包括.和..

  -A:同-a,但不包含.和..

  -d:显示目录,不显示具体内容

  -h:size单位转换

  -i:显示file inode号

  -l:长模式显示

  -n:将属主与属组已id形式显示

  -p:将目录以/方式显示

  -1:一个文件一行显示

[root@localhost ~]# ls -l /usr
total 108
dr-xr-xr-x.  2 root root 16384 Jun 15 13:41 bin
drwxr-xr-x.  2 root root     6 Aug 12  2015 etc
drwxr-xr-x.  2 root root     6 Aug 12  2015 games
drwxr-xr-x.  3 root root    22 May 15 23:08 include
dr-xr-xr-x. 26 root root  4096 May 15 23:09 lib
dr-xr-xr-x. 41 root root 20480 Jun 15 13:41 lib64
drwxr-xr-x. 15 root root  4096 May 15 23:09 libexec
drwxr-xr-x. 12 root root  4096 May 15 23:07 local
dr-xr-xr-x.  2 root root 12288 May 15 23:09 sbin
drwxr-xr-x. 73 root root  4096 Jun 15 13:41 share
drwxr-xr-x.  4 root root    32 May 15 23:07 src
lrwxrwxrwx.  1 root root    10 May 15 23:07 tmp -> ../var/tmp

cat:文件内容查看

Usage:cat [OPTION]… [FILE]…  

[root@localhost ~]# cat /etc/fstab 
#
# /etc/fstab
# Created by anaconda on Sun May 15 23:07:50 2016
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=dba51523-2705-40e9-9c5f-bc69750cfb73 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0

  touch:改变文件时间戳,同时也可以创建文件

[root@localhost home]# touch mytest.sh
[root@localhost home]# ls
mytest.sh  srcipts  user1

  stat:查看文件的属性信息

[root@localhost home]# stat mytest.sh 
  File: ‘mytest.sh’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 34575321    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:home_root_t:s0
Access: 2016-06-21 18:58:31.086792577 +0800
Modify: 2016-06-21 18:58:31.086792577 +0800
Change: 2016-06-21 18:58:31.086792577 +0800
 Birth: -

  通过stat可以查看刚创建的mytest.sh的属性,包含大小、块信息、属主、属组、详细时间戳等

 cp:主要用于复制拷贝文件

 cp SRC DEST

    1.SRC是文件:

       如果目标不存在:新建DEST,并将SRC中内容填充至DEST中;

       如果目标存在:

          如果DEST是文件:将SRC中的内容覆盖至DEST中;此时建议为cp命令使用-i选项;

        如果DEST是目录:在DEST下新建与原文件同名的文件,并将SRC中内容填充至新文件中;

    2.SRC…:多个文件

        DEST必须存在,且为目录,其它情形均会出错;

    3,SRC是目录:此时使用选项:-r

       如果DEST不存在:则创建指定目录,复制SRC目录中所有文件至DEST中;

       如果DEST存在:

          如果DEST是文件:报错

          如果DEST是目录:

[root@localhost home]# cp mytest.sh mytest.sh.bak
[root@localhost home]# ls
mytest.sh  mytest.sh.bak  srcipts  user1
[root@localhost home]# ls /tmp
[root@localhost home]# cp  mytest.sh /tmp/
[root@localhost home]# ls /tmp
mytest.sh
[root@localhost home]#

mv:移动文件,用法同cp

rm:删除文件

  常用选项:

    -i: 交互式

    -f: 强制删除

    -r: 递归

[root@localhost home]# rm mytest.sh
rm: remove regular empty file ‘mytest.sh’? y
[root@localhost home]# ls
mytest.sh.bak  srcipts  user1
[root@localhost home]#

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

  • bash之命令执行状态返回值

命令执行完成后有两种结果状态:成功、失败,在bash中使用特殊变量$?保存最近一条命令的执行结果状态:

0:表示命令执行成功;

1-255:表示命令执行失败

[root@localhost home]# ls
mytest.sh.bak  srcipts  user1
[root@localhost home]# echo $?
0
[root@localhost home]# lss
-bash: lss: command not found
[root@localhost home]# echo $?
127
[root@localhost home]#
  • 命令行展开

~:展开当前用户的主目录

~USERNAME:展开指定用户的主目录

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

[root@localhost home]# id
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[root@localhost home]# cd ~
[root@localhost ~]# pwd
/root
[root@localhost ~]# cd ~user1
[root@localhost user1]# pwd
/home/user1
[root@localhost home]# mkdir test{1,2,3}
[root@localhost home]# ls
mytest.sh.bak  srcipts  test1  test2  test3  user1

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

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

[root@localhost home]# mkdir /tmp/{a,b}_{c,d}
[root@localhost home]# ls /tmp
a_c  a_d  b_c  b_d  mytest.sh

   (2)、创建/tmp/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

[root@localhost home]# mkdir -p /tmp/mylinux/{bin,boot/grub,dev,etc/{rc.d/init.d,sysconfig/network-script},lib/modules,lib64,proc,sbin,sys,tmp,usr/local/{bin,sbin},var/{lock,log,run}}
[root@localhost home]# tree
-bash: tree: command not found
[root@localhost home]# ls -lr /tmp/mylinux/
total 0
drwxr-xr-x. 5 root root 37 Jun 21 19:24 var
drwxr-xr-x. 3 root root 18 Jun 21 19:24 usr
drwxr-xr-x. 2 root root  6 Jun 21 19:24 tmp
drwxr-xr-x. 2 root root  6 Jun 21 19:24 sys
drwxr-xr-x. 2 root root  6 Jun 21 19:24 sbin
drwxr-xr-x. 2 root root  6 Jun 21 19:24 proc
drwxr-xr-x. 2 root root  6 Jun 21 19:24 lib64
drwxr-xr-x. 3 root root 20 Jun 21 19:24 lib
drwxr-xr-x. 4 root root 33 Jun 21 19:24 etc
drwxr-xr-x. 2 root root  6 Jun 21 19:24 dev
drwxr-xr-x. 3 root root 17 Jun 21 19:24 boot
drwxr-xr-x. 2 root root  6 Jun 21 19:24 bin

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

文件的元数据信息包含文件的大小、属主、属组、创建时间、访问时间、修改时间、权限等

可通过ls -l或者stat查看文件的元数据信息,touch可以修改文件的时间戳信息,具体“用法在文件管理类命令”中已介绍。

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

alias可以定义别名:

1.alias显示当前所有命令别名
[root@localhost home]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
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'
2.alias NAME='VALUE‘,定义别名
[root@localhost network-scripts]# alias cdnet='cd /etc/sysconfig/network-scripts/'
3.unalias取消别名
unalias cdnet

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

[root@localhost /]# ls -d /var/1*[0-9]*[a-z]
ls: cannot access /var/1*[0-9]*[a-z]: No such file or directory

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

[root@localhost etc]# ls -d /etc/[0-9]*[^0-9]
/etc/23dfdf  /etc/2abcd

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

[root@localhost etc]# ls -d /etc/[^[:alpha:]][[:alpha:]]*
/etc/2abcd  /etc/3acbc2

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

[root@localhost etc]# touch /tmp/tfile-`date +%Y-%m-%d-%H-%M-%S`
[root@localhost etc]# ls /tmp
a_c  a_d  b_c  b_d  mylinux  mytest.sh  tfile-2016-06-21-19-52-38

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

[root@localhost etc]# mkdir -p /tmp/mytest1
[root@localhost etc]# cp -r /etc/p*[^0-9] /tmp/mytest1/
[root@localhost etc]# ls /tmp/mytest1/
pam.d   passwd-  plymouth  popt.d   ppp             printcap  profile.d  python
passwd  pki      pm        postfix  prelink.conf.d  profile   protocols

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

[root@localhost etc]# cp -r /etc/*.d /tmp/mytest2/
[root@localhost etc]# ls /tmp/mytest2
bash_completion.d  dracut.conf.d  modules-load.d  rc0.d  rc6.d       sysctl.d
binfmt.d           grub.d         my.cnf.d        rc1.d  rc.d        tmpfiles.d
chkconfig.d        init.d         pam.d           rc2.d  rsyslog.d   xinetd.d
cron.d             ld.so.conf.d   popt.d          rc3.d  rwtab.d     yum.repos.d
depmod.d           logrotate.d    prelink.conf.d  rc4.d  statetab.d
dnsmasq.d          modprobe.d     profile.d       rc5.d  sudoers.d

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

[root@localhost etc]# ls /tmp/mytest3
ld.so.conf     libuser.conf  logrotate.conf  mke2fs.conf
libaudit.conf  locale.conf   man_db.conf     nsswitch.conf

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

(0)
上一篇 2016-06-23 11:06
下一篇 2016-06-23 11:06

相关推荐

  • 再不自动化就晚啦!优云教你4步打造基于CentOS的产品镜像

    随着Linux程序的增多,软件的安装过程中经常出现如下问题: 1、硬件配置类似或者相同时,批量安装系统和软件,希望实现自动化安装,减少安装时间和人为出错。 2、工程实施人员在不同客户现场进行系统和软件安装(硬件配置不同),由于硬件有差别,不容易实现自动化。 笔者针对以上场景,从提高生产效率,减少误操作的立场,提出如下解决方案,希望能达到抛砖引玉的目的,并能与…

    系统运维 2016-07-16
  • 马哥教育网络班20期+第七周博客作业

    1、创建一个10G分区,并格式为ext4文件系统     (1) 要求其block大小为2048,预留空间百分比为2,卷标为MYDATA,默认挂载属性包含acl fdisk /dev/sdbnp1110Gw[root@llww3317 ~]# mke2fs -t ext…

    Linux干货 2016-08-02
  • 苦上半年时间

    这是一种享受

    Linux干货 2016-08-08
  • Linux的终端类型

    Linux的终端类型       终端是一种字符型设备,它有多种类型,通常使用tty来简称各种类型的终端设备,linux终端大致有设备终端,物理终端、虚拟终端、图形终端、串行终端、伪终端等。 一.设备终端   设备终端的是一些看的见摸得着的一些实物,比如鼠标、键盘、显示器、打印机等之类的实物。 二.…

    Linux干货 2016-10-14
  • 初识Linux

        Linux系统现在如日中天,特别是在如今的互联网+大潮下,大数据、云计算、移动设备嵌入式设备等,无不显现着linux的身影。 现在,问题来了,Linux究竟是个什么鬼?我们平时经常接触的RedHat,CentOS等又是什么东东?别急,让我们一起来慢慢了解一下。     Linux是个类Unix操作系统,设计风格…

    Linux干货 2016-05-23
  • linux下的打包与压缩

    linux压缩或解压缩工具有很多,除了已经很少有人使用的compress外,现在常用的还有tar,bzip2,xz 和gzip等,我们来说说它们的用法。 先来说bzip2。bunzip2和bzcat可以由bzip2指定选项来执行同样的结果,这里只介绍bzip2的用法。使用bzip2这个工具创建的文件以.bz2,.bz,.tbz,.tar.bz2或者…

    Linux干货 2017-04-16

评论列表(1条)

  • 马哥教育
    马哥教育 2016-06-23 12:48

    写的非常棒,有案例做为辅助,但是最后几个好像是有点小瑕疵,在仔细测试一下