Tangwx

Tangwx

博客网站

vim usage

Using vim#

mkdir folder_name creates a folder

rm -rf file_name deletes a file

cp original_file_path destination_path copies a single file

cp original_file_path -r destination_path copies a folder

mv original_file_or_folder_name renamed_name renames a file or folder

View file: cat file_name

Save the searched content as a different 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 - copy the entire line
p - paste

Delete lines:
dd - delete the line where the cursor is
ndd - delete the next n lines after the cursor
dG - delete from the cursor to the last line

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

Multiple file editing:
:n - edit the next file
:N - edit the previous file
:files - list all currently open files in vim

DOS and Linux line breaks:
dos2unix [-kn] file [newfile] - convert to Unix format
unix2dos [-kn] file [newfile] - convert to DOS format

Wildcards and special characters:
* - represents zero or more characters (or digits)
? - represents exactly one letter
# - comment, commonly used in scripts as an explanation
\ - escape character, used to treat special characters or wildcards as regular characters

| - separates two piped commands

; - separates consecutive commands (note: different from pipe command)

& - makes a command run in the background

! - logical "not" operator

/ - path separator symbol

> and >> - output redirection, replace or append

' - does not support variable substitution

" - supports variable substitution

` ` - execute the command inside the backticks first

() - marks the start and end of a subshell

[] - represents a combination of characters

{} - represents a combination of command blocks

Piped commands (pipe):

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

  2. Remove shell's carriage return
    cat my_shell.sh | tr -d '\r' > my_shell.sh

------------------------Regular Expressions----------------------------------------------------------

  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 sensitivity
    -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 searches for both 'tast' and 'test'
    grep -n '[^g]oo' filenames: selects strings before 'oo' that do not contain 'g'
    grep -n '[^a-z]oo' filenames: selects strings before 'oo' that do not contain lowercase letters (if the requirement is numbers and letters, it would be [a-zA-Z0-9])

  3. Beginning and end of line characters ^ $

grep -n '^the' filenames`: selects strings that start with 'the' (note: ^ inside and outside [] has different meanings, inside [] it represents negation, outside [] it represents the beginning of a line)
grep -n '\.$' filenames`: selects lines that end with '.'
  1. Any character . and repeated character *
    grep -n 'g..d' filenames: selects lines that contain 'g', 'd', and two characters in between
    grep -n 'oo*' filenames: selects lines that contain one or more 'o' characters (* represents zero or more characters)

  2. Limiting the range of repeated characters {}
    grep -n 'o\{2,5\}' filename: selects 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': deletes the 2nd to 12th lines
    cat -n /etc/passwd | sed '12,$d': deletes the 12th line to the last line
    cat -n /etc/passwd | sed '2a hello wanlx': adds 'hello wanlx' after the 2nd line
    cat -n /etc/passwd | sed '5i hello wanlx': adds 'hello wanlx' after the 5th line
    cat -n /etc/passwd | sed '2,5c hello wanlx': replaces lines 2-5 with 'hello wanlx'
    cat -n /etc/passwd | sed -n '5,7p': displays lines 5-7

  4. Introduction to awk tool (useful)
    Split strings by spaces and tabs

//--------------------Learning Shell Script-------------------------------------------------------------

  1. Using conditional symbols []
    1.1 Separate each component with spaces, for example: [ "$HOME" == "$MAIL" ]
    1.2 Use double quotes to set variables inside the brackets.
    1.3 Use single or double quotes to set constants inside the brackets.

  2. Tracing and debugging shell scripts
    sh [-nvx] filename.sh
    -n: only check syntax without executing the script
    -v: display the script's content before executing it
    -x: display the script's content during execution (trace the script's execution)

//----------------Routine commands for loop execution--------------------------------------------------
crontab [-u username] [-l|-e|-r]
Parameters:
-u: only root can execute this task, used to create/delete crontab for other users
-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 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

Configure the email address for system mail sending
vi /etc/crontab and modify MAILTO

//---------------Managing background jobs-------------------------------------------
kill -signal %jobnumber
Parameters:
-l: list the signals that can be used with kill
signal: indicates what action to take for the job (not sure what it does). Use man 7 signal to find out.
-1: reread the parameter settings file (similar to reload)
-2: perform the same action as when [ctrl]-c is pressed
-9: forcefully terminate a job immediately
-15: terminate a job in a normal program manner

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