Home  >  Article  >  Operation and Maintenance  >  Improve your command-line skills: A complete guide to Bash and Linux symbols and commands

Improve your command-line skills: A complete guide to Bash and Linux symbols and commands

王林
王林forward
2023-06-25 15:02:161732browse

This guide will lead you to explore the vast world of Linux and Bash symbols and commands. We'll cover a variety of topics such as file system navigation, text processing, networking, system administration, and more, and provide practical examples for each command. Whether you're an experienced Linux user or just getting started, this guide can help you improve your command line skills and become more efficient in your daily work. So open your terminal and let’s get started!

提升你的命令行技能:Bash 和 Linux 符号与命令的完整指南

Redirection

One of the most basic yet powerful features of Bash is the ability to redirect the input and output of commands. Here are some symbols that can be used for redirection:

  • >: Redirects the output of the command to a file, overwriting its contents if the file already exists. For example, ls > linuxmi.txt writes the output of the command to ls named linuxmi.txt.
  • >>: Redirects the output of the command to a file, if the file already exists. Append it to the end of the file. For example, echo "Hello, world!www.linuxmi.com" >> linuxmi.txt will append the string "Hello, world!www.linuxmi.com" to the end of the file.
  • <: Change the input of the command from the keyboard to the file. For example, sort < linuxmi.txt will sort the contents of linuxmi.txt.
  • 2>: Used to redirect the error output of the command to a file.
  • 2>>: Used to append the command’s error output to a file.
  • &>: Used to redirect both the output and error output of the command to a file.
  • &>>: Used to append the output and error output of the command to a file.

提升你的命令行技能:Bash 和 Linux 符号与命令的完整指南

Pipeline

You can use pipes to connect multiple commands together so that the output of one command becomes the input of another command. The symbol for a pipe is |. Here is an example:

ls -l | grep "linuxmi"

This command lists the contents of the current directory in long format ( ls -l ), then passes the output to the grep command, which searches the output for the string "linuxmi".

  • tee: Used to split the output of a command and send it to files and standard output.

提升你的命令行技能:Bash 和 Linux 符号与命令的完整指南

Wildcards and Wildcards

Bash supports a powerful feature called wildcards, which allows you to use wildcards to match filename patterns. Here are some of the most commonly used symbols in globbing:

  • *: Matches any number of characters in the file name. For example, ls *.txt will list all files in the current directory that begin with .txt.
  • ?: and match any single character in their names. For example, ls file?.txt will list all files in the current directory that begin with file and are followed by only one character, followed by .txt.
  • [ ]: Matches any one of a set of characters. For example, ls file[123].txt will list all files in the current directory that start with or and end with file, followed by .123.txt

common expression

  • grep: Used to search for patterns in files or streams.
  • sed: Used to perform text conversion on files or streams.
  • awk: used for graphics scanning and processing.
  • egrep: Regular expression for expansion.
  • fgrep: used for fixed string regular expressions.
  • regex: Use special characters and operators to specify symbols for text patterns.

Variable expansion and environment variables

  • $: used to reference the value of a variable.
  • export: used to create or modify environment variables in Bash.
  • ${}: used for variable expansion.
  • $(()): used for arithmetic expansion.
  • env: used to display the current environment variables.
  • echo: used to display the text or value of environment variables.

提升你的命令行技能:Bash 和 Linux 符号与命令的完整指南

通配符和通配符

  • *: 匹配文件名中任意数量的字符。
  • ?: 匹配文件名中的任何单个字符。

否定

  • !:与其他命令结合使用,执行取反。

算术运算

  • +:算术表达式中的加法运算符。例如,echo $((2+3))将输出5.
  • -:算术表达式中的减法运算符。例如,echo $((16-5))将输出11.
  • *:算术表达式中的乘法运算符。例如,echo $((4*5))将输出20.
  • /: 算术表达式中的除法运算符。例如,echo $((20/2))将输出10.
  • $((expression)):用于在 Bash 中执行算术运算。例如,echo $((2 + 6))将打印值 8。
  • expr:用于执行算术运算。

提升你的命令行技能:Bash 和 Linux 符号与命令的完整指南

比较

  • ==:用于比较条件表达式中两个值是否相等。例如,if [ "$name" == "linuxmi" ]; then echo "Hello, linuxmi!"; fi将输出“Hello, linuxmi!” 如果变量的name值为“linuxmi”。
  • !=:用于比较条件表达式中的两个值是否不相等。例如,if [ "$name" != "linuxmi" ]; then echo "You're not linuxmi!"; fi将在变量name的值不为“John”输出“You’re not linuxmi!”。

条件执行

  • &&:用于仅在前一个命令成功执行时执行后续命令。例如,command1 && command2 会仅在 command1 成功执行后才执行command2。
  • ||:用于仅在前一个命令失败时执行后续命令。例如,command1 || command2会仅在 command1 失败后才执行 command2。

条件语句

  • if:用于检查条件是否为真并相应地执行命令。例如,if [[ $foo == "bar" ]]; then echo "foo is bar"; fi将检查变量是否foo等于“bar”,如果为真则打印“foo is bar”。
  • elifif:如果语句中的先前条件为假,则用于检查其他条件。例如,if [[ $foo == "bar" ]]; then echo "foo is bar"; elif [[ $foo == "baz" ]]; then echo "foo is baz"; else echo "foo is something else"; fi将检查是否foo等于“bar”并在为真时打印“foo is bar”,否则检查是否foo等于“baz”并在为真时打印“foo is baz”,最后打印“foo is something else”如果前面两个条件都为假。
  • [[ ]]:用于在 Bash 中执行条件测试。
  • case:用于将变量与模式列表进行匹配并相应地执行命令。例如:
case $fruit inapple)echo "It's an apple";;banana)echo "It's a banana";;*)echo "It's something else";;esac

将检查变量的值fruit,如果等于“apple”则打印“It’s an apple”,如果等于“banana”则打印“It’s a banana”,对于任何其他值则打印“It’s something else”。

Loop

  • for: Used to create a loop that traverses a list of items. For example, for fruit in apple banana orange; do echo $fruit; done will output "apple", "banana", and "orange" on separate lines.
  • while: Used to create a loop that will continue to execute commands as long as the condition is true. For example, while true; do echo "Hello"; sleep 1; done will repeatedly output "Hello" until the script is interrupted.
  • until: Used to execute commands until the condition is true.

Function

  • function: used to define a function in Bash. For example, function hello { echo "Hello, $1!"; } defines a function named hello that accepts one parameter and prints "Hello," followed by the parameter.
  • $1, $2, etc.: used to refer to the first, second, third, etc. parameters passed to the function. For example, in the hello function defined above, $1 refers to the first parameter passed to the function.

Array

  • []: used to define an array in Bash. For example, my_array=(apple banana orange) defines an array named my_array "apple", "banana", and "orange".
  • ${array[@]}: used to reference all elements in an array in Bash. For example, echo ${my_array[@]} will output "apple banana orange" in one line.

Process Control and Management

  • &: Used to run commands in the background.
  • ctrl-c: Used to terminate a running command.
  • ctrl-z: Used to pause a running command.
  • ps: Used to display information about running processes.
  • kill: Used to terminate a running process.
  • nohup: Used to run commands in the background, even if the terminal is closed.
  • nice: used to set the priority of the process.
  • renice: used to modify the priority of an already running process.

File system operations

  • cp: used to copy files.
  • mv: used to move or rename files.
  • rm: used to delete files.
  • mkdir: used to create directories.
  • rmdir: used to delete directories.
  • ln: Used to create links between files or directories.

File and text operations

  • head: Used to display the first few lines of the file.
  • tail: used to display the last few lines of the file.
  • sort: Used to sort lines of text alphabetically or numerically.
  • uniq: used to remove duplicate lines of text.
  • cut: Used to extract specific columns or fields from a line of text.
  • paste: Used to merge lines from multiple files.

Permissions

  • chmod: used to change the permissions of files and directories.
  • chown: used to change the owner of a file or directory.
  • chgrp: Group used to change files or directories.
  • umask: used to set the default permissions for new files and directories.

String operations, searches and comparisons

  • cut: used to extract specific columns from a file or stream.
  • tr: Used to translate or delete characters.
  • sort: Used to sort lines of text.
  • uniq: Used to remove duplicate lines from a file or stream.
  • grep: used to search for patterns in files or streams.
  • sed: Used to perform text conversion on files or streams.
  • awk: used for graphics scanning and processing.
  • diff: Used to compare two files line by line.
  • cmp: Used to compare two files byte by byte.

Network

  • ping: used to test the connection to the network host.
  • curl: used to transfer data to or from the server.
  • wget: Used to download files from the Internet.
  • ssh: Used to connect to remote computers securely.
  • nslookup: Used to query a DNS server to resolve hostnames to IP addresses.
  • ifconfig: used to display and configure network interfaces.
  • netstat: Used to display network statistics and the number of active connections.

SSH and Encryption

  • ssh-keygen: Used to generate and manage SSH keys for secure remote access.
  • scp: Used to securely transfer files between systems over an SSH connection.
  • openssl: For cryptographic functions such as generating and managing SSL/TLS certificates, encryption and decryption.
  • gpg: used for encryption, decryption and digital signature verification.

System Information, Maintenance and Administration

  • sudo: Used to run commands with elevated privileges.
  • su: Used to switch to a different user account.
  • systemctl: Used to manage system services in systemd-based Linux distributions.
  • uptime: Used to display how long the system has been running.
  • journalctl: used to view system logs.
  • fdisk: used to create and operate disk partitions.
  • mount: Used to mount the file system on the system.
  • top: Used to display system resource usage.
  • htoptop: A more advanced version that provides real-time system monitoring and resource usage information.
  • ps: Used to display information about running processes.
  • df: used to display disk space usage.
  • du: Used to display the size of a file or directory.
  • apt-get: Package management tool for installing, removing and updating software packages.
  • dpkg: Package management tool for installing and managing software packages.
  • shutdown: Used to shut down or restart the system.
  • free: Used to display system memory usage.
  • uname: Used to display system information such as kernel version, host name, etc.
  • lscpu: used to display CPU information.
  • lsusb: Used to display the USB devices connected to the system.
  • nmon: Used to display detailed system performance information, including CPU, memory, disk I/O and network usage.
  • sar: Used to collect and report system performance data over a period of time.
  • lshw: used to display system hardware information.
  • dmidecode: used to display the BIOS and hardware information of the system.
  • lsblk: Used to display information about block devices (such as hard disks) in the system.
  • lspci: Used to display PCI device information connected to the system.
  • lsusb: Used to display USB device information connected to the system.

User input

  • read: Used to read user input from the command line.
  • select: Used to display a menu of options to the user.

Command line editing

  • ctrl-a: used to move the cursor to the beginning of the line.
  • ctrl-e: Used to move the cursor to the end of the line.
  • ctrl-k: Used to delete text from the cursor position to the end of the line.
  • ctrl-u: Used to delete text from the cursor position to the beginning of the line.

Text Edit

  • nano: A simple command line text editor.
  • vi or vim: A more advanced text editor with many features.
  • cat: Used to display the contents of a file on the terminal.
  • head: Used to display the first few lines of the file.
  • tail: used to display the last few lines of the file.

Job Control

  • jobs: Used to display the background job list.
  • fg: used to bring background jobs to the foreground.
  • bg: Used to move paused or stopped jobs to the background.
  • kill: Used to terminate a running process.

User and Group Management

  • useradd: used to create new user accounts.
  • usermod: used to modify existing user accounts.
  • userdel: used to delete user accounts.
  • groupadd: used to create new user groups.
  • groupmod: used to modify existing user groups.
  • groupdel: used to delete user groups.
  • passwd: used to change user password.

Debugging and Troubleshooting

  • strace: Used to trace system calls and signals issued by a process.
  • ltrace: Used to track library calls made by a process.
  • gdb: A powerful debugger for debugging C and C programs.
  • tcpdump: Used to capture and analyze network traffic.
  • wireshark: GUI-based tool for analyzing network traffic. ellaneous
  • echo: Used to display messages on the terminal.
  • printf: used to format and display data.
  • date: used to display or set the system date and time.
  • tar: Used to create or extract compressed archives.

Various

  • cal: used to display the calendar.
  • date: Used to display or modify the system date and time.
  • sleep: Used to delay the execution of a script or command for a specified amount of time.
  • tee: Used to redirect output to the screen and files.
  • history: Used to display the command history of the current shell session.
  • echo: Used to print text to the terminal or output to a file.

Please note that the classification of symbols described in this guide is not absolute but is based on a possible classification. Some symbols may apply to more than one category, and there may be some overlap or duplication. The list of symbols is not completely exhaustive and may vary between Linux distributions and versions. Readers are encouraged to consult their system documentation for more information. This guide is intended to provide a comprehensive overview of Linux and Bash flags and commands, but is not intended to be a definitive resource.

Thanks for reading our comprehensive guide to Linux and Bash symbols and commands. I hope you found this guide helpful in mastering the command line. To stay up-to-date on the latest tips, tricks, and best practices for using these commands in your daily work, be sure to stay tuned to Linux Mito www.linuxmi.com for future articles. We'll provide detailed examples and practical examples to help you become a more proficient Linux user and command line expert.

The above is the detailed content of Improve your command-line skills: A complete guide to Bash and Linux symbols and commands. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete