馬哥教育網絡班21期+第2周課程練習

#

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

### A.cp(copy,復制)

基本格式:cp 需要復制的文件路徑 復制目的目錄路徑

~~~

[root@iZ23olnit2eZ ~]# ls

jdk-8u91-linux-x64.tar.gz 

[root@iZ23olnit2eZ ~]# pwd

/root

[root@iZ23olnit2eZ ~]# cp /root/jdk-8u91-linux-x64.tar.gz /tmp

[root@iZ23olnit2eZ ~]# ls /tmp

jdk-8u91-linux-x64.tar.gz  

~~~

### B.mv(move,移動)

基本格式:mv 需要移動的文件路徑 復制移動文件路徑(可改原文件名)

~~~

[root@iZ23olnit2eZ ~]# mv /root/jdk-8u91-linux-x64.tar.gz /tmp/myname

[root@iZ23olnit2eZ ~]# ls /tmp

myname

~~~

### C.rm(remove,刪除)

基本格式:rm 需刪除文件(不需確認則加-f參數,涉及文件夾需要遞歸刪除時加上-rf選項)

~~~

[root@iZ23olnit2eZ ~]# rm /tmp/myname 

rm: remove regular file `/tmp/myname'? y

[root@iZ23olnit2eZ ~]# ls /tmp

~~~

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

### A.查看命令執行狀態

echo $? 查看命令執行返回值,為0表示執行成功,非0表示執行失敗

~~~

[root@iZ23olnit2eZ ~]# ls 

jdk-8u91-linux-x64.tar.gz  

[root@iZ23olnit2eZ ~]# echo $?

0

[root@iZ23olnit2eZ ~]# ls nothing

ls: cannot access nothing: No such file or directory

[root@iZ23olnit2eZ ~]# echo $?

2

~~~

### B.命令行展開

~為當前系統用戶家目錄

~~~

[root@iZ23olnit2eZ ~]# cd /tmp

[root@iZ23olnit2eZ tmp]# cd ~

[root@iZ23olnit2eZ ~]# pwd

/root                                                

~~~

-為上一次使用目錄

~~~

[root@iZ23olnit2eZ ~]# cd –

/tmp                                                

~~~

{}在大括號中加上帶逗號的列表,可展開為相應目錄

~~~

[root@iZ23olnit2eZ tmp]# mkdir {a_{a,b},{c,d}_e}

[root@iZ23olnit2eZ tmp]# ls

a_a  a_b  c_e  d_e                                                 

~~~

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

~~~

   (1)、創建/tmp目錄下的:a_c, a_d, b_c, 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

~~~

### A.創建/tmp目錄下的:a_c, a_d, b_c, b_d

~~~

[root@iZ23olnit2eZ tmp]# ls

[root@iZ23olnit2eZ tmp]# mkdir {a_{c,d},b_{c,d}}

[root@iZ23olnit2eZ tmp]# ls

a_c  a_d  b_c  b_d

~~~

### B.按圖示創建/tmp/mylinux目錄下的目錄

~~~

[root@iZ23olnit2eZ 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: created directory ‘/tmp/mylinux’

mkdir: created directory ‘/tmp/mylinux/bin’

mkdir: created directory ‘/tmp/mylinux/boot’

mkdir: created directory ‘/tmp/mylinux/boot/grub’

mkdir: created directory ‘/tmp/mylinux/dev’

mkdir: created directory ‘/tmp/mylinux/etc’

mkdir: created directory ‘/tmp/mylinux/etc/rc.d’

mkdir: created directory ‘/tmp/mylinux/etc/rc.d/init.d’

mkdir: created directory ‘/tmp/mylinux/etc/sysconfig’

mkdir: created directory ‘/tmp/mylinux/etc/sysconfig/network-scripts’

mkdir: created directory ‘/tmp/mylinux/lib’

mkdir: created directory ‘/tmp/mylinux/lib/modules’

mkdir: created directory ‘/tmp/mylinux/lib64’

mkdir: created directory ‘/tmp/mylinux/proc’

mkdir: created directory ‘/tmp/mylinux/sbin’

mkdir: created directory ‘/tmp/mylinux/sys’

mkdir: created directory ‘/tmp/mylinux/tmp’

mkdir: created directory ‘/tmp/mylinux/usr’

mkdir: created directory ‘/tmp/mylinux/usr/local’

mkdir: created directory ‘/tmp/mylinux/usr/local/bin’

mkdir: created directory ‘/tmp/mylinux/usr/local/sbin’

mkdir: created directory ‘/tmp/mylinux/var’

mkdir: created directory ‘/tmp/mylinux/var/lock’

mkdir: created directory ‘/tmp/mylinux/var/log’

mkdir: created directory ‘/tmp/mylinux/var/run’

 

[root@iZ23olnit2eZ tmp]# 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

~~~

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

### A.查看文件元數據命令 stat (大致信息可通過ls -l(或者ll)查詢)

~~~

[root@iZ23olnit2eZ tmp]# stat a_c 

  File: `a_c'                                                               #文件名

  Size: 4096 #文件大小  Blocks: 8  #占用塊個數  IO Block: 4096  #占用塊大小  directory    #文件類型

Device: ca01h/51713d  #存儲設備 Inode: 538986 #索引節點     Links: 2 #鏈接數

Access: (0755/drwxr-xr-x)  #權限  Uid: (    0/    root) #屬主   Gid: (    0/    root) #屬組

Access: 2016-07-17 18:26:43.827784089 +0800   #訪問時間,讀取文件時間,atime

Modify: 2016-07-17 18:26:43.827784089 +0800   #修改時間,內容(數據)改變,mtime

Change: 2016-07-17 18:26:43.827784089 +0800   #改變時間,元數據改變,ctime

~~~

### B.修改文件時間戳 touch

~~~

touch -a #修改atime

touch -m #修改mtime

修改atime或者mtime都會導致ctime的改變

~~~

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

### A.定義別名 alias 

~~~

[root@iZ23olnit2eZ tmp]# alias cls='clear'

[root@iZ23olnit2eZ tmp]# type cls

cls is aliased to `clear'

~~~

### B.引用另一命令執行結果 |(管道)

~~~

[root@iZ23olnit2eZ tmp]# b=2|echo $b-1

-1

~~~

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

~~~

find /var -name "1*[0-9]?*[[:lower:]]"

~~~

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

~~~

find /var -name "[0-9]?*[^0-9]"

~~~

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

~~~

find /var -name "[^[:alpha:]][[:alpha:]]*"

~~~

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

~~~

[root@iZ23olnit2eZ ~]# touch /tmp/tfile-`date +%Y-%m-%d-%H-%M-%S`

[root@iZ23olnit2eZ ~]# ls /tmp

tfile-2016-07-17-19-16-24

~~~

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

~~~

[root@iZ23olnit2eZ ~]# mkdir /tmp/mytest{1,2,3}

[root@iZ23olnit2eZ ~]# ls /tmp/mytest{1,2,3}

/tmp/mytest1:

/tmp/mytest2:

/tmp/mytest3:

[root@iZ23olnit2eZ ~]# cp -r `find /etc -name "p*[^0-9]"` /tmp/mytest1/

cp: cannot overwrite non-directory `/tmp/mytest1/profile' with directory `/etc/lvm/profile'

cp: overwrite `/tmp/mytest1/passwd'? y

cp: will not overwrite just-created `/tmp/mytest1/passwd' with `/etc/pam.d/passwd'

cp: overwrite `/tmp/mytest1/poweroff'? y

cp: will not overwrite just-created `/tmp/mytest1/poweroff' with `/etc/pam.d/poweroff'

cp: overwrite `/tmp/mytest1/prelink'? y

cp: will not overwrite just-created `/tmp/mytest1/prelink' with `/etc/cron.daily/prelink'

cp: overwrite `/tmp/mytest1/psacct'? y

cp: will not overwrite just-created `/tmp/mytest1/psacct' with `/etc/rc.d/init.d/psacct'

cp: cannot overwrite non-directory `/tmp/mytest1/postfix' with directory `/etc/postfix'

[root@iZ23olnit2eZ ~]# ls /tmp/mytest{1,2,3}

/tmp/mytest1:

pads.pp           pem                           policy.kern      preprobe

pam.d             permissivedomains.pp          policykit.pp     printcap

pam_env.conf      phar.ini                      popt.d           print_event.conf

pango             php.conf                      portmap.pp       private

pango.modules     php.d                         portreserve.pp   privoxy.pp

pangox.aliases    php.ini                       postfix          procmail.pp

passenger.pp      php.ini.zabbixbak             postfix.pp       profile

passwd            pinforc                       postgresql.pp    profile.d

passwd-           pingd.pp                      postgrey.pp      profiles

password          piranha.pp                    post_report.xml  protected.d

password-auth     pkcs11.txt                    power.conf       protocols

password-auth-ac  pkcsslotd.pp                  power.d          psacct

path              pki                           poweroff         psad.pp

pcmcia            pluginconf.d                  power.sh         ptchown.pp

pcp.pp            plugins                       ppp              publicfile.pp

pcscd.pp          plugins.d                     ppp.pp           pulseaudio.pp

pdo.ini           plymouth                      prefdm           puppet.pp

pdo_mysql.ini     plymouthd.conf                prefdm.conf      pw

pdo_odbc.ini      plymouthd.pp                  prefixes         python.conf

pdo_sqlite.ini    plymouth-shutdown.conf        prelink          python_event.conf

pear              pm                            prelink.cache    pyzor.pp

pear.conf         pm-utils-hd-apm-restore.conf  prelink.conf

peers             podsleuth.pp                  prelink.conf.d

pegasus.pp        policy                        prelude.pp

/tmp/mytest2:

/tmp/mytest3:

~~~

##11、復制/etc目錄下所有以.d結尾的文件或目錄至/tmp/mytest2目錄中

~~~

[root@iZ23olnit2eZ ~]# yes|cp -r /etc/*\.d /tmp/mytest2

[root@iZ23olnit2eZ ~]# ls /tmp/mytest2

bash_completion.d  init.d         monit.d         rc0.d  rc6.d        sudoers.d

chkconfig.d        ld.so.conf.d   pam.d           rc1.d  rc.d         xinetd.d

cron.d             logrotate.d    php.d           rc2.d  rsyslog.d    yum.repos.d

depmod.d           lsb-release.d  popt.d          rc3.d  rwtab.d

dracut.conf.d      makedev.d      prelink.conf.d  rc4.d  setuptool.d

event.d            modprobe.d     profile.d       rc5.d  statetab.d

~~~

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

~~~

[root@iZ23olnit2eZ ~]# yes|cp -r /etc/[l,m,n]*\.conf /tmp/mytest3

[root@iZ23olnit2eZ ~]# ls /tmp/mytest3

ld.so.conf     libuser.conf    mke2fs.conf  nscd.conf      ntp.conf

libaudit.conf  logrotate.conf  monit.conf   nsswitch.conf

~~~

原創文章,作者:N21_郁藍,如若轉載,請注明出處:http://www.www58058.com/19964

(0)
N21_郁藍N21_郁藍
上一篇 2016-07-17
下一篇 2016-07-17

相關推薦

  • Centos6.5上搭建openvpn

    一、openvpn原理 二、安裝openvpn 三、制作相關證書     3.1 制作CA證書     3.2 制作Server端證書     3.3 制作Client端證書 四、配置Server端 五、配置C…

    Linux干貨 2016-04-28
  • 到處都是Unix的胎記

    一說起Unix編程,不必多說,最著名的系統調用就是fork,pipe,exec,kill或是socket了(fork(2),execve(2), pipe(2), socketpair(2), select(2), kill(2), sigaction(2))這些系統調用都像是Unix編程的胎記或簽名一樣,表…

    Linux干貨 2015-04-03
  • 馬哥教育網絡班20期第1周課程練習

    答: 1、 ①控制器:是計算機的中樞神經,協調計算機各部分工作及內存與外設的訪問等 ②運算器:運算器的功能是對數據進行各種算術運算和邏輯運算,即對數據進行加工處理。 ③儲存器:存儲器的功能是存儲程序、數據和各種信號、命令等信息,并在需要時提供這些信息。 ④IO:輸入設備是將數據或控制命令等信息輸入到計算機。輸出設備把機算機的各種數據符號及文字或各種控制信號等…

    Linux干貨 2016-06-23
  • 建立私有CA的方法

    建立私有CA的方法 建立私有CA的工具:     OpenCA     Openssl 證書申請及簽署步驟:     1,生成申請請求:     2,RA核驗;    &…

    Linux干貨 2016-09-19
  • N27_第五周作業

    一、顯示當前系統上root、fedora或者user1用戶的默認shell; [root@localhost ~]# grep -E “^(root|fedora|user1)” /etc/passwd | cut -d: -f7 /bin/bash /bin/tcsh /bin/bash二、找出/etc/rc.d/init.d/fu…

    Linux干貨 2017-10-20
  • 北京朝陽四惠東招運維一名

    感興趣的可以發送簡歷到undinelee@163.com

    2017-03-29

評論列表(1條)

  • 馬哥教育
    馬哥教育 2016-07-17 22:03

    寫的很好,排版還可以在改進一下,第9個有沒有更簡單的命令,加油

欧美性久久久久