文件目录管理

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

5.4 Chattr

常用参数:

-R:递归处理,将指定目录下的所有文件及子目录一并处理。

+ :在原有参数设定基础上,追加参数。

- :在原有参数设定基础上,移除参数。

i:设定文件不能被删除、改名、设定链接关系,同时不能写入或新增内容。i参数对于文件

a: 即append,设定改参数后只能向文件中添加数据,而不能删除,多用于服务器日志文件安全,只有root才有这个属性

chattr +/- i 2.txt

chattr +/-a 2.txt only possible to add on, cannot be revised (can use touch to update time)

chattr is to set the auth and lsattr is to check the auth

chattr +i cannot make further update for both file and directory, but +i/+a both can be used for sub files from directory

chattr +i cannot make further update for both file and directory

同时加了i隐藏权限也不可以被删除

同样无法通过touch更改时间

chattr +a

as for file, cannot revise but possible for add on: head -n2 /etc/passwd >> 1.txt and use touch command to for date update

as for directory, not possible to revise directory but possible to revise the sub files under this directory.

Lsattr -R 111: to check all sub directories and files under sub directories.

lsattr -a 显示所有目录/文件权限,包括隐藏的

5.5 set_uid

使普通用户拥有所有者的权限

rws s means auth for set_uid

password file

how to set uid? (temp set /usr/bin/ls as root autority)

chmod u=rws /usr ----- rwS(withouth x auth, need to add x as chmod u+x /usr/bin/ls)

5.6 set_gid

使普通用户拥有所属组的权限

temp set /usr/bin/ls as group authority 让执行这个文件的普通用户临时拥有所属组的权限

*note: 当作用在目录的时候,当创建子目录或者子文件其所属组和该所属组保持一致

5.7 Stick_bit

防删除位,例如/tmp/的rwt("t")权限

*user1目录下的1.txt是否有写权限,不是决定于删除文件本身权限,而是决定于它所在的目录,所以他能被删除(要删除1.txt要看其所在目录的权限)

6. 软连接Symbolic Link & 硬链接 Hard Link

further explaination: https://blog.csdn.net/u013982161/article/details/52434309

6.1 软连接

是指一个文件里面存了另一个文件的路径/目录同理(节省空间),可以软链接文件,目录并且可以跨分区

eg. /bin 就是一个软连接文件,他应该在usr/bin下面

如何做软链接

也可以为目录做软链接

以下软链接(相对路径的软链接)仅仅是在当前目录下,(不推荐)

**假如你把所有文件拷贝到另一个文件夹里,你会发现文件不存在。 改了一个名字就会导致软链接初见问题,所以软链接尽量使用绝对路径

如何解决服务占用磁盘空间的问题

随着文写的写入/boot下面某文件会越来越大,解决方法是先把文件拷贝到剩余空间大的分区中去,并做软链接到/boot 下面,路径为原路径

6.2 硬链接

硬链接不支持对目录做硬链接,只支持对文件做硬链接,不能跨分区

创建了一个文件,新文件与源文件共用一个inode并且相互为硬链接,硬链接删除源文件无影响,软链接会报错,*硬链接并不会占用多余空间

软链接和硬链接比较

硬链接:其实就是同一个文件具有多个别名,具有相同inode,而dentry不同。

1. 文件具有相同的inode和data block;

2. 只能对已存在的文件进行创建;

3. 不能交叉文件系统进行硬链接的创建

4. 不能对目录进行创建,只能对文件创建硬链接

5. 删除一个硬链接并不影响其他具有相同inode号的文件;

软链接:软链接具有自己的inode,即具有自己的文件,只是这个文件中存放的内容是另一个文件的路径名。因此软链接具有自己的inode号以及用户数据块。

1. 软链接有自己的文件属性及权限等;

2. 软链接可以对不存在的文件或目录创建;

3. 软链接可以交叉文件系统;

4. 软链接可以对文件或目录创建;

5. 创建软链接时,链接计数i_nlink不会增加;

6. 删除软链接不会影响被指向的文件,但若指向的原文件被删除,则成死链接,但重新创建指向 的路径即可恢复为正常的软链接,只是源文件的内容可能变了。

7. Shutcut Key Consolidate

CTRL-L 清屏

CTRL-D 退出终端=log out=EXIT

CTRL-C 撤销当前命令

CTRL-U 删掉前面的全部输入

CTRL-E 会把光标移动到最后面去

CTRL-A 把光标移动到最开始

8. Linux 和window 文件互传

普通Linux和Windows之间的文件共享方法,主要有建立nfs实现文件共享,和tftp之类的方法,但是都很麻烦,而如果只是小文件(几十 K,几百K),那么直接用rz/sz,就显得极其地方便了

注意要在xshell的环境下

yum install -y lrzsz

如下图显示SZ 和RZ 的区别

注意:单独用rz会有两个问题:上传中断、上传文件变化(md5不同),解决办法是上传是用rz -be,并且去掉弹出的对话框中“Upload files as ASCII”前的勾选。

猜你喜欢

转载自blog.csdn.net/nongfuchui/article/details/83271008
今日推荐