SoFunction
Updated on 2025-03-10

A summary of shell commands to improve your work efficiency

Preface

Everyone knows that Shell is a program written in C language, and it is a bridge for users to use Linux. Shell is a command line interpreter. Its function is to explain and execute the user's command. When the user enters a command, the shell explains and executes it. This method is called Interactive. The following article mainly shares some shell commands for improving work efficiency. Without further ado, let’s take a look at the detailed introduction:

1. Switch directory

Notice:The current user is xiaochao, the system is centos6, and the shell command is strictly case-sensitive.

Show the current directory path: pwd

pwd
output:/home/xiaochao/Download

Switch directory: cd

1. Use relative paths

cd Download #Enter the current pathDownloadTable of contents

2. Use absolute paths

cd /var/log

Use of dot (.)

cd . # means entering the current directorycd .. # means entering the previous directory of the current directorycd ... #Indicates entering the upper two-level directory of the current directory,thisbashNot supported,zshsupport。Analogous in sequence。

Use of tilde (~)

  • The tilde represents the user directory, that is, the alias of the environment variable $HOME. For the tutorial environment, it is /home/xiaochao.
  • If the cd command does not add any parameters, it is equivalent to executing the cd ~ command.

Use of short horizontal sign (-)

cd - represents the directory entered by the last cd command, and its function is similar to the rear leg function of the windows file manager. However, when you use cd - to enter the last directory, the current directory becomes the last directory, for example.

Assume that the current directory is /home/xiaochao

cd Download #The current directory is /home/xiaochao/Downloadcd /home/xiaochao #The current directory is /home/xiaochaocd - #The current directory is /home/xiaochao/Downloadcd - #The current directory is /home/xiaochaocd - #The current directory is /home/xiaochao/Downloadcd - #The current directory is/home/xiaochao

Transfer

When we have two directories, and the contents in these two directories are the same and the directory names are inconsistent, the common scenario is the backup directory and the source directory. To switch between two directories, you can use the CD transfer function, for example.

Suppose we have a directory connected, /home/xiaochao/aa/bb/cc/dd, /home/xiaochao//bb/cc/dd

cd /home/xiaochao/aa/bb/cc/dd #Enter the directorycd aa  #Enter/home/xiaochao//bb/cc/dd

2. Execute multiple commands

The latter command depends on the output of the previous command, and can be piped (|)

ls | wc -l #Current directory files

The latter command must be run after the previous command is successfully run. You can use the double-Agree number (&&)

aa && ls #Run onlyaa,lsNot running

The latter command must wait for the previous command to run, and do not care whether it is successful or not, use the '&' number'

aa & ls #aaandlsAll run,butlsMust waitaaRunning。

Execute multiple commands in parallel, using two vertical numbers (||)

aa || ls #aaandlsParallel execution,No effect on each other。

3. The wonderful use of ctrl key

  • ctrl+a: Return to the current input/ and insert characters at the beginning of the line, no need to hold down the arrow keys.
  • ctrl+e: contrary to the previous combination, return to the end of the line.
  • ctrl+l: Clear the current terminal interface, the effect is equivalent to the clear command.
  • ctrl+u: Clear all inputs in the current input line. Suppose you enter aa bb and press this key combination, aa bb will be deleted.
  • ctrl+y: It is to paste the string deleted by ctrl+u back.
  • ctrl+r:Historical command search. After pressing ctrl+r, the command containing the string you entered will be searched.
  • ctrl+c: Terminates the program running in the current terminal.
  • ctrl+d:Push the current terminal.
  • ctrl+z: Put the currently running program on the terminal in the background to run.

4. Other commonly used shell commands

  • $?: The result returned by the previous command.
  • !$:The last string of the previous command
  • !!:Previous command
  • man ascii: Check the ascii code table and press q to exit.
  • >: Create a file, shorter than touch.
  • du -s * | sort -n | tail: Lists the largest 10 files in the current directory.
  • ssh user@server bash < : Remotely execute a shell script. No copying required.
  • convert -gravity NorthWest -background transparent -extent 720×200: Change the size of the picture, don't need to install something as big as ps.
  • fgrep -r "Hello World" ./* : Query the file containing hello world under the current target. -r means that the query includes subdirectories.
  • locate: Query files with a specific file name, but mlocate needs to be installed and the index is updated periodically using the updatedb command.

Summarize

The above is the entire content of this article. I hope that the content of this article will be of some help to everyone’s learning or using shell. If you have any questions, you can leave a message to communicate. Thank you for your support.