Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What does Linux file and directory management mean

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the meaning of Linux file and directory management, the article is very detailed, has a certain reference value, interested friends must read it!

Basic command

Basic command

Pwd command

Function: displays the name of the current working directory

Option:-p displays the real path of the link

Cd command

Function: switch the current working directory

Commonly used:

Cd switch to root directory cd-return to the previous working directory cd.. Switch the working directory to one level above the current directory cd / usr/local switch the working directory to / usr/local

Ls command

Function: display directory and file information

Common options:

-a displays all files, including. The beginning of the hidden file-h humanized display capacity information-l long format displays the details of the document-u shows the time when the file or directory was last accessed-t is sorted by modification time, and ls sorts by file name by default-R recursively displays subdirectories

Touch command

Function: create a file or modify the file time

For example, $touch hello.txt

If the hello.txt file does not exist, the file is created, and if so, all the time of the hello.txt is modified to the time of the current system.

Mkdir command

Purpose: create a directory

Option:-p create multi-level directory

For example, $mkdir-p / tmp/data/mysql

If the / tmp/data directory does not exist, the / data directory is automatically created and then the mysql directory is created.

Cp command

Function: copy a file or directory

Options:

-r recursion, copy subfiles and subdirectories (copy directory must be added)-a copy retains all attributes of the source document

Rm command

Function: delete a file or directory

Options:

-f does not prompt, force delete-I delete, prompt whether to delete-r recursively delete, delete the directory and all the contents under the directory

Be careful: rm-rf *

Mv command

Function: move or rename a file or directory

Such as:

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 capacity information-a view the capacity information of all directories and files-s displays only the total capacity

View file contents command

Cat command

Purpose: view the contents of the file

Options:

-b displays the line number, blank line does not show the line number-n displays the line number, 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

Tac command

Function: view the contents of the file in reverse order (as opposed to cat)

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

Head command

Function: view the high-quality content of the file, and the first 10 lines are displayed by default.

Options:

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

Example: head-5 / etc/passwd displays the first five lines of the / etc/passwd file.

Tail command

Function: look at the end of the file, and the last 10 lines are displayed by default.

Options:

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

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

Less command

Function: view the contents of the file in pages

Note: press the space bar to view the next page, Q key to exit view, ctrl b to turn the page up, ctrl f to turn the page down, g to jump to the first line, G to jump to the last line.

Example: less / var/log/jenkins/jenkins.log

More command

Function: view the contents of the file in pages

Description: press the space bar to view the next page, Q key to exit view, ctrl b to turn the page up, ctrl f to turn the 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

Authority management

The permissions of files under Linux generally include three kinds of control: read, write and executable, which can be expressed in letters or numbers.

The letters are expressed as r, w, x numbers as 4, 2, 1.

That is to say, ringing 4, want2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 2, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4

We can use the ls-l command to view the permissions of a file or directory.

[root@moli_linux1] $ls-l total dosage 8drwxr-xr-x. 3 root root 23 June 6 2018 192.168.229 Murray RWMI. 1 root root 1538 May 19 2018 anaconda-ks.cfgdrwxr-xr-x. 3 root root 18 July 27 2018 git_data-rw-r--r-- 1 root root 33 March 5 13:37 hello.txtdrwxr-xr-x. 4 root root 33 July 4 2018 serverdrwxr-xr-x 4 root root 68 October 2 15:48 shell-100drwxr-xr-x 4 root root 67 November 16 21:29 test-find

The first character in the first column represents the file type, and the next nine characters are permissions, while the last character in the first column is either a dot or not, which indicates that the file is restricted by selinux.

With the above drwxr-xr-x. As an example:

The first character is the letter d, indicating that the file type is a directory. Common ones are: the letter l for linked files, the letter b or c for devices, the letter s for socket files, and so on. The second to ninth characters represent permissions. A group of three is owner (a) permission, group (g) permission, and other user (o) permission. The permissions of this file are owner permissions are readable, writable and executable, group permissions are readable, executable and non-writable, and other user permissions are executable, unreadable and writable. The last character is a dot, which indicates that it is restricted by selinux.

The representation of permissions can be seen in the following table:

Alphanumeric file directory 4r View file content view file and directory name 2w modify file content add, delete, modify file and directory name 1x can be executed, generally programs or scripts can enter the directory with the cd command

Linux default permissions

File default permissions

The default maximum permission for a file is 644 (rw-r--r--). Generally speaking, 644 permissions are given to a file.

Directory default permissions

The default maximum permission for a directory is 755 (rwxr-xr-x). Generally, 755 permissions are given to a directory.

Chmod command

Function: modify the permissions of a file or directory

Format: chmod [options] permission file or directory

Option:-R recursively applies permissions to all subdirectories and subfiles

Permissions can be represented by numbers or letters, or by a (owner), u (for owner), g (group), o (other users).

Example:

Environment preparation, there are 3 files and 1 directory test-1 in the test directory, and the file hello.txt in the subdirectory test-1. The permissions for each file directory are as follows:

[root@moli_linux1 test] $ll-R.: total dosage 0txtqrkashi-1 root root 0 March 7 14:43 1.txtkashi rkashi-1 root root 0 Mar 7 14:43 2.txtMuhashi-1 root root 0Mar 7 14:43 3.txtdrwxr-xr-x 2 root root 23 Mar 7 14:44 test-1./test-1: total dosage 0kw RW- -Rmuri-1 root root 0 March 7 14:44 hello.txt [root @ moli_linux1 test] $tree. ├── 1.txt ├── 2.txt ├── 3.txt └── test-1 └── hello.txt1 directory 4 files

The permission to modify the 1.txt file is 600

$chmod 600 1.txt or $chmod upright RWX Glyrme o Murr 1.txt

Add executable permissions to the owner permissions of the 1.txt file

$chmod axiomx 1.txt or $chmod uprix journal gregxmemogramx 1.txt

Set all files in the test directory to be readable and writable for everyone

$chmod-R a=rw test

Suggestion: modify permissions with numbers as long as numbers are available.

Chown command

Function: modify the owner and group of a file or directory

Format: chown [options] [owner]: [group] file name / directory name

Option:-R recursively applies permissions to all subdirectories and subfiles

When you use the ls-l command to view a file or directory, the third and fourth columns are the owners and groups of the file or directory.

[root@moli_linux1 test] $ls-l 1.txt-rwx- 1 root root 0 March 7 14:43 1.txt

For example, the owner of the above 1.txt file is the root user, and the group to which it belongs is the root group.

Modify the owner of the 1.txt file to mori and the group to which it belongs to mail

[root@moli_linux1 test] $chown mori:mail 1.txt [root@moli_linux1 test] $ll 1.txt-rwx- 1 mori mail 0 March 7 14:43 1.txt# owner and group have changed umask

Linux default permission control, that is, the role of umask is to set the default permissions for a file or directory when the user creates it.

When the Linux terminal enters umask, the number 0022 appears, which is the default umask value.

[root@moli_linux1 test] $umask0022

Modify umask

We can modify the system default umask value by entering the command $umask 0032

[root@moli_linux1 test] $umask0032 [root@moli_linux1 test] $umask0032

Umask inverse mask calculation

Default permissions = maximum permissions-umask value when the umask mask is even

The maximum permission for a file is 666, so the default permission for creating a file is 666-022room644 the maximum permission for a directory is 777, so the default permission for creating a directory is 777-022room755

When the umask mask is odd, add 1 to the odd bit when calculating the default permissions of the file

Such as: umask=032

For files: 666-032mm 634, then odd digits + 163401000644 for directories: 777-03200745, then odd digits + 17450101655

File system permissions

Set file system permissions command chattr

Chattr + a (append) can only append chattr + I invincible and cannot do anything.

View file system permissions command lsattr

Example:

[root@moli_linux1 test] $ll 2.txt-rw-r--r-- 1 root root 0 March 7 14:43 2.txt [root @ moli_linux1 test] $chattr + a 2.txt # add hidden permission [root@moli_linux1 test] $echo "hello world" > 2.txt # does not allow redirection-bash: 2.txt: disallowed operation [root@moli_linux1 test] $echo "hello world" > > 2.txt # allow Heavier Direction [root@moli_linux1 test] $mv 2.txt helloworld.txt # does not allow mv: cannot move "2.txt" to "helloworld.txt": disallowed operation [root@moli_linux1 test] $rm-f 2.txt # does not allow rm: cannot delete "2.txt": disallowed operation [root@moli_linux1 test] $lsattr 2.txt # View Hidden permissions- -a-2.txt [root @ moli_linux1 test] # chattr-a 2.txt # remove hidden permissions [root@moli_linux1 test] $lsattr 2.txt # No hidden permissions-2.txt [root @ moli_linux1 test] $rm-f 2.txt # delete again Success

Link file

Linked files in Linux are divided into soft links and hard links. Soft connections can span partitions, but source files cannot be deleted. Hard links cannot be across partitions, but source files can be deleted.

Create soft and hard links using the command ln

Create a soft connection: ln-s file name link name

Create hard links: ln file name link name

Soft connection

A soft link is an ordinary file, but the content of the data block is a little special. Soft links have their own inode numbers and user data blocks.

Therefore, the creation and use of soft links do not have many restrictions similar to hard links:

Soft links have their own file attributes and permissions; soft links can create soft links to files or directories that do not exist; soft links can cross file systems; that is, they can cross partitions. Soft links can be created for files or directories; when creating soft links, the link count i_nlink does not increase; deleting soft links does not affect the file being pointed to, but if the original file is deleted, the relevant soft links are called dead links (that is, dangling

Link, if the path file is recreated, the dead link can be restored to a normal soft link).

Example:

[root@moli_linux1 ~] $ln-s / test/hello.txt / tmp/hi.txt # create file soft connection [root@moli_linux1 ~] $ln-s / test/ / var/test # create directory soft connection [root@moli_linux1 ~] $rm / test/hello.txt;cat / tmp/hi.txt # after deleting the source file, the link cannot be used

Hard link

Because hard links are files with the same inode number and only different file names, hard links have the following characteristics:

Files with the same inode and data block; can only create existing files; hard links cannot be created across file systems; directories cannot be created, only files can be created; deleting a hard link file does not affect other files with the same inode number.

Example:

[root@moli_linux1 ~] $ln / test/hello.txt / tmp/hi.txt # create file hard link [root@moli_linux1 ~] $rm / test/hello.txt # is still available after deleting the source file. All of the above is what Linux file and directory management means. Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report