Linux command line and shell script programming ------ self-summary

1. Traverse the directory

cd xxx

The pwd command displays the current directory for that shell session

A single dot (.) indicates the current directory
and a double dot (…) indicates the parent directory of the current directory

If necessary, you can use multiple double dots to switch up the directory
cd .../.../xxx

2. List of files and directories

ls command: Display the files and directories in the current directory. Note that the output is sorted alphabetically (by column)

Parameters:
ls -F //distinguish between files and directories

ls -a. //Display hidden files and ordinary files and directories together

ls -R //List the files in the subdirectories contained in the current directory

ls -l //Display additional information in detail

To filter the output list, use wildcards.
A question mark (?) represents one character. An
asterisk (*) represents zero or more characters.
Brackets can also be used, such as
ls -lf[ai]ll
ls -lf[!a]ks. / / Use an exclamation point (!) to exclude unwanted content

3. Processing files

touch xxx. //Create a file, you can also change the modification time of the file

cp source object target object // target object is the new modification time

cp -i //If the target file exists, it will be queried
cp -R //Recursively copy the contents of the entire directory
such as cp -R source object target object directory/

mv command:
1. You can rename
2. Move files/directories
Note: The inode number and timestamp remain unchanged, because mv only affects the file name and file location

mv -i parameter, when the command tries to overwrite existing files, there will be a prompt message

rm deletes files
rm -i prompts when deleting files
rm -f force deletes

Link files:
1. Symbolic link
2. Hard link
Linux-------soft link and hard link

4. Processing directory

mkdir command: Create a directory
mkdir -p xxx/xxx/xxx Create directories and subdirectories at the same time

rmdir command: delete a directory, only empty directories can be deleted
rm -rf xxx Recursively delete directories and files
Use the tree tool to beautifully display directories, subdirectories and files in them

5. View file content

file command: You can view some information about the
file file file name/directory name

cat command: view the entire file
cat file name
cat -n plus line number

more command

less command
Linux command—less

tail command: View the tail information of the file
tail -n 100 file. //View the tail 100 lines of information
tail -f file View the tail information of the file in real time

head command: View file header information

5. Monitor disk space

df -h //Display the information of each mounted file system with data
df -h file. //Display the information of the currently mounted file system

du -h -d 1 [directory]
du -h --max-depth=1 directory
//-d option restricts the directory level, -d option is the abbreviation of –max-depth
// only view the first layer of subdirectories to use storage Condition

ps, top command

6. Built-in commands and external commands

External commands, sometimes called file system commands, are programs that exist outside of bash/shell;
they are not part of the shell program;
external command programs are usually located in /bin, /usr/bin, /sbin, /usr /sbin;
when an external command is executed, a subprocess (subshell) will be created.

Built-in commands: do not need to use subprocesses to execute, and exist as an integral part of shell tools.

You can use the type command to know whether a command is built-in.
example:

type cd 

type -a echo 
//查看命令的不同实现,使用type命令的-a选项,因为有些命令既有内建命令也有外部命令

which pwd //which命令只显示外部命令文件,如果想要使用外部命令实现,直接指明对应的文件即可

which pwd
/bin/pwd
//例如,要使用外部命令pwd,可以输入/bin/pwd

7. History command

history command:
1. Used to display historical records and executed commands
2. When logging in to the shell or exiting, it will automatically read and store

 history (选项)(参数)

n Display the latest n records
-a Write the commands in the historical command buffer into the historical command file
-c Delete all the history content in the current shell, the actual deletion is false
-r Read the commands in the historical command file into Current history command buffer
-w Write the current history command buffer command into the history command file
-d Delete the specified line in the history

!Number//Execute the Nth command in the history command
! String//Search the last command starting with xxxx character in the history command, for example! vim

Quickly search historical commands
1. Requirements description

When executing a command, you can use the up and down keys to search for the historical commands that have been entered. If it is a few recent commands, it is easy to find.

Two, the solution

1. Method 1: history command + history command

history|grep mysql
insert image description here
2. Method 2 ctr+r shortcut key (recommended)

A faster method is to use the "ctrl+r" shortcut key, use ctrl+r on the command line, ctrl+r is reverse search (reverse-i-search)

The effect is as shown below

Enter the keyword of the command you want to find, and it will be displayed in the second red mark position. If the corresponding command is found, it will be displayed in the third mark position. If it is not the command you expect, you can use "ctrl+r" multiple times to switch Display the command, then press enter or -> to execute the command.
insert image description here

Original link: https://blog.csdn.net/u010865136/article/details/78059645

Guess you like

Origin blog.csdn.net/weixin_43743711/article/details/128462059