Linux Note 1: Linux Command Line Basics

basic concept

Shell and Bash:

User>Service Program>System Call Interface>Kernel>Hardware

  • Hardware devices are directly managed by the system kernel, but because the complexity of the kernel is too high, there is a greater risk when accessing, so users cannot directly access the kernel.
  • Shell is a general term for terminal programs, acting as a "translator" between humans and the kernel (hardware), and will call the corresponding service program to complete a certain job according to the user's command.
  • Currently, the default terminal program used by mainstream Linux systems is the Bash (Bourne-Again SHell) interpreter.

Advantages of the Bash interpreter:

  1. Use the up and down arrow keys to call the executed Linux commands;
  2. Commands or parameters can be completed with the Tab key only by entering the first few digits;
  3. Has a powerful batch script;
  4. Has the environment variable feature used.

Common Linux command formats:

command name [command parameter] command object

Command name: what you want to do, such as creating users, viewing files, restarting the system, etc.;

Command parameters: used to adjust the command, such as creating a user coded as 888, viewing only the first 20 lines of the file, etc.;

Command object: generally refers to the name of resources such as files, directories, and users to be processed, that is, the recipient of command execution, such as viewing a file called salary table, restarting a system with an IP of 192.168.1.1.

Precautions:

Command parameters are divided into long format (man --help) and short format (man -h)

Command names, command parameters, and command objects must be separated by spaces, and letters are strictly case-sensitive

View the help information of the command:

Here is an example of [enter the man man command in the command line terminal to view the help information of the man command itself]:

The structure and representative meaning of the help information in the man command
structure name representative meaning
NAME command name
SYNOPSIS General usage of parameters
DESCRIPTION Introduction
EXAMPLES Demo (with simple instructions)
OVERVIEW overview
DEFAULTS default function
OPTIONS Specific available options (with description)
ENVIRONMENT environment variable
FILES files used
SEE ALSO relating documents
HISTORY Maintenance History and Contacts

Commonly used four shortcut keys:

Tab key: realize the automatic completion of command name, command parameter and command object name, effectively avoiding the problem of manual input that is easy to make mistakes

Ctrl+C key: Terminate the running of the current process

Ctrl+D key: Indicates the end of keyboard input

Ctrl+I key: Clear the existing content in the current terminal (equivalent to clearing the screen)


Commonly used system work commands

1.echo command

The echo command is used to output the string or the extracted value of the variable on the terminal device. The syntax format is:

echo [string] [$variable]

Example: output string and variable value respectively

2. date command

The date command is used to display or set the time and date of the system, the syntax format is:

date [+ specified format]

Parameters in the date command and their functions
parameter effect
%S seconds (00~59)
%M minutes (00~59)
%H hour (00~23)
%I hour (00~12)
%P show AM or PM
%m month (1~12)
%a Abbreviated day of the week name (for example, Sun)

%A

Full day of the week name (for example, Sunday)
%b Abbreviated month name (for example, Jan)
%B Full month name (for example, January)
%q Quarter (1~4)
%y Abbreviated year (eg, 22)
%Y Full year (eg, 2022)
%d day of the month
%j day of the year
%n Newline character (equivalent to pressing the Enter key)
%t Tabbing (equivalent to pressing the Tab key)

example:

Print the current system time in the default format

Print the current system time according to the specified format

Modify the system time to the specified time

View today is the day of the year (can be used to distinguish the early and late backup time)

3. timedatectl command

The timedatectl command is used to set the system time. The English full name is "time date control". The syntax format is:

timedatectl [parameters]

Parameters and functions in the timedatectl command
parameter effect
status show status information
list-timezones list known timezones

set-time

set system time
set-timezone Set effective time zone

example:

View system time and time zone

Set time zone manually

Manually modify the system date 

Manually modify the system time

4. reboot command

The reboot command is used to restart the system, enter this command and press Enter to execute (this operation will involve the management authority of hardware resources, so it is best to reboot as a root administrator, ordinary users may be blocked when executing this command rejected), the syntax format is:

reboot

5.poweroff command

poweroff命令用于关闭系统,输入该命令后按回车执行即可(同样最好以root管理员身份来操作),语法格式:

poweroff

6.wget命令

wget命令用于在终端命令行中下载网络文件,英文全称为“web get”,语法格式为:

wget [参数] 网址

wget命令中的参数以及作用
参数 作用
-b 后台下载模式
-p 下载到指定目录
-t 最大尝试次数
-c 断点续传
-p 下载页面内所有资源,包括图片、视频等
-r 递归下载

 例:下载图灵社区的一张图片

例:递归下载网站内的所有页面数据以及文件

wget -r -p https://www.baidu.com

7.ps命令

ps命令用于查看系统中的进程状态,英文全称为“processes”,语法格式为:

ps [参数]

(通常会将ps命令与管道符技术搭配使用,用来抓取与某个指定服务进程相对应的PID号码)

ps命令中的参数以及作用
参数 作用
-a 显示所有进程(包括其他用户的进程)
-u 用户以及其他详细信息
-x 显示没有控制终端的进程

例:输入ps aux命令查看所有进程

标题栏:进程的所有者、进程ID号、运算器占用率、内存占用率、虚拟内存使用量(单位:KB)、占用的固定内存量(单位:KB)、所在终端、进程状态、被启动的时间、实际使用CPU的时间、命令名称与参数

Linux系统中时刻运行着许多进程,需要进行合理的管理,以优化系统的性能。

Linux系统中五种常见的进程状态(STAT):

  • R(运行):进程正在运行活在运行队列中等待
  • S(中断):进程处于休眠中(当某个条件形成后或者接收到信号时,则脱离该状态)
  • D(不可中断):进程不响应系统异步信号(即便kill命令也不能将其中断)
  • Z(僵死):进程已经终止,但进程描述符依然存在(直到父进程调用wait4()系统函数后将进程释放)
  • T(停止):进程收到停止信号后停止运行

五种进程状态的补充形式:

  • <(高优先级)
  • N(低优先级)
  • L(被锁进内存)
  • s(包含子进程&#x

Guess you like

Origin blog.csdn.net/weixin_58695100/article/details/122727581