Skip to main content

Shell Basic Commands

Quickly Write to File​

  1. Method 1
cat > a.txt << 'EOF'
afasdf
EOF
  1. Method 2
tee a.txt <<- 'EOF'
afsafdsf
EOF

Linux System Scheduled Network Time Synchronization​

Reference Article

# Set scheduled task
crontab -e
*/5 * * * * ntpdate time.nist.gov

# View scheduled task list
crontab -l

# View scheduled task log
tail -f /var/log/cron

# Several time servers
ntpdate time.nist.gov
ntpdate time.nuri.net
ntpdate 0.asia.pool.ntp.org
ntpdate 1.asia.pool.ntp.org
ntpdate 2.asia.pool.ntp.org
ntpdate 3.asia.pool.ntp.org

crontab scheduled task parameter meaning

FieldMandatoryAllowed ValuesAllowed Special CharactersRemarks
SecondsYes0–59*``,``-Standard implementation does not support this field.
MinutesYes0–59*``,``-
HoursYes0–23*``,``-
Day of monthYes1–31*``,``-``?``L``W?``L``W only partially implemented by software
MonthYes1–12 or JAN–DEC*``,``-
Day of weekYes0–7 or SUN–SAT*``,``-``?``L``#?``L``# only partially implemented by software. Linux and Spring allow 0-7, 0 and 7 are Sunday. Quartz allows 1-7, 1 is Sunday
YearNo1970–2099*``,``-Standard implementation does not support this field.

vmware Network Settings​

Note best to use vmware 15, vmware 16 version seems to have network problems.

image-20211216221256892

image-20211216221335326

Query Total Size of Current Folder​

du -sh ./

Query Directory Tree of First n Levels in Current Directory Structure​

Show first level directory for /d %i in (*) do @echo %i

Install lrzsz​

yum install -y lrzsz

sz download rz upload

Close Java Process in Linux​

kill -9 $(ps -ef | grep -w "xxx-1.0.jar" | grep -v "grep" | awk '{print $2}')

grep Command​

1️⃣ Basic Command​

grep [options] PATTERN FILE
  • Search for lines containing PATTERN in file
  • Common options:
    • -i β†’ Ignore Case
    • -v β†’ Invert Match (lines not containing PATTERN)
    • -n β†’ Show Line Number
    • -r / -R β†’ Recursive Search Directory
    • -E β†’ Enable Extended Regular Expressions (ERE)

2️⃣ Basic Examples​

2.1 Match Keyword

grep "error" logfile.txt

Match lines containing "error"


2.2 Ignore Case

grep -i "error" logfile.txt

Match "error", "Error", "ERROR" etc.


2.3 Show Line Number

grep -n "error" logfile.txt

Output Line Number + Line Content


2.4 Invert Match

# Output lines **not containing `error`**
grep -v "error" logfile.txt
# Records containing a but not b
awk '/a/ && !/b/' logfile.txt
grep "a" logfile.txt | grep -v "b"

2.5 Recursive Search Directory

grep -r "TODO" ./src

Search all files in src directory and subdirectories


3️⃣ grep -E Extended Regex​

  • Support more powerful regex syntax:
    • | β†’ OR
    • + β†’ Previous character 1 or more times
    • ? β†’ Previous character 0 or 1 time
    • () β†’ Grouping
  • No need to use backslash escape like normal grep

3.1 Match Multiple Keywords

grep -E "error|fail|fatal" logfile.txt

Match "error", "fail" or "fatal"


3.2 Match Repeated Characters

echo -e "aaa\naa\nb" | grep -E "a+"

Match 1 or more consecutive a Output:

aaa
aa

3.3 Optional Characters

echo -e "color\ncolour" | grep -E "colou?r"

u? β†’ u can appear 0 or 1 time Output:

color
colour

3.4 Group Matching

echo -e "cat\nbat\nrat" | grep -E "(c|b)at"

Match "cat" or "bat" Output:

cat
bat

3.5 Line Number + Ignore Case

grep -Eni "error|fail" logfile.txt
  • -n β†’ Line Number
  • -i β†’ Ignore Case
  • -E β†’ Extended Regex

3.6 Complex Example

grep -E '(error|fail|fatal) [0-9]{3}' logfile.txt

Parse:

  • (error|fail|fatal) β†’ Match error, fail or fatal
  • β†’ Space
  • [0-9]{3} β†’ Match three digits
  • Output entire matching line content

4️⃣ Match pip list Example (Combined with Extended Regex)​

Assuming you have Python package list:

pip list | grep -E "sentence"
# Output:
# sentence-transformers 5.1.0

If want to match sentence-transformers, use .* wildcard:

pip list | grep -E "sentence.*transformers"

5️⃣ Tips​

  1. Pattern wrapped with single quotes '...', avoid shell escape
  2. Match entire word: \bpattern\b
  3. Match numbers: [0-9]+
  4. Match any character: .
  5. Match repeated characters: + / {n,m}

βœ… Summary

  • grep β†’ Basic Search
  • grep -E β†’ Extended Regex, supports |, +, ?, ()
  • Combinable options: -i, -v, -n, -r
  • Common tips: Wrap with single quotes, .* wildcard, [- ] match multiple separators
Agreement
The code part of this work is licensed under Apache License 2.0 . You may freely modify and redistribute the code, and use it for commercial purposes, provided that you comply with the license. However, you are required to:
  • Attribution: Retain the original author's signature and code source information in the original and derivative code.
  • Preserve License: Retain the Apache 2.0 license file in the original and derivative code.
The documentation part of this work is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License . You may freely share, including copying and distributing this work in any medium or format, and freely adapt, remix, transform, and build upon the material. However, you are required to:
  • Attribution: Give appropriate credit, provide a link to the license, and indicate if changes were made.
  • NonCommercial: You may not use the material for commercial purposes. For commercial use, please contact the author.
  • ShareAlike: If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.