馬哥2016全新Linux+Python高端運維班第二週作業

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

  • CP

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

  • -i 以交換形式拷貝文件

[15:03:44 root@qa36 /tmp]#cp -i file_1 file_2
cp:是否覆蓋"file_2"? y
[15:03:54 root@qa36 /tmp]#ll
總用量 4
-rw-r--r-- 1 root root    0 8月   9 15:03 file_1
-rw-r--r-- 1 root root    0 8月   9 15:03 file_2
  • -r 遞歸操作

[15:55:18 root@qa36 /tmp]#cp directroy_1/ directroy_2/
cp: 略過目錄"directroy_1/"
[15:58:02 root@qa36 /tmp]#cp -r directroy_1/ directroy_2/
  • -p 制時保留文件屬性

[15:13:17 root@qa36 /tmp]#ls -lrt file_1 
-rw-r--r-- 1 root root 0 8月   9 15:03 file_1
[15:13:52 root@qa36 /tmp]#cp -p file_1 file_2
[15:14:07 root@qa36 /tmp]#cp file_1 file_3
[15:14:23 root@qa36 /tmp]#ls -lrt file*
-rw-r--r-- 1 root root 0 8月   9 15:03 file_2
-rw-r--r-- 1 root root 0 8月   9 15:03 file_1
-rw-r--r-- 1 root root 0 8月   9 15:14 file_3
  • -P 只拷貝符號鏈接文件(只拷貝符號鏈接文件)

[15:39:52 root@qa36 /tmp]#ll
總用量 8
drwxr-xr-x 2 root root 4096 8月   9 15:22 directroy_1
-rw-r--r-- 1 root root    0 8月   9 15:03 file_1
lrwxrwxrwx 1 root root    6 8月   9 15:39 file_2 -> file_1
drwxr-xr-x 2 root root 4096 8月   9 13:38 hsperfdata_root

[15:39:52 root@qa36 /tmp]#cp file_2 file_3
[15:40:51 root@qa36 /tmp]#ll
總用量 8
drwxr-xr-x 2 root root 4096 8月   9 15:22 directroy_1
-rw-r--r-- 1 root root    0 8月   9 15:03 file_1
lrwxrwxrwx 1 root root    6 8月   9 15:39 file_2 -> file_1
-rw-r--r-- 1 root root    0 8月   9 15:40 file_3

[15:40:53 root@qa36 /tmp]#cp -P file_2 file_4
[15:41:07 root@qa36 /tmp]#ll
總用量 8
drwxr-xr-x 2 root root 4096 8月   9 15:22 directroy_1
-rw-r--r-- 1 root root    0 8月   9 15:03 file_1
lrwxrwxrwx 1 root root    6 8月   9 15:39 file_2 -> file_1
-rw-r--r-- 1 root root    0 8月   9 15:40 file_3
lrwxrwxrwx 1 root root    6 8月   9 15:41 file_4 -> file_1
  • -a 會保留原文件或目錄的屬性

[15:50:08 root@qa36 /tmp]#ll ./directroy_1/
總用量 0
-rw-r--r-- 1 root root 0 8月   9 15:47 file_1
lrwxrwxrwx 1 root root 6 8月   9 15:48 file_2 -> file_1

[15:52:04 root@qa36 /tmp]#cp -a directroy_1/ directroy_2/

[15:52:47 root@qa36 /tmp]#ll ./directroy_2/directroy_1/
總用量 0
-rw-r--r-- 1 root root 0 8月   9 15:47 file_1
lrwxrwxrwx 1 root root 6 8月   9 15:48 file_2 -> file_1
  • MV

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

  • 移動文件

[16:01:39 root@qa36 /tmp]#mv file_1 directroy_1/
[16:04:35 root@qa36 /tmp]#ll 
總用量 16
drwxr-xr-x 2 root root 4096 8月   9 16:04 directroy_1
-rw-r--r-- 1 root root    0 8月   9 16:01 file_2
-rw-r--r-- 1 root root    0 8月   9 16:01 file_3

[16:04:43 root@qa36 /tmp]#ll directroy_1/
總用量 0
-rw-r--r-- 1 root root 0 8月   9 16:01 file_1
  • 移動多個文件

[16:06:07 root@qa36 /tmp]#mv file_2 file_3 directroy_2/
[16:06:21 root@qa36 /tmp]#ll
總用量 16
drwxr-xr-x 2 root root 4096 8月   9 16:04 directroy_1
drwxr-xr-x 3 root root 4096 8月   9 16:06 directroy_2
drwxr-xr-x 3 root root 4096 8月   9 15:52 directroy_3
[16:06:22 root@qa36 /tmp]#ll directroy_2/
總用量 4
-rw-r--r-- 1 root root    0 8月   9 16:01 file_2
-rw-r--r-- 1 root root    0 8月   9 16:01 file_3
  • 移動目錄

[16:06:25 root@qa36 /tmp]#mv directroy_1 directroy_4
[16:07:35 root@qa36 /tmp]#ll directroy_4/
總用量 0
-rw-r--r-- 1 root root 0 8月   9 16:01 file_1
  • 重命名文件或目錄

我們也用 mv 命令來重命名文件或目錄。不過目標位置和源位置必須相同纔可以。然後文件名必須不同。
[16:09:41 root@qa36 /tmp]#mv file_1 file_2
  • -i 使用交互模式

[16:13:32 root@qa36 /tmp]#mv -i file_1 file_2
mv:是否覆蓋"file_2"? y
[16:13:38 root@qa36 /tmp]#
  • -f 強制覆蓋

[16:14:36 root@qa36 /tmp]#mv -f file_1 file_2
  • rm

       rm [OPTION]... FILE...

  • -i 交互模式

[16:22:47 root@qa36 /tmp]#rm -i file_1 
rm:是否刪除普通空文件 "file_1"?y
  • -r 遞歸刪除某個目錄下文件以及子目錄

[16:22:58 root@qa36 /tmp]#rm -r directroy_2/
  • -f 強制執行

[16:25:51 root@qa36 /tmp]#rm -rf directroy_3/

二、bash的工作特性之命令執行狀態返回值和命令行展開所涉及的內容及其示例演示。

  • 命令執行狀態返回值

echo $? 
返回 0,表示該函數成功執行完畢
[16:59:43 root@qa36 /tmp]#mkdir directroy_1
[16:59:51 root@qa36 /tmp]#echo $?
0

返回1~255,則說明命令執行過程中發生了錯誤。
[16:59:57 root@qa36 /tmp]#mkdi directroy_1
-bash: mkdi: command not found
[17:00:15 root@qa36 /tmp]#echo $?
127
  • 命令行展開

~:展開爲用戶的主目錄
~USERNAME:展開爲指定用戶主目錄
{}:可承載一個以逗號分隔的列表,並將其展開爲多個路徑
[17:24:04 root@qa36 /tmp]#touch file_{1,2,3}
[17:24:13 root@qa36 /tmp]#ll
總用量 4
-rw-r--r-- 1 root root    0 8月   9 17:24 file_1
-rw-r--r-- 1 root root    0 8月   9 17:24 file_2
-rw-r--r-- 1 root root    0 8月   9 17:24 file_3

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

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

[16:29:22 root@qa /tmp]#mkdir /tmp/{a,b}_{c,d}
[16:29:40 root@qa /tmp]#ll /tmp/
總用量 20
drwxr-xr-x 2 root root 4096 8月   8 16:29 a_c
drwxr-xr-x 2 root root 4096 8月   8 16:29 a_d
drwxr-xr-x 2 root root 4096 8月   8 16:29 b_c
drwxr-xr-x 2 root root 4096 8月   8 16:29 b_d

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

[16:51:03 root@qa36 /var]#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}
[16:51:26 root@qa36 /var]#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

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

  • 元數據信息

[17:43:32 root@qa36 /tmp]#ls -l file_1
-rw-r--r-- 1 root root 0 8月   9 17:24 file_1
-,它表示文件類型,說明file_1是常規文件(如果是目錄文件,則應顯示d)
rw-r--r--,它們用於表示文件權限
1,表示硬連接數目
root root,表示文件的擁有者和所屬組
0,表示文件大小,單位爲字節(byte)
8月   9 17:24,表示文件的上一次寫入的時間

[17:43:35 root@qa36 /tmp]#stat file_1 
  File: "file_1"
  Size: 0               Blocks: 0          IO Block: 4096   普通空文件
Device: 803h/2051d      Inode: 1703943     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-08-09 17:24:13.741674017 +0800        #訪問時間,簡寫爲atime
Modify: 2016-08-09 17:24:13.741674017 +0800        #修改時間,簡寫爲mtime
Change: 2016-08-09 17:24:13.741674017 +0800        #改變時間,簡寫爲ctime
  • 文件時間戳信息修改

touch [OPTION]... FILE...
-a     change only the access time    #只改變訪問時間
[10:29:27 root@qa36 /tmp]#touch -a  file_1
[10:31:12 root@qa36 /tmp]#stat file_1 
  File: "file_1"
  Size: 0               Blocks: 0          IO Block: 4096   普通空文件
Device: 803h/2051d      Inode: 1703943     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-08-10 10:31:12.533006587 +0800
Modify: 2016-08-10 10:00:00.000000000 +0800
Change: 2016-08-10 10:31:12.533006587 +0800

-m     change only the modification time    #只改變修改時間
[10:31:16 root@qa36 /tmp]#touch -m  file_1
[10:32:24 root@qa36 /tmp]#stat file_1 
  File: "file_1"
  Size: 0               Blocks: 0          IO Block: 4096   普通空文件
Device: 803h/2051d      Inode: 1703943     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-08-10 10:31:12.533006587 +0800
Modify: 2016-08-10 10:32:24.880625564 +0800
Change: 2016-08-10 10:32:24.880625564 +0800

-t STAMP    use [[CC]YY]MMDDhhmm[.ss] instead of current time    #更改爲自定義時間戳    
#[10:29:09 root@qa36 /tmp]#touch -t 201608101000.00 file_1
[10:29:20 root@qa36 /tmp]#stat file_1 
  File: "file_1"
  Size: 0               Blocks: 0          IO Block: 4096   普通空文件
Device: 803h/2051d      Inode: 1703943     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-08-10 10:00:00.000000000 +0800
Modify: 2016-08-10 10:00:00.000000000 +0800
Change: 2016-08-10 10:29:20.056045865 +0800
-c, --no-create    do not create any files    #如果文件不存在則不創建


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

  • 命令別名

通過alias命令實現
alias
  顯示當前shell進程的所有可用的命令別名
alias NAME='VALUE'
  定義別名NAME,其相當於執行命令VLAUE
在命令漢中定義的別名,僅對當前shell進程有效,如果想有就有效,要定義在配置文件中
  僅對當前用戶:~/.bashrc
  對所有用戶有效:/etc/bashrc
  • 在命令中引用另一個命令的執行結果

COMMAND1 | COMMAND2 | COMMAND3
最後一個命令會在當前shell進程的子shell進程中執行

[10:32:26 root@qa36 /tmp]#ll
總用量 4
-rw-r--r-- 1 root root    0 8月  10 10:32 file_1
-rw-r--r-- 1 root root    0 8月   9 17:24 file_2
-rw-r--r-- 1 root root    0 8月   9 17:24 file_3
[10:46:46 root@qa36 /tmp]#ll | grep file_1
-rw-r--r-- 1 root root    0 8月  10 10:32 file_1

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

ll /var/l*[[:digit:]]*[[:lower:]]

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

ll /etc/[[:digit:]]*[^[:digit:]]

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

ll [^[:alpha:]][[:alpha:]]*

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

[17:21:21 root@qa36 /var]#touch /tmp/tfile-`date +%Y-%m-%d-%H-%M-%S`
[17:21:25 root@qa36 /var]#ll /tmp
總用量 8
drwxr-xr-x  2 root root 4096 8月   8 17:09 hsperfdata_root
drwxr-xr-x 17 root root 4096 8月   8 16:51 mylinux
-rw-r--r--  1 root root    0 8月   8 17:21 tfile-2016-08-08-17-21-25

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

[17:34:04 root@qa36 /var]#mkdir /tmp/mytest1/ && cp -r /etc/p*[^[:digit:]] /tmp/mytest1/
[17:34:56 root@qa36 /var]#ll /tmp/mytest1/
總用量 292
drwxr-xr-x 2 root root   4096 8月   8 17:34 pam.d
drwxr-xr-x 3 root root   4096 8月   8 17:34 pango
-rw-r--r-- 1 root root   1287 8月   8 17:34 passwd
-rw-r--r-- 1 root root   1248 8月   8 17:34 passwd-
-rw-r--r-- 1 root root   1362 8月   8 17:34 pbm2ppa.conf
drwxr-xr-x 2 root root   4096 8月   8 17:34 pcmcia
-rw-r--r-- 1 root root   2872 8月   8 17:34 pinforc
drwxr-xr-x 9 root root   4096 8月   8 17:34 pki
drwxr-xr-x 2 root root   4096 8月   8 17:34 plymouth
drwxr-xr-x 5 root root   4096 8月   8 17:34 pm
-rw-r--r-- 1 root root    370 8月   8 17:34 pm-utils-hd-apm-restore.conf
-rw-r--r-- 1 root root   6300 8月   8 17:34 pnm2ppa.conf
drwxr-xr-x 2 root root   4096 8月   8 17:34 popt.d
drwxr-xr-x 2 root root   4096 8月   8 17:34 portreserve
drwxr-xr-x 2 root root   4096 8月   8 17:34 postfix
drwxr-xr-x 3 root root   4096 8月   8 17:34 ppp
-rw-r--r-- 1 root root 193255 8月   8 17:34 prelink.cache
-rw-r--r-- 1 root root    789 8月   8 17:34 prelink.conf
drwxr-xr-x 2 root root   4096 8月   8 17:34 prelink.conf.d
-rw-r--r-- 1 root root    233 8月   8 17:34 printcap
-rw-r--r-- 1 root root   1990 8月   8 17:34 profile
drwxr-xr-x 2 root root   4096 8月   8 17:34 profile.d
-rw-r--r-- 1 root root   6455 8月   8 17:34 protocols
drwxr-xr-x 2 root root   4096 8月   8 17:34 pulse

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

[17:35:03 root@qa36 /var]#mkdir /tmp/mytest2/ && cp -r /etc/*.d /tmp/mytest2/
[17:36:47 root@qa36 /var]#ll /tmp/mytest2/
總用量 108
drwxr-xr-x  2 root root 4096 8月   8 17:36 bash_completion.d
drwxr-xr-x  2 root root 4096 8月   8 17:36 chkconfig.d
drwxr-xr-x  2 root root 4096 8月   8 17:36 cron.d
drwxr-xr-x  2 root root 4096 8月   8 17:36 depmod.d
drwxr-xr-x  2 root root 4096 8月   8 17:36 dracut.conf.d
drwxr-xr-x  2 root root 4096 8月   8 17:36 event.d
lrwxrwxrwx  1 root root   11 8月   8 17:36 init.d -> rc.d/init.d
drwxr-xr-x  3 root root 4096 8月   8 17:36 latrace.d
drwxr-xr-x  2 root root 4096 8月   8 17:36 ld.so.conf.d
drwxr-xr-x  2 root root 4096 8月   8 17:36 logrotate.d
drwxr-xr-x  2 root root 4096 8月   8 17:36 lsb-release.d
drwxr-xr-x  2 root root 4096 8月   8 17:36 makedev.d
drwxr-xr-x  2 root root 4096 8月   8 17:36 modprobe.d
drwxr-xr-x  2 root root 4096 8月   8 17:36 oddjobd.conf.d
drwxr-xr-x  2 root root 4096 8月   8 17:36 pam.d
drwxr-xr-x  2 root root 4096 8月   8 17:36 popt.d
drwxr-xr-x  2 root root 4096 8月   8 17:36 prelink.conf.d
drwxr-xr-x  2 root root 4096 8月   8 17:36 profile.d
lrwxrwxrwx  1 root root   10 8月   8 17:36 rc0.d -> rc.d/rc0.d
lrwxrwxrwx  1 root root   10 8月   8 17:36 rc1.d -> rc.d/rc1.d
lrwxrwxrwx  1 root root   10 8月   8 17:36 rc2.d -> rc.d/rc2.d
lrwxrwxrwx  1 root root   10 8月   8 17:36 rc3.d -> rc.d/rc3.d
lrwxrwxrwx  1 root root   10 8月   8 17:36 rc4.d -> rc.d/rc4.d
lrwxrwxrwx  1 root root   10 8月   8 17:36 rc5.d -> rc.d/rc5.d
lrwxrwxrwx  1 root root   10 8月   8 17:36 rc6.d -> rc.d/rc6.d
drwxr-xr-x 10 root root 4096 8月   8 17:36 rc.d
drwxr-xr-x  2 root root 4096 8月   8 17:36 request-key.d
drwxr-xr-x  2 root root 4096 8月   8 17:36 rsyslog.d
drwxr-xr-x  2 root root 4096 8月   8 17:36 rwtab.d
drwxr-xr-x  3 root root 4096 8月   8 17:36 sane.d
drwxr-xr-x  2 root root 4096 8月   8 17:36 setuptool.d
drwxr-xr-x  2 root root 4096 8月   8 17:36 statetab.d
drwxr-x---  2 root root 4096 8月   8 17:36 sudoers.d
drwxr-xr-x  2 root root 4096 8月   8 17:36 xinetd.d
drwxr-xr-x  2 root root 4096 8月   8 17:36 yum.repos.d

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

[17:38:08 root@qa36 /var]#mkdir /tmp/mytest3/ && cp -r /etc/{l,m,n}*.conf /tmp/mytest3/
[17:38:38 root@qa36 /var]#ll /tmp/mytest3/
總用量 48
-rw-r--r-- 1 root root  1662 8月   8 17:38 latrace.conf
-rw-r--r-- 1 root root    28 8月   8 17:38 ld.so.conf
-rw-r----- 1 root root   191 8月   8 17:38 libaudit.conf
-rw-r--r-- 1 root root  2293 8月   8 17:38 libuser.conf
-rw-r--r-- 1 root root   662 8月   8 17:38 logrotate.conf
-rw-r--r-- 1 root root 10814 8月   8 17:38 ltrace.conf
-rw-r--r-- 1 root root   827 8月   8 17:38 mke2fs.conf
-rw-r--r-- 1 root root  3390 8月   8 17:38 nfsmount.conf
-rw-r--r-- 1 root root  1688 8月   8 17:38 nsswitch.conf
-rw-r--r-- 1 root root  1778 8月   8 17:38 ntp.conf


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