A few common concepts about the Linux directory
- Path
The description mechanism of the file location information refers to a path from a directory hierarchy in the tree directory to a file within it, and is divided into relative paths and absolute paths;
- working Directory
Once logged into the system, the user is always in a directory, which is the working directory, or the current directory
- root directory
Top-level directory of the Linux tree directory structure
- user "Home" directory
Also known as the user home directory, usually located in the/s directory with the user name of the same directory, the user logged into the system by default in this directory
Path-related
Description: Path is a pathway from one directory to another directory or file, identified by the name of the directory at each level
- Absolute Path
Path from root directory/start to end of file name
Example:/usr/local/tomcat7/logs/catalina.out
- relative Path
Path not starting from the root directory
For example: CD. /catalina.out
several special symbols
. Represents the current path
.. Represents the upper level directory
~ Represents the user root directory
~user represents user's root directory
When the system logs in, the user will automatically jump to their home directory, the root user's home directory is/root, and the other user's default home directory is/home/user
Environment variable Path
The role of the environment variable path is to provide a possible path to the user to find the file
When a new suite is installed, you may need to write the path of the executable to the environment variable
View the Path method
Echo $PATH
File, Directory Operations Command
Overview:
Directory Operations Command
ls pwd cd mkdir rmdir
text File View commands
cat more less head tail
File Operations Command
file Touch CP RM MV
file Content Statistics command
WC
File Link Command
ln
1. Directory Operations Command
ls
command |
function |
|
pwd |
|
CD |
|
mkdir |
|
rmdir |
Delete the directory of the specified name |
- ls command
function Description:
Outputs the specified directory contents to a standard output device;
Displays the contents of the current directory if you do not specify a file or directory parameter
By default, the LS command displays all information alphabetically by file name
when the LS command displays the contents of a directory, it does not display a file entry with a name that begins with a. (dot) unless the-a or-a flag
command format is used:
ls [-aadffhilrs] Directory name
LS [–color={none,auto,never,always}] Directory name
LS [–full-time] directory name
ls command common options BR>-A lists all entries except. (points) and: (Point-to-point)
-a lists all items in the directory, including items starting with. (points)
-l (lowercase in L) display, number of links, owner, group, size (in bytes), and last modified time for each file
d This is a table of contents
b This entry is a block special file
C This entry is a character special file
L This entry is a symbolic link
p This entry is a FIFO special file
s This is a local socket
-This item is a normal file
-R Reverses the sort order, sorts the appropriate alphabetically, or sorts the oldest file first
-R recursively lists all subdirectories
-H to more visually display file sizes
–color=never Do not follow the file properties with a different color
–color=always display color
–color=auto Let the system set itself to show the color
–full-time output in full time mode
–time={atime,ctime} Output access time or time to change permissions, not modify time
- pwd Command
function Description:
Display the path name of the working directory (print working directory)
command Format
pwd [-p]
The PWD command writes the full path name (absolute path) of the current directory to the standard output device; All directories are separated by A/(slash); The first/represents the root directory, the last directory is the current directory
[-p] shows the actual path, not the path of the connection
/var/mail is a connection file for/var/spool/mail
- CD Command
function Description:
Changing the current working directory (change directory)
command format:
CD [Relative path or absolute path]
Instructions for use:
user must have execute permissions in the specified directory
If you do not specify a directory parameter, the CD command sets the current working directory as the login directory
Supports relative paths and absolute paths
Application Examples:
CD ~user to switch to User's primary (home) directory
. (dots) indicates the current directory
.. (dot) indicates the parent directory
CD ~ Return to the current user home directory
CD-Returns the last directory where the user was located
- mkdir Command
function Description:
Create one or more new directories
command format:
mkdir [-MP] Directory name
Instructions for use:
Each new directory contains standard items. (points) and. (Point-point)
-P creates the missing intermediate path name directory, and if the-p flag is not specified, the parent directory of each newly created directory must already exist
-m set permissions on newly created directories, regardless of default permissions
- rmdir Command
function Description:
Delete Directory
command format:
rmdir [-p] Directory name
Instructions for use:
Must be empty, and must have write permission to the parent directory
-P removes all directories along the path name specified by the parameter
For example, if you want to delete ~/test, ~/test/test1, ~/test/test1/xyz, you can use the following command
rmdir –p ~/test/test1/xyz
2. File Operation commands
Command |
function |
File |
Displays the type of the specified file |
Touch |
Create a file with the specified name or update the file time |
Cp |
Copy a file or directory |
Rm |
Delete a file or directory |
Mv |
Move a file or directory, file or directory rename |
- File Command
function Description:
Determine file type
command format:
File File name
Instructions for use:
Files can be regular files, directories, FIFO (specified pipes), block special files, character special files, symbolic links, or socket types
For regular files with a length of zero, it will be recognized as an empty file
For symbolic link files, this link is followed by the file referenced by the symbolic link by default
UTF, which is the abbreviation for Unicode text format, meaning Unicode literal
Touch Command
function Description:
Create a new file
Update file access and modification times
Modified CTime (Use–change time)
Access time Atime (Access)
format:
Touch [-opt] {file| Directory}
Common Options
-A changes the access time for files specified by the file variable
-C do not create if the file does not exist
-m change the file modification time
-T time uses the specified times instead of the current time.
The time variable is specified in decimal form [YYMMDDHHMM]
YY The following two digits for the specified year
MM Specifies which January of the year (from 01 to one)
DD specifies which day of January (from 01 to +)
hh Specifies which hour of the day (from 00 to all)
mm Specify which minute of the hour (from 00 to a)
Command Instance
RM Command
function Description:
Delete (link) a file or directory
command format:
RM [opt] File/dir
Instructions for use:
If the file you want to delete is a symbolic link, the link is dropped, but the file or directory referenced by the symbolic link is still
Common options
-F does not prompt before deleting a write-protected file
-I prompt before deleting each file
-R recursively deletes a directory and its contents when the target parameter is a directory
-R with-R
Command Instance
- MV Command
function Description:
Move (rename) a file or directory
The MV command can overwrite an existing file
command format:
MV [opt] source destination
parameter Description:
-F: Force direct transfer without asking
-I: If the target file already exists ask whether overwrite
-U: If the source file already exists, but the source file is newer, the update
Command Instance
- CP Command
function Description:
Copy a file or directory
command format:
CP [-OPT] [Src_file|src_dir] [Dst_file|dst_dir]
Common Options
-a use to copy directories, retain links, file attributes, and recursively copy directories
-D Keep Links when copying
-F If the specified target file exists, first forcibly overwrite
-I require the user to confirm before overwriting the target file
-p copy files, including their modification time and access rights
-R replicates content in a directory recursively when replicating directories
-R as-R, but special files will be recreated instead of copied
Command Instance
Command instance diagram
3. text File View commands
cat
command |
function |
display text file contents |
tac |
Starting from the last line, TAC and cat alphabetical order opposite |
nl |
|
more |
pagination displays text file contents |
less |
pagination displays text file contents and can be easily browsed repeatedly |
head |
|
tail |
|
Cat Command (CONCATENATE)
function Description:
Display text file contents
command format:
Cat [-aentv] File name
parameter Description
-N or –number the number of rows for all outputs starting from 1
-V displays nonprinting characters as visual characters, in addition to jumping Geff, line breaks, and page breaks;
-e displays a $ (dollar sign) at the end of each line
-T displays the jump-^i character as a, combined with-V;-t=-vt
-A equals to-vet
function 1:
Displays the specified file continuously on the screen from the first line, which is not applicable when the file is too large
such as Cat Install.log
function 2:
To connect two or more files together
Format:
Catfile1 file2 ... >filen: Copy the contents of File1, file2, etc. to the new file Filen
Command instance
TAC Command
function Description:
Starting from the last line, TAC and cat are in reverse alphabetical order
Command Instance
- NL Command
function Description:
When displayed, the output line number
command format:
NL [-BNW] File
parameter Description:
-B: Specify how line numbers are specified
-B A: Indicates that line numbers are also listed, regardless of whether they are blank lines
-B T: Blank lines do not list line numbers if there are blank lines
-N: List the representation of line numbers
-N-Ln: line number appears at the left of the field
-N RN: line number is displayed to the right of the field without adding 0
-N RZ: line number appears to the right of the field, plus 0
-W: Number of digits occupied by the line number field
- More Commands
function Description:
Pagination display of text file contents
command format:
More file names
Instructions for use:
More commands to read the file and display the content on the standard output device
This command pauses after each screen and prints the word more and the percentage of content displayed at the bottom of the screen
Enter: More command will display a line down again
SPACEBAR: The more command displays a different screen of text.
- Less command
function Description:
Pagination Displays text file contents, and can be easily browsed repeatedly
command format:
Less file name
Instructions for use:
The less command functions almost as much as the more command, and is also used to display files on a page, except that the less command allows the user to scroll forward and backward through the file while displaying the file
Common function keys
PageUp
PageDown
/Search Down
? Search up
- Head Command
function Description:
Show file Header content
command format:
Head [–n Num] File ...
Option Description:
The-n option specifies the number of lines to display for file
Instructions for use:
Displays the first few lines or the first few bytes of a file or multiple files
The first 10 rows are displayed by default
The input file must be a text file
- Tail Command
function Description:
Show file Trailer content
command format:
tail [-nf num] File
Option Description:
The-n option is used to specify the number of lines from the beginning of each specified file to be displayed
+num Displays the contents from the beginning of Num line to the end of the file
-num Displays the content from the end of the file to the end of the file
-F If the file content is growing, this option dynamically displays the increased content until the user ends with "CTRL + C"
Instructions for use:
We often use the tail command when we look at the server logs.
We always use this.
-f /usr/local/tomcat/logs/catalina.out
4. File Lookup command
Command |
function |
which |
The Whereis command will find the eligible files in a specific directory; |
where |
Starting from the last line, TAC and cat are in reverse alphabetical order |
Locate |
Pagination display of text file contents |
Find |
When displayed, the output line number |
- which command
function Description:
To view the path to an executable file
command format:
which [-A] command
Common options:
-a lists all the commands that can be found, not just the first command found
Example:
pwdwhich lsattr
whereis Command
function Description:
Find "Specific Files"
The Whereis command will find the eligible files in a specific directory;
command format:
Whereis [Options] File list
Common Options :
-B finds only binary files
-M only looks up the description file
-S only find source files
-U files without Help files
Command Instance
Locate Command
function Description:
Used to find eligible files, it will go to save the file and directory name in the database, find the eligible files or directories
command format:
Locate file name
Instructions for use:
The database needs to be established before it can be looked up
The UpdateDB command is used to generate the most recent file name database, which generates MLOCATE.DB database under directory/var/lib/mlocate/
It looks faster than find, but the process of updating the database itself can be time-consuming
- Find Command
function Description:
Search for files in the directory structure and perform the specified actions
command format:
Find start directory lookup conditions [action]
Instructions for use:
Start directory: The directory path that the Find command looks for, such as available. To represent the current directory, with/to represent the system root directory
Search criteria: Matching mechanism, such as file name, file permissions, modification time and other files to find
Action: A specific processing method that is found after a specified condition, such as displaying the found file on the screen, or modifying permissions on it, or deleting the found file, and so on
Find Command search conditions (1)
-name ' string ' to find files by file name
-lname ' string ' Find all symbolic link files that match the file name
-gid N Find all files belonging to the user group with ID number n
-uid N Find all files belonging to a user with ID number n
-group ' Strings ' Find all files that belong to the same user group
-user ' string ' to find all files belonging to the same user
-empty looking for files of size 0
-path ' string ' Find path name matches all files of the given string
-perm permissions to find files and directories with the specified permissions, which can be represented as 755,644
Command Instance
Find Command search conditions (2)
-nogroup finds a file that does not have a valid owning group, that is, the group to which the file belongs does not exist in/etc/groups
-nouser find a file without a valid owner, that is, the owner of the file does not exist in the/etc/passwd
-type finding a file of a certain type
-size N [bck] finds files with a file length of n, with C indicating the length of the file in bytes
-depth when looking for a file, first find the file in the current directory, and then look in its subdirectories
Command Instance
Find command search conditions (3)
Find time-based conditions
-amin +n-n by access time;-n means less than nth minutes, +n means n minutes ago
-atime +n-n by access time;-n refers to the nth day or less, +n means n days ago
-cmin +n-n Search by creation time
-ctime +n-n Search by creation time
-mmin +n-n Search by modified time
-mtime +n-n Search by modified time
-newer file lists files that are newer than
actions that can be performed
-exec command {} \; Execute the given command on a file that meets the criteria
{} indicates that the parameter for the command is the found file
At the end of the command must end with "\;"
-ok command {} \; same as-exec, but asks the user whether to execute the command
Command Instance
/-name g.-exec-l\;
example of the Find command
Find all file names starting with RC in the/etc directory
-name “rc*-print
Find all normal files with file lengths of 0 in the system and list their full paths
/-type-size0-exec-l\;
Find out if a user named Redhat is found in all files that start with passwd in/etc
find /etc –name “passwd*” –execgrep “redhat” {} \;
Find all files in the current directory that end in TXT and have not been accessed in 2 minutes
.-name “*.-amin+2-print
four different search command differences
which:
Search by the environment variable path specified path
whereis,locate:
Database that stores system file information
Find:
Direct Search for hard drives
5. Default permissions and hidden permissions for files and directories
umask
command |
function |
|
chattr |
|
lsattr |
|
set UID |
|
set GID |
|
sticky Bit |
|
- umask Command
function Description:
Mask to set new file permissions
command format:
umask [mode]
Instructions for use :
Mask value for the new file permission mode
The default permission for a directory in a Linux system is drwxrwxrwx, and the default permission for a file is-rw-rw-rw-. Therefore, the following formula is available:
Permissions for new directory = drwxrwxrwx-Default permission mask
Permissions for new file =-rw-rw-rw--Default permission mask
For security reasons, the Linux system does not allow the file's default permissions to have execution rights
The umask command with no parameters displays the current default permission mask value
Permissions for the new directory = drwxrwxrwx-(d--w–w-)
= Drwxr-xr-x
Permissions for new files =-rw-rw-rw--(-–w–w-)
=-rw-r–r–
- chattr command
- Function Description:
Set hidden properties for a file
Command format:
chattr [+-=] [asacdistu] File or directory
Parameter description:
A: Only add data, cannot delete, only root can set
I: Can make the file "cannot be deleted, renamed, set connection, cannot write or add data"
- lsattr Command
function Description:
Show hidden properties of a folder
command format:
lsattr [-ar] File or directory
parameter Description:
-A: Show properties of hidden files
-R: Listed along with subdirectories data
- Set UID command
Special Permissions
S and T: Allows the average user to temporarily have permission to the owner of the program when executing certain programs.
- set GID command
function Description:
If S's permission is in the user group, called set GID, abbreviated Sgid
File: If Sgid is set on a binary file, no matter who the user is, its effective user group will become the owner of the Change program User group when the program is executed.
Directory: If Sgid is set on the a directory, then the user group of the file or directory established within that directory will be the user group of this a directory.
- Sticky Bit Command
function Description:
Valid only for Catalogs
In the directory with Sbit, if the user has W and x permissions in the directory, then when the user creates a file or directory under that directory, only the owner and root of the file have the right to delete it.
suid/sgid/sbit permission Settings
Suid:4
Sgid:2
Sbit:1
Format: chmod xxxx filename
Command Instance
Linux Refresher (iii) Linux file and directory management