网络班22期+第二周作业练习

常用的文件管理命令:

  • ls,显示文件或目录

    • -l:显示文件或目录的包括权限、属主、属组大小创建日期等详细信息

      • [root@centos7 ~]# ls -l
        total 16
        -rw-r--r--. 1 root root   64 Aug 27 18:33 11
        -rw-r--r--. 1 root root   63 Aug 27 17:54 22
        -rw-------. 1 root root 1242 Jul 29 17:51 anaconda-ks.cfg
        drwxr-xr-x. 2 root root    6 Aug 21 00:45 Desktop
        drwxr-xr-x. 2 root root    6 Aug 21 00:45 Documents
        drwxr-xr-x. 2 root root    6 Aug 21 00:45 Downloads
    • -a:显示所有文件,-A:显示所有文件但不包过. ..

    • -d:显示目录本身而非目录下的文件,一般配合-l使用

      • [root@centos7 ~]# ls Desktop
        mytest.out
        [root@centos7 ~]# ls -dl Desktop
        drwxr-xr-x. 2 root root 23 Sep  5 17:07 Desktop
    • -h:配合 -l ,以K、M、G等方式显示文件大小,更易读,但文件大小不精确

    • -r:逆序显示,-R:递归显示

  • cp,复制文件

    • -i:覆盖交互提示

    • -r:递归复制

    • -f:强制覆盖

    • 单源复制

      • [root@centos7 tmp]# cp test1.pic testdir
    • 多源复制

      • [root@centos7 tmp]# cp testscr.pic testusb.pic testdir
    • 目录复制

      • [root@centos7 tmp]# cp -rf cups testdir
        [root@centos7 tmp]# ls testdir
        cups
  • mv:移动文件或改名,用法类似cp

    • [root@centos7 tmp]# mv test1.pic testdir
      [root@centos7 tmp]# mv testscr.pic testpic
  • rm:删除文件或目录,用法类似cp

    • -r:递归删除,慎用

    • -f:强制删除

    • -d:删除空目录

  • mkdir:创建文件夹

    • -p:如要创建的目标目录的父目录不存在,则自动为其创建

      • [root@centos7 tmp]# mkdir -pv d1/d2/d3
  • touch:创建空文件或修改文件时间戳

    • -c:目标文件不存在时,不予创建文件

    • -a:修改Access time

    • -m:修改Modify time

    • -t:指定时间

  • pwd:显示当前路径

  • cd:切换目录

    • cd ~:切换回家目录

    • cd -:上次和本次的目录来回切换

bash特性之命令状态返回值

linux命令有2种结果状态,一种是命令结果输出,一种是命令执行状态

如需要显示/tmp目录下的内容:

[root@centos7 ~]# ls /tmp
cups  d1  t1  t2  testdir  testpic  testusb.pic

如只需要知道 testpic文件是否存在,而不需要显示任何信息:

[root@centos7 tmp]# ls testpic &> /dev/null
[root@centos7 tmp]# echo $?
0

将命令执行结果(和错误信息)重定向至linux黑洞/dev/null,不让其显示任何信息

通过echo $?来检测命令是否执行成功,$?保存了上条命令的执行状态值

0:执行成功

非0:失败

bash特性之命令行展开

创建/tmp下的a、b、c,

一般为mkdir /tmp/a /tmp/b /tmp/c;

如用命令行展开特性则是 

mkdir /tmp{a,b,c},相当于mkdir /tmp/a /tmp/b /tmp/c

练习:

1、创建a_c,a_d,b_c,b_d

[root@centos7 t1]# mkdir -pv {a,b}_{c,d}
mkdir: created directory ‘a_c’
mkdir: created directory ‘a_d’
mkdir: created directory ‘b_c’
mkdir: created directory ‘b_d’

2、创建/tmp/mylinux下的若干目录

[root@centos7 ~]# mkdir -p /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}}

[root@centos7 ~]# tree /tmp/mylinux
/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

24 directories, 0 files

文件元数据

[root@centos7 ~]# stat 11
  File: ‘11’
  Size: 64        	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 138944011   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2016-08-27 18:33:45.928314700 +0800
Modify: 2016-08-27 18:33:36.369314412 +0800
Change: 2016-08-27 18:33:36.369314412 +0800
 Birth: -

文件名、大小、属主、属组,时间戳等等

用touch修改 Access、Modify、Change时间

命令别名 alias

显示别名alias

设置别名:

[root@centos7 ~]# alias chkver='uname -a'
[root@centos7 ~]# chkver
Linux centos7 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

删除别名:unalias chkver

[root@centos7 ~]# unalias chkver
[root@centos7 ~]# chkver
bash: chkver: command not found...

引用一个命令的执行结果

·· 和 $(cmd)

[root@centos7 ~]# mkdir $(date "+%Y-%m-%d-%T")

练习:

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

  2. [root@centos7 ~]# ls -d /var/l*[0-9]*[a-z]
  3. 显示/etc下,以任意1个数字开头,且以非数字结尾的文件或目录

  4. [root@centos7 ~]# ls /etc/[0-9]*[^0-9]
  5. 显示/etc下以非字母开头,后跟了一个字母及其他任意长度任意字符的文件或目录

  6. [root@centos7 ~]# ls /etc/[^a-z][a-z]*
  7. 在/tmp下创建以tfile开头,后面跟当前日期和时间的文件

  8. [root@centos7 ~]# mkdir $(date "+%Y-%m-%d-%T")
  9. 复制/etc下所有以p开头,以非数字结尾的文件或目录到/tmp/mytest1

    复制/etc下所有以.d结尾的文件到/tmp/mytest2

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

[root@centos7 tmp]# mkdir -pv  mytest{1,2,3}
mkdir: created directory ‘mytest1’
mkdir: created directory ‘mytest2’
mkdir: created directory ‘mytest3’
[root@centos7 tmp]# cp -rf /etc/p*[^0-9] mytest1
[root@centos7 tmp]# cp -rf /etc/*.d mytest2
[root@centos7 tmp]# cp -rf /etc/[l,m,n]*.conf mytest3

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

(0)
上一篇 2016-09-05 18:02
下一篇 2016-09-06 08:50

相关推荐

  • vim编辑器和bash算术入门

    vim末行模式:       内建的命令行接口      (1)地址界定          :start_pos[,end_pos]          #: 特定的第#行,例如5即第5行; &nbsp…

    Linux干货 2016-12-23
  • 文件系统

            文件系统表现为单个统一的层次结构:从目录/开始并通过若干数量的子目录继续向下扩展,/也叫做根目录。这种单一的层次系统和 Windows 的不一样,后者的分区有专门的名字空间。        &n…

    Linux干货 2016-02-28
  • 马哥教育网络班21期+第7周课程练习

    1、创建一个10G分区,并格式为ext4文件系统; (1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl; [root@CentOS7 ~]# fdisk /dev/sdb Command (m for help): n Select…

    Linux干货 2016-08-22
  • 用户、组及文件相关的一些命令

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其他用户均没有任何访问权限。 cp -r /etc/skel /home/tuser1,因为是目录文件所以要有-r ls -ld /etc/skel chmod 000 -R /home/tuser1 ls -al /home/tuser1/ 2、编辑/…

    2017-07-23
  • 重定向、管道——Linux基本命令(9)

    1.     输出重定向 Linux默认输入是键盘,输出是显示器。可以用重定向来改变这些设置。比如用wc命令的时候本来是要手动输入一篇文字来计算字符数的,可以直接把一个已经写好的文件用‘<’指向这条命令,就直接可以统计这个文件的字符数等了。   STDOUT(标准输出)和STDERR(标准错误)可以被重…

    2017-07-20
  • 第九周作业

    1. 写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现; # awk -F: ‘{if($7!=”/sbin/nologin”) {printf “Logined user %s\n”…

    Linux干货 2017-03-01