Commonly used commands in Linux

ls

ls [options] [directory or file]

Function: For a directory, list all subdirectories and files under the directory; for a file, list the file name and other attributes of the file

Common options:

-a: List all files in the directory, including hidden files starting with .

-l: List the detailed information of the file.

Note: ls -l can be written as ll

Options can contain multiple 


Uncommon options:
-d Display directories as files, rather than the files under them. For example: ls -d specifies the directory
-i outputs the index information of the i node of the file. For example, ls -ai specifies the file
-k to express the size of the file in the form of k bytes. ls -alk specifies the file
-n to replace the name with a numerical UID and GID. (Introduce UID, GID)
-F Append a character after each file name to describe the type of the file, "*" means an executable ordinary file; "/" means a directory; "@"
means |" means FIFOs; "=" means sockets. (directory type identification)
-r Reverse sorting on directories.
-t Sort by time.
-s Output the size of the file after the l filename. (Sort by size, how to find the largest file in the directory)
-R List the files in all subdirectories. (recursively)
-1 Output only one file per line.


pwd

pwd

Function: Display the directory where the user is currently located


cd

In the Linux system, the files and directories on the disk are formed into a directory tree, and each node is a directory or a file.

 For example: /home/bit/test.c

/ is a path separator, especially the root directory /, which itself is a path separator

cd directory name

 The directory name can be a relative path or an absolute path

Function: Change the working path, change the current path to the specified path

Each folder has two hidden files. and .., . indicates the path of the current folder, .. indicates the path of the parent directory of the current folder

example:

cd .. : Return to the parent directory
cd /root/Documents : Absolute path
cd ../day02/ : Relative path, indicating the day02 directory under the parent directory
cd ~: Enter the user’s home directory
cd -: Return the most recent visit (last visit) Table of contents


touch

touch [options] filename

Function: It is mainly used to create a file that does not exist . The touch command parameter can change the date and time of the document or directory, including access time and change time.
Options:
-a or --time=atime or --time=access or --time=use only changes the access time.
-c or --no-create Do not create any documentation.
-d Use the specified datetime instead of the current time.
-f This parameter will be ignored and not processed, it is only responsible for solving the compatibility problem of the BSD version touch command.
-m or --time=mtime or --time=modify only change the change time.
-r Set 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.
-t Use the specified datetime instead of the current time.


mkdir

mkdir [options] directory name

Function: Create a directory under the current directory
Common options:
-p: 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 exist, that is, multiple directories can be created at one time;

Note: tree is a tool for printing directory files in a tree view, enter the following command to install

Use yum -y install tree in centos  and apt-get install tree in
ubuntu


is rm  

rmdir [options] directory name

Function: rmdir is a command corresponding to mkdir. mkdir is to create a directory, and rmdir is to delete a directory. But only empty directories can be deleted
Applicable objects: All users with the current directory operation authority
Common options:
-p When a subdirectory is deleted, if the parent directory also becomes an empty directory, delete the parent directory together.


rm

rm [options] filename

Function: delete file

Common options:

  • -i Ask for confirmation one by one before deleting.
  • -f Even if the original file attribute is set to read-only, it will be deleted directly without confirmation one by one (will not ask whether to delete) .
  • -r deletes the directory and the following files one by one. (This feature option enables rm to delete directories)
ask to delete


Do not ask to delete

Recursive deletion of multi-level directories without asking

man

Function: It is a user manual of Linux-related tool instructions, you can find the instructions you don’t know

man [options] command

Common options

  1. -k search manpage by keyword
  2. num is only found in chapter num
  3. -a will display all the chapters, such as man printf, it will start searching from the first chapter by default, stop when you know it, use the a option, when you press q to exit, it will continue to search until all chapters are searched .

The manual is divided into 8 chapters.
1 is common commands.
2 is system calls, such as open and write. (Through this, at least you can easily find out what header files need to be added to call this function.) 3 For c language library
functions , such as printf, fread

4 is a special file, that is, various device files under /dev
5 refers to the format of the file, such as passwd, it will explain the meaning of each field in this file
6 is reserved for the game, defined by each game itself
7 is an attachment There are also some variables, such as global variables such as environ, which are explained here.
8 are commands for system management. These commands can only be used by root, such as ifconfig

example:

Linux printf vs C language printf

Because the printf command of Linux is in the first chapter of the manual, man printf will find it first. If you want to query the printf of C language in chapter 3, you must use man 3 printf. If you are interested, you can check the difference and content of the two. too much will not show

man printf

man 3 printf


cp

cp [options] source file or directory destination file or directory



Function: Common options for copying files or directories :
-f or --force Forcefully copy files or directories, regardless of whether the destination file or directory already exists
-i or --interactive Ask the user before overwriting files
-r -R or --recursive recursive Process, process the files and subdirectories under the specified directory together. If the form of the source file or directory does not belong to a directory or a symbolic link, it will be treated as an ordinary file

normal file copy;

touch test.txt
echo 'hello world' > test.txt  //向文件输入0
cat test.txt //显示文件内容
cp test.txt ../test_backup.txt //将文件复制到上级目录
cat ../test_backup.txt //查看上级目录的复制文件内容

echo will send the input string to standard output 

> Indicates output redirection, outputting the string to a file

cat can view the contents of the file

Copy of directory: (must use -r)

//单级目录
mkdir dir
cp -r dir d
//多级目录
mkdir -p d1/d2/d3/d4
cp -r d1 ../


mv

mv [options] source file or directory target file or directory

Commonly used options:
-f: force means to force, if the target file already exists, it will not be asked but overwritten directly
-i: if the target file (destination) already exists, it will ask whether to overwrite!

Functions:
1. Move files (cut+copy). If the name of the source file (directory) and the target file (directory) under the path are the same, move the source file to the path where the target file is located

2. File renaming, if the path of the source file (directory) and the target file (directory) are the same, but the names are different, there will be a renaming effect.

3. Move the file and rename the file. If the path of the target file (directory) of the source file (directory) is different, and the names of the target file (directory) under the source file (directory) and the path are also different, the file will be moved and renamed.


command to view documentation

cat

cat [options] [file]

Function: View the content of the target file
Common options:
-b Number non-empty output lines
-n Number all output lines
-s Do not output multiple empty lines 

There are many lines of code in this file, it will be troublesome to use cat to view, so cat is more suitable for reading small files


more

more -n [file]

Function: the more command, similar to cat, will display the content near n lines. If n is not specified, it will be displayed from the first line, press Enter to scroll down,. Finally press q to exit


less

less -n [file]

Function: Display the content near n lines. If n is not specified, it will be displayed from the first line. Unlike more, less can use the up and down arrow keys to flip through the document. Finally press q to exit


head and tail

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 is to see the end of the file.

head -n file

Function:
Display the first n lines at the beginning of the file to the standard output, if the size of n is not specified, the default head command prints the first 10 lines of the corresponding file.

tail -n file

Function:
Display the last n lines at the end of the file to the standard output. If the size of n is not specified, the default tail command prints the last 10 lines of the corresponding file.


Practical operation: How to view part of the line content of a file?

Method 1: Use cat to view, but it is not recommended, if you want to find 10000-10020 lines, you will be exhausted

cat -n filename

Method 2: use more or less,

more -10000 filename

less -10000 filename

Method 3: first use head to save the content to a temporary file, and then use tail to process the temporary file

head -10020 filename > tmpfile

tail -20 tmpfile

Method 4: Use head and tail with pipelines

head -10020 filename | tail -20


date

Function: Display the current time and timestamp, and also complete the conversion between the two

date +[marker]

The user can set the format to be displayed. The format is set as a plus sign followed by several marks. The list of commonly used marks is as follows 
%H : hour (00..23)
%M : minute (00..59)
% S : seconds (00..61)
%X : equivalent to %H:%M:%S
%d : day (01..31)
%m : month (01..12)
%Y : full year (0000. .9999)
%F : Equivalent to %Y-%m-%d

%s: Timestamp (Unix timestamp (English: Unix epoch, Unix time, POSIX time or Unix timestamp) is the number of seconds elapsed since January 1, 1970 (midnight in UTC/GMT), regardless of leap seconds )

date Display time in the specified format: date +%Y:%m:%d

Current time -> timestamp: date +%s

Timestamp -> time: date -d@1689152845


cal

Function: view kilometer calendar

View calendar for year cal -y [year]

View the previous month, current month and next month of the system time cal -3


find -name

Function: Find files by file name

find [path] -name [filename]

By default, the specified path is searched from the current path


grep

Function: Search the string line by line in the file, and print out the line containing the string

grep [options] [string] [filename]

options:

-i Ignore the difference in the case of the string when searching, and letters with different case will be treated as the same

-n displays the line number of the found line in the file

-v reverse selection, that is, lines that do not contain strings will be printed


zip/unzip

Function: Compress/decompress directories or files

zip [option] [compressed file name] [compressed file/directory (can be multiple)]

unzip [the name of the decompressed file] -d [the path of the decompressed file] (you can not use -d, and decompress to the current directory by default)

  

  


tar

Functions: pack, unpack, preview pack files

tar [options] [file or directory]

Three commands are commonly used:

Packing and compression: tar czf [the file name after packing and compressing] [the file or directory to be packed and compressed]

Preview package file: tar tzf [package compressed file name]

Unpack and decompress: tar xzf [package compressed file name]

 

options: 

  •  -c : Create a parameter command for a compressed file (meaning create);
  • -x : Unzip the parameter command of a compressed file!
  • -t : View the files inside the tarfile!
  • -z : Does it also have the attribute of gzip? That is, does it need to be compressed with gzip?
  • -j : Does it also have the attributes of bzip2? That is, does it need to be compressed with bzip2?
  • -v : Show files during compression! This is commonly used, but not recommended for background execution!
  • -f: Use the file name, please note that the file name must be followed immediately after f! Do not add parameters!
  • -C : Unzip to the specified directory

Pay special attention, the file name after the parameter f is taken by ourselves, and we usually use .tar as the identification. If the z parameter is added, .tar.gz or .tgz will be used to represent the gzip-compressed tar file; if the j parameter is added, .tar.bz2 will be used as the file name extension.


uname 

uname [options]

Function: Used to obtain information about the computer and operating system.
Supplementary note: uname can display basic information such as the version of the operating system used by the Linux host and the name of the hardware.
Commonly used options:
-a or –all output all information in detail, followed by kernel name, host name, kernel version number, kernel version, hardware name, processor type, hardware platform type, operating system name


Several important hotkeys [Tab], [ctrl]-c, [ctrl]-d

[Tab] button: It has the functions of command completion and file completion
[Ctrl]-c button: stop the current program
[Ctrl]-d button: usually means: the end of keyboard input (End Of File, EOF or End Of Input) means; in addition, he can also
be used to replace exit


Guess you like

Origin blog.csdn.net/weixin_74269833/article/details/131251721