Introduction to Linux file and directory management (with code)

不言
Release: 2019-03-12 17:11:54
forward
3315 people have browsed it

This article brings you an introduction to Linux file and directory management (with code). It has certain reference value. Friends in need can refer to it. I hope it will be useful to you. helped.

Basic commands

Basic commands

pwd command
Function: Display the name of the current working directory
Options: -p Display links The real path

cd command
Function: Switch the current working directory
Commonly used:

cd Switch to the root directory cd - Return to the previous working directory cd.. Switch the working directory Go to the upper level directory of the current directory cd /usr/local Switch the working directory to /usr/local

ls command
Function: Display directory and file information
Common options:

-a Display all files, including hidden files starting with. -h Humanized display capacity information -l Display detailed information of the document in long format -u Display the last time the file or directory was accessed -t Sort by modification time, ls default Sort by file name -R Recursively display subdirectories

touch command
Function: create files or modify file times
For example: $ touch hello.txt
If the hello.txt file does not exist, Then create the file, and if it exists, modify all the times in hello.txt to the current system time.

mkdir command
Function: Create a directory
Options: -p Create a multi-level directory
For example: $ mkdir -p /tmp/data/mysql
If the /tmp/data directory If it does not exist, the /data directory will be automatically created and then the mysql directory will be created.

cp command
Function: Copy files or directories
Options:

-r Recursive, copy sub-files and sub-directories (required when copying directories)-a Keep the source when copying All attributes of the document

rm command
Function: Delete files or directories
Options:

-f Do not prompt, force deletion-i Before deletion, prompt whether to delete-r Recursive Delete, delete the directory and all contents under the directory

Be careful: rm -rf *

mv command
Function: move or rename files or directories
For example:

Rename the file $ mv hello.txt hello.doc Move the file to the specified directory $ mv hello.txt /usr/local/src Move the file to the specified directory and rename it $ mv hello.txt /usr/local/src/ hello.doc

du command
Function: Calculate the size (capacity) of a file or directory
Options:

-h Humanized display of capacity information-a View all directories and files Capacity information -s only displays the total capacity

View file content command

cat command
Function: View file content
Option:

-b Display line number, blank lines do not display line numbers -n displays line numbers, including blank lines

Example:

[root@moli_linux1 ~]$ cat -b hello.txt 
     1    one:hello world

     2    two:hello world
[root@moli_linux1 ~]$ cat -n hello.txt 
     1    one:hello world
     2    
     3    two:hello world
Copy after login

tac command
Function: View the file contents in reverse order (opposite to cat)

[root@moli_linux1 ~]$ tac  hello.txt 
two:hello world

one:hello world
Copy after login

head command
Function: View the file header content, the first 10 lines are displayed by default.
Options:

-c nK displays the contents of the first nKB of the file -n displays the contents of the first n lines of the file

Example: head -5 /etc/passwd displays the contents of the /etc/passwd file 5 lines of content.

tail command
Function: View the content at the end of the file. The last 10 lines are displayed by default.
Options:

-c nK displays the contents of the first nKB of the file -n displays the contents of the first n lines of the file

Example: tail -5 /etc/passwd displays the contents of the /etc/passwd file 5 lines of content.

less command
Function: View file content in pages
Instructions: Press the space bar to view the next page, q key to exit viewing, ctrl b to page up, ctrl f to page down, g to jump to the first line, G jumps to the last line.
Example: less /var/log/jenkins/jenkins.log

more command
Function: View the file content in pages
Instructions: Press the space bar to view the next page, and the q key to exit the view , ctrl b to page up, ctrl f to page down
Example: `more /var/log/jenkins/jenkins.log
`

wc command
Function: Display file lines , word and byte statistics
Options:

-c Display file byte statistics -l Display file line statistics -w Display file word statistics

Example:

[root@moli_linux1 ~]$ wc hello.txt 
 3  4 33 hello.txt
[root@moli_linux1 ~]$ wc -c hello.txt 
33 hello.txt
[root@moli_linux1 ~]$ wc -l hello.txt 
3 hello.txt
[root@moli_linux1 ~]$ wc -w hello.txt 
4 hello.txt
Copy after login

Permission management

The permissions of files under Linux generally include three types of control: read, write, and executable, which can be represented by letters or numbers.

The letters are represented as r, w, x and the numbers are represented as 4, 2, 1

That is, r=4, w=2, x=1.
We can use the ls -l command to view the permissions of a file or directory.

[root@moli_linux1 ~]$ ls -l
总用量 8
drwxr-xr-x. 3 root root   23 6月   6 2018 192.168.229
-rw-------. 1 root root 1538 5月  19 2018 anaconda-ks.cfg
drwxr-xr-x. 3 root root   18 7月  27 2018 git_data
-rw-r--r--  1 root root   33 3月   5 13:37 hello.txt
drwxr-xr-x. 4 root root   33 7月   4 2018 server
drwxr-xr-x  4 root root   68 10月  2 15:48 shell-100
drwxr-xr-x  4 root root   67 11月 16 21:29 test-find
Copy after login

The first character in the first column indicates the file type, and the next 9 characters are the permissions. Some of the last characters in the first column are a dot, and some are not. This dot represents the file. Limited by selinux.
Take the above drwxr-xr-x. as an example:

The first character is the letter d, which indicates that the file type is a directory. Common ones are: the letter l represents a link file, and the letter b or c represents Device, letter s stands for socket file, etc. The second to ninth characters represent permissions. The three groups are the owner (a) permissions, the group (g) permissions, and other users (o) permissions. The permissions of this file are read, write, and executable for the owner, read, executable, and writable for the group it belongs to, and executable, read, and write for other users. The last character is a dot, indicating that it is restricted by selinux.

关于权限的表示可看下表:

数字 字母 文件 目录
4 r 查看文件内容 查看目录下的文件与目录名称
2 w 修改文件内容 在目录下增、删、改文件与目录名称
1 x 可执行,一般为程序或者脚本 可用cd命令进入目录

Linux默认权限

文件默认权限

文件默认最大权限为644 (rw-r--r--)一般会给文件644权限

目录默认权限

目录默认最大权限为755(rwxr-xr-x)一般会给目录755权限

chmod命令

作用:修改文件或者目录的权限
格式:chmod [选项] 权限 文件或目录
选项:-R 递归将权限应用于所有的子目录与子文件
其中权限的表示可用数字或者字母,也可以用a(所有人)、u(代表所有者)、g(所属组)、o(其他用户)。
示例:
环境准备,test目录下有3个文件和1个目录test-1,子目录test-1里有文件hello.txt。各个文件目录权限如下:

[root@moli_linux1 test]$ ll -R
.:
总用量 0
-rw-r--r-- 1 root root  0 3月   7 14:43 1.txt
-rw-r--r-- 1 root root  0 3月   7 14:43 2.txt
-rw-r--r-- 1 root root  0 3月   7 14:43 3.txt
drwxr-xr-x 2 root root 23 3月   7 14:44 test-1

./test-1:
总用量 0
-rw-r--r-- 1 root root 0 3月   7 14:44 hello.txt
[root@moli_linux1 test]$ tree
.
├── 1.txt
├── 2.txt
├── 3.txt
└── test-1
    └── hello.txt

1 directory, 4 files
Copy after login

修改1.txt文件的权限为600

$ chmod 600 1.txt 或者 $ chmod u=rwx,g-r,o-r 1.txt
Copy after login

对1.txt文件的所有人权限添加可执行权限

$ chmod a+x 1.txt 或者 $ chmod u+x,g+x,o+x 1.txt
Copy after login

对test目录下的所有文件设置所有人可读可写

$ chmod -R a=rw test
Copy after login

建议:修改权限能用数字就用数字...

chown命令

作用:修改文件或目录的所有者与所属组
格式:chown [选项] [所有者]:[所属组] 文件名/目录名
选项:-R 递归将权限应用于所有子目录与子文件
使用ls -l命令查看文件或目录的时候,第三列和第四列就是文件或目录的所有者与所属组。

[root@moli_linux1 test]$ ls -l 1.txt 
-rwx------ 1 root root 0 3月   7 14:43 1.txt
Copy after login

比如上面1.txt文件的所有者是root用户,所属组是root组。

修改1.txt文件的所有者为mori,所属组为mail

[root@moli_linux1 test]$ chown mori:mail 1.txt 
[root@moli_linux1 test]$ ll 1.txt 
-rwx------ 1 mori mail 0 3月   7 14:43 1.txt
# 所有者与所属组已改变
Copy after login

umask

Linux默认权限控制,也就是说umask的作用是设置用户创建文件或者目录时,这个文件或目录的默认权限。
在Linux终端输入umask时,出现数字0022这个就是默认的umask值。

[root@moli_linux1 test]$ umask
0022
Copy after login

修改umask    
我们可以通过输入命令$ umask 0032修改系统默认的umask值

[root@moli_linux1 test]$ umask 0032
[root@moli_linux1 test]$ umask
0032
Copy after login

umask反掩码计算
当umask掩码是偶数的时候,默认权限=最大权限-umask值

文件最大权限是666,因此创建文件的默认权限是666-022=644目录最大权限是777,因此创建目录的默认权限是777-022=755

当umask掩码是奇数的时候,计算文件的默认权限的时候,要在奇数位加1
如:umask=032

对于文件:666-032=634,然后奇数位+1,634+010=644对于目录:777-032=745,然后奇数位+1,745+010=755

文件系统权限

设置文件系统权限命令chattr

chattr +a(append) 只能追加chattr +i 无敌,不能进行任何操作

查看文件系统权限命令lsattr
示例:

[root@moli_linux1 test]$ ll 2.txt 
-rw-r--r-- 1 root root 0 3月   7 14:43 2.txt

[root@moli_linux1 test]$ chattr +a 2.txt # 添加隐藏权限

[root@moli_linux1 test]$ echo "hello world" > 2.txt # 不允许重定向
-bash: 2.txt: 不允许的操作

[root@moli_linux1 test]$ echo "hello world" >> 2.txt # 允许追加重定向

[root@moli_linux1 test]$ mv 2.txt helloworld.txt # 不允许移动 
mv: 无法将"2.txt" 移动至"helloworld.txt": 不允许的操作

[root@moli_linux1 test]$ rm -f 2.txt # 不允许删除
rm: 无法删除"2.txt": 不允许的操作

[root@moli_linux1 test]$ lsattr 2.txt  #查看隐藏权限
-----a---------- 2.txt

[root@moli_linux1 test]# chattr -a 2.txt # 去除隐藏权限 

[root@moli_linux1 test]$ lsattr 2.txt  # 没有隐藏权限
---------------- 2.txt
[root@moli_linux1 test]$ rm -f 2.txt # 再次删除,成功
Copy after login

链接文件

Linux中链接文件分为软连接和硬链接。软连接可以跨分区,但源文件不可删除。硬链接不可以跨分区,但可以将源文件删除。
创建软硬链接使用命令ln 
创建软连接:ln -s 文件名 链接名
创建硬链接:ln 文件名 链接名

软连接

软链接就是一个普通文件,只是数据块内容有点特殊。软链接有着自己的 inode 号以及用户数据块。
因此软链接的创建与使用没有类似硬链接的诸多限制:

软链接有自己的文件属性及权限等;可对不存在的文件或目录创建软链接;软链接可交叉文件系统;即,可跨分区。软链接可对文件或目录创建;创建软链接时,链接计数 i_nlink 不会增加;删除软链接并不影响被指向的文件,但若被指向的原文件被删除,则相关软连接被称为死链接(即 dangling
 link,若被指向路径文件被重新创建,死链接可恢复为正常的软链接)。

示例:

[root@moli_linux1 ~]$ ln -s /test/hello.txt /tmp/hi.txt        #创建文件软连接
[root@moli_linux1 ~]$ ln -s /test/ /var/test                #创建目录软连接
[root@moli_linux1 ~]$ rm /test/hello.txt;cat /tmp/hi.txt    #删除源文件后,链接无法使用
Copy after login

硬链接

由于硬链接是有着相同 inode 号仅文件名不同的文件,因此硬链接存在以下几点特性:

文件有相同的 inode 及 data block;只能对已存在的文件进行创建;不能交叉文件系统进行硬链接的创建;不能对目录进行创建,只可对文件创建;删除一个硬链接文件并不影响其他有相同 inode 号的文件。

示例:

[root@moli_linux1 ~]$ ln /test/hello.txt /tmp/hi.txt    #创建文件硬链接
[root@moli_linux1 ~]$ rm /test/hello.txt             #删除源文件后依旧可用
Copy after login

The above is the detailed content of Introduction to Linux file and directory management (with code). For more information, please follow other related articles on the PHP Chinese website!

source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!