linux中檔案萬用字元,檔案建立、複製等操作

來源:互聯網
上載者:User

1:bash特性之狀態傳回值

變數$?,用於儲存命令執行成功與否的狀態,0表示成功,1-255表示失敗,以命令ls為例:

執行成功

[root@localhost ~]# ls /usr/   
bin  etc  games  include  lib  lib64  libexec  local  sbin  share  src  tmp
[root@localhost ~]# echo $?
0
命令輸入錯誤

[root@localhost ~]# lss /usr
-bash: lss: command not found
[root@localhost ~]# echo $?
127
命令參數輸入錯誤


[root@localhost ~]# ls /usrrr
ls: cannot access /usrrr: No such file or directory
[root@localhost ~]# echo $?
2
其他錯誤狀態,$?也會是非0結果。

2:命令列展開

2.1:使用命令列展開,建立/tmp下的a\_c,a\_d,b\_c,b\_d檔案。


[root@localhost ~]# 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'
[root@localhost ~]# ls -d {a,b}_{c,d}
a_c  a_d  b_c  b_d
2.2:在/tmp下建立如下檔案。

這裡寫圖片描述


[root@localhost ~]# mkdir -pv /tmp/mylinux/{bin,boot/grub,etc{/rc.d/init.d,/sysconfig/network-scripts},dev,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
|-- proc
|-- sbin
|-- sys
|-- tmp
|-- usr
|   `-- local
|       |-- bin
|       `-- sbin
`-- var
    |-- lock
    |-- log
    `-- run24 directories, 0 files

3:如何定義一個命令的別名?如何在一個命令中引用另一個命令的結果?
使用反引“或$()可引用其他命令的執行結果:

使用反引號

[root@localhost ~]# dirname `which touch`
/bin
 
# 顯示命令touch所在的路徑名
使用$()

[root@localhost ~]# dirname $(which touch)
/bin

4:顯示/var目錄下所有以字母“l”開頭,以一個小寫字母結尾,且中間至少出現一位元字(可以有其他字元)的檔案和目錄


[root@localhost ~]# ls -d /var/l*[[:digit:]]*[[:lower:]]
/var/l13u  /var/lfs3k
5:顯示/etc目錄下以任意一個數字開頭且以一個非數字結尾的檔案和目錄
1
2
[root@localhost ~]# ls -d /etc/[[:digit:]]*[^[:digit:]]
/etc/222kkk  /etc/333uuu

6:顯示/etc目錄下以非字母開頭且後面跟了一個字母和其他任意長度任一字元的檔案和目錄


[root@localhost ~]# ls -d /etc/[^[:alpha:]][[:alpha:]]*
/etc/1djj

7:在/tmp下建立以tfile開頭,後跟目前時間的檔案


[root@localhost ~]# touch /tmp/tfile-`date +%F-%H-%M-%S`
[root@localhost ~]# touch /tmp/tfile-`date +%F-%H-%M-%S`
[root@localhost ~]# ls /tmp
tfile-2016-08-25-08-14-35  tfile-2016-08-25-08-14-36

8:複製/etc目錄下以字母“p”開頭,以非數字結尾的檔案到/tmp/mytest1目錄中


[root@localhost ~]# mkdir /tmp/mytest1
[root@localhost ~]# cp -r /etc/p*[^[:digit:]] /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

9:複製/etc目錄下所有以.d結尾的目錄和檔案至/tmp/mytest2目錄中


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

10:複製/etc目錄下所有以字母l或n或m開頭,以.conf結尾的檔案至目錄/tmp/mytest3中


[root@localhost ~]# mkdir /tmp/mytest3
[root@localhost ~]# cp -r /etc/[lmn]*.conf /tmp/mytest3
[root@localhost ~]# ls /tmp/mytest3
ld.so.conf  libaudit.conf  libuser.conf  logrotate.conf  mke2fs.conf  nsswitch.conf

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.