學以致用

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

cp – copy files and directories

復制文件或目錄

使用格式:

單文件復制

cp [OPTION]… [-T] SOURCE DEST

如果DEST不存在,即創建文件并復制源文件數據流;

如果DEST存在,且為非目錄文件時,將覆蓋目標文件;如果為目錄文件時,將在此目錄下創建一個同名的新文件。

多文件復制

cp [OPTION]… SOURCE… DIRECTORY

cp [OPTION]… -t DIRECTORY SOURCE…

如果DIRE不存在,將報錯,提示:cannot create regular file ‘root/’: Not a directory;

如果DIRE存在,復制到目標目錄內,保持原名。

常用選項:

      -a,–archive :same as -dr –preserve=all,常用于文檔歸檔、備份

      -d:復制鏈接文件本身,不會指向鏈接文件源文件

      -f,–force:如果目標文件存在,強制覆蓋目標文件

      -i,–interactive:覆蓋前提示

      -l,–link:硬鏈接文件代替復制

      -r,-R,–recursive:遞歸,復制目錄及其子文件和目錄

      –preserve[=ATTR_LIST]:保留被復制文件的:mode權限,ownership從屬關系,timestamps時間戳,context安全上下文,links鏈接關系,all所有

      –parents: use full source file name under DIRECTORY

e.g:

[root@yyy tmp Wed Oct 12 22:17]# cp /root/Zhahaha /tmp/

[root@yyy tmp Wed Oct 12 22:31]# ll /tmp/Zhahaha

-rwxr-xr–. 1 root root 0 Oct 12 22:31 /tmp/Zhahaha

 

mv – move (rename) files 移動(重命名)文件

使用格式:

      mv [OPTION]… [-T] SOURCE DEST

mv [OPTION]… SOURCE… DIRECTORY

mv [OPTION]… -t DIRECTORY SOURCE…

常用選項:

      -f,–force:覆蓋前不提示

      -i,–interactive:覆蓋前提示

e.g:

重命名:

[root@yyy tmp Wed Oct 12 22:31]# ll /tmp/Zhahaha

-rwxr-xr–. 1 root root 0 Oct 12 22:31 /tmp/Zhahaha

[root@yyy tmp Wed Oct 12 22:31]# mv Zhahaha Yhahaha

[root@yyy tmp Wed Oct 12 22:32]# ll /tmp/Yhahaha

-rwxr-xr–. 1 root root 0 Oct 12 22:31 /tmp/Yhahaha

移動文件:

[root@yyy tmp Wed Oct 12 22:32]# ll /root/a.out

-rwxr-xr-x. 1 root root 8511 Sep  6 01:00 /root/a.out

[root@yyy tmp Wed Oct 12 22:33]# mv /root/a.out /tmp/

[root@yyy tmp Wed Oct 12 22:33]# ll /root/a.out

ls: cannot access /root/a.out: No such file or directory

[root@yyy tmp Wed Oct 12 22:33]# ll /tmp/a.out

-rwxr-xr-x. 1 root root 8511 Sep  6 01:00 /tmp/a.out

 

rm – remove files or directories 刪除文件或目錄

使用格式:

rm [OPTION]… FILE…

常用選項:

      -i,–interactive:刪除前提示prompt

      -f,–force:強制刪除不提示

      -r,-R,-recursive:遞歸刪除,用于刪除目錄(rmdir只能刪除空目錄,非空不能刪除)

e.g:

      [root@yyy tmp Wed Oct 12 22:34]# rm a.out

rm: remove regular file ‘a.out’? y

[root@yyy tmp Wed Oct 12 22:38]# ll /tmp/a.out

ls: cannot access /tmp/a.out: No such file or directory


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

執行狀態返回值:

每一條命令在執行完成后,不論成功失敗,都會有0-255中的1個數值來表示執行結果,這個值將保存在shell的內置變量"?"中.但是,只能保存最近一次執行命令的返回值.

其返回值主要分兩類:

成功,則返回0

失敗,則返回非0(1-255)

獲取執行狀態返回值:

#echo $?

命令行展開用法

~ 自動展開為用戶家目錄

e.g:

[root@yyy tmp Wed Oct 12 23:03]# cd ~

[root@yyy ~ Wed Oct 12 23:05]#

{} 可承載以逗號分割的路徑列表,并能夠展開為多個路徑

e.g:

創建/tmp/y1,/tmp/y2,/tmp/y1/a,/tmp/y1/b目錄

[root@yyy tmp Wed Oct 12 23:08]# mkdir -pv /tmp/{y1/{a,b},y2}

mkdir: created directory ‘/tmp/y1’

mkdir: created directory ‘/tmp/y1/a’

mkdir: created directory ‘/tmp/y1/b’

mkdir: created directory ‘/tmp/y2’
3、請使用命令行展開功能來完成以下練習:
   (1)、創建/tmp目錄下的:a_c, a_d, b_c, b_d

[root@yyy tmp Wed Oct 12 23:08]# mkdir -v {a,b}_{c,d}

mkdir: created directory ‘a_c’

mkdir: created directory ‘a_d’

mkdir: created directory ‘b_c’

mkdir: created directory ‘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

[root@yyy tmp Wed Oct 12 23:15]# mkdir -pv /tmp/mylinux/{bin,boot/grup,dev,etc/{ec.d/init.d,sysconfig/network-script},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/grup’

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

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

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

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

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

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

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@yyy tmp Wed Oct 12 23:16]# tree mylinux/

mylinux/

├── bin

├── boot

│   └── grup

├── dev

├── etc

│   ├── ec.d

│   │   └── init.d

│   └── sysconfig

│       └── network-script

├── lib

│   └── modules

├── lib64

├── proc

├── sbin

├── sys

├── tmp

├── usr

│   └── local

│       ├── bin

│       └── sbin

└── var

    ├── lock

    ├── log

    └── run

 

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

文件的兩類數據:

數據:data(文件正文內容)

元數據:metadata,主要有:

文件屬性,

文件大?。簊ize

塊:所占blocks數

文件類型:常規文件、執行程序、目錄等等

inode:存放在分區上的那個節點

links:鏈接到該文件的硬鏈接數目

權限:用戶、組、其他用戶對此文件的操作權限

uid:文件所屬者

屬組:文件所屬組

安全上下文:有點類似文件系統的rwx,主體(subject,進程)與目標(object,文件系統)的安全上下文必須保持一致。

時間戳:atime 最近一次訪問時間 mtime 最近一次修改時間 ctime最近一次改變時間(指文件屬性改變)

 

查看命令介紹:stat

stat – display file or file system status 顯示文件或文件系統的狀態

[root@yyy tmp Wed Oct 12 23:21]# stat yyy

  File: ‘yyy’

  Size: 88         Blocks: 0          IO Block: 4096   directory

Device: fd00h/64768d Inode: 37203114    Links: 7

Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)

Context: unconfined_u:object_r:user_tmp_t:s0

Access: 2016-10-12 22:10:30.146038308 +0800

Modify: 2016-10-12 22:10:22.292075536 +0800

Change: 2016-10-12 22:10:22.292075536 +0800

 Birth: –

 

修改文件時間戳方法介紹:

使用touch命令可對文件時間戳進行修改:

touch – change file timestamps 改變文件的時間戳

使用格式:

touch [OPTION]… FILE…

常用選項:

-c,–no-create 不創建任何文件

-m 僅改變文件的修改時間

-a 僅改變文件的訪問時間

-t STAMP 可使用[[CC]YY]MMDDhhmm[.ss]代替通用時間,e.g:

      -t 201508080808.08


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

命令別名主要命令:alias

Define or display aliases.定義或者取消別名

使用格式:

alias [-p] [name[=value] … ]

使用方法:

查看系統已有別名設置:

[root@yyy ~ Wed Oct 12 23:43]# alias

alias cp='cp -i'

alias egrep='egrep –color=auto'

alias fgrep='fgrep –color=auto'

定義命令別名:

alias name="value"

[root@yyy ~ Wed Oct 12 23:45]# alias lh="ls -l -h"

[root@yyy ~ Wed Oct 12 23:46]# alias

alias cp='cp -i'

alias lh='ls -l -h'

[root@yyy ~ Wed Oct 12 23:46]# lh

-rw-r–r–. 1 root    root  7.6M Sep  1 23:10 abc.zip

-rw——-. 1 root    root  1.2K May 29 22:54 anaconda-ks.cfg

-rw-r–r–. 1 root    root  137K Oct 12 23:46 crontabtest

取消別名定義:

unalias name

[root@yyy ~ Wed Oct 12 23:47]# unalias lh

[root@yyy ~ Wed Oct 12 23:49]# lh

bash: lh: command not found…

 

注意:使用命令定義命令別名僅在當前shell進程中有效,退出shell進程后失去效果,如需永久生效,可根據情況在/etc/profile,/etc/profile.d/以及~/.bash_profile里進行添加即可。


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

(一)

[root@yyy tmp Thu Oct 13 00:13]# ll -d /var/l*[0-9]*[a-z]

drwxr-xr-x. 2 root root 6 Oct 13 00:13 /var/lee34ff

-rw-r–r–. 1 root root 0 Oct 13 00:13 /var/ljsj7hhh

(二)

 [root@yyy tmp Wed Oct 12 23:55]# ls -R /var | grep -E '^l.*[[:digit:]].*[[:lower:]]$'

lee34ff

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

(一)

[root@yyy tmp Thu Oct 13 00:22]# ll -d /etc/[0-9]*[^0-9]

drwxr-xr-x. 2 root root 6 Oct 13 00:15 /etc/9aaaaaaaaaaaaa

(二)

 [root@yyy tmp Thu Oct 13 00:03]# ls -R /etc/ | grep -E "^[[:digit:]].*[^[:digit:]]$"

9aaaaaaaaaaaaaaaaaaaaaa

0hourly

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

(一)

[root@yyy tmp Thu Oct 13 00:22]# ll -d /etc/[^a-zA-Z][a-zA-Z]*

drwxr-xr-x. 2 root root 6 Oct 13 00:15 /etc/9aaaaaaaaaaaaa

(二)

[root@yyy tmp Wed Oct 12 23:58]# ls -R /etc | grep -E "^[[:digit:]][[:alpha:]].*"

0hourly

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

[root@yyy tmp Thu Oct 13 00:04]# touch /tmp/tfile-`date +%Y-%m-%d-%H-%M-%S`
10
、復制/etc目錄下所有以p開頭,以非數字結尾的文件或目錄到/tmp/mytest1目錄中。

[root@yyy tmp Thu Oct 13 00:25]# cp -r /etc/p*[^0-9] /tmp/mytest1/

[root@yyy tmp Thu Oct 13 00:27]# ls /tmp/mytest1/

pam.d   passwd-       php.d    pinforc  plymouth  pnm2ppa.conf  postfix  prelink.conf.d  profile    protocols  purple

passwd  pbm2ppa.conf  php.ini  pki      pm        popt.d        ppp      printcap        profile.d  pulse

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

[root@yyy tmp Thu Oct 13 00:26]# cp -r /etc/*.d /tmp/mytest2/

[root@yyy tmp Thu Oct 13 00:26]# ls /tmp/mytest2

bash_completion.d  dnsmasq.d      init.d        modprobe.d      php.d           rc1.d  rc6.d          sane.d       tmpfiles.d

binfmt.d           dracut.conf.d  ipsec.d       modules-load.d  popt.d          rc2.d  rc.d           setuptool.d  usb_modeswitch.d

chkconfig.d        exports.d      ld.so.conf.d  my.cnf.d        prelink.conf.d  rc3.d  request-key.d  statetab.d   xinetd.d

cron.d             gdbinit.d      libibverbs.d  oddjobd.conf.d  profile.d       rc4.d  rsyslog.d      sudoers.d    yum.repos.d

depmod.d           grub.d         logrotate.d   pam.d           rc0.d           rc5.d  rwtab.d        sysctl.d
12、復制/etc/目錄下所有以l或m或n開頭,以.conf結尾的文件至/tmp/mytest3目錄中。

[root@yyy tmp Thu Oct 13 00:28]# cp /etc/{l,m,n}*.conf /tmp/mytest3/

[root@yyy tmp Thu Oct 13 00:29]# ls /tmp/mytest3/

ld.so.conf     libuser.conf  logrotate.conf  mke2fs.conf  nfsmount.conf  numad.conf

libaudit.conf  locale.conf   man_db.conf     mtools.conf  nsswitch.conf

 

原創文章,作者:成都-yyy,如若轉載,請注明出處:http://www.www58058.com/50956

(0)
成都-yyy成都-yyy
上一篇 2016-10-12
下一篇 2016-10-13

相關推薦

  • Shell腳本中循環淺析

    在shell腳本中,循環是很重要的一環。循環可以不斷的執行某個程序段落,直到用戶設置的條件達成為止。在shell中,除了這種依據判斷時達成與否的不定循環之外,還有另外一種已經固定要跑多少次的循環,可稱之為固定循環。下面,我們主要對for,while,until三種循環做一下介紹。   一、for循環 For循環是給定變量列表的固定次數循環,其執行機…

    Linux干貨 2016-08-21
  • linux基礎命令: tr

    基礎命令:  tr   tr命令 功能:刪除和轉換字符 語法:tr  [OPTION]…. SET1  [SET2] 參數: -d : 刪除所有屬于第一字符集的字符(刪除某些特殊字符) -s : 把連續重復的字符以單獨一個字符表示(刪除空行很有用) 舉例: 1.把小寫字母轉換為大寫字母; cat &nbsp…

    Linux干貨 2016-08-03
  • 03葵花寶典之linux用戶

    介紹了linux上有關用戶和組的相關命令及配置文件

    2018-03-16
  • linux下的打包與壓縮

    linux壓縮或解壓縮工具有很多,除了已經很少有人使用的compress外,現在常用的還有tar,bzip2,xz 和gzip等,我們來說說它們的用法。 先來說bzip2。bunzip2和bzcat可以由bzip2指定選項來執行同樣的結果,這里只介紹bzip2的用法。使用bzip2這個工具創建的文件以.bz2,.bz,.tbz,.tar.bz2或者…

    Linux干貨 2017-04-16
  • 編譯安裝bind9

    一、下載bind9.9.5源碼包     可以通過www.isc.org站點來獲得源碼包。 二、將源碼包解壓到任意目錄     源碼包通常都是.tar.gz文件,因此我們需要先將其解壓: 三、編譯安裝bind     1、安裝前的準備工作 …

    Linux干貨 2015-05-05
  • mysql復制與備份

    備份策略: 完全+差異+binlog 完全+增量+binlog binlog最好能實時備份到另一個節點上。 完全備份,多久一次? 數據變化量:有20%,建議使用完全備份。 可用的備份存儲空間: 數據變化量很大,可以每天做一個完全備份,每周做一次增量備份。 數據變化量不大,可以每月做一次完全備份,每天做一次增量或者差異。 數據 備份工具: mysqldump:…

    Linux干貨 2016-12-05
欧美性久久久久