1. 程式人生 > >檔案目錄管理1

檔案目錄管理1

1.System Category Structure

ls = list

useradd XXX (home category is /home)

authorized_keys must be under .ssh (stipulated by sshd configuration ----- ls /etc/ssh/sshd_config)

1.1 Catalogue

1.1.1 bin & sbin

command under sbin is basically used by root account and bin is used by normal account (sbin means super bin)

儲存於系統的常用命令,sbin下儲存的為僅供root使用者使用的命令,其他使用者不能用

1.1.2 boot

系統啟動相關的檔案

boot up related files (grub- like singleuser mode)

1.1.3 dev

系統特有的一些裝置檔案

device files(mouse, keyboard etc..)

1.1.4 etc

system configuration files (Network card setup files: ls /etc/sysconfig/network-scripts/ifcfg-ens33 & ls /etc/ssh/sshd_config)

Excerpted from https://zhidao.baidu.com/question/398779738.html

1.1.5 home

home category of users

1.1.6 lib & lib 64

for dll (Dynamic Link Library)

 

Excerpted from https://baike.baidu.com/item/%E5%BA%93%E6%96%87%E4%BB%B6/2550595

1.1.7 proc

Excerpted from https://www.cnblogs.com/DswCnblog/p/5780389.html

1.1.8 run

Temp file when system progressing, only exsit before system reboot

1.1.9 sys

core related files

1.1.10 tmp

temp directory

1.1.11 usr

user files

自己編譯的一些檔案會放在/usr/local

1.1.12 var

Excerpted from https://zhidao.baidu.com/question/398779738.html

/var/log : log files

2. Frequent command

2.1 LS

*inode please refer to https://www.cnblogs.com/itech/archive/2012/05/15/2502284.html

2.2 CD

cd - alternate to previous command

cd move to the home directory of current directory

cd ~ go back to user's home directory

cd .. go back to previous directory

2.3 MV

file move and name change

*如果在同一個目錄下那就是改名字,如果目標的檔案已經存在就會問是否會覆蓋,如果目標是一個目錄且目錄不存在它就會更該目錄名字,如果目標是目錄源也是一個存在的目錄那它就會放在目標目錄裡面去

2.4 mkidr/ rmdir and rm

&mkdir -p 級聯建立

if directory 1/2 needed to be created simultaneously, try to use -p after mkdir/rmdir, (-pv for visually)

&touch for file creating

touch /tmp/1/2/1.txt

&rm unsed to remove the files,

use -f after rm command and skip the notification and delete directly

notes ! command also can be used, refer to most recent command in history

!$ refer to the last character in most recent command in history

&rmdir only can be used as blank directory removal, so normally we dunt use it, and we use rm instead

&rm -r to remove all directory and files

2.5 CP

把一個檔案複製到一個目標檔案中,而且目標檔案已存在,該目標檔案內容將被破壞,此命令中所有引數可以是絕對路徑或相對路徑通常以。或。。的形式.

 

eg. cp ../fred/222/1.txt .

cp指定的目錄必須是已經存在的,cp無法建立目錄

cp aaa/* /bbb :複製目錄aaa下所有到/bbb下

拷貝一個目錄時,左邊目錄會放在右邊的目錄下,不存在是會改名字

 

cp A B

cp -r if copy a directory

2.6 File related file command.

2.6.1 Cat check file details

to add contents from cat /etc/passwd into anaconda-ks.cfg.1 追加重定向

2.6.2 tac check file details reversely

2.6.3 more check file details by screen (space move forward, CTRL+B move back)

2.6.4 less command more like "more". but can use search under less (N for next page)

example /cdrom ?cdrom search from bottom to up ("n"鍵會追蹤下一個符合搜尋選項的詞)

shift g and G, set to front row and end row (g定位到行首和行尾)

 

2.6.5 wc -l check how many rows they have in a file

2.6,6 head check a file with top 10 rows

2.6.7 tail check a file with last 10 rows

tail -n 2 anaconda-ks.cfg.1 check the last 2 rows

tail -f real-time update the latest info

2.7 alias

alias命令可以檢視系統中所有的alias

Add new command (fred) into alias: alias fred='ls -lha' / unalias to cxl the comand add-in

如何簡化 /root/123/abc 命令

alias fred="/root/123/abc"

ln /root/123/abc /bin/abc

加入環境變數path

2.8 Find 命令*****

其他搜尋檔案命令: which/ whereis(只有更新後的檔案才能找到)/ locate(未安裝, yum install -y mlocate-需要每天生成 updatedb手動生成)

Please input text here

檔案種類有很多: d, f, l, s, c and b

find / -mtime(modify), -ctime(change) and -atime(access)

mtime 是指更改檔案內容,ctime,是指更改許可權。 更改檔案內容mtime,ctime一定會隨之變化

cat 1.txt 訪問1.txt內容

state + file 檢視檔案3個time

檔案的 Access time,atime 是在讀取檔案或者執行檔案時更改的。

檔案的 Modified time,mtime 是在寫入檔案時隨檔案內容的更改而更改的。

檔案的 Create time,ctime 是在寫入檔案、更改所有者、許可權或連結設定時隨 Inode 的內容更改而更改的。

eg. 如果更改許可權則對應Ctime會改變,如果增加內容,則ctime/mtime會改變

+1/_1 表示檔案更改時間一天以外/以內,同時也可以嘗試按分鐘搜尋(同理+/-10k 也是按檔案大小搜尋)

 

通過inode搜尋硬連結檔案

搜尋某檔案目錄並list 出來(針對於某種需求就是要某檔案)

Fine總結

(1)Find工具-name引數案列:

find /data/ -name "*.txt" #查詢/data/目錄以.txt結尾的檔案;

find /data/ -name "[A-Z]*" #查詢/data/目錄以大寫字母開頭的檔案;

find /data/ -name "test*" #查詢/data/目錄以test開頭的檔案;

(2)Find工具-type引數案列:

find /data/ -type d #查詢/data/目錄下的資料夾;

find /data/ ! -type d #查詢/data/目錄下的非資料夾;

find /data/ -type l #查詢/data/目錄下的連結檔案。

find /data/ -type d|xargs chmod 755 -R #查目錄型別並將許可權設定為755;

find /data/ -type f|xargs chmod 644 -R #查檔案型別並將許可權設定為644;

(3)Find工具-size引數案列:

find /data/ -size +1M #查檔案大小大於1Mb的檔案;

find /data/ -size 10M #查檔案大小為10M的檔案;

find /data/ -size -1M #查檔案大小小於1Mb的檔案;

(4)Find工具-perm引數案列:

find /data/ -perm 755 #查詢/data/目錄許可權為755的檔案或者目錄;

find /data/ -perm -007 #與-perm 777相同,表示所有許可權;

find /data/ -perm +644 #檔案許可權在644以上;

(5)Find工具-mtime引數案列:

atime,access time 檔案被讀取或者執行的時間;

ctime,change time 檔案狀態改變時間;

mtime,modify time 檔案內容被修改的時間;

find /data/ -mtime +30 -name "*.log" #查詢30天以前的log檔案;

find /data/ -mtime +30 -name "*.log" #查詢30天以前的log檔案;

find /data/ -mtime 30 -name "*.txt"#查詢第30天的log檔案;

find /data/ -mmin +30-name "*.log" #查詢30分鐘以前修改的log檔案;

find /data/ -

-30 -name "*.txt" #查詢30分鐘以內被訪問的log檔案;

find /data/ -cmin 30 -name "*.txt"#查詢第30分鐘改變的log檔案。

(6)Find工具引數綜合案列:

#查詢/data目錄以.log結尾,檔案大於10k的檔案,同時cp到/tmp目錄;

find /data/ -name "*.log" –type f -size +10k -exec cp {} /tmp/ /;

#查詢/data目錄以.txt結尾,檔案大於10k的檔案,許可權為644並刪除該檔案;

find /data/ -name "*.log" –type f -size +10k -m perm 644 -exec rm –rf {} /;

#查詢/data目錄以.log結尾,30天以前的檔案,大小大於10M並移動到/tmp目錄;

find /data/ -name "*.log" –type f -mtime +30 –size +10M -exec mv {} /tmp/ /;

3. File type

capital letter

"d" for directory

"-" for normal file (can be checked by "cat" command directly)

"c" for character device (yellow color), mouse keyboard, tty file

Excerpted from https://baike.baidu.com/item/%E5%AD%97%E7%AC%A6%E8%AE%BE%E5%A4%87/6637035

"b" for block device (disk)

“s” for socket file. communication, data transmission

"l" for symbolic link. 符號連結 又稱軟連線,包含了另一個檔案的絕對路徑或者相對路徑

Excerpted from https://baike.baidu.com/item/%E5%A5%97%E6%8E%A5%E5%AD%97/9637606?fr=aladdin

 

4. Path

Absolute Path & Relative Path

 

Excerpted from https://baike.baidu.com/item

簡單理解絕對路徑就是從根開始的就是絕對路徑,相對路徑就是相對當前位置的路徑

if your command is under one of below directory, we dunt need to input absolute Path

How to add /tmp/ into PATH? (temprorary)

PATH=/usr/local/sbin:/usr/local/bin:/us/sbin:/usr?bin:/root/bin:/(tmp/)通過重新定義PATH=來臨時更改環境變數

How to add /tmp/ into PATH? (permenant)

5. Authority

5.1 File & Directory Auth chmod

r-read w-write x-executable

eg.

前三位為所有者許可權user:可讀寫不可執行

中間三位為所屬組許可權group:僅可讀

後三位為其他使用者許可權other:僅可讀

r=4 w=2 x=1, rw-r--r-- =644

change auth for 2.txt

chmod -R 700 fred revise all sub directories under fred

chmod a/g/o/u+x fred/ 會把fred目錄的所有/所有者/所屬組/其他使用者許可權賦予可執行

5.2 Chown -change owner

chown and chgrp

chown -R 選項級聯的更改所選目錄以及子目錄/檔案的所有者所屬組

change owener and group

chwon :user1 XXX 命令可以直接用來修改組,類似於命令 chgrp user1 xxx

5.3 Umask

新建立的檔案許可權為644,而新建立的目錄許可權為755

所有的目錄都會有x(可執行)許可權,因為只有可執行才可以瀏覽,這裡與檔案不一樣

umask 022 by default , directory = 777 file =666

Excerpted from https://baike.baidu.com/item/umask/6048811