Location>code7788 >text

Linux Basics

Popularity:727 ℃/2024-10-30 09:12:07
1 What is a shell

The shell is the user interface of the Linux system, providing an interface for the user to interact with the kernel, it receives commands entered by the user and sends them to the kernel for execution, so it is also known as the command interpreter of Linux.

Display the shell currently in use on the system

echo ${SHELL}				

# /bin/bash

View all shells currently in use on the system

cat /etc/shells 

# /bin/sh
# /bin/bash
# /usr/bin/sh
# /usr/bin/bash
# /bin/tcsh
# /bin/csh
2 Setting the hostname
# Temporarily effective
hostname localhost

# Persistent, supports CentOS 7 and Ubuntu 18.04 or above.
hostnamectl set-hostname locahost
3 Command Prompt

Prompt formatting instructions:

  • \e Controller\033
  • \u Current User
  • \h Hostname short name
  • \H Hostname
  • \w Current working directory
  • \W Current working directory base name
  • \t 24-hour time format
  • \T 12-hour time format
  • ! Command History
  • Number of command history after power on

# View the prompt format
cat $PS1

# Implementing persistent prompt formatting on CentOS systems
echo 'PS1="\[\e[1;32m\][\t \[\e[1;33m\]\u\[\e[35m\]@\h\[\e[1;31m\] \W\[\e[1;32m\]]\\\[\e[0m\]\\$"' > /etc//

# Implement persistent prompt formatting for Ubuntu systems.
echo "PS1='\[\e[1;35m\][\u@\h \W]\\$\[\e[0m\]'" > > .bashrc
4 Internal versus external commands
  • Internal commands: These commands come with the shell and are provided in the form of a command, which is automatically loaded after the user logs in and is resident in memory.
  • External commands: there are corresponding executable program files under the file system path, which are loaded from disk to memory when executing commands, and deleted from memory after execution is completed.

Distinguish between internal and external commands

type commond

Internal command management

help # View all internal commands
enable commond # Enable internal commands
enable -n commond # Disable internal commands
enable -n # View all disabled internal commands

External command management

# View external command file execution path
which -a --skip-alias ${commond}

Hash Cache Table

System initial hash table is empty, when an external command is executed, the default will look for the command from the PATH path, after finding the path of the command will be recorded in the hash table, when the command is used again, the shell interpreter will first check the hash table, if it exists, it will be executed, if it does not exist, it will go to look for the path under the PATH, the use of the hash caching table can greatly improve the The use of the hash cache table can greatly improve the rate of command invocation.

Common Uses of the hash Command

  • hash Show hash cache
  • hash -l show hash cache, can be used as input
  • hash -p path name aliases the full command path to name
  • hash -t name prints the path to name in the cache
  • hash -d name clears name cache
  • hash -r clear cache
5 Command aliases

For longer commands that are executed frequently, they can be defined as shorter aliases to make it easier to execute them

alias # View all currently defined aliases
alias name='commond' # Define an alias
alias name # Undo the specified alias
unlias -a # Undo all aliases

Note: aliases defined on the command line are only valid in the current shell process, if you want them to be permanent, define them in the configuration file

  • ~/.bashrc only works for the current user
  • /etc/bashrc Effective for all users

Editing the configuration file to write in the new configuration will not take effect immediately and will require the bash process to re-read the configuration file

source ~/.bashrc
source /etc/bashrc
6 Common Commands
lscpu									
cat /proc/cpuinfo					
free									
cat /proc/meminfo	
lsblk
cat /proc/partitions
arch
uname -r
cat /etc/os-release
cat /etc/redhat-release
lsb_release -a
cat /etc/os-release
cat /etc/issue
lsb_release -a
lsb_release -is
lsb_release -cs
lsb_release -rs
halt
poweroff
reboot
shutdown -h/-r now/+n
shutdown -h/-r hh:mm
whoami
who
w
7 Session management
screen -ls # View all sessions
screen -S name # create session
ctrl+a,d # Divest session
screen -x name # join session
exit # Exit and close a session
screen -r name # resume a session

Tmux is a terminal multiplexer (terminal multiplexer), similar to screen, but easier to use and more powerful, Tmux is the "unbundling" tool of the session and the window, to completely separate them, the function is as follows:

  • It allows multiple sessions to be accessed at the same time, in a single window. This is useful for running multiple command line programs at the same time.
  • It allows a new window to "plug in" to an existing session.
  • It allows multiple connection windows per session, so multiple people can share a session in real time.
  • It also supports arbitrary vertical and horizontal splitting of windows
# New session
tmux new -s session_name

# Detach session
tmux detach
ctrl+b+d

# Attach session
tmux attach -t <session-name>

# Kill session
tmux kill-session -t <session-name> # kill-session

# Switch session
tmux switch -t <session-name>

# Split window panes up and down
tmux split-window

# Split-window left and right
tmux split-window -h

# List all shortcut keys and their corresponding commands
tmux list-keys

# List all tmux commands and their arguments
tmux list-commands
Ctrl+b %: #Divide left and right panes
Ctrl+b ": #Divide the upper and lower panes"
Ctrl+b <arrow key>: the cursor switches to another pane. The <arrow key> is the arrow key that points to the pane you want to switch to, for example to switch
to the lower pane, press ↓.
Ctrl+b ;: the cursor switches to the previous pane.
Ctrl+b o: the cursor switches to the next pane.
Ctrl+b {: the current pane is moved to the left
Ctrl+b }: current pane moves right
Ctrl+b Ctrl+o: current pane moves up
Ctrl+b Alt+o: move down current pane
Ctrl+b x: close current pane
Ctrl+b ! : split the current pane into a separate window
Ctrl+b z: display the current pane full-screen, use it again to return to the original size.
Ctrl+b Ctrl+<arrow key>: resize the pane in the direction of the arrow.
Ctrl+b q: show pane numbering.
tmux new-window # Create a new window.
tmux new-window -n <window-name> # Create a new window with the specified name.
tmux select-window -t <window-number> # Switch to the specified window.
tmux select-window -t <window-name> # Switch to the specified window.
Ctrl+b c: creates a new window, the status bar will show information about multiple windows.
Ctrl+b p: switch to the previous window (in the order on the status bar).
Ctrl+b n: switch to the next window.
Ctrl+b <number>: switch to the window with the specified number, where <number> is the window number on the status bar
Ctrl+b w: select window from the list
Ctrl+b ,: rename the window.
8 echo output message
echo [-neE] [String]

Options:

  • -E (default) not supported \ Interpretation function
  • -n No automatic line feeds
  • -e Enable interpretation of \ characters
echo "$VAR_NAME"

Enable the -e command option, so that if the following characters appear in a string, they will be handled specifically and not output as normal text

  • \a Warning sound
  • \b Backspace
  • \c No line breaks at the end
  • \e escape, equivalent to \033
  • \n Line feeds and cursor moves to the beginning of the line
  • \r Carriage return, i.e., the cursor moves to the beginning of the line, but does not change lines
  • \t insert tab
  • \ Insertion of \characters
  • \0nnn Insert the ASCII character represented by nnn (octal)
  • \xHH insert the ASCII number represented by HH (hexadecimal) (man 7 ascii)

In the terminal, ANSI defines Escape Screen Control Codes for on-screen displays with colored characters in the following format.

"\033[character background color;font color m string\033[0m"

\033[30m -- \033[37m Setting the foreground color

\033[40m -- \033[47m Setting the background color

# Character background color range: 40--47
40:Black
41:Red
42:Green
43:Yellow
44:Blue
45:Purple
46:Dark Green
47:White
#Font Color: 30--37
30: Black
31: Red
32: Green
33: Yellow
34: Blue
35: Purple
36: Dark Green
37: White

Adding color is just one of the following control codes; here are some common ANSI control codes:

\033[0m Close all properties
\033[1m Setting the highlighting
\033[4m Underline
\033[5m Blinking
\033[7m Reflective
\033[8m Fade
\033[nA Cursor up n lines
\033[nB Cursor moves down n lines.
\033[nC Cursor right n columns
\033[nD Cursor left n columns
\033[x;yH Set cursor position x rows and y columns.
\033[2J Clear screen
\033[K Clear from cursor to end of line
\033[s Save cursor position
\033[u Restore cursor position
\033[?25l Hide Cursor
\033[?25h Show cursor
\033[2J\033[0;0H Clear the screen and put the cursor on top.
9 bash shortcuts
Ctrl + l clears the screen, equivalent to the clear command.
Ctrl + o Execute the current command and redisplay the command
Ctrl + s Block screen output, lock the screen
Ctrl + q Allow screen output, unlocked
Ctrl + c Terminate command
Ctrl + z Hang the command
Ctrl + a Cursor moves to the beginning of the command line, which is equivalent to Home.
Ctrl + e Moves the cursor to the end of the command line, which is equivalent to End.
Ctrl + f Moves the cursor one character to the right.
Ctrl + b moves the cursor one character to the left.
Ctrl + xx moves the cursor between the beginning of the command line and the cursor.
Alt + f Cursor moves one word to the right at the end of the word
Alt + b moves the cursor one word to the beginning of the word to the left
Ctrl + u Delete from cursor to beginning of command line
Ctrl + k Delete from cursor to end of command line
Alt + r Delete the current line
Ctrl + w Delete left from cursor to beginning of word
Alt + d Delete right from cursor to end of word
Alt + Backspace Delete left word
Ctrl + d Delete a character at the cursor
Ctrl + h Delete a character before the cursor
Ctrl + y Paste the deleted character after the cursor.
Alt + c Changes the word to the right from the cursor to the word with the first letter capitalized.
Alt + u Changes a word to the right from the cursor to upper case
Alt + l Starting at the cursor, change the right word to lowercase
Ctrl + t Swap cursor and previous character position
Alt + t Swaps cursor and previous word position
Alt + # Repeats the character # times after prompting for the specified character.
10 Document wildcards
* Matches zero or more characters, but not "." files, i.e., hidden files
? Match any single character, a Chinese character is also counted as a character.
~ current user home directory
~mage User mage home directory
. and ~+ current working directory
~- previous working directory
[0-9] Match a range of numbers
[a-z] one letter
[a-z] one letter
[wang] matches any one character in the list
[^wang] matches all but one character in the list
[^a-z] matches all but one character in the list
[:digit:]: any number, equivalent to 0-9
[:lower:]: any lowercase letter, a-z
[:upper:]: Any uppercase letter, A-Z.
[:alpha:]: Any uppercase or lowercase letter.
[:alnum:]: arbitrary numbers or letters
[:blank:]: horizontal blank characters
[:space:]: Horizontal or vertical blank characters.
[:punct:]: punctuation mark
[:print:]: printable characters
[:cntrl:]: control (non-printable) characters
[:graph:]: Graphics characters
[:xdigit:]: Hexadecimal characters
11 Secure deletion of documents

Although rm deletes the file, the deleted file may still be recovered, and in scenarios with high security requirements, you can use shred to securely delete the file.

  • -z Add 0 to the last overwrite to hide the overwrite operation
  • -v can display the progress of the operation
  • -u Truncate and delete files after overwriting
  • -n # Specify the number of times to overwrite the contents of the file (default is 3)

Tip

Please, this article for the author to organize the study notes, only for personal reference and combing ideas, errors and omissions, please provide more guidance.

Such as, this article refers to a variety of resources to organize not, and therefore did not apply for the original statement, if any infringement, please contact to delete.

If, if you need to reprint please indicate the source of the big brother, the garden is not much more generous.

Also, this article is for learning purposes only, please do not use it in illegal ways.