Tangwx

Tangwx

博客网站

Using vim

Using vim#

mkdir folder name to create a folder

rm -rf file name to delete a file

cp original file path destination path to copy a single file

cp original file path -r destination path to copy a folder

mv original file or folder name renamed name to rename a file or folder

View file: cat file name

Save the searched content as another file grep search content file name>new file name

cat file name | grep search content file name>new file name

Add permissions

chmod +x file name

Create test.sh:

date

Add x permission

chmod +x test.sh

Run sh file

./test.sh
[test@iZuf64fh3qqfih9qs3bh1oZ tang]$ vi test.sh
[test@iZuf64fh3qqfih9qs3bh1oZ tang]$ ll
total 36
drwxrwxr-x 9 test test  4096 Sep 15 17:15 1
-rw-rw-r-- 1 test test   878 Sep 15 17:32 SentimentModel_def
-rw-rw-r-- 1 test test 14794 Sep 15 17:10 SentimentModel.py
-rw-rw-r-- 1 test test     5 Sep 15 20:22 test.sh
-rw-rw-r-- 1 test test    76 Sep 15 19:16 test.txt
drwxrwxr-x 2 test test  4096 Sep 15 19:21 xieqiang
[test@iZuf64fh3qqfih9qs3bh1oZ tang]$ chmod +x test.sh
[test@iZuf64fh3qqfih9qs3bh1oZ tang]$ ll
total 36
drwxrwxr-x 9 test test  4096 Sep 15 17:15 1
-rw-rw-r-- 1 test test   878 Sep 15 17:32 SentimentModel_def
-rw-rw-r-- 1 test test 14794 Sep 15 17:10 SentimentModel.py
-rwxrwxr-x 1 test test     5 Sep 15 20:22 test.sh
-rw-rw-r-- 1 test test    76 Sep 15 19:16 test.txt
drwxrwxr-x 2 test test  4096 Sep 15 19:21 xieqiang
[test@iZuf64fh3qqfih9qs3bh1oZ tang]$ ./test.sh 
Wed Sep 15 20:23:15 CST 2021

Create empty file touch file name

Display line breaks in Linux text files
cat -A filename

Copy and paste
yy to copy the entire line
p to paste

Delete lines
dd to delete the line where the cursor is located
ndd to delete the next n lines where the cursor is located
dG to delete from the cursor to the last line of data

Delete selected text
If in command mode, use v or Ctrl+v to select a block of text, then press x to delete

Copy selected text
If in command mode, use v or Ctrl+v to select a block of text, then press y to copy and p (or P) to paste

Display line numbers
:set nu

Disable line numbers
:set nonu

Editing multiple files
:n to edit the next file
:N to edit the previous file
:files to list all files currently open in vim

DOS and Linux line breaks
dos2 unix [-kn] file [newfile] to convert to Unix
unix2 dos [-kn] file [newfile] to convert to DOS

Wildcards and special characters
* represents zero or more characters (or numbers)
? represents exactly one letter
# is used for comments, commonly used in scripts as an explanation
\ is the escape character, used to restore "special characters or wildcards" to normal characters

| separates two piped commands

; is used to separate consecutive commands (note that it is different from the pipe command)

& makes the command work in the background

! logically means "not"

/ is the path separator symbol

> and >> are used for output redirection, for "replace" and "append" respectively

' does not have variable substitution functionality

" has variable substitution functionality

`` ` ` ` between two "`" is a command that can be executed first

() is used to indicate the start and end of a subshell

[] is used to indicate a combination of characters

{} is used to indicate a combination of command blocks

Pipe command (pipe)

  1. cut
    echo $PATH|cut -d ':' -f 3

  2. Remove the \r of the shell
    cat my_shell.sh|tr -d '\r' > my_shell.sh

//------------------------Regular Expression----------------------------------------------------------

  1. grep [-acinv] 'search string' filenames
    Parameters:
    -a: Search data in binary files as text files
    -c: Count the number of times the 'search' string is found
    -i: Ignore case differences
    -n: Output line numbers
    -v: Select lines that do not contain the 'search string'

  2. Search for a set of strings using []
    grep -n 't[ae]st' filenames: [] represents one character, so it will search for both 'tast' and 'test'
    grep -n '[^g]oo' filenames: Extract the string before 'oo' that does not contain 'g'
    grep -n '[^a-z]oo' filenames: Extract the string before 'oo' that does not contain lowercase letters (if the requirement is numbers and English, it will be [a-zA-Z0-9])

  3. Line start and end characters ^ $

grep -n '^the' filenames`: Extract lines that start with 'the' (note that ^ inside [] and outside [] is different, inside [] it represents reverse selection, outside [] it represents positioning at the beginning of the line)
grep -n '\.$' filenames`: Extract lines that end with '.'
  1. Any character . and repeated character *
    grep -n 'g..d' filenames: Extract lines that contain 'g' and 'd' characters, with two characters between 'g' and 'd'
    grep -n 'oo*' filenames: Extract lines that contain one or more 'o' ( * represents zero or more any characters)

  2. Limit the range of repeated characters {}
    grep -n 'o\{2,5\}' filename: Extract lines that contain 2 to 5 'o' characters (note: {} has special meaning in shell, so it needs to be escaped with )

  3. Introduction to sed tool
    Delete
    cat -n /etc/passwd | sed '2,2d': Delete lines 2 to 12
    cat -n /etc/passwd | sed '12,$d': Delete line 12 to the last line
    cat -n /etc/passwd | sed '2a hello wanlx': Add 'hello wlx' after line 2
    cat -n /etc/passwd | sed '5i hello wanlx': Add 'hello wlx' after line 5
    cat -n /etc/passwd | sed '2,5c hello wanlx': Replace lines 2 to 5 with 'hello wlx'
    cat -n /etc/passwd | sed -n '5,7p': Display lines 5 to 7

  4. Introduction to awk tool (good stuff)
    Split strings by space and tab

//--------------------Learning shell scripts-------------------------------------------------------------

  1. Use the comparison symbol []
    1.1 Separate each component with a space, for example: [ "$HOME" == "$MAIL" ]
    1.2 Variables inside the brackets are better set with double quotes.
    1.3 Constants inside the brackets are better set with single or double quotes.

  2. Tracing and debugging shell scripts
    sh [-nvx] filename.sh
    -n: Do not execute the script, only check for syntax issues
    -v: Print the contents of the script to the screen before executing the script
    -x: Display the script content used (trace the execution of the script)

//----------------Routine commands executed in loops--------------------------------------------------
crontab [-u username] [-l|-e|-r]
Parameters
-u: Only root can execute this task, that is, help other users to create/delete crontab
-e: Edit the work content of crontab
-l: View the work content of crontab
-r: Delete the work content of crontab (all content)

30 10 * * * /bin/sh /root/shell_script/mongodb/mongo_log_mgr.sh

Edit the task
crontab -e
*/1 * * * * cd /home/wanlx/myworkspace;date >> date.wlx
Minute Hour Day Month Week

After editing, restart the crond service (not necessary for Red Hat 6.4)
/etc/init.d/crond restart

Compile the system's email address
vi /etc/crontab and then modify MAILTO

//---------------Managing background jobs-------------------------------------------
kill -signal %jobnumber
Parameters
-l: List the signals that can be used with kill
signal: Indicates what instruction to give to the job behind (not sure what it does). Use man 7 signal to find out
-1: Indicates to reread the parameter settings file once (similar to reload)
-2: Indicates the same action as [ctrl]-c entered from the keyboard
-9: Immediately force delete a job
-15: Terminate a job in a normal programmatic way

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.