馬哥教育網絡班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.www58058.com/19050

(0)
dcstrikedcstrike
上一篇 2016-06-23
下一篇 2016-06-23

相關推薦

  • gawk基礎及進階

    GUN awk: 文本處理三工具:grep,sed,awd grep,egrep,fgrep:文本過濾工具:pattern sed:行編輯器 模式空間、保持空間 awk:報告生成器,格式化文本輸出; AWK:Aho,Weinberger,Kernighan –> New AWK,NAWK GNU awk,gawk gawk – …

    Linux干貨 2017-05-22
  • 二進制、八進制、十進制、十六進制之間的轉換

    二進制、八進制、十進制、十六進制之間的關系:   二進制:0,1 八進制:0,1,2,3,4,5,6,7, 十進制:0,1,2,3,4,5,6,7,8,9 十六進制:0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F   二進制與十進制之間的轉換: 十進制轉二進制:   二進制轉十進制:   二進制與八進制…

    2017-04-01
  • Linux第四周學習博客作業

    對第四周學習的內容進行總結

    Linux干貨 2017-12-23
  • Linux 第四天: (07月28日) 練習和作業

    Linux 第四天: (07月28日) 練習和作業         定義別名命令baketc, 每天將/etc/目錄下所有文件, 備份到/testdir獨立的子目錄下, 并要求子目錄格式為backupYYYY-mm-dd, 備份過程可見 alias baketc='cp -a /etc/ /testdir/b…

    Linux干貨 2016-08-08
  • 虛擬化技術介紹、Xen的簡單實現

    虛擬化是什么? 虛擬化是一種資源管理技術, 是將計算機的各實體資源, 如服務、網絡、內存及存儲等, 予以抽象、轉換后呈現出來, 打破實體之間的不可切割的障礙, 使用戶可以比原本的配置更好的方式來應用這些資源。這些資源的新虛擬部分是不受現有資源的架設方式, 地域或物理配置所限制。一般情況下, 虛擬化資源包括計算能力和數據存儲 —<轉自維基百科&…

    2016-05-31
  • 鏈接分析算法之:主題敏感PageRank

      前面的討論提到。PageRank忽略了主題相關性,導致結果的相關性和主題性降低,對于不同的用戶,甚至有很大的差別。例如,當搜索“蘋果”時,一個數碼愛好者可能是想要看 iphone 的信息,一個果農可能是想看蘋果的價格走勢和種植技巧,而一個小朋友可能在找蘋果的簡筆畫。理想情況下,應該為每個用戶維護一套專用向量,但面對海量用戶這種方法顯然不可行。所…

    Linux干貨 2016-02-17

評論列表(1條)

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

    寫的非常棒,有案例做為輔助,但是最后幾個好像是有點小瑕疵,在仔細測試一下

欧美性久久久久