1. 程式人生 > >文件與目錄管理命令(三)

文件與目錄管理命令(三)

信息 touch share 單位換算

目錄管理類命令包括:cd,pwd,ls,mkdir,rmdir,tree,cp,mv,rm,touch,stat


1.1.ls

功能:列出文件或目錄

命令幫助:

man ls

info ls

ls --help

help cd #只有內部命令才能用
ls /usr/share/man 文檔位置

常用選項

-a:列出所有的文件(包含隱藏文件)

-l:以長格式的形式列出信息

-i:顯示文件inode號

-h:單位換算

-m:以逗號分割文件名列出

-t:按照修改時間來排序

-r:按照修改時間來反向排列

-S:按大小排

示例:

將家目錄下的所有文件列出來(含屬性與隱藏檔)

[[email protected] /]# ls -al ~

total 88dr-xr-x---.  3 root root  4096 May  8 22:27

 .......長格式列出

 [[email protected]
/* */ ~]# ls -l total 44-rw-------. 1 root root 1166 May 7 10:08 anaconda-ks.cfg -rw-r--r--. 1 root root 17957 May 7 10:08 install.log -rw-r--r--. 1 root root 4178 May 7 10:06 install.log.syslog -rw-r--r--. 1 root root 7273 May 7 21:30 system.sh 按照修改時間倒序 [[email protected] ~]# ls -lt total 44-rw-r--r--. 1 root root 7273 May 7 21:30 system.sh -rw-------. 1 root root 1166 May 7 10:08 anaconda-ks.cfg -rw-r--r--. 1 root root 17957 May 7 10:08 install.log -rw-r--r--. 1 root root 4178 May 7 10:06 install.log.syslog 按照修改時間正序 [[email protected]
/* */ ~]# ls -lrt total 44-rw-r--r--. 1 root root 4178 May 7 10:06 install.log.syslog -rw-r--r--. 1 root root 17957 May 7 10:08 install.log -rw-------. 1 root root 1166 May 7 10:08 anaconda-ks.cfg -rw-r--r--. 1 root root 7273 May 7 21:30 system.sh

3.2.cd

功能:更改工作路徑

cd是Change Directory的縮寫

用法:

cd 直接回車 直接切換到當前用戶的家目錄裏

cd ~:直接切換到當前用戶的家目錄裏
cd .或者 ./:切換到當前目錄
cd ..:切換到上一級目錄
cd -:切換到上一次的工作目錄
示例:

絕對路徑切換:
[[email protected] ~]# cd /root/test

相對路徑切換:
[[email protected] /]# cd ./test
當前是root,那就是回到root家目錄

[[email protected] /]# cd ~

回到上一級目錄

[[email protected] test]# cd ..


3.3.pwd(Print Working Directory)

功能:顯示當前所在位置的絕對路徑。

選項:

-P:顯示出實際的路徑,而非使用鏈接(link)路徑。

示例:

單純顯示出目前的工作目錄
[[email protected] ~]# pwd
/root   

顯示實際的工作目錄,而非連接擋本身的目錄名而已
[email protected] ~]# cd /var/mail   <==註意,/var/mail是一個連結檔
[[email protected] mail]# pwd
/var/mail         <==列出目前的工作目錄
[[email protected] mail]# pwd -P
/var/spool/mail   <==怎麽跟上面顯示不一樣?
[[email protected] mail]# ls -ld /var/mail
lrwxrwxrwx 1 root root 10 Sep  4 17:54 /var/mail -> spool/mail
# /var/mail 是連結檔,連結到 /var/spool/mail
# 加上 pwd -P 的選項後,會不以連結檔的數據顯示,而是顯示正確的完整路徑

3.4.touch

功能

創建文件,更改文件的時間戳

當目標文件不存在:創建新的,空的文件
touch filename
touch file1 file2
touch {a,b,c}.txt
touch file{1..5}
當文件命有特殊字符時,可以進行如下轉義:
touch a\ b\ c
touch "a b c"
touch ‘a b c‘
 當目標文件存在時,touch修改文件的時間戳:
# stat file1  查看文件詳細信息(時間戳)
  File: `file1‘
  Size: 0         Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768dInode: 275119      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-03-30 14:34:51.724484689 +0800  訪問時間
Modify: 2016-03-30 14:34:45.915484930 +0800  修改時間
Change: 2016-03-30 14:34:45.915484930 +0800  屬性時間
 
touch -a file1 -t 202011111111  修改文件的訪問時間
touch -m file1 -t 201011111212  修改文件的修改時間
touch -d 20201010 filename      修改文件的訪問時間和修改時間

block 塊,操作系統塊是一次IO的單位,系統塊默認大小為4096字節;以後oracle裏也會學到塊的概念,oracle塊默認為8192字節

inode 是文件系統給文件的一個編號,類似於文件的一個門牌號碼


一個優化有關的問題:

塊大好,還小好?

塊大,一次IO數據量大,IO吞吐量大,空間浪費較多

塊小,正好和塊大相反

批量創建文件:

第一種:touch {1..10}

第二種seq 11 2 20 |xargs touch seq產生一個序列, |符號是管道(表示把前面的結果傳到後面)

第三種:寫shell腳本做循環

#!/bin/bash

. /etc/init.d/functions 

for i in `seq 10`;do   

    touch file$i && File_Status=$?   

    if [ $File_Status -eq 0 ];then      

       action "this is file$i successed" /bin/true  

    else      

       action "this is file$i failed" /bin/false   

    fi

done


3.5.stat

功能:顯示文件或文件系統狀態

常用選項:

-Z 顯示selinux安全上下文

-f 顯示文件系統狀態

-c 指定格式輸出內容

%y Time of last modification(最近修改時間)

%x Time of last access (最近訪問時間)

-t 以簡潔的形式打印

示例:

顯示文件詳細信息:

[[email protected] ~]# stat anaconda-ks.cfg  

File: `anaconda-ks.cfg‘  

Size: 1148          Blocks: 8          IO Block: 4096   regular file

Device: fd00h/64768d    Inode: 267953      Links: 1

Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2017-04-05 10:38:15.690999958 +0800

Modify: 2017-04-05 10:38:16.614999958 +0800C

hange: 2017-04-05 10:38:18.527999957 +0800

只顯示文件訪問時間:

[[email protected] ~]# stat -c %x anaconda-ks.cfg

2017-04-05 10:38:15.690999958 +0800

3.6.mkdir

功能:創建新目錄(make directory)

語法:mkdir [OPTION]... DIRECTORY...

常用選項:

-m :配置文件的權限

-p :遞歸創建目錄

-v:顯示創建過程

示例:

請到/tmp底下嘗試創建數個新目錄看看:

[[email protected] tmp]# pwd

/tmp

[[email protected] tmp]# mkdir test #創建test新目錄

[[email protected] tmp]# mkdir -p test1/test2/test3/test4  遞歸創建多個目錄

[[email protected] tmp]# tree test1 查看創建成功

test1

└── test2

    └── test3

        └── test4



directories, 0 files

範例:創建權限為rwx--x--x的目錄

[[email protected] tmp]# mkdir -m 655 test2

[[email protected] tmp]# ll -d test2

drw-r-xr-x 2 root root 4096 May 24 10:19 test2

 

上面的權限部分,如果沒有加上 -m 來強制配置屬性,系統會使用默認屬性。

創建多個目[[email protected] tmp]# mkdir {1,2}

[[email protected] tmp]# ll

total 24

drwxr-xr-x  2 root root 4096 May 24 10:21 1

drwxr-xr-x  2 root root 4096 May 24 10:21 2

創建連續目錄:

[[email protected] tmp]# mkdir {3..10}

[[email protected] tmp]# ll

total 56

drwxr-xr-x  2 root root 4096 May 24 10:23 1

drwxr-xr-x  2 root root 4096 May 24 10:23 10

drwxr-xr-x  2 root root 4096 May 24 10:23 2

drwxr-xr-x  2 root root 4096 May 24 10:23 3

drwxr-xr-x  2 root root 4096 May 24 10:23 4

drwxr-xr-x  2 root root 4096 May 24 10:23 5

drwxr-xr-x  2 root root 4096 May 24 10:23 6

drwxr-xr-x  2 root root 4096 May 24 10:23 7

drwxr-xr-x  2 root root 4096 May 24 10:23 8

drwxr-xr-x  2 root root 4096 May 24 10:23 9

3.7.rmdir

功能:刪除空的目錄

語法:rmdir [OPTION]... DIRECTORY...

選項:

-p:連同上一級『空的』目錄也一起刪除

-v:顯示過程

示例:

[[email protected] ~]# rmdir -v a #這裏的a是空目錄,所以可以直接刪除
rmdir: removing directory, `a‘
[[email protected] ~]# rmdir -v 1  #這裏報錯,是因為1不是空目錄,通過tree就看的出。
rmdir: removing directory, `1‘
rmdir: failed to remove `1‘: Directory not empty
[[email protected] ~]# tree 1
1
└── 2
    └── 3
        └── 4
3 directories, 0 files
[[email protected] ~]# rmdir -p 1 #使用遞歸刪除時這樣寫刪除也是會報錯
rmdir: failed to remove `1‘: Directory not empty
[[email protected] ~]# rmdir -p 1/2/3/4 #這樣寫的意思就是先刪除4,再刪除3,依次遞歸



3.8.tree

功能:以樹狀圖列出目錄的內容;

語法:語法很繁瑣,這裏就不介紹了

常用選項:

-d:只顯示目錄

-L level:指定顯示的層級數目

-P pattern:只顯示由指定pattern匹配到的路徑,這裏的P是大寫;

-p:列出權限

示例:

[[email protected] ~]# ll
total 444
-rw-------. 1 root root   1148 Apr  5 10:38 anaconda-ks.cfg
drwxr-xr-x  2 root root   4096 May 22 10:52 b
drwxr-xr-x  2 root root   4096 May 22 10:52 c
drwxr-xr-x  2 root root   4096 May 22 10:52 d
drwxr-xr-x  2 root root   4096 May 22 10:52 e
-rw-r--r--  1 root root 389473 May  9 15:22 inotify-tools-3.13.tar.gz
drwxr-xr-x  2 root root   4096 May 22 10:48 install
-rw-r--r--. 1 root root  17957 Apr  5 10:38 install.log
-rw-r--r--. 1 root root   4178 Apr  5 10:37 install.log.syslog
drwxr-xr-x  2 root root   4096 May 22 10:48 tmp
[[email protected] ~]# tree -d .
.
├── b
├── c
├── d
├── e
├── install
└── tmp
[[email protected] ~]# mkdir -p 1/2/3/4/5/6
[[email protected] ~]# tree -L 2 1 #從1開始的2個層級,到3
1
└── 2
    └── 3
2 directories, 0 files
[[email protected] ~]# tree 1 #不帶L參數,全部層級都顯示
1
└── 2
    └── 3
        └── 4
            └── 5
                └── 6
5 directories, 0 files
[[email protected] ~]# tree -L 3 1 #從1開始的3個層級,到4 
1
└── 2
    └── 3
        └── 4
[[email protected] ~]# tree -P *.gz .
.
├── 1
│   └── 2
│       └── 3
│           └── 4
│               └── 5
│                   └── 6
├── b
├── c
├── d
├── e
├── inotify-tools-3.13.tar.gz
├── install
└── tmp
[[email protected] ~]# tree -P *.log .
.
├── 1
│   └── 2
│       └── 3
│           └── 4
│               └── 5
│                   └── 6
├── b
├── c
├── d
├── e
├── install
├── install.log
└── tmp
[[email protected] ~]# tree -p *.gz .
inotify-tools-3.13.tar.gz [error opening dir]
.
├── [drwxr-xr-x]  1
│   └── [drwxr-xr-x]  2
│       └── [drwxr-xr-x]  3
│           └── [drwxr-xr-x]  4
│               └── [drwxr-xr-x]  5
│                   └── [drwxr-xr-x]  6
├── [-rw-------]  anaconda-ks.cfg
├── [drwxr-xr-x]  b
├── [drwxr-xr-x]  c
├── [drwxr-xr-x]  d
├── [drwxr-xr-x]  e
├── [-rw-r--r--]  inotify-tools-3.13.tar.gz
├── [drwxr-xr-x]  install
├── [-rw-r--r--]  install.log
├── [-rw-r--r--]  install.log.syslog
└── [drwxr-xr-x]  tmp

3.9.cp

功能:復制文件或目錄

語法:

cp [OPTION]... [-T] SOURCE DEST

cp [OPTION]... SOURCE... DIRECTORY

cp [OPTION]... -t DIRECTORY SOURCE...

常用選項:

-a 歸檔,相當於-dR --perserv=all

-b 目標文件存在創建備份,備份文件是文件名跟~

-f 強制復制文件或目錄

-r 遞歸復制目錄

-p 保留原有文件或目錄屬性,--perserv=mode,ownership,timestamp

-i 交互式,即復制文件或目錄會詢問

-u 當源文件比目的文件修改時間新時才復制

-v 顯示復制信息,--verbose

區別:

如果是目錄需要拷貝所有屬性,用-a或者-rp

如果是文件需要拷貝所有屬性,用-p或者-a

註意:cp在管理員的情況下慎用,盡量用交互式來確定是否真的想cp覆蓋文件或目錄

3.10.mv

功能:移動文件或目錄,或重命名

語法:

mv [OPTION]... [-T] SOURCE DEST

mv [OPTION]... SOURCE... DIRECTORY

mv [OPTION]... -t DIRECTORY SOURCE..

常用選項:

-b 目標文件存在創建備份,備份文件是文件名跟~

-u 當源文件比目的文件修改時間新時才移動

-v 顯示移動信息

-i 交互式

-f 強制

註意:mv也是一個危險命令,使用時慎之又慎


3.11.rm

功能:remove,刪除文件或目錄

語法:rm [OPTION]... FILE...

常用選項:

-i:交互式

-f:強制刪除

-r:遞歸

註意:rm相對於mv,cp,在生產環境就更危險了,好多同仁受其害不止一次兩次,建議禁用。


練習:

(1)如何創建目錄/tmp/x/y1,/tmp/x/y2,/tmp/x/y1/a, /tmp/x/y1/b, /tmp/x/y2/a,/tmp/x/y2/b,要求一次命令運行?

[[email protected] ~]# mkdir /tmp/x /tmp/x/{y1,y2} /tmp/x/y1/{a,b} /tmp/x/y2/{a,b}
[[email protected] ~]# tree /tmp/x
/tmp/x
├── y1
│   ├── a
│   └── b
└── y2
    ├── a
    └── b
更簡單的方法:
[[email protected] tmp]# mkdir -p /tmp/x/{y1,y2}/{a,b}
[[email protected] tmp]# tree /tmp/x
/tmp/x
├── y1
│   ├── a
│   └── b
└── y2
    ├── a
    └── b

2)如何創建目錄 x_m,y_m,x_n,y_n,要求一次命令運行?

[[email protected] tmp]# mkdir {x,y}_{m,n}
drwxr-xr-x  2 root root 4096 May 22 14:20 x_m
drwxr-xr-x  2 root root 4096 May 22 14:20 x_n
drwxr-xr-x  2 root root 4096 May 22 14:20 y_m
drwxr-xr-x  2 root root 4096 May 22 14:20 y_n

(3)如何創建目錄/tmp/bin, /tmp/sbin,/tmp/usr,/tmp/user/bin,/tmp/usr/sbin,要求一次命令運行?

[[email protected] tmp]# mkdir -p /tmp/{bin,sbin} /tmp/user/bin /tmp/usr/sbin 
[[email protected] tmp]# tree bin sbin user usr
bin
sbin
user
└── bin
usr
└── sbin

(4)看一個實驗

[[email protected] tmp]# touch test
[[email protected] tmp]# ll -i test
396797 -rw-r--r-- 1 root root 0 May 22 14:26 test
[[email protected] tmp]# cp test test1
[[email protected] tmp]# ll -i test test1
396797 -rw-r--r-- 1 root root 0 May 22 14:26 test
396798 -rw-r--r-- 1 root root 0 May 22 14:26 test1
[[email protected] tmp]# ll -i test1 test3
396798 -rw-r--r-- 1 root root 0 May 22 14:26 test1
396797 -rw-r--r-- 1 root root 0 May 22 14:26 test3

說明:cp一個文件,新文件inode會改變,由396797->396798,mv一個文件,inode號不變,由396797->396797,這是同一分區不變

那不同分區會改變嗎:

[[email protected] ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       18G  1.4G   15G   9% /
tmpfs                 491M     0  491M   0% /dev/shm
/dev/sda1             477M   28M  425M   6% /boot
/dev/mapper/VolGroup-lv01
                      4.8G   10M  4.6G   1% /data
[[email protected] tmp]# ll -i test1 test3
396798 -rw-r--r-- 1 root root 0 May 22 14:26 test1
396797 -rw-r--r-- 1 root root 0 May 22 14:26 test3
[[email protected] tmp]# mv test3 /data/test4
[[email protected] tmp]# ll -i /data/test4
12 -rw-r--r-- 1 root root 0 May 22 14:26 /data/test4
把文件從根分區mv到data分區,跨區域移動,發現inode是改變了

(5)如何把/etc/skel/目錄裏以.開頭的隱藏文件拷到/tmp目

有桌面可以用下面命令打開目錄

nautilus /etc/skel/         --圖形打開一個目錄
# cp /etc/skel/* /tmp/ -rf    --錯誤用法,*號不能代表隱藏文件
# cp /etc/skel/.* /tmp/ -rf   --錯誤用法, .*也包括..,也就是會把上級目錄的東西也拷過去
方法一:
# cp /etc/skel/.[a-Z]*  /test/ -rf --正確做法
方法2:
#ls -a |xargs -i cp {} /test

本文出自 “爛筆頭” 博客,請務必保留此出處http://lanbitou.blog.51cto.com/9921494/1928884

文件與目錄管理命令(三)