Encyclopedia of Commonly Used Commands in Linux Operating System: System Management

Linux Operating System Instructions 1: System Management

User Management

  • view all usernames

    ls /home

  • New users

    adduser [Name]

  • change Password

    passwd [Name]

  • delete users

    userdel [Name]

System Management

  • View the system path for a specific command

    which [command]

    # 例如:which ls / which which

  • alias the command

    # Aliasing the command is for the convenience of operation, and the life cycle is one login .

    alias alias = '[command]'

    # Example: alias back='cd ...'

ls command

Syntax: ls [options] [directory or file]

Function: For a directory, this command lists all subdirectories and files under the directory. For files, the filename is listed along with other information.

  • Parameter information

    • -a List all files in the directory, including hidden files starting with .
    • -d Display directories as files instead of the files under them. Such as: ls -d specifies the directory
    • -i Outputs index information for the i-node of the file. Such as ls -ai specified file
    • -k Indicates the size of the file in k bytes. ls –alk specifies the file
    • -l Lists detailed information on files.
    • -n Use numeric UID, GID instead of name. (introducing UID, GID)
    • -F Append a character after each file name to indicate the type of the file, "*" means executable ordinary file; "/" means directory; "@" means
    • Symbolic link; "|" means FIFOs; "=" means sockets. (directory type identification)
    • -r Sort directories in reverse.
    • -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 files in all subdirectories. (recursive)
    • -1 Output only one file per line.

cd command

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.

image-20220727201557253

Syntax: cd [directory name]
**Function: **Change the working directory. Change the current working directory to the specified directory.
Example:

cd … : Return to the parent directory
cd /home/litao/linux/ : Absolute path
cd …/day/ : Relative path
cd ~: Enter the user’s Home directory
cd -: Return to the most recently visited directory

man command

Linux official document instructions. We can get help by looking at the online manual. The man command is to access the Linux man page

Instructions:

man [选项] 命令  

Some Linux operating systems run on cloud servers, which may cause man commands to be unavailable or incomplete. You can install the corresponding man-page package as needed

yum install -y man			#安装man
yum install -y man-pages	#安装man-page

Common options:

man is used according to specific parameters, usually there are 9 parameters, and the digital command represents the number of its manual, and its command description is as follows: the most commonly used ones are 1, 2, 3

image-20220728183050556

  1. is an ordinary command (the default parameter of man)
  2. It is a system call, such as open, write and the like (through this, at least you can easily find out what header files need to be added to call this function)
  3. It is a library function, such as printf, fread4 is a special file, that is, various device files under /dev
  4. A description of device information, usually in a file under /dev
  5. Refers to the format of the file, such as passwd, which will explain the meaning of each field in this file
  6. It is reserved for the game, defined by each game itself
  7. It is an attachment and there are some variables, such as global variables such as environ, which are explained here. For example, Linux file system, network protocol, ASCII code, etc.
  8. It is a command for system management, these commands can only be used by root, such as ifconfig
  9. Other, files used to store kernel routines (by the current Linux operating system.

in,

  • -k search manpage by keyword

  • num is only found in chapter num

  • -a will display all the chapters, such as man printf, it will start searching from the first chapter by default, and stop when you know it, use the a option, when you press

  • Press q to exit, and he will continue to search until all chapters are searched.

Example:

man 3 printf

man 2 fork

date command

One of the most important environments in the operating system is time. Without time, the entire operating system is out of order. So date can be viewed or formatted to read the system time of Linux.

**Function:** Display time in specified format

Usage: date [OPTION]… [+FORMAT]

In terms of display, the user can set the format to be displayed. The format is set as a plus sign followed by several symbols. The commonly used symbols are listed as follows
:

  • %H : hours (00…23)
  • %M : minutes (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

Example:

date +%Y-%m-%d

# 2022-07-29

If you do not fill in the formatting parameters, all time information will be output

# Fri Jul 29 13:17:27 CST 2022

Set system time (root only)

  • date -s //Set the current time, only the root authority can be set, others can only be viewed.
  • date -s 20080523 //Set to 20080523, which will set the specific time to empty 00:00:00
  • date -s 01:01:01 //Set the specific time and will not change the date
  • date -s “01:01:01 2022-05-23″ // format %H:%M:%S %Y-%m-%d set time
  • date -s “01:01:01 20220523″ // format %H:%M:%S %Y%m%d set time
  • date -s “2022-05-23 01:01:01″ // format %Y-%m-%d %H:%M:%S set time
  • date -s “20220523 01:01:01″ // format %Y%m%d %H:%M:%S set time

Timestamp conversion method

Time to timestamp: date +%s
Timestamp to time: date -d@1508749502
Unix timestamp is the number of seconds elapsed since January 1, 1970, Universal Time (Greenwich Mean Time), regardless of leap seconds .

cal command

The cal command can be used to display the Gregorian (Gregorian) calendar . The Gregorian calendar is the current international calendar, also known as the Gregorian calendar, collectively referred to as the Gregorian calendar. "Gregorian calendar", also known as "solar calendar", is based on the fact that the earth revolves around the sun for one year, which is commonly used by Western countries, so it is also called "Western calendar".

Command format: cal [parameter] [month] [year]
function : used to view calendar and other time information, if there is only one parameter, it means the year (1-9999), if there are two parameters, it means the month and year
common options :

  • -3 Display the calendar of the previous month, the current month, and the next month of the system
  • -j Displays the day of the year (the date of the year is counted by day, starting from January 1st, and the number of days in the current month in the year is displayed by default)
  • -y displays the calendar for the current year

image-20220729145700730

find command

  • The find command under Linux searches for files in the directory structure and performs specified operations.

  • Even if the system contains a network file system (NFS), the find command is also valid in the file system, as long as you have the corresponding permissions.

  • When running a very resource-intensive find command, many people tend to put it in the background, because traversing a large file system may take a long time, but when the first find command search is completed, then A search speed will be greatly accelerated.

  • The find command under Linux provides quite a lot of search conditions, and the function is very powerful. Because find has powerful functions, it has many options, most of which are worth our time to understand. Here we just give a simple example without going into details. For details, you can use 'man find' to view

image-20220729232102387

Syntax: find [pathname] [-options] [targetName]
Function: Used to find files in the file tree and make corresponding processing.
Common options:
-name Find files by filename

Example:

find ./ -name test.c

# Find the file named test.c in the current path

grep command

Row filtering tool, to search and filter batches of text by row. (wildcards are supported)

Function: Search for a string in a file and print out the found lines

Syntax: grep [options] search strings file

Common options:

  • -i : Ignore the difference in case, so the case is treated as the same
  • -n : By the way, output the line number
  • -v: Reverse selection, that is, display the line without the content of the 'search string'

Example:

grep -i “main” test.c

# row filtering

cat file.txt | grep “6”

grep “6” file.txt

grep "52[0-1]" file.txt # Find the string of numbers starting with 52 and ending with 0-1

grep '^x' file.txt # Find the text starting with x in the file

ps axj | grep bash # command output search, (this command is to output the process running in the current system)

zip/upzip command

Function: Compress the directory or file into zip format, and decompress the package.

**Usage:**zip is used to compress files. Usually, when no parameters are specified, a single file is directly compressed by default, and the compression effect is very good. If you need to compress the entire folder, you need to use parameters to recursively compress, and the suffix of the compressed result is unified as .zip. And upzip is used to decompress the compressed package of .zip

grammar:

zip [compressed file.zip] [directory or file]

unzip [compressed file.zip] [option] [decompression path]

Common options:

-r Recursive processing, compress all files and subdirectories under the specified directory together

-d specifies the decompression path

Example:

zip file.zip file.txt # compress file.txt file

zip -r test.zip test/ # Compress the test folder. Remember, if you need to compress the folder, you must add -r to indicate recursive compression, otherwise a new empty compressed package will be generated.

unzip file.zip # Unzip the file.zip file in place

unzip test.zip -d unzipFolder # Unzip the test.zip file into the unzipFolder folder

tar command

**Function:**Package the files.

**Syntax:**tar [-cxtzjvf] files and directories...

Common parameters:

  • -c : Create a parameter command for a compressed file (meaning create);
  • -x : Unzip the parameter command of a compressed file
  • -t : View the files in the tarfile (you can directly view the contents without decompression)
  • -z : use gzip compression
  • -j : use bzip2 compression
  • -v : display files during compression
  • -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

Remarks: When writing related commands, some systems may have - to indicate before the parameters, or they may not need to be operated according to the actual situation.

Example:

tar czf test.tgz testFolder # Package testFolder folder as test.tgz package (tgz is .tar.gz abbreviation)

tar xzvf test.tgz # unpack test.tgz into the current directory, and display the unpacking process information

tar xzf test.tgz -C test # Unpack the test.tgz package into the test folder directory

Supplementary note : the difference between packaging and compression

Packing is to put the content together in different categories, and compression is a packing method adopted to save the storage space occupied by files.

bc command

**Function: **Calculator under Linux operating system

**Syntax:** Type ->bc to enter the calculator mode, then enter an expression in one line and press Enter to start calculation. Enter quit to exit .

Example:

bc

1+1*2*3

# output: 7

It can be used not only in direct calculator operation, but also in combination with command line and pipeline

echo "1+1+2*2*3" | bc
#output: 14

uname command

Syntax: uname [options]
Function: uname is 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.
Common 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

bc

1+1*2*3

# output: 7

It can be used not only in direct calculator operation, but also in combination with command line and pipeline

echo "1+1+2*2*3" | bc
#output: 14

uname command

Syntax: uname [options]
Function: uname is 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.
Common 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

epilogue

In the future, we will continue to update the notes related to the Linux operating system. If you like our articles, one-click three consecutive articles. Don’t forget to pay attention to our official account "01 Programming House" to read the latest articles in the future! Pay attention to the hut, learn programming without getting lost. Your support is the motivation for us to keep going!

Guess you like

Origin blog.csdn.net/weixin_43654363/article/details/126394673