第二周 文件管理

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

mkdir—— 創建目錄 ?make directory

命令格式——mkdir [OPTION]… DIRECTORY…

-p:自動按需創建父目錄;

-v:verbose,顯示詳細過程;

-m MODE:創建目錄直接給定權限,否則是默認權限;

注意:路徑基名方為命令的作用對象;基名之前的路徑必須得存在

示例:

1.創建名為letter 的目錄,在它的目錄下包含有子目錄 important

[root@localhost ~]# mkdir -p letter/important

[root@localhost ~]# ls -R

.:

letter

 

./letter:

important

 

./letter/important:

2.打印創建ubuntu redhat slackware目錄的過程

[root@localhost ~]# mkdir -v ubuntu redhat slackware

mkdir: created directory ‘ubuntu’

mkdir: created directory ‘redhat’

mkdir: created directory ‘slackware’

3.創建le目錄,并賦予le目錄為只讀權限

[root@localhost ~]# mkdir -m=r– le

[root@localhost ~]# ls -l

dr–r–r– 2 root root 4096 Dec 10 05:53 le

 

rmdir—— 刪除目錄 remove empty directories 移除空目錄

命令格式——rmdir [OPTION]… DIRECTORY…

-p:刪除某目錄后,如果其父目錄為空,則一并刪除之;

-v:顯示過程;

 

touch—— 創建文件 創建空白文本文件與設置文件的各種時間

命令格式——touch [選項] [文件]

-a:僅修改“訪問時間”(atime)

-m:僅修改“更改時間”(mtime)

-d:同時修改atime與mtime

示例

[root@localhost tmp]# touch -d “2017-12-09 10:22” issue.out

[root@localhost tmp]# ls -l issue.out

-rw-r–r– 1 root root 23 Dec 9 10:22 issue.out

 

cp—— 文件復制 copy 復制文件是復制文件的數據

源文件source;目標文件dest;

單源復制:cp [OPTION]… [-T] SOURCE DEST

多源復制:cp [OPTION]… SOURCE… DIRECTORY

cp [OPTION]… -t DIRECTORY SOURCE…

 

單源復制:cp [OPTION]… [-T] SOURCE DEST

如果DEST不存在:則事先創建此文件,并復制源文件的數據流至DEST中;

如果DEST存在:

如果DEST是非目錄文件:則覆蓋目標文件;

如果DEST是目錄文件:則先在DEST目錄下創建一個與源文件同名的文件,并復制其數據流;

 

多源復制:cp [OPTION]… SOURCE… DIRECTORY

cp [OPTION]… -t DIRECTORY SOURCE…

 

如果DEST不存在:錯誤;

如果DEST存在:

如果DEST是非目錄文件:錯誤;

如果DEST是目錄文件:分別復制每個文件至目標目錄中,并保持原名;

-i:交互式復制,即覆蓋之前提醒用戶確認;

-f:強制覆蓋目標文件;

-r, -R:遞歸復制目錄;

-d:復制符號鏈接文件本身,而非其指向的源文件;

-a:-dR –preserve=all, archive,用于實現歸檔;

–preserv=

mode:權限

ownership:屬主和屬組

timestamps:時間戳

context:安全標簽

xattr:擴展屬性

links:符號鏈接

all:上述所有屬性

示例

[root@localhost ~]# cp a /tmp

[root@localhost ~]# ls -l /tmp

total 92

-rw-r–r– 1 root root???? 0 Dec 10 06:27 a

 

[root@localhost ~]# cp a b c /tmp

[root@localhost ~]# ls -l /tmp

total 92

-rw-r–r– 1 root root???? 0 Dec 10 06:30 a

-rw-r–r– 1 root root???? 0 Dec 10 06:30 b

-rw-r–r– 1 root root???? 0 Dec 10 06:30 c

 

mv—— 移動并重命名文件 ???move

命令格式

mv [OPTION]… [-T] SOURCE DEST

mv [OPTION]… SOURCE… DIRECTORY

mv [OPTION]… -t DIRECTORY SOURCE..

-i:交互式

-f:force 強制覆蓋

實例

將文件ex3改名為new1

mv ex3 new1

將目錄/usr/men中的所有文件移到當前目錄(用.表示)中:

mv /usr/men/* .

 

rm—— 刪除文件  remove

命令格式——rm [OPTION]… FILE…

-i:interactive 交互式

-f:force 強制

-r:recursive 遞歸

刪除目錄:rm -rf /PATH/TO/DIR

危險操作:rm -rf /*

注意:所有不用的文件建議不要直接刪除,而是移動至某個專用目錄;(模擬回收站)

 

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

命令執行的狀態結果:

bash通過狀態返回值來輸出此結果:

成功:0

失?。?-255

 

命令執行完成之后,其狀態返回值保存于bash的特殊變量$?中;

 

命令正常執行時,有的還回有命令返回值:

根據命令及其功能不同,結果各不相同;

 

[root@localhost ~]# ls /etc/sysconfig

anaconda?? ebtables-config?? irqbalance ? network???????? selinux

authconfig firewalld???????? kdump?????? network-scripts sshd

cbq???????? grub???????????? kernel???? rdisc?????????? wpa_supplicant

console???? init???????????? man-db???? readonly-root

cpupower?? ip6tables-config modules?? ? rsyslog

crond?????? iptables-config?? netconsole ? run-parts

[root@localhost ~]# echo $?

0

[root@localhost ~]# ls /etc/sysconfigg

ls: cannot access /etc/sysconfigg: No such file or directory

[root@localhost ~]# echo $?

2

[root@localhost ~]# lss /etc/sysconfig

-bash: lss: command not found

[root@localhost ~]# echo $?

127

 

3.請命令行展開功能來完成以下練習:

(1)創建/tmp下 a_c a_d b_c b_d

[root@localhost ~]# mkdir -v /tmp{a,b}_{c,d}

mkdir: created directory ‘/tmpa_c’

mkdir: created directory ‘/tmpa_d’

mkdir: created directory ‘/tmpb_c’

mkdir: created directory ‘/tmpb_d’

 

(2)創建/tmp/mylinux目錄下的:

untitled

[root@localhost ~]# 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’

 

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

文件:每一文件都有兩類數據

元數據:metadata

數據: data

 

stat—— 顯示文件和文件系統的狀態 display file or file system statu

命令格式——stat FILE…

[root@localhost ~]# stat anaconda-ks.cfg

File: ‘anaconda-ks.cfg’      //文件名

Size: 1423???? ????????Blocks: 8???????? IO Block: 4096?? regular file?? //大小 塊 IO塊 普通文件

Device: 802h/2050d????????Inode: 529006???? Links: 1?? //塊設備號 Inode號 被硬鏈接的次數

Access: (0600/-rw——-) ? Uid: (?? 0/?? root) ? Gid: (?? 0/?? root)?? //訪問權限 UID GID

Access: 2017-12-09 17:59:19.374026733 +0800?? //訪問時間

Modify: 2017-12-09 17:59:19.380026733 +0800 //修改時間

Change: 2017-12-09 17:59:19.380026733 +0800 //更改時間

Birth: –

 

touch—— 修改文件的時間戳 change file timestamps

命令格式——touch [OPTION]… FILE…

[root@localhost ~]# stat /tmp/functions

File: ‘/tmp/functions’

Size: 17500???? ????????Blocks: 40???????? IO Block: 4096?? regular file

Device: 802h/2050d????????Inode: 786457???? ? Links: 1

Access: (0644/-rw-r–r–) Uid: (?? 0/?? ? root)?? Gid: (?? 0/?? ? root)

Access: 2017-12-05 19:20:34.088464502 +0800

Modify: 2017-12-05 19:20:34.088464502 +0800

Change: 2017-12-05 19:20:34.088464502 +0800

Birth: –

[root@localhost ~]# touch /tmp/functions

[root@localhost ~]# stat /tmp/functions

File: ‘/tmp/functions’

Size: 17500???? ????????Blocks: 40???????? IO Block: 4096?? regular file

Device: 802h/2050d????????Inode: 786457???? ? Links: 1

Access: (0644/-rw-r–r–) Uid: (?? 0/?? ? root)?? Gid: (?? 0/?? ? root)

Access: 2017-12-05 19:20:53.516463898 +0800

Modify: 2017-12-05 19:20:53.516463898 +0800

Change: 2017-12-05 19:20:53.516463898 +0800

Birth: –

-c: 指定的文件路徑不存在時不予創建;

-a: 僅修改access time;

[root@localhost ~]# touch -a /tmp/func

[root@localhost ~]# stat /tmp/func

File: ‘/tmp/func’

Size: 0???????? ????????Blocks: 0???????? IO Block: 4096?? regular empty file

Device: 802h/2050d????????Inode: 786490???? ? Links: 1

Access: (0644/-rw-r–r–) Uid: (?? 0/?? ? root)?? Gid: (?? 0/?? ? root)

Access: 2017-12-09 17:41:17.468048427 +0800

Modify: 2017-12-09 17:41:17.467048427 +0800

Change: 2017-12-09 17:41:17.468048427 +0800

Birth: –

 

-m:僅修改modify time;

[root@localhost ~]# touch -m /tmp/func

[root@localhost ~]# stat /tmp/func

File: ‘/tmp/func’

Size: 0???????? ????????Blocks: 0???????? IO Block: 4096?? regular empty file

Device: 802h/2050d????????Inode: 786490???? ? Links: 1

Access: (0644/-rw-r–r–) Uid: (?? 0/?? ? root)?? Gid: (?? 0/?? ? root)

Access: 2017-12-09 17:41:17.468048427 +0800

Modify: 2017-12-09 17:42:08.951049234 +0800

Change: 2017-12-09 17:42:08.951049234 +0800

Birth: –

 

-t STAMP 指定某一時間

[[CC]YY]MMDDhhmm[.ss]年月日小時分鐘秒

[root@localhost ~]# touch -m -t 200212011010.10 /tmp/functions

[root@localhost ~]# stat /tmp/functions

File: ‘/tmp/functions’

Size: 17500???? ????????Blocks: 40???????? IO Block: 4096?? regular file

Device: 802h/2050d????????Inode: 786457???? ? Links: 1

Access: (0644/-rw-r–r–) Uid: (?? 0/?? ? root)?? Gid: (?? 0/?? ? root)

Access: 2017-12-09 19:20:53.516463898 +0800

Modify: 2002-12-01 10:10:10.000000000 +0800

Change: 2017-12-09 19:27:08.544452236 +0800

Birth: –

 

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

命令別名:

獲取所有可用別名的定義:

[root@localhost ~]# 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’

 

定義別名:

[root@localhost ~]# alias cls=clear

[root@localhost ~]# alias

alias cls=’clear’

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’

 

注意:定義別名使用小寫,如果命令之間沒有空格,不需要加單引號,但僅對當前shell進程有效

 

引用命令的執行結果:

$(COMMAND)

或`COMMAND`

[root@localhost ~]# date

Tue Dec 5 18:23:28 CST 2017

[root@localhost ~]# date +%T

18:23:46

[root@localhost ~]# date +%H-%M-%S

18-24-44

[root@localhost ~]# mkdir $(date +%H-%M-%S)

[root@localhost ~]# ls

18-25-29 anaconda-ks.cfg

[root@localhost ~]# mkdir $(date +%H-%M-%S)

[root@localhost ~]# ls

18-25-29 18-25-50 ? anaconda-ks.cfg

[root@localhost ~]# mkdir `date +%H-%M-%S`

[root@localhost ~]# ls

18-25-29 18-25-50 ? 18-27-01 anaconda-ks.cfg

 

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

[root@localhost ~]# ls -d /var/l*[[:digit:]]*[[:lower:]]

 

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

[root@localhost ~]# ls -d /etc/[0-9]*[^0-9]

 

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

[root@localhost ~]# ls /etc/[^a-z][a-z]*

 

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

[root@localhost ~]# mkdir -pv /tmp/tfile-$(date +”%F-%H-%M-%S”)

mkdir: created directory ‘/tmp/tfile-2017-12-10-18-22-43’

[root@localhost ~]# ls -d /tmp/tfile*[0-9]

/tmp/tfile-2017-12-10-18-22-43

[root@localhost ~]# touch /tmp/file-$(date +”%F-%H-%M-%S”)

[root@localhost ~]# ls -d /tmp/tfile*[0-9]

/tmp/tfile-2017-12-10-18-22-43

[root@localhost ~]# touch /tmp/tfile-$(date +”%F-%H-%M-%S”)

[root@localhost ~]# ls -d /tmp/tfile*[0-9]

/tmp/tfile-2017-12-10-18-22-43 /tmp/tfile-2017-12-10-18-26-16

 

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

[root@localhost ~]# mkdir /tmp/mytest1

[root@localhost ~]# cp -a /etc/p*[^0-9] /tmp/mytest1/

[root@localhost ~]# ls -d /tmp/mytest1/*

/tmp/mytest1/pam.d???? /tmp/mytest1/pm???????????? /tmp/mytest1/printcap

/tmp/mytest1/passwd?? /tmp/mytest1/popt.d???????? /tmp/mytest1/profile

/tmp/mytest1/passwd- ?/tmp/mytest1/postfix???????? /tmp/mytest1/profile.d

/tmp/mytest1/pki???? ? /tmp/mytest1/ppp?????????? ? /tmp/mytest1/protocols

/tmp/mytest1/plymouth /tmp/mytest1/prelink.conf.d /tmp/mytest1/python

 

[root@localhost ~]# \cp -rf /etc/p*[^0-9] /tmp/mytest1/

[root@localhost ~]# ls -d /tmp/mytest1/*

/tmp/mytest1/pam.d???? /tmp/mytest1/pm???????????? /tmp/mytest1/printcap

/tmp/mytest1/passwd?? /tmp/mytest1/popt.d???????? /tmp/mytest1/profile

/tmp/mytest1/passwd- ?/tmp/mytest1/postfix???????? /tmp/mytest1/profile.d

/tmp/mytest1/pki???? ? /tmp/mytest1/ppp?????????? ? /tmp/mytest1/protocols

/tmp/mytest1/plymouth /tmp/mytest1/prelink.conf.d /tmp/mytest1/python

 

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

[root@localhost ~]# mkdir -v /tmp/mytest2

mkdir: created directory ‘/tmp/mytest2’

[root@localhost ~]# ls -d /etc/*.d

/etc/bash_completion.d ? /etc/ld.so.conf.d?? /etc/rc0.d???? /etc/rwtab.d

/etc/binfmt.d???????? ? /etc/logrotate.d???? /etc/rc1.d???? /etc/statetab.d

/etc/chkconfig.d?????? ? /etc/modprobe.d???? /etc/rc2.d???? /etc/sudoers.d

/etc/cron.d?????????? ? /etc/modules-load.d /etc/rc3.d???? /etc/sysctl.d

/etc/depmod.d???????? ? /etc/my.cnf.d?????? /etc/rc4.d???? ? /etc/tmpfiles.d

/etc/dracut.conf.d???? ? /etc/pam.d?????????? /etc/rc5.d???? /etc/xinetd.d

/etc/grub.d?????????? ? /etc/popt.d???????? /etc/rc6.d???? /etc/yum.repos.d

/etc/init.d???????????? /etc/prelink.conf.d /etc/rc.d

/etc/krb5.conf.d?????? /etc/profile.d?????? /etc/rsyslog.d

[root@localhost ~]# cp -a /etc/*.d /tmp/mytest2

[root@localhost ~]# ls -d /tmp/mytest2/*

/tmp/mytest2/bash_completion.d /tmp/mytest2/modules-load.d /tmp/mytest2/rc6.d

/tmp/mytest2/binfmt.d?????????? /tmp/mytest2/my.cnf.d?????? /tmp/mytest2/rc.d

/tmp/mytest2/chkconfig.d?????? /tmp/mytest2/pam.d?????????? /tmp/mytest2/rsyslog.d

/tmp/mytest2/cron.d???????????? /tmp/mytest2/popt.d???????? /tmp/mytest2/rwtab.d

/tmp/mytest2/depmod.d?????????? /tmp/mytest2/prelink.conf.d /tmp/mytest2/statetab.d

/tmp/mytest2/dracut.conf.d???? /tmp/mytest2/profile.d?????? /tmp/mytest2/sudoers.d

/tmp/mytest2/grub.d???????????? /tmp/mytest2/rc0.d???????? ? /tmp/mytest2/sysctl.d

/tmp/mytest2/init.d?????????? ? /tmp/mytest2/rc1.d???????? ? /tmp/mytest2/tmpfiles.d

/tmp/mytest2/krb5.conf.d?????? /tmp/mytest2/rc2.d???????? ? /tmp/mytest2/xinetd.d

/tmp/mytest2/ld.so.conf.d?????? /tmp/mytest2/rc3.d???????? ? /tmp/mytest2/yum.repos.d

/tmp/mytest2/logrotate.d?????? /tmp/mytest2/rc4.d

/tmp/mytest2/modprobe.d???????? /tmp/mytest2/rc5.d

 

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

[root@localhost ~]# mkdir -v /tmp/mytest3

mkdir: created directory ‘/tmp/mytest3’

[root@localhost ~]# ls -d /etc/[lmn]*.conf

/etc/ld.so.conf???? /etc/libuser.conf /etc/logrotate.conf /etc/mke2fs.conf

/etc/libaudit.conf /etc/locale.conf?? /etc/man_db.conf???? /etc/nsswitch.conf

[root@localhost ~]# cp -a /etc/[lmn]*.conf /tmp/mytest3

[root@localhost ~]# ls -d /tmp/mytest3/*

/tmp/mytest3/ld.so.conf???? /tmp/mytest3/locale.conf???? /tmp/mytest3/mke2fs.conf

/tmp/mytest3/libaudit.conf /tmp/mytest3/logrotate.conf /tmp/mytest3/nsswitch.conf

/tmp/mytest3/libuser.conf ? /tmp/mytest3/man_db.conf

本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/89813

(0)
N28_noonN28_noon
上一篇 2017-12-10
下一篇 2017-12-10

相關推薦

  • DNS

    這里都以我本機的實驗為例 正向解析:就是從主機名到IP的解析過程 先在工作目錄/var/named/創建一個區域數據文件 以zcylinux.io域為例:vim/var/named/zcylinux.io.zone $TTL  600     #設置全局變量TTL的值為600s zcylinux.io.&nb…

    Linux干貨 2017-05-30
  • week1

    一,計算機的組成 五大基本部件 運算器:  算術運算,邏輯運算等各種各樣的運算的, 控制器:  控制總線的使用權限,內存尋址 控制權限訪問是讀還是寫               寄存器,內部的存儲器,都是用來存儲數據的  加速和提高cpu性能   &…

    Linux干貨 2016-10-28
  • linux基礎中的基礎 —- 用戶管理、文本處理、正則表達式等命令的使用(博客第三周作業)

    1、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可。     who | cut -d' ' -f1 | uniq 2、取出最后登錄到當前系統的用戶的相關信息。     who | tail -1 3、取出當前系統上被用戶當作其默認shell的最…

    Linux干貨 2016-07-22
  • linux防火墻介紹

    一、前言firewall(防火墻):工作在網絡進入或者流包,進出的網絡數據包進行一定的規則進行檢查過濾系統。包括iptables和netfilter組件。iptables 是與 Linux 內核集成的 IP 信息包過濾系統。如果 Linux 系統連接到因特網或 LAN、服務器或連接 LAN 和因特網的代理服務器, 則該系統系統中更好地控制 IP 信息包過濾和…

    2017-04-30
  • 馬哥教育網絡班21期第7周課程練習

    1、創建一個10G分區,并格式為ext4文件系統;    (1) 要求其block大小為2048, 預留空間百分比為2, 卷標為MYDATA, 默認掛載屬性包含acl;    (2) 掛載至/data/mydata目錄,要求掛載時禁止程序自動運行,且不更新文件的訪問時間戳; [root@centos7study&nbs…

    Linux干貨 2016-08-29
  • 腳本練習

    腳本練習: 1、編寫腳本/root/bin/systeminfo.sh,顯示當前主機系統信息,包括主機名,IPv4地址,操作系統版本,內核版本,CPU型號,內存大小,硬盤大小。 #!/bin/bash #功能:編寫腳本/root/bin/systeminfo.sh,顯示當前主機系統信息,包括主機名,IPv4地址,操作系統版本,內核版本,CPU型號,內存大小,…

    Linux干貨 2016-08-12

評論列表(1條)

  • 馬哥教育
    馬哥教育 2017-12-16 12:28

    總結很詳細的一篇。繼續加油~

欧美性久久久久