Linux Common Instructions(2)

Replenish

(*), can match any file


rm * -rf – — This is very useful~~

whoami, check the current account
su + username, change the account

cd ~/ directory, back to a directory under the home directory
cd -, back to the previous directory (path)

Note: cd ~/ directory can only be followed by a directory, as can be seen from the above example, it cannot be followed by ordinary files

which + directive - see where the directive exists


The which command can only work on the command.
The location of the command is: /usr/bin/


man

describe:

The man command looks at the contents of the command in more detail

usage:

man + (manual) + command/function interface

example




Install man: Enter yum install -y man-pages (root user only)
When using the man command, scroll up: up arrow, scroll down: down arrow or 'Enter', exit view: 'q'
When using the man command , you don’t need to add the manual, and the manual will be queried in the order of 1, 2, 3, 4… by default (the default query is the instruction) – manual
1 is the instruction
– manual 2 is the system call
– manual 3 is the function interface



echo

describe:

output string

usage:

echo string, output a string to the display
echo string > file, output a string to this file

example echo string

Let's not be limited by the string form of the familiar C language. Strings in Linux can be single quotes, double quotes, or without quotes. Remember:
echo must be followed by a string, no matter what the form

Example echo string > file


Originally echo should be displayed on the monitor by default, but it is printed in the file. This behavior is called output redirection , and the symbol is>

cat + file is to read data from the file and output the contents of the file to the display.
If the output redirection file itself does not exist, the file will be created automatically

Example append redirection (>>)


Output redirection (>>), add new content after the original content.
echo defaults to newline, printf does not default to newline
Here you can use output redirection (>) to have a clearing step, so you can use this to clear the file

Example output redirection (>) to create file && (>) to empty file



To summarize the magic of output redirection (>):

  1. If the file after > does not exist, then the file is created
  2. If the file after > exists, then the content of the file is emptied (regardless of whether the command adds a space)

cat

describe:

The cat command is used to print the contents of the file

usage:

cat ----- read data from keyboard file by default
cat file -- read data from this file, and output the file content to the display (file)
cat < file -- read data from this file, will Output the content of the file to the display (file)
b — — number non-empty output lines
-n — — number all lines of output
-s — — do not output multiple blank lines

Example cat && cat file


cat reads data:

  1. The default is to read from the keyboard file
  2. Followed by a file, then it will be read from the file

Replenish:

  1. What is the instruction? Where does the instruction exist??


In Linux, instructions are stored in a special path - /usr/bin/, ==> It can be deduced from this that the instruction is a directory, and the instruction is a file. Since the instruction can be executed again => it can be derived from
this The output command is an executable file.
Summary: The command is actually an executable file, which is just stored in a special path of the system. It is not much different from the executable program we programmed (the difference is also written by others good~~)

Note: put the executable file we wrote into the instruction path, it can only be done under the root user

  1. Everything is a file in Linux

There are two main explanations here, the keyboard and the display

  1. The user can read data from the keyboard, and the read function can be considered specific, but cannot write data from the keyboard, and the write function can be considered empty
  2. Users can write data from the display, it can be considered that the writing function is specific, but it cannot read data from the display, it can be considered that the reading function is empty ==> both the
    keyboard file and the display file have the ability to read and write , so it is considered a kind of file

Example cat file && cat < file


Originally read data from the keyboard file, but read data to the file, this behavior is called input redirection (<)

Some veterans will have doubts: These two methods have the same result, why bother?
Note: Although the result is the same, but the meaning is different. Due to the limited knowledge learned now, this will be explained in detail later~, Remember to remind Lao Chen!!

cat -s/ -b/ -n

cp

describe:

The cp command is to copy files or directories

usage:

cp source file destination file - copy the source file to the destination file
cp -r source directory destination directory - copy the source directory to the destination directory

Example cp source file destination file

Example cp -r original directory destination directory

Copy and cut are confusing ==> cp and mv are confusing

mv

describe:

The mv command can move files (or directories) or rename files (or directories)

usage:

mv filename(directoryname) newname - rename file(directory)
mv filename(directoryname) path - move file(directory)
mv filename(directoryname) path+newname - move file (directory) and rename

Example mv file name (directory name) new name

Example mv file name (directory name) path

Example mv filename (directory name) path + new name

Summarize the mv instruction

We can have both moving and renaming aspects of the mv command

Rename behavior: mv a.txt b.txt == mv a.txt ./b.txt
Move behavior: mv c.txt .../ == mv c.txt .../c.txt

insert image description here

alias

describe:

The alias command is to give the command an alias

usage:

alias alias = 'command operation'

Example alias alias = 'command operation'


We can find that the alias has the same functional
alias characteristics as the original name:

  • Temporary and not written in the file
  • If you don't want to use this alias, there are two ways:
    • alias alias = ''
    • Just shut down and restart
  • Alias='Original Name' ==> There can be no spaces in between, don't ask why, just try it~~

Although the alias is simple, don't be greedy


ll is also an alias


We are looking for the location of the ll command, but the result shows the location of ls~~ Look closely at
the origin of ll: alias l='ls -l --color=auto', the previous one is an alias, so it goes without saying that
the latter The --color=auto, is the color scheme, which means: the color scheme is automatically assigned

more

describe:

It is the same as cat, mainly used to view large files (projects, logs...)
The more command follows the less command to be discussed later, and the less command is more recommended

usage:

more + file name — — enter more viewing mode
option (-n) — —— query mode
option (/n) — — query mode

Example more -n && /n


In the more mode, you can only scroll down—'Enter', exit—'q'


The / option can be used in more mode, but the - option cannot be used

example/n


Example more - n

Before entering the more mode, you can actually choose which line to look at

less

describe:

  • The less tool is also a tool for displaying files or other output in pages. It should be said that it is an orthodox tool for viewing file content in Linux, and its functions are extremely powerful.
  • The usage of less is more flexible than that of more. In more, we have no way to turn forward, we can only look back
  • But if you use less, you can use [pageup][pagedown] and other key functions to browse files back and forth, which is easier to use to view the content of a file!
  • In addition, you can have more search functions in less, you can not only search downward, but also search upward.

usage:


Less is similar to more, but you can use less to browse files at will, while more can only move forward, but not backward, and less will not load the entire file before viewing .

-i -- ignore case when searching
-N -- show line number for each line
/ string -- function to search down for "string"
? string -- function to search up for "string"
n -- -- Repeat previous search (relative to / or ?)
N — — Repeat previous search in reverse (relative to / or ?)

example/string


example? string

  1. There is this content in front of the current interface:

  2. There is no such content in front of the current interface


head && tail

describe:

head and tail are as easy to understand as their names. They are used to display a certain number of text blocks at the beginning or end. head is used to display the beginning of the file to the standard output, and tail takes it for granted. end. The default is the first (last) 10 lines.

usage:

head file - the first 10 lines of the output file by default
tail file - the last 10 lines of the output file by default
head -n file - the first n lines of the output file
tail -n file - the last n lines of the output file

Example head/tail -n file

Supplement (Pipeline)

There are the first n lines of the output file, and the last n lines of the output file, so are there n lines in the middle of the output?? In
fact, there are two ways: (extract 1000 - 1020 lines of test.txt)

  1. create temporary file

head - 1020 test.txt > tem.txt
tail -21 tem.txt

insert image description here
This method will create a temporary file in the disk, which will lead to waste

  1. with pipes

head -1020 test.txt | tail -21


This | is the pipeline file, which is a memory-level file

  • Understand the memory level:
    If we want to modify a file, we need to load the data into the memory first, and then operate it. ==> It can be said that in a certain period, any file is a memory file. But some files will eventually enter to disk, some do not go to disk, and exist in memory.
  • Understand the pipeline:
    the pipeline has two ports, the entrance and the exit. Enter the data from the left side of the pipeline, and will not create a temporary file in the disk, and then output the data to the right for use

Drunk lying on the battlefield Lord Mo Xiao, how many people have fought in ancient times.

Guess you like

Origin blog.csdn.net/qq_67549203/article/details/130444740