N25_第二周作業_leon

第二周博客作業_leon

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

常用文件管理類命令有:

mkdir、touch、rmdir、cp、rm、mv

 

mkdir—-創建目錄

-m  直接設置權限(非默認權限)

-v   顯示創建目錄的過程

-p   自動按需創建父目錄

[root@localhost ~]# cd /tmp

[root@localhost tmp]# ls

myfirst.sh  mytmp.WBjMq  test.xl2cYj

[root@localhost tmp]# mkdir 123

[root@localhost tmp]# ls

123  myfirst.sh  mytmp.WBjMq  test.xl2cYj

 

-p自動按需創建父目錄

[root@localhost tmp]# ls

[root@localhost tmp]# mkdir 123/234/345

mkdir: cannot create directory `123/234/345': No such file or directory

[root@localhost tmp]# mkdir -p 123/234/345

[root@localhost tmp]# ls

123

[root@localhost tmp]# tree /tmp

/tmp

└── 123

    └── 234

        └── 345

3 directories, 0 files

 

-m直接設置權限(非默認權限)

[root@localhost tmp]# ls

[root@localhost tmp]# umask

0022

[root@localhost tmp]# mkdir 123

[root@localhost tmp]# ls -ld 123/

drwxr-xr-x. 2 root root 4096 Dec 13 09:07 123/

[root@localhost tmp]# mkdir -m 666 234

[root@localhost tmp]# ls

123  234

[root@localhost tmp]# ls -ld 234/

drw-rw-rw-. 2 root root 4096 Dec 13 09:08 234/

 

 

-v顯示創建目錄的過程

[root@localhost tmp]# ls

myfirst.sh  mytmp.WBjMq  test.xl2cYj

[root@localhost tmp]# mkdir -v 1 2 3 4

mkdir: created directory ‘1’

mkdir: created directory ‘2’

mkdir: created directory ‘3’

mkdir: created directory ‘4’

[root@localhost tmp]# ls

1  2  3  4  myfirst.sh  mytmp.WBjMq  test.xl2cYj

 

touch—-更改文件的時間戳 也可用于創建空文件

[root@localhost tmp]# ls

test2

[root@localhost tmp]# touch aa

[root@localhost tmp]# ls

aa  test2

[root@localhost tmp]# ls -la aa

-rw-r–r–. 1 root root 0 12 24 03:10 aa

 

rmdir—-刪除空目錄

[root@localhost tmp]# ls

123  2  myfirst.sh  mytmp.WBjMq  test.xl2cYj

[root@localhost tmp]# rmdir 123

[root@localhost tmp]# ls

2  myfirst.sh  mytmp.WBjMq  test.xl2cYj

[root@localhost tmp]# rmdir 2

[root@localhost tmp]# ls

myfirst.sh  mytmp.WBjMq  test.xl2cYj

 

cp—-文件拷貝

-r 拷貝目錄

-p 保留文件屬性及權限

-P 不拷貝鏈接文件的原文件

-f 目標文件存在時,強制覆蓋

[root@localhost tmp]# ls

aa  test2

[root@localhost tmp]# cp /etc/passwd passwd.bak

[root@localhost tmp]# ls

aa  passwd.bak  test2

 

 

 

 

-r 拷貝目錄

[root@localhost tmp]# ls

aa  passwd.bak  test2

[root@localhost tmp]# cp test2/ test2.bak

cp: 略過目錄"test2/"

[root@localhost tmp]# cp -r test2/ test2.bak

[root@localhost tmp]# ls

aa  passwd.bak  test2  test2.bak

 

-p 保留文件屬性及權限

[root@localhost tmp]# ls -la /etc/inittab

-rw-r–r–. 1 root root 884 1  14 2016 /etc/inittab

[root@localhost tmp]# cp /etc/inittab inittab.bak

[root@localhost tmp]# ls -ls inittab.bak

4 -rw-r–r–. 1 root root 884 12 24 03:27 inittab.bak

[root@localhost tmp]# cp -p /etc/inittab inittab.bak2

[root@localhost tmp]# ls -la inittab.bak inittab.bak2

-rw-r–r–. 1 root root 884 12 24 03:27 inittab.bak

-rw-r–r–. 1 root root 884 1  14 2016 inittab.bak2

 

cp -P 不拷貝鏈接文件的原文件

[root@localhost ~]# cp /etc/system-release /tmp

[root@localhost ~]# ls -la /tmp/system-release

-rw-r–r–. 1 root root 27 12 24 03:33 /tmp/system-release

[root@localhost ~]# cat /tmp/system-release

CentOS release 6.3 (Final)

[root@localhost ~]# cp -P /etc/system-release /tmp/system-release.bak

[root@localhost ~]# ls -la /tmp/{system-release,system-release.bak}

-rw-r–r–. 1 root root 27 12 24 03:33 /tmp/system-release

lrwxrwxrwx. 1 root root 14 12 24 03:34 /tmp/system-release.bak -> centos-release

 

cp -f 目標文件存在時,強制覆蓋

[root@localhost tmp]# ls

inittab.bak2  test2

[root@localhost tmp]# cp /etc/inittab inittab.bak2

cp:是否覆蓋"inittab.bak2"? y

[root@localhost tmp]# ls

inittab.bak2  test2

[root@localhost tmp]# cp -f /etc/inittab inittab.bak2

[root@localhost tmp]# ls

inittab.bak2  test2

 

 

 

rm—-刪除文件或目錄

-f 強制刪除

-i 刪除之前提示

-r遞歸刪除

[root@localhost tmp]# ls

inittab.bak2  test2

[root@localhost tmp]# rm inittab.bak2

rm:是否刪除普通文件 "inittab.bak2"?y

[root@localhost tmp]# ls

test2

 

rm -f 強制刪除

[root@localhost tmp]# cp /etc/inittab ./

[root@localhost tmp]# ls

inittab  test2

[root@localhost tmp]# rm -f inittab

[root@localhost tmp]# ls

test2

 

rm -r遞歸刪除

[root@localhost tmp]# ls

123  test2

[root@localhost tmp]# tree

.

├── 123

   └── 234

       └── 345

└── test2

    ├── test1

    ├── test3

    └── test4

 

4 directories, 3 files

[root@localhost tmp]# rm 123

rm: 無法刪除"123": 是一個目錄

[root@localhost tmp]# rm -r 123/

rm:是否進入目錄"123"? y

rm:是否進入目錄"123/234"? y

rm:是否刪除目錄 "123/234/345"?^C

[root@localhost tmp]# rm -rf 123/

[root@localhost tmp]# ls

test2

 

 

mv—-移動(或改名)文件

-f 強制覆蓋,不提示

-I 覆蓋前提示

[root@localhost tmp]# ls

test2

[root@localhost tmp]# touch aa

[root@localhost tmp]# ls

aa  test2

[root@localhost tmp]# touch bb

[root@localhost tmp]# ls

aa  bb  test2

[root@localhost tmp]# mv aa bb

mv:是否覆蓋"bb" y

[root@localhost tmp]# ls

bb  test2

[root@localhost tmp]# touch aa

[root@localhost tmp]# mv -f aa bb

[root@localhost tmp]# ls

bb  test2

 

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

bash的工作特性之命令執行狀態返回值:

bash通過狀態返回值來輸出命令運行結果:成功 0 ;失敗 1-255

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

[root@localhost tmp]# ls

bb  test2

[root@localhost tmp]# cp 123 ./234

cp: 無法獲取"123" 的文件狀態(stat): 沒有那個文件或目錄

[root@localhost tmp]# echo $?

1

 

[root@localhost tmp]# ls

bb  test2

[root@localhost tmp]# cp bb ./234

[root@localhost tmp]# echo $?

0

 

bash的工作特性之命令行展開:

~:自動展開為用戶的家目錄,或指定的用戶的家目錄

[root@localhost tmp]# cd ~

[root@localhost ~]# pwd

/root

[root@localhost ~]# whoami

Root

{}:可承載一個以逗號分隔的路徑列表,并能夠將其展開為多個路徑:

[root@localhost tmp]# ls

test2

[root@localhost tmp]# mkdir 22

[root@localhost tmp]# cp /etc/{inittab,issue} ./22

[root@localhost tmp]# ls -la 22

總用量 16

drwxr-xr-x. 2 root root 4096 12 24 05:38 .

drwxrwxrwt. 5 root root 4096 12 24 05:38 ..

-rw-r–r–. 1 root root  884 12 24 05:38 inittab

-rw-r–r–. 1 root root   47 12 24 05:38 issue

 

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

mkdir: 已創建目錄 "a+c"

mkdir: 已創建目錄 "a+d"

mkdir: 已創建目錄 "b+c"

mkdir: 已創建目錄 "b+d"

[root@localhost tmp]# ls

22  a+c  a+d  b+c  b+d  test2

 

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

(1)       創建/tmp目錄下的:a_c, a_d, b_c, b_d

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

mkdir: 已創建目錄 "a_b"

mkdir: 已創建目錄 "a_d"

mkdir: 已創建目錄 "c_b"

mkdir: 已創建目錄 "c_d"

[root@localhost tmp]# ls

a_b  a_d  c_b  c_d  test2

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

[root@localhost tmp]# ls

test2

[root@localhost 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: 已創建目錄 "/tmp/mylinux"

mkdir: 已創建目錄 "/tmp/mylinux/bin"

mkdir: 已創建目錄 "/tmp/mylinux/boot"

mkdir: 已創建目錄 "/tmp/mylinux/boot/grub"

mkdir: 已創建目錄 "/tmp/mylinux/dev"

mkdir: 已創建目錄 "/tmp/mylinux/etc"

mkdir: 已創建目錄 "/tmp/mylinux/etc/rc.d"

mkdir: 已創建目錄 "/tmp/mylinux/etc/rc.d/init.d"

mkdir: 已創建目錄 "/tmp/mylinux/etc/sysconfig"

mkdir: 已創建目錄 "/tmp/mylinux/etc/sysconfig/network-scripts"

mkdir: 已創建目錄 "/tmp/mylinux/lib"

mkdir: 已創建目錄 "/tmp/mylinux/lib/modules"

mkdir: 已創建目錄 "/tmp/mylinux/lib64"

mkdir: 已創建目錄 "/tmp/mylinux/proc"

mkdir: 已創建目錄 "/tmp/mylinux/sbin"

mkdir: 已創建目錄 "/tmp/mylinux/sys"

mkdir: 已創建目錄 "/tmp/mylinux/tmp"

mkdir: 已創建目錄 "/tmp/mylinux/usr"

mkdir: 已創建目錄 "/tmp/mylinux/usr/local"

mkdir: 已創建目錄 "/tmp/mylinux/usr/local/bin"

mkdir: 已創建目錄 "/tmp/mylinux/usr/sbin"

mkdir: 已創建目錄 "/tmp/mylinux/var"

mkdir: 已創建目錄 "/tmp/mylinux/var/lock"

mkdir: 已創建目錄 "/tmp/mylinux/var/log"

mkdir: 已創建目錄 "/tmp/mylinux/var/run"

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

 

24 directories, 0 files

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

文件的元數據有:

access:文件的訪問時間

modify:文件的修改時間

change:文件的改變時間

 

使用stat命令查看

[root@localhost tmp]# ls

mylinux  test2

[root@localhost tmp]# touch ss

[root@localhost tmp]# ls

mylinux  ss  test2

[root@localhost tmp]# stat ss

  File: "ss"

  Size: 0          Blocks: 0          IO Block: 4096   普通空文件

Device: 803h/2051d Inode: 791845      Links: 1

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

Access: 2016-12-24 06:03:43.885419559 +0800

Modify: 2016-12-24 06:03:43.885419559 +0800

Change: 2016-12-24 06:03:43.885419559 +0800

 

修改文件的訪問時間:

touch -a

[root@localhost tmp]#

[root@localhost tmp]# stat ss

  File: "ss"

  Size: 0          Blocks: 0          IO Block: 4096   普通空文件

Device: 803h/2051d Inode: 791845      Links: 1

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

Access: 2016-12-24 06:05:39.063719506 +0800

Modify: 2016-12-24 06:05:39.063719506 +0800

Change: 2016-12-24 06:05:39.063719506 +0800

[root@localhost tmp]# touch -a ss

[root@localhost tmp]# stat ss

  File: "ss"

  Size: 0          Blocks: 0          IO Block: 4096   普通空文件

Device: 803h/2051d Inode: 791845      Links: 1

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

Access: 2016-12-24 06:08:05.122793372 +0800

Modify: 2016-12-24 06:05:39.063719506 +0800

Change: 2016-12-24 06:08:05.122793372 +0800

 

 

 

touch -m 修改文件的修改時間

[root@localhost tmp]# touch -m ss

[root@localhost tmp]# stat ss

  File: "ss"

  Size: 0          Blocks: 0          IO Block: 4096   普通空文件

Device: 803h/2051d Inode: 791845      Links: 1

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

Access: 2016-12-24 06:08:05.122793372 +0800

Modify: 2016-12-24 06:09:53.767543500 +0800

Change: 2016-12-24 06:09:53.767543500 +0800

 

[root@localhost tmp]# touch -t 202011111111.11 ss

[root@localhost tmp]# stat ss

  File: "ss"

  Size: 0          Blocks: 0          IO Block: 4096   普通空文件

Device: 803h/2051d Inode: 791845      Links: 1

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

Access: 2020-11-11 11:11:11.000000000 +0800

Modify: 2020-11-11 11:11:11.000000000 +0800

Change: 2016-12-24 06:13:15.432338068 +0800

 

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

alias命令定義一個命令的別名

[root@localhost tmp]# alias

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 tmp]# alias cp='cp -i'

[root@localhost tmp]# alias

alias cp='cp -i'

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 tmp]# ls

mylinux  ss  test2

[root@localhost tmp]# cp ss dd

[root@localhost tmp]# cp ss dd

cp:是否覆蓋"dd"? y

[root@localhost tmp]# ls

dd  mylinux  ss  test2

 

       使用管道命令在命令中引用另一個命令的執行結果

       [root@localhost tmp]# cp /etc/issue issue.bak

[root@localhost tmp]# ls

dd  issue.bak  mylinux  ss  test2

[root@localhost tmp]# cat issue.bak

CentOS release 6.3 (Final)

Kernel \r on an \m

[root@localhost tmp]# cat issue.bak | tr 'a-z' 'A-Z'

CENTOS RELEASE 6.3 (FINAL)

KERNEL \R ON AN \M

 

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

[root@localhost tmp]# ls -d /var/1*[0-9]*[[:lower:]]

ls: 無法訪問/var/l*[0-9]*[[:lower:]]: 沒有那個文件或目錄

[root@localhost tmp]# touch /var/1A4???b

[root@localhost tmp]# ls -d /var/1*[0-9]*[[:lower:]]

/var/1A4???b

 

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

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

ls: 無法訪問/etc/[0-9]*[^0-9]: 沒有那個文件或目錄

[root@localhost tmp]# mkdir /etc/5iu

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

/etc/5iu

 

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

[root@localhost tmp]# ls -d /etc/[^a-z][^[:alpha:]]*

ls: 無法訪問/etc/[^a-z][^[:alpha:]]*: 沒有那個文件或目錄

[root@localhost tmp]# touch /etc/46jaojg

[root@localhost tmp]# ls -d /etc/[^a-z][^[:alpha:]]*

/etc/46jaojg

 

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

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

[root@localhost tmp]# ls

dd  issue.bak  mylinux  ss  test2  tfile-2016-12-24-06-59-46

 

 

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

[root@localhost tmp]# mkdir mytest1

[root@localhost tmp]# ls

mytest1  test2

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

[root@localhost tmp]# ls

mytest1  test2 [root@localhost tmp]# ls -la mytest1/

總用量 72

drwxr-xr-x. 12 root root 4096 12 24 07:04 .

drwxrwxrwt.  5 root root 4096 12 24 07:03 ..

drwxr-xr-x.  2 root root 4096 12 24 07:04 pam.d

drwxr-xr-x.  3 root root 4096 12 24 07:04 pango

-rw-r–r–.  1 root root 1018 12 24 07:04 passwd

-rw-r–r–.  1 root root 1055 12 24 07:04 passwd-

drwxr-xr-x.  8 root root 4096 12 24 07:04 pki

 

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

[root@localhost tmp]# ls

mytest1  test2

[root@localhost tmp]# mkdir mytest2

[root@localhost tmp]# ls

mytest1  mytest2  test2

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

[root@localhost tmp]# ls -la mytest2

總用量 88

drwxr-xr-x. 22 root root 4096 12 24 07:07 .

drwxrwxrwt.  6 root root 4096 12 24 07:07 ..

drwxr-xr-x.  2 root root 4096 12 24 07:07 bash_completion.d

drwxr-xr-x.  2 root root 4096 12 24 07:07 chkconfig.d

drwxr-xr-x.  2 root root 4096 12 24 07:07 cron.d

drwxr-xr-x.  2 root root 4096 12 24 07:07 depmod.d

drwxr-xr-x.  2 root root 4096 12 24 07:07 dracut.conf.d

drwxr-xr-x.  2 root root 4096 12 24 07:07 event.d

lrwxrwxrwx.  1 root root   11 12 24 07:07 init.d -> rc.d/init.d

drwxr-xr-x.  2 root root 4096 12 24 07:07 ld.so.conf.d

drwxr-xr-x.  2 root root 4096 12 24 07:07 logrotate.d

drwxr-xr-x.  2 root root 4096 12 24 07:07 makedev.d

drwxr-xr-x.  2 root root 4096 12 24 07:07 modprobe.d

drwxr-xr-x.  2 root root 4096 12 24 07:07 pam.d

drwxr-xr-x.  2 root root 4096 12 24 07:07 popt.d

drwxr-xr-x.  2 root root 4096 12 24 07:07 profile.d

 

12.   復制/etc目錄下所有以1mn開頭,以.com結尾的文件至/tmp/mytest3目錄中

[root@localhost tmp]# touch /etc/l2534.com

[root@localhost tmp]# cp /etc/[l,m,n]*.com ./mytest3

[root@localhost tmp]# ls mytest3

l2534.com

[root@localhost tmp]# ls -la mytest3

總用量 8

drwxr-xr-x. 2 root root 4096 12 24 07:13 .

drwxrwxrwt. 7 root root 4096 12 24 07:13 ..

-rw-r–r–. 1 root root    0 12 24 07:13 l2534.com

原創文章,作者:leon,如若轉載,請注明出處:http://www.www58058.com/64587

(0)
leonleon
上一篇 2016-12-23
下一篇 2016-12-24

相關推薦

  • Linux百科

    百度百科摘

    Linux干貨 2018-03-26
  • 運行級別

    運行級別(Runlevel)指的是Unix或者Linux等類Unix操作系統下不同的運行模式。運行級別通常分為7等,分別是從0到6,但如果必要的話也可以更多。 例如在大多數Linux操作系統下一共有如下7個典型的運行級別: 0 停機,關機 1 單用戶,無網絡連接,不運行守護進程,不允許非超級用戶登錄 2 多用戶,無網絡連接,不運行守護進程 3 多用戶,正常啟…

    Linux干貨 2017-07-10
  • Linux運維學習歷程-第六天-Linux重定向和管道

    Linux運維學習歷程-第六天-Linux重定向和管道 2 本章內容我們將學習linux中的重定向和管道兩大用法   I/O輸入與輸出設備   重定向   管道   tee命令與tr命令 一、I/O設備   1、什么是I/O設備   管理和控制計算機的所有輸入/輸出(I/O)設備是操作系統…

    Linux干貨 2016-08-03
  • linux文件權限詳解

    基本命令 1.cut :?cat /etc/passwd | cut -d’:’ -f7| uniq -c| sort -nr 2.authconfig 修改加密方式 –passalgo=sha256 — update 3.scp 上傳文件 -r dir ip:path 傳目錄 file ip:path傳文件 …

    Linux干貨 2017-04-03
  • 自制kickstart文件——完成CentOS系統的自動化安裝

    CentOS 系統安裝: ·安裝程序:anaconda         bootloader–>kernel(initrd(rootfs))–>anaconda ·anaconda:<兩種模式>    …

    Linux干貨 2016-09-16
  • 2. 初識shell

        在Linux早期, 可以用來工作的只有shell. 那時, 系統管理員, 程序員和系統用戶都坐在Linux命令行終端前, 輸入文本命令, 查看文本輸出. 而現在, 因為有了絢麗的圖形化桌面環境, 在系統上找到shell提示符都變得困難起來. 接下來將會討論提供命令行環境需要什么, 然后帶你逐步了解可能會在各種L…

    Linux干貨 2016-11-06
欧美性久久久久