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

Linux file directory and management

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Absolute path; the path must be written from the root directory /, for example, / usr/share/doc

L relative path; the path is not written from /, for example, by / usr/share/doc to / usr/share/man, it can be written as "cd … / man" this is how the relative path is written. The relative path refers to the path relative to the current working directory.

The use of relative paths

Some special directories followed by cd

. Representative hierarchical directory

.. Represents the previous level of directory

-represents the previous working directory

~ represents the Zhu folder where "current user identity" is located

~ account represents the home folder of the user account (account is an account name)

Pay special attention to the two directories that will exist under all directories, namely. With; with. Represent the meaning of this layer and the upper directory respectively

Commands created to process directories

Cd: switching directories

Pwd; displays the current directory

Mkdir; create a new directory

Rmdir; deletes an empty directory

Cd (relative or absolute path)

Cd ~ vbird represents going to the home folder of the user vbird, namely / home/vbird

Cd ~ means to go back to your home folder, that is, the directory / root

Cd does not add any path, and it still means to go back to your home folder.

Cd.. It means to go to the current upper directory, that is, the upper directory of / root.

Cd-indicates which directory you went back to

Cd / var/spool/mail is the writing of the absolute path, directly specifying the full path name to go to

Cd.. / mqueue this is the relative path, let's write it from / var/spool/mail to / var/spool/mqueue.

Pwd (shows the directory where it is currently located)

Pwd (- P) shows the path of the schedule instead of using the link path

When the-P parameter is added, the correct full path is displayed instead of the data of the connection file.

Mkdir (create new directory)

Mkdir (- mp) directory name

Parameters.

-p; helps you to create the desired directory (including the upper directory) recursively

Add-p to create a multi-tier directory recursively

-m: the permissions of the configuration file. You do not need to see the default permissions (umask) for this setting.

Mkdir-m

Example; create a new directory with rwx-x-x permission

-m to force permissions for the directory, otherwise the system will use the default permissions

Rmdir-p (delete the "empty" directory)

Parameters.

-p; delete along with the upper "empty" directory

Empty multi-tier recursive directories can be deleted using the-p parameter. This rmdir can only "delete empty directories".

Variable about the path to the execution file: $PATH

Examples

When you use general account A to execute ifconfig, the word "- bash:ifconfig:commandnot found" appears, because ifconfigshi is placed under / sbin, and from the above result, we can also find that the PATH of vbird is not set / sbin, so it cannot be executed by default.

But you can use / sbin/ifconfig eth0 to execute this command, because ordinary users can still use ifconfig to query the parameters of the system IP. Since PATH is not specified to / sbin, we can also execute this command using "absolute path".

U different users with different identities have different default PATH and different commands that can be executed at will by default (such as root and vbird)

U PATH can be modified, so ordinary users can still modify PATH to execute some commands under / sbin or / usr/sbin to query.

U it is more correct to use absolute or relative paths to directly specify the file name of a command than to query PATH

The u command should be placed under the correct directory to make it easier to execute.

U this directory (.) had better not be put in PATH.

File and directory management

In the management of files and directories, it is nothing more than "display properties", "copy", "delete files" and "move files or directories" and so on.

View files and directories: ls

Ls (- aAdfFhilnrRst) directory name

Ls (--color= {never,auto,always}) directory name

Ls (--full-time) directory name

Parameters.

-a; all files, along with hidden files (beginning with. The files are displayed together.

-A; list all files (along with hidden files, but not including. With; with. These two directories)

-d: list only directory capabilities, not file data in the directory

-f: list the results directly without sorting (ls sorts by file name by default)

-F; give additional data structures based on files, directories, etc., such as

*: represents executable file; /: represents directory; =: represents socket file; |: represents FIFO file

-h: list the file capacity in a human-readable way (such as GB,kb, etc.)

-I: list the inode number

-l: lists long data strings, including file attributes, permissions and other data

-n; list UID and GID; instead of the names of users and user groups (UID and GID will be mentioned in account management)

-r: output the sort result in reverse, for example, the original file name changes from small to large, while the reverse changes from large to small.

If it is listed together with the contents of the subdirectory, all the files in that directory will be displayed.

-S; sort by file capacity size, not by file name

-t: sort by time, not by file name

-- color=never: do not give color display based on the characteristics of extra years

-- color=always: display color

-- color=auto: let the system judge whether or not to give colors according to the settings

-- full-time: output in full time mode (including year, month, day, hour, minute)

-- time= {atime,ctime}; output access time or change permission attribute event (ctime) instead of content change time

Case one

List all files under the home folder (including attributes and hidden files)

-an all files, along with hidden files (beginning with. File of)

-l lists long data strings, including file attributes, permissions and other data

Following the above question, the color is not displayed, but the type represented by the file name is shown at the end of the file name.

Example 3: the modification time of the complete presentation file

The correct complete time format can be found through-full-time.

Copy, delete, and move: cp,rm,mv

To copy a file, you can use the command cp (copy), you can also create a connection file (shortcut), compare the old and new files with the update, and copy the entire directory, etc. As for moving directories and files, use mv (move), this command can also be directly renamed, as for delete is the rm (remove) command

Cp (copy a file or directory)

Cp (- adfilprsu) source file (source) destination file (destination)

Cp (options) source1 source2 source3... Directory

Parameters.

-a: quite equivalent to-pdr, as for pad, please refer to the following (commonly used)

-d: if the source file is the attribute of the connection file (link file), copy the attribute of the connection file instead of the file itself

-f: for force, if the target file already exists and cannot be opened, delete it and try again

-I; if the target file (destination) already exists, the operation will be asked first when overwriting (commonly used)

-l: create a connection file for a hardware connection (hard link) instead of copying the file itself

-p; copy it along with the properties of the file instead of using the default attributes (commonly used for backup)

-r; recursive continuous replication for directory replication behavior (commonly used)

-s; copy as a symbolic link file (symbolic link), that is, a "shortcut" file

Update destination if destination is older than source

Finally, it is important to note that if there are more than two source files, the last destination file must be a "directory"!

The cp command is very important. People with different identities will produce different results when executing this command. In particular, the parameters of-a _ mai _ r _ p are very different for different identities.

Just add-a to copy the file and its features.

Cp / etc to / tmp directory

If it is a directory, it cannot be copied directly. Add the parameter-r.

Cp-r / etc / tmp

-r you can copy directories, but the permissions of files and directories may be changed

So, you can also use-a to execute commands, especially in the case of backup

Cp-s bashrc bashrc_slink

Cp-l bashrc bashrc_hlink

Ls-l bashrc*

Using-s-l creates a so-called connection file

-l is the so-called hard link. Bashrc_shlink is a "shortcut," which connects to bashrc, so you will see a symbol pointing to (- >) on the side of the file name.

The attributes and permissions of bashrc_hlink file and bashrc are exactly the same, and the difference between them is that the number of link in the second column has changed from 1 to 2.

If ~ / .bashrc is newer than / tmp/bashrc.

Cp-u ~ / .bashrc / tmp/bashrc

The feature of this-u is copied only when the target file is different from the source file.

Therefore, it is more commonly used with "in the work of backup"

Rm (remove files or directories)

Rm (- fir) file or directory

Parameters.

-f: it means force. Ignore files that do not exist and there will be no warning messages.

-I; interactive mode, which asks the user whether to operate or not before deletion

-r; recursive deletion, which is most commonly used in directory deletion, which is a very dangerous parameter!

Rm-I bashrc*

Delete all the file names that begin with bashrc in the directory

* represents 0 to infinitely many arbitrary characters

Delete the / tmp/etc/ directory created in the cp example

Rmdir / tmp/etc

Can't delete it because it's not an empty directory!

Rm-r / tmp/etc

Press y continuously to delete

If you don't want to press y

\ rm-r / tmp/etc

By adding a backslash before the command, you can ignore the specified parameters of alias

Mv (move files or directories, or rename)

Mv (- fiu) source destination

Mv (options) source1 source2 source3... Directory

-f:force force means that if the target file already exists, it will be overwritten without being asked.

-I: if the target file (destianation) already exists, you will be asked whether to overwrite it

-u: if the target file already exists and the source is relatively new, it will be updated (update)

Basename / etc/sysconfig/network

Network is simple, just get the last file name

Dirname / etc/sysconfig/network

/ etc/sysconfig gets the directory name

Consult the contents of the document

You can use cat and more, namely less, to display the contents of the file.

U Cat; displays the contents of the file from the first line

U Tac; shows from the last line that tac is the inverted form of cat.

When u nl; is displayed, output the line number by the way

U more; displays the contents of the file page by page

U less; is similar to more, but better than more, it turns the page forward.

U head; only looks at the first few lines

U tail; only looks at the last few lines'

U od; reads the contents of the file in binary mode

Cat (- AbEnTv)

Parameters.

-A: an integration parameter equivalent to-vET, which lists some special characters instead of whitespace

-b; list line numbers. Line numbers are displayed only for non-blank lines. Blank lines are not marked with line numbers.

-E; display the line break character $at the end

-n; print the line number, which will also have a line number together with a blank line, which is different from the parameter of-b

-T: display the (Tab) button as ^ I

-v: list some unrecognizable special characters

More (page by page)

More / etc/man.config

Space bar: represents turning down one page

Enter: stands for scrolling down one line

/ string; represents that in this display, query down the keyword "string"

: F immediately show the file name and the number of lines currently displayed

Q means to leave more immediately and no longer display the contents of the file.

B or (ctrl)-b means to turn the page back, but this operation is only useful for files, not for pipes.

With more, you can't turn the page forward, but with less, you can use functions such as the up and down keys to flip through the files.

Spacebar: flip down

Pagedown turns a page down

Pageup flipped up all night.

The function of / string to query down "string"

? The function of / string to query "string" up

N repeat the previous query (and / or? Related)

N reverses the previous query (and / or? Related)

Q leave the less program

Data selection

We can make a simple selection of the output data, that is, the function of head and tail text, but both head and tail select data on the basis of behavior units.

Head (take out the first few lines)

Head (- n number) file

-n: followed by a number, which means how many lines are displayed

Tail (take out the next few lines)

Tail-n number file

The number of number lines in the last few lines of the file

Time when the file was modified or a new file was created: touch

Three main time changes under Liunx

Modification time

This time is updated when the content data of the file changes, which refers to the contents of the file, not the properties or permissions of the file

Status time

When the state of the file changes, the time is updated, for example, when permissions and properties are changed.

Access time

The read time is updated when the contents of the file are accessed. For example, if we use cat to read / etc/man.config, we will update the atime of the file

Touch (- acdmt) file

Parameters.

-a: only modify the access time

-c: only the time when the file was modified, if the file does not exist, no new file is created

-d: you can follow the date you want to modify instead of the current date, or you can use-date= "date or time"

-m: modify only mtime

-t: you can change the time you want later instead of the current time

A bashrc file was created at 2:02 on 2007-09-15

Touch-t 0709150202 bashrc

-t is followed by the time of modification

Through touch, you can easily modify the date and time of the file, and you can also create an empty folder to see when the file has been changed to view the ctime, even if it is a copied file, there is no way to modify this time.

Create an empty file

Modify a file date to the current date (mtime and atime)

In addition to rwx permissions, we can also set other system hidden properties under linux's Ext2/Ext3 file system. This part can be set using chattr, while viewed by lsattr, the most important property is that it can be set immutable, so that even the owner of the file cannot be modified. This attribute is very important, especially in terms of security mechanisms.

Create a directory under / tmp named qwe and all this is dmstai, and the user group is users, and anyone can enter the directory to browse files, but no one can modify the files in this directory except dmstai

Create a new directory: Mkdir / tmp/qwe

Modify attribute: Chown-R dmstai:users/tmp/qwe

Modify permission: chmod-R 755 / tmp/qwe

File default attribute: umask

Umask is specified; the default value of the user's permissions when creating a new file or directory

022 permissions for anti-mask

If you enter umask directly, you can see the permission setting score of the digital form. If you add the-S parameter, it will be displayed as a symbol type. The first group is for special permissions.

-if the user creates a "file", there is no executable permission (x) by default, that is, there are only two options: rforce w, that is, a maximum of 666. The default permissions are as follows

-rw-rw-rw-

-if the user creates a new "directory", since x is related to whether you can enter this directory, all permissions are enabled by default, that is, 777 points. The default permissions are as follows.

-Drwxrwxrwx

Umask has a lot to do with default permissions for new directories and files.

The permissions of umask can be modified by umask 002.

File hiding property chattr,lsattr

The following chattr only works on the file systems of ext2 and ext3

Chattr (set the hidden properties of the file)

Chattr (+-=) (ASacdistu) file or directory name

Parameters.

+: add a special parameter, while other existing parameters remain unchanged

-: delete a special parameter, while other existing parameters remain unchanged

=: only the parameters that follow

A: when the property An is set, if you have access to this file (or directory), his access event atime will not be modified, which can prevent slower machines from overaccessing the disk, which is helpful to slower computers.

S: generally, files are written to disk asynchronously. If you add the attribute S, when you make any changes to the file, the action will be synchronized to disk.

A: when an is set, this file can only add data, not delete or modify data. Only root can set this property.

C: once this property is set, the file is automatically compressed and decompressed when it is read. However, when storing, it will be compressed first and then stored (effective for large files).

D: when the dump program is executed, setting the d property will prevent files (or directories) from being backed up by dumnp

I: this I it can make a file "cannot be deleted, renamed, set connection cannot write or add data" is very helpful for system security, only root can set this property.

S: if the file is deleted when the s property is set, it will be deleted completely from the hard disk space

U: on the contrary, when u is used to set up a file, if the file is deleted, the data contents are actually stored on disk and can be used to retrieve the file

Note: property settings are common for an and I settings, and many setting values must be set as root.

I on the data security group of the system, because these attributes are hidden, you need to use lsattr to see this attribute, the most important of which are the attributes + I and + a. + I can make a file impossible to understand.

In addition, if it is a login file like logfile, you need + a, which can add but not modify the old data and delete parameters.

Lsattr (Show File Hidden Properties)

Lsattr (- adR) file or directory

Parameters.

-a; show the properties of the hidden file as well

-d; if you are following a directory, only the attributes of the directory itself are listed, not the file names in the directory

-R; the data from the same subdirectory is also displayed.

After using the chattr setting, you can use lsattr to view hidden properties

Special permissions for files, SUID,SGID,SBIT

SetUID appears

When the s flag appears on the x permission of the file owner, such as the above permission, it is called set UID, abbreviated as the special right of SUID. Suid has such restrictions and functions.

L SUID permissions are valid only for binary programs

The executor needs to have the executable permission of x for the program

This permission is valid only during the execution of the program.

L the executor will have the authority to change all the rules of the program

SUID can only be used on binary programs, but not on shell script. This is because shell script just calls a lot of binary files to execute, so the permission part of Sudi still depends on the program settings called by shell script, not shell script itself. Of course, SUID is invalid to the target.

Set GID

When the s flag is suid in the x project of the file owner, then s is called set GID,SGID in the x of the user group.

View files with SGID permissions

Unlike SUID, SGID can be set for files or directories. If it is for files, SGID has the following functions

L SGID is useful for binary programs

The executor of the program needs to have the permission of x for the program.

L the executor will get the support of the program user group in the process of execution.

StickyBit

This Sticky Bit (SBIT) is currently only valid for directories and has no effect on files. The role of SBIT for directories is

When the user has the permission to write to this directory if he or she has the permission to write to this directory

When a user creates a file or directory under this directory, only himself and root have the right to delete the file

SUID/SGID/SBIT permission Settings

4 is SUID

2 is SGID

1 is SBIT

L example

Suppose you want to change a universal key permission to-rwsr-xr-x, because s is in the user's right, so it is GID, so add 4, that is, "chmod4755 filename" to set it before the original 755. In addition, there are the generation of big S and big T, see below

L touch test creates an empty file for testing

Chmod 4775 test; join with SUID permission

Ls-l test view

L chmod 6755 test; join with SUID/SGID permission

Ls-l test view

L chmod 7666 test; has empty SUID/SGID permissions

Ls-l test

St replaces the x permission because it executes 666 and does not have the permission to execute.

Symbols instead of

SUID is upright, SGID is glossary, SBIT is ofrot.

Multiply, add SGID and SBIT to the above file permissions

View file types; file

File; is followed by the files you need to view.

Query of commands and files

Query for script file name

Which (looking for "execution file")

Which (- a) command

Parameters.

-a: list all commands that can be found in the PATH directory instead of the name of the first command to be found

This command queries the file name of the "execution file" according to the path specified by the environment variable PATH. So, the point is to find the execution file, and Which is followed by the full file name. If you add the-a parameter, you can list all the executable files with the same name that can be found instead of just the first one.

L Which defaults to finding directories that are specified in PATH.

Lookup of file name

Whereis (looking for specific files)

Whereis (- bmsu) file or directory name

Parameters.

-b; only look for files in binary format

-m; only look for files under the manual path of the description file

-s; only look for source source files

-u; find other special files that are not among the above three options

The data found in Locate (the data found is from the created database / var/lib/mlocate)

Locate (- ir) keyword

-I: ignore case differences

-r: the way in which regular expressions are displayed

Example; find out all passwd-related file names in the system

Locatepasswd

It is found through the database, and the creation of the database is performed once per machine by default, and the database updatedb is updated manually

Updated; looks up the file name in the system hard disk according to the setting of / etc/updated.conf, and updates the database file in / var/lib/mlocate

Locate; finds out the keyword file name entered by the user based on the database records in / var/lib/mlocate

Find

Find (PATH) (option) (action)

Parameters.

1. The parameters related to practice are-atime,-ctime and-mtime, which are described as-mtime below.

-mtime n: a number that means a file that has been changed "within one day" before n days

-mtime + n: lists the file names that have been changed before n days (excluding the n days themselves)

-mtime-n; lists the file names that have been changed within n days (including the n days themselves)

-newer file: file is an existing file that lists filenames newer than file

Find /-mtime 0

0 represents the current event, so the universal keys that have changed the content will be listed from the beginning to 24 hours before you start, if it is within 24 hours 3 days ago? Fine

/-mtime 3 means that all files that have been changed are listed

Look for the file under / etc and list it if the file date is newer than / etc/passwd

Find / etc-newer / etc/passwd

-newer is useful to distinguish between old and new relationships between two files

Find out the file name that has been changed in 4 days.

Find / var-mtime-4

Find out the day four days ago

Find/var-mtime 4

Did you add +?-there's a big difference.

+ 4 represents a file name greater than or equal to 5 days ago; find / var-mtime + 4

-4 represents a file name less than or equal to 4 days ago; find / var-mtime-4

4 is for 4-5 to mention the file name of your day: find / var-mtime 4

Parameters.

Parameters related to the user or user group name

-uidn: n is a number, which is the user's account number ID. That is, UID. This UID is the number recorded in / etc/passwd corresponding to the account name.

-gidn: n is a number, which is the ID of the user group name. That is, GID, this GID is recorded in / etc/group

-username: name is the user group name, such as users

-nouser: looking for someone whose owner of the file does not exist / etc/passwd

-nogroup: look for files where all user groups of files do not exist in / etc/group. When you install the software yourself, it is possible that there is no file owner in the properties of the software. At this time, you can use-nouser and-nogroup to find files.

Find the files under / home that belong to vbird

Find/home-user vbird

When we want to find all the files of any user in the system, we can use this command to find all the files that belong to a user.

Find files in the system that do not belong to anyone

Find /-nouser

With this command, you can easily find out which files are not normal.

Parameters related to file permissions and names

-name filename; looks for a file named filename

-size (+ -) SIZE; looks for files larger (+) or smaller (-) than SIZE. The specifications of this size are: C: for byte;k: for 1024bytes. So to find a file larger than 50kb, it is "- size + 50k".

-typeTYPE: find attributes such as general formal file (f), device file (bjournal c), directory (d), connection file (l), socket (s), and fifi (p)

-permmode; look for files whose file permissions are "exactly equal" to mode, this mode and a property value similar to chmod, for example, the attribute of-rwsr-xr-r- is 4755

-perm-mode; find files whose file permissions "must include all mode permissions". For example, if we want to find-rwxr--r--, that is, 0744 files, use-perm- 0744, when a file's permission is-rwsr-wr-x, that is, 4755, it will also be listed, because the attribute of-rwsr-xr-x already includes the attribute of-rwxr-r-.

-perm+mode: find files with file permissions that "contain arbitrary mode permissions". For example, when we look for-rwxr-xr-x, that is,-perm+ 755, but a file attribute of-rw- will also be listed because it has-rw... The property of exists

Example

Find the file named passwd

Find/-name apsswd

Use this-name to find the file name

Find out what the file names of file type Socket are in the / var directory.

Find/var-type s

Find attributes that contain SGID or SUID or SBIT in the file

Find/-perm + 7000

The so-called 7000 is s--s-t, so whatever contains s or t is listed.

So of course you have to use + 7000. Using-7000 means to include all three permissions of-s-s-t

So it's + 7000.

List any files in the / bin directory that have SUID or SGID

Find/bin-perm + 6000

Because SUID is 4 and SGID is 2, a total of 6, you can use + 6000 to handle this permission.

Other actions that can be done

-execcommand:command is another command, and-exec can be followed by other commands to process the results found.

-print: print the result to the screen. This is the default operation.

List the files found in the previous example using ls-l

Find/-perm + 7000-exec ls-l {}\

-perm is followed by permissions for SUID and SGID

-exec is followed by additional commands such as ls-l

{} represents "content found by find", and the result obtained by find will be placed in the {} position.

-exec until "\;" is the keyword, representing the beginning of the find extra command (- exec) to the end (/;) in the middle is the extra command within the find command, in this case ls-l {}

Because ";" has a special meaning in the bash environment; therefore, it uses a backslash to escape.

Find files in the system that are greater than 1MB

Find/-size + 1000k

Find the file under / etc whose filename contains httpd

Find/etc-name'* httpd*;'

You can not only specify the directory to look for (with subdirectories). And, uh, you can use additional parameters to find the most correct file name.

The relationship between permissions and commands (extremely important)

Permissions are very important for user accounts because they limit whether users can read / create / delete / modify files or directories. Combine the two in this section to explain what commands can be run under what permissions.

L basic permission to enable users to access a directory to become a "working directory"

Commands that can be used; for example, commands such as cd to switch working directories

The permissions required by the l directory; the user needs to have at least x permissions for this directory

L additional requirements, if the user wants to use ls to look up the file name in this directory, the user also needs r permission to this directory.

What is the basic permission for a user to read a file in a directory

L commands available

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

Database

Wechat

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

12
Report