Article directory
Linux study notes will be continuously updated starting today. In the first part, we start from the basic instructions.
1. Basic instructions under Linux
Many people say, isn’t it good for us to use graphical interfaces? We have to learn Linux. I want to say that for us computer majors, especially those doing C/C++, we pay more attention to the bottom layer than others, and Linux It is about dealing with the bottom layer, so it is necessary for us to learn it carefully.
Next, we officially start learning our instructions:
1.ls command
Syntax: ls [options][directory or file]
**Function:**For a directory, this command lists all subdirectories and files in the directory. For files, the file name is listed along with other information.
Commonly used options: 1: -a lists all files in the directory, including implicit files starting with . 2: -d displays the directory like a file, rather than displaying the files under it. For example: ls -d specifies directory 3: -i outputs the index information of the i node of the file. For example, ls -ai specifies file 4: -k
indicates the size of the file in k bytes. ls –alk specifies file 5: -l lists detailed information about the file. 6: -n uses numeric UID and GID instead of names. (Introduction to
UID, GID) 7: -F Attach a character after each file name to indicate the type of the file. "*" represents an executable ordinary file; "/" represents a directory; "@" represents a symbolic link
; "|" represents FIFOs; "=" represents sockets. (Directory type identification) 8: -r sorts the directory in reverse order. 9: -t
sorts by time. 10: -s outputs the size of the file after the l file name. (Size sorting, how to find the largest file in a directory) 11: -R lists files in all subdirectories.
(Recursive) 12:-1 Only output one file per line.
ls #Only display file names and subdirectories in the current directory
ls -l #Display detailed information about files and directories
ls -a #List all files and directories in the current directory, including hidden files and directories (starting with .)
Note: -a and -l can be used together. ls -al / ls -la (there is no difference between a and l which comes first).
What is the use of . (current directory)?
For example, if we write a C language program now, and we want to run this program now, we enter the command ./a.out, here we use . If a.out is run directly, it will not work.
Here, it tells the system to find a.out in the current directory and then run it.
2.pwd command
** Syntax:** pwd
function : Display the directory where the user is currently located
Indicates that we are in the code directory under the root directory.
Take a look at the directory corresponding to our windows:
3.cd command
In the Linux system, the files and directories on the disk are organized into a directory tree, and each node is a directory or file. For leaf nodes, it is an ordinary file or empty directory.
**Syntax: **cd directory name
** Function: **Change the working directory. Change the current working directory to the specified directory.
Example: 1: cd …: Return to the upper-level directory
2: cd /home/litao/linux/: Absolute path
3: cd …/day02/: Relative path
4: cd ~: Enter the user’s home directory
5: cd -: Return to recent visits Table of contents
Here we talk about ** relative paths and absolute paths:**
Absolute path: regular path, the path starting from the root directory to the destination node, is always valid;
relative path: the path relative to our current location. Has its own valid range.
The ./a.out above is also a relative path.
cd ~ #Enter home directory
cd … #Return to the upper directory
cd - #Return to the last visited directory
4.touch command
**Syntax:**touch [options]... File...
Function: The touch command parameters can change the date and time of the document or directory, including access time and change time, or create a new file that does not exist.
Commonly used options:
1: -a or –time=atime or –time=access or –time=use only changes the access time.
2: -c or –no-create does not create any documents.
3: -d uses the specified date and time instead of the current time.
4: -f This parameter will be ignored and will not be processed. It is only responsible for solving the compatibility problem of the BSD version of the touch command.
5: -m or –time=mtime or –time=modify only changes the change time.
6: -r sets the date and time of the specified document or directory to be the same as the date and time of the reference document or directory.
7: -t uses the specified date and time instead of the current time.
5.mkdir command
Syntax: mkdir [options] dirname...
**Function:** Create a directory named "dirname" in the current directory.
Common options:
-p, --parents can be a path name. At this time, if some directories in the path do not exist yet, after adding this option, the system will automatically create those directories that do not yet exist, that is, multiple directories can be created at one time.
6.rmdir command&& rm command
rmdir is a command corresponding to mkdir. mkdir is to create a directory, and rmdir is a delete command. Syntax: rmdir [-p][dirName]
** Applicable objects: ** All users with operating permissions for the current directory ** Function: ** Common options for
deleting empty directories : -p When the subdirectory is deleted, if the parent directory is also If it becomes an empty directory, it will be deleted together with the parent directory.
The rm command can delete files or directories at the same time. Syntax: rm [-firv][dirName/dir]
** Applicable objects: ** All users ** Function: ** Common options
for deleting files or directories : -f Even if the file attribute is only Read (i.e. write protected), also delete directly -i Ask for confirmation one by one before deleting -r Delete the directory and all files under it
If we confirm that we want to delete and do not want to be prompted ( ordinary users are not prompted), then we can do this:
rm -r can delete the directory and all files/directories under the directory:
7.man command
Linux commands have many parameters, and it is impossible for us to remember them all. We can get help by checking the online manual. The command to access the Linux manual page is man syntax: man
[option]
Common command options
1: -k Search online help based on keywords
2: num Find only in chapter num
3: -a Display all chapters, such as man printf starts searching from the first chapter by default and stops when it knows it. Use the a option. When you press q to exit, it will continue to search until all chapters are searched.
We need to install man. If we are using it for the first time, install it first (as root): yum -y install man-pages
Usage: similar to how we use it
8.cp command
Syntax: cp [option] Source file or directory Target file or directory
Function: Copy file or directory
Description: The cp command is used to copy files or directories. If more than two files or directories are specified at the same time, and the final destination is an already directory exists, it will copy all previously specified files or directories to this directory. If multiple files or directories are specified at the same time, and the final destination is not an existing directory, an error message will appear.
Common options:
-f or --force forcefully copy files or directories, regardless of whether the destination file or directory already exists
- i or --interactive asks the user before
overwriting the file -r recursively processes the files in the specified directory and subdirectories together. If the form of the source file or directory does not belong to a directory or symbolic link, it will be treated as an ordinary file for processing
-R or --recursive recursive processing, and the files and subdirectories in the specified directory will be processed together.
Copy of directory:
9.mv command
The mv command is the abbreviation of move. It can be used to move files or rename files (move (rename)
files). It is a commonly used command in Linux systems and is often used to back up files or directories.
Syntax: mv [options] Source file or directory Target file or directory
Function:
- Depending on the type of the second parameter in the mv command (whether it is a target file or a target directory), the mv command renames the file or moves it to a new directory.
- When the second parameter type is a file, the mv command completes the file renaming. At this time, there can only be one source file (it can also be the source directory name). It will rename the given source file or directory to the given one. Target file name.
- When the second parameter is the name of an existing directory, there can be multiple source files or directory parameters, and the mv command will move all the source files specified by each parameter to the target directory.
Commonly used options:
-f: force means force. If the target file already exists, it will be overwritten directly without asking.
-i: If the target file (destination) already exists, it will be asked whether to overwrite it!
When mv src (source file/directory) dst (destination), src must exist. If dst exists, it will be cut. If it does not exist, it will be renamed.
Cut directory:
10.cat command
Syntax: cat [option][file]
Function: View the contents of the target file
Common options:
-b Number non-empty output lines
-n Number all output lines
-s Do not output multiple blank lines
The cat command can also read the file contents backwards. Writing cat backwards outputs the file contents backwards:
11.more command
Syntax: more [option][file]
Function: more command, similar in function to cat
Common options:
-n Number all output lines q Exit more
Suitable for viewing large files.
12.less instruction
The less tool is also a tool for paging display of files or other output. It should be said that it is an orthodox tool for viewing file contents in Linux and is extremely powerful. The usage of less
is more flexible than that of more. When using more, we have no way to turn forward and can only look back. But when using less, we can use the
[pageup][pagedown] and other key functions to flip back and forth through the file, which is easier to use for viewing. The contents of a file! In addition,
you can have more search functions in less, not only you can search down, but you can also search up.
Syntax: less [parameter] File
function: less is similar to more, but you can use less to browse the file at will, while more can only move forward, but not backward, and less will not load the entire file before viewing.
Options:
-i ignore case when searching
-N display line number for each line/ string: search down for "string" function? string: search up for "string" function n: repeat previous search ( Relevant to / or ?) N: Repeat the previous search in reverse (relevant to / or ?) q:quit
Less supports up and down translation, so it is recommended to use less than more.