megeedu Linux+Python高級運維班 3期 第二週作業

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

    答:Linux上常用的文件管理類命令有cp(複製)、mv(移動/改名)、rm(刪除)等命令。

         ①文件複製命令

            命令格式:

                   cp [OPTION]... [-T] SOURCE DEST
                   cp [OPTION]... SOURCE... DIRECTORY
                   cp [OPTION]... -t DIRECTORY SOURCE...

           常用選項:
                  -i:交互式
                  -r, -R: 遞歸複製目錄及內部的所有內容;
                  -a: 歸檔,相當於-dR --preserv=all

           解釋:

               cp SRC DEST
                    SRC是文件:
                          如果目錄不存在:新建DEST,並將SRC中內容填充至DEST中;

#將root目錄下的文件a.txt 複製到目錄/root/test下
[root@localhost ~]# ls
anaconda-ks.cfg  a.txt  install.log  install.log.syslog
[root@localhost ~]# cp /root/a.txt /root/test
[root@localhost ~]# ls
anaconda-ks.cfg  a.txt  install.log  install.log.syslog  test
[root@localhost ~]# cat a.txt 
aaa
bb
cc
[root@localhost ~]# cat test 
aaa
bb
cc
[root@localhost ~]#

                          如果目錄存在:將SRC中的內容填充之DEST中;

                                如果DEST是文件:將SRC中的內容覆蓋至DEST中;此時建議爲cp命令使用-i選項;

#將root目錄下的文件a.txt 複製到已存在文件/root/test2中
[root@localhost ~]# touch test2
[root@localhost ~]# ls
anaconda-ks.cfg  a.txt  install.log  install.log.syslog  test  test1 test2
[root@localhost ~]# cp -i /root/a.txt /root/test2 
cp:是否覆蓋"/root/test2"? y
[root@localhost ~]# ls
anaconda-ks.cfg  a.txt  install.log  install.log.syslog  test  test1  test2
[root@localhost ~]# cat a.txt
aaa
bb
cc
[root@localhost ~]# cat test2
aaa
bb
cc

                                 如果DEST是目錄:在DEST下新建與原文件同名的文件,並將SRC中內容填充至新文件中;

#將root目錄下的文件a.txt 複製到已存在目錄/root/test1下
[root@localhost ~]# mkdir test1
[root@localhost ~]# ls
anaconda-ks.cfg  a.txt  install.log  install.log.syslog  test  test1
[root@localhost ~]# cp /root/a.txt /root/test1/
[root@localhost ~]# ls
anaconda-ks.cfg  a.txt  install.log  install.log.syslog  test  test1
[root@localhost ~]# ls test1
a.txt
[root@localhost ~]#

        cp SRC... DEST
            SRC...:多個文件
                DEST必須存在,且爲目錄,其它情形均會出錯;

#如果DEST存在且爲文件
[root@localhost ~]# ls
anaconda-ks.cfg  a.txt  install.log  install.log.syslog  test  test1  test2
[root@localhost ~]# cp install.log* test    #將/root目錄下的install.log和install.log.syslog複製到已存在文件test,會產生報錯信息。
cp: 目標"test" 不是目錄
#如果DEST存在且爲目錄
[root@localhost ~]# cp install.log* test1
[root@localhost ~]# ls /root/test1
a.txt  install.log  install.log.syslog
[root@localhost ~]#


        cp SRC DEST
            SRC是目錄:
                此時使用選項:-r
                    如果DEST不存在:則創建指定目錄,複製SRC目錄中所有文件至DEST中;

#將目錄/root/test1中的內容複製到不存在的/root/mageedu目錄中
[root@localhost ~]# ls
anaconda-ks.cfg  a.txt  install.log  install.log.syslog  test  test1  test2
[root@localhost ~]# ls test1
a.txt  install.log  install.log.syslog
[root@localhost ~]# cp -r  test1 mageedu
[root@localhost ~]# ls
anaconda-ks.cfg  a.txt  install.log  install.log.syslog  mageedu  test  test1  test2
[root@localhost ~]# ls mageedu/
a.txt  install.log  install.log.syslog

                    如果DEST存在:
                        如果DEST是文件:報錯
                        如果DEST是目錄:在目標目錄中創建源目錄下的目錄及文件

#將/etc/rc.d/下的內容複製到/root/mageedu/下
[root@localhost ~]# ls /etc/rc.d/
init.d  rc  rc0.d  rc1.d  rc2.d  rc3.d  rc4.d  rc5.d  rc6.d  rc.local  rc.sysinit   
[root@localhost ~]# cp -r /etc/rc.d/ mageedu
[root@localhost ~]# ls mageedu/
a.txt  install.log  install.log.syslog  rc.d
[root@localhost ~]# ls mageedu/rc.d/
init.d  rc  rc0.d  rc1.d  rc2.d  rc3.d  rc4.d  rc5.d  rc6.d  rc.local  rc.sysinit
[root@localhost ~]#

        ②mv: move,移動文件(通常也用其對文件或目錄重命名)
           命令格式:      

                  mv [OPTION]... [-T] SOURCE DEST
                  mv [OPTION]... SOURCE... DIRECTORY
                  mv [OPTION]... -t DIRECTORY SOURCE...
           常用選項:
                 -i: 交互式
                 -f: 強制

#將root/test移動到/root/test1下
[root@localhost ~]# ls
anaconda-ks.cfg  a.txt  install.log  install.log.syslog  test  test1  test2
[root@localhost ~]# ls test1
a.txt  install.log  install.log.syslog
[root@localhost ~]# mv /root/test /root/test1/
[root@localhost ~]# ls
anaconda-ks.cfg  a.txt  install.log  install.log.syslog  test1  test2
[root@localhost ~]# ls test1
a.txt  install.log  install.log.syslog  test
[root@localhost ~]#
#將root/test2重命名爲test
[root@localhost ~]# pwd
/root
[root@localhost ~]# ls
anaconda-ks.cfg  a.txt  install.log  install.log.syslog  test  test1
[root@localhost ~]# mv test2 test  //由於現在處在root目錄下,故沒有使用絕對路徑
[root@localhost ~]# ls
anaconda-ks.cfg  a.txt  install.log  install.log.syslog  test  test1
[root@localhost ~]#

        ③rm: remove,刪除
            命令格式:      

                   rm [OPTION]... FILE...
           常用選項:
                 -i: 交互式
                 -f: 強制刪除
                 -r: 遞歸

#  rm -rf     //使用時注意路徑

#將/root/mageedu 目錄及內容刪除
[root@localhost ~]# ls
anaconda-ks.cfg  a.txt  install.log  install.log.syslog  mageedu  test  test1  test2
[root@localhost ~]# rm -rf /root/mageedu/
[root@localhost ~]# ls
anaconda-ks.cfg  a.txt  install.log  install.log.syslog  test  test1  test2

2、bash的工作特性之命令執行狀態返回值和命令行展開所涉及的內容及其示例演示。
bash的基礎特性(1):
    ①命令行展開
        ~: 展開爲用戶的主目錄

[root@localhost ~]# echo ~
/root

        ~USERNAME:展開爲指定用戶的主目錄

[root@localhost ~]# echo ~ftp
/var/ftp

        {}:可承載一個以逗號分隔的列表,並將其展開爲多個路徑

#例如/root/{a,b}_{c,d}相當於/root/a_c,/root/a_d,/root/b_c,/root/b_d
[root@localhost ~]# mkdir /root/{a,b}_{c,d}
[root@localhost ~]# ls
a_c  a_d  anaconda-ks.cfg  a.txt  b_c  b_d  install.log  install.log.syslog  test  test1
[root@localhost ~]#

    ②命令的執行結果狀態只有兩種(成功)或者(失敗)
            $?保存最近一條命令的執行狀態結果:
                      0:成功
                      1-255:失敗
       程序執行有兩類結果:
            程序的返回值;
            程序的執行狀態結果;

3、請使用命令行展開功能來完成以下練習:

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

[root@localhost ~]# ls /tmp
yum.log  yum_save_tx-2016-08-01-01-55m_XEdJ.yumtx
[root@localhost ~]# mkdir /tmp/{a,b}_{c,d}
[root@localhost ~]# ls /tmp
a_c  a_d  b_c  b_d  yum.log  yum_save_tx-2016-08-01-01-55m_XEdJ.yumtx
[root@localhost ~]#

(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 ~]# 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@localhost ~]# tree /tmp/mylinux
/tmp/mylinux/
├── bin
├── boot
│   └── grub
├── dev
├── etc
│   ├── rc.d
│   │   └── init.d
│   └── sysconfig
│       └── network-scripts
├── lib
│   └── modules
├── lib64
├── lock
├── log
├── proc
├── run
├── sbin
├── sys
├── tmp
├── usr
│   └── local
│       ├── bin
│       └── sbin
└── var

24 directories, 0 files
[root@localhost ~]#

4、文件的元數據信息有哪些,分別表示什麼含義,如何查看?如何修改文件的時間戳信息。

    答:文件元數據信息包括File,Size,Device,Access,Modify,Change

           File:表示文件的路徑。

           Size:表示文件大小,包括塊大小等。

           Device:表示文件的信息,包括inode節點數和link數等。

           Access:文件的訪問信息和權限。

           Modify:該文件修改時間。

           Change:狀態改變時間。

    使用命令stat查看文件元數據

[root@localhost ~]# stat /root/test
  File: "/root/test"
  Size: 10            Blocks: 8          IO Block: 4096   普通文件
Device: fd00h/64768d    Inode: 393234      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-08-12 13:43:36.174273556 +0800
Modify: 2016-08-12 13:44:00.617273161 +0800
Change: 2016-08-12 14:21:51.148263979 +0800
[root@localhost ~]#

        三個時間戳:
            access time:訪問時間,簡寫爲atime,讀取文件內容
            modify time: 修改時間, mtime,改變文件內容(數據)
            change time: 改變時間, ctime,元數據發生改變

    touch命令:(修改三個時間戳的前兩個)
        touch [OPTION]... FILE...
            -a: only atime
            -m: only mtime
            -t STAMP:
                 [[CC]YY]MMDDhhmm[.ss]
            -c: 如果文件不存在,則不予創建

5、如何定義一個命令的別名,如何在命令中引用另一個命令的執行結果?

     使用命令alias定義別名
            alias NAME='VALUE'

      撤消別名:unalias
            unalias [-a] name [name ...]

     使用管道符“|”引用另一個命令的執行結果

[root@localhost ~]# cat /etc/passwd | grep "ftp"
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[root@localhost ~]#

6、顯示/var目錄下所有以l開頭,以一個小寫字母結尾,且中間至少出現一位數字(可以有其它字符)的文件或目錄。

    答:使用命令

[root@localhost var]# touch  l{e3,3e,22,333,2e3}d
[root@localhost var]# ls
cache  db     games  l2e3d  l3ed  lib    lock  mail  opt       run    tmp
crash  empty  l22d   l333d  le3d  local  log   nis   preserve  spool  yp
[root@localhost var]# ls /var | egrep 1?[0-9][a-z]
l22d
l2e3d
l333d
l3ed
le3d
[root@localhost var]#

7、顯示/etc目錄下,以任意一個數字開頭,且以非數字結尾的文件或目錄。

[root@localhost ~]# touch /etc/{0,9}{e3,33,r,3w,djei,334w}
[root@localhost ~]# ls /etc/ | more
033
0334w
03w
0djei
0e3
0r
933
9334w
93w
9djei
9e3
9r
--More--
[root@localhost ~]# ls  /etc/[0-9]*[[:alpha:]]
/etc/0334w  /etc/0djei  /etc/9334w  /etc/9djei
/etc/03w    /etc/0r     /etc/93w    /etc/9r

8、顯示/etc目錄下,以非字母開頭,後面跟了一個字母以及其它任意長度任意字符的文件或目錄。

[root@localhost ~]# ls -d /etc/[^[:alpha:]][a-z]*
/etc/0djei  /etc/0e3  /etc/0r  /etc/9djei  /etc/9e3  /etc/9r
[root@localhost ~]#

9、在/tmp目錄下創建以tfile開頭,後跟當前日期和時間的文件,文件名形如:tfile-2016-08-06-09-32-22。

[root@localhost ~]# touch /tmp/tfile-"$(date +'%F-%H-%M-%S')"
[root@localhost ~]# ls /tmp
a_c  b_c  mylinux                    yum.log
a_d  b_d  tfile-2016-08-12-17-01-01  yum_save_tx-2016-08-01-01-55m_XEdJ.yumtx
[root@localhost ~]#

10、複製/etc目錄下所有以p開頭,以非數字結尾的文件或目錄到/tmp/mytest1目錄中。

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

11、複製/etc目錄下所有以.d結尾的文件或目錄至/tmp/mytest2目錄中。

[root@localhost ~]# mkdir -p /tmp/mytest2 && cp -a /etc/*.d /tmp/mytest2
[root@localhost ~]# ls /tmp/mytest2
bash_completion.d  cron.d    dracut.conf.d  ld.so.conf.d  makedev.d   pam.d   prelink.conf.d  rc0.d  rc2.d  rc4.d  rc6.d  rsyslog.d  statetab.d  sysctl.d  yum.repos.d
chkconfig.d        depmod.d  init.d         logrotate.d   modprobe.d  popt.d  profile.d       rc1.d  rc3.d  rc5.d  rc.d   rwtab.d    sudoers.d   xinetd.d
[root@localhost ~]#

12、複製/etc/目錄下所有以l或m或n開頭,以.conf結尾的文件至/tmp/mytest3目錄中。

[root@localhost ~]# mkdir -p /tmp/mytest3 && cp -a /etc/[l,m,n]*.conf /tmp/mytest3
[root@localhost ~]# ls /tmp/mytest3
ld.so.conf  libaudit.conf  libuser.conf  logrotate.conf  mke2fs.conf  nsswitch.conf


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章