Shell Scripting for Gas Technology Stack: Building on Linux Commands

Shell Scripting for Gas Technology Stack: Building on Linux Commands

Shell scripts are used to accomplish complex tasks through a series of Shell commands. Mastering common Shell commands is a prerequisite for writing Shell scripts.

Essentially, Shell commands are mature programs provided by the system, and some commands are very powerful, such as <span>find</span>, <span>grep</span>, <span>awk</span>, <span>sed</span>, etc.

This article summarizes some commonly used Shell commands, including file operations, text processing, and viewing system information, hoping everyone can gain something from it 🙂

File Operations

In Linux systems, everything is a file, so file operations are the most basic operations in Shell scripts, including file creation, deletion, copying, moving, and renaming.

Creating Files and Directories

The <span>touch</span> command primarily modifies the access time of a file; if the file does not exist, it creates an empty file. However, it is commonly used to create files.

touch data.txt # Create a file

# Create multiple files
touch data1.txt data2.txt

It supports brace expansion, allowing Shell commands to do cool things, such as creating 100 files at once.

touch data{1..100}.txt

<span>touch</span> command options:

  • <span>-a</span> only updates the access time, use <span>ls -lt -u</span> to view the new access time<span>atime</span>
  • <span>-m</span> only updates the modification time, use <span>ls -lt</span> to view the new modification time<span>mtime</span>
  • <span>-r</span> replaces the current time with the specified file’s time
  • <span>-t</span> updates to the specified time
  • <span>-c</span> does not create a file
# Modify the atime of hello.js to the current time
touch -a hello.js
# Modify the mtime of hello.js to the current time
touch -m hello.js
# Change the mtime of b.js to be the same as a.js
touch -r a.js b.js

# Long list format, the time field defaults to the file's modification time mtime
ls -l .
# Long list format, the time field shows the file's access time atime
ls -lt -u .

# Update to June 7, 2025, 11:20 AM
# Time format: [[CC]YY]MMDDhhmm[.ss].
touch -t 202506071120 hello.txt

Creating directories

# Create a directory
mkdir images
# Create multiple directories
mkdir images styles
# Recursively create directory hierarchy
mkdir -p src/static/images

# Specify directory permissions
mkdir -p -m 777 backup/code
mkdir -p -m a=rwx backup/code # Same as above

Deleting Files and Directories

The <span>rm</span> command is used to delete files or directories. The <span>-r</span> option indicates recursive deletion, meaning it deletes the directory and its contents, while the <span>-f</span> option indicates forced deletion, which deletes without prompting for confirmation.

Syntax:

  • <span>rm file1 file2..</span>
  • <span>rm -r dir</span>
# Delete a file
rm data.txt
# Delete a directory and its contents
rm -r dir
# Delete an empty directory; if the directory is not empty, it cannot be deleted
rmdir dir

<span>rm</span> command is quite dangerous because once a file or directory is deleted, it cannot be recovered. To avoid accidental deletion, you can use the <span>-i</span> option, which prompts for confirmation before deleting a file or directory.

The joke about deleting the database and running away refers to using <span>rm -rf /</span> to completely delete all files on the server system. If you are not sure, replace <span>-f</span> with <span>-i</span> option.

<span>rm</span> command supports filename wildcards, such as <span>*</span>, <span>?</span>, and <span>[...]</span>, which can match multiple files, and it also supports brace expansion <span>{}</span>, making it very flexible.

# Create a.jpg, b.jpg, c.jpg
touch {a..c}.jpg
# Delete jpg image files
rm *.jpg

# Create a.png, b.png, c.png
touch {a..c}.png
# Filename expansion + brace expansion, delete jpg and png files
rm *.{jpg,png}
# Note: [a,b,c] filename expansion, elements can only be single characters, so brace expansion is better
rm *.[jpg,png] # error

# Note: Distinguish between brace expansion and filename expansion
touch data{K,Q}.txt
rm data[K,Q].txt
rm data{K,Q}.txt # Same as above


# Force delete all contents of a directory
rm -rf dir

# Use filename expansion for deletion
rm *.log
# Prompt before deletion
rm -i  *.js
# Filename only has 3 characters
rm ???
rm *.??
rm a*
# Delete a.js b.js
rm [ab].js

Copying Files and Directories

The <span>cp</span> command is used to copy files or directories. The <span>-r</span> option indicates recursive copying, meaning it copies the directory and its contents, while the <span>-i</span> option prompts for confirmation if a file with the same name exists, as <span>cp</span> by default will overwrite existing files with the same name.

Various syntax:

  • <span>cp <srcFile> <dstFile></span>
  • <span>cp <file1> <file2>... <dstFolder></span>
  • <span>cp -t <dstFolder> <file1> <file2>...</span>
  • <span>cp -r <srcFolder> <dstFolder></span>
# Copy a file
cp a.jpg a2.jpg
# Copy a directory
cp -r codes codes_bk

# Copy multiple files
cp a.js b.js src
cp -t src a.js b.js

# Copy all files in the current directory to tmp
cp * /tmp
# Preserve all file attributes (owner, permissions, access time, etc.)
cp -p server.js /codes/test/server.js

Prompt for confirmation if a file with the same name exists

# Copy a file, if lufy.txt already exists, prompt for confirmation
cp -i data.txt lufy.txt

If the object being copied is a directory, there are two possibilities: either the source directory is copied as a new target directory, or the source directory is copied into the target directory, depending on whether the target directory already exists.

# List directories starting with folder folder1 folder2
ls -d folder*

# Because folder2 already exists, the result is that folder1 is copied into folder2
cp -r folder1 folder2

# Because folder_back does not exist, the result is that folder1 is copied as folder_back
cp -r folder1 folder_back

<span>cp</span> command supports filename wildcards, such as <span>*</span>, <span>?</span>, and <span>[...]</span>, which can match multiple files.

# Copy jpg image files to images directory
cp *.jpg images

Moving and Renaming

The <span>mv</span> command is used to move files or directories. The <span>-r</span> option indicates recursive moving, meaning it moves the directory and its contents. If a file or folder is moved to the current directory, it is equivalent to renaming; thus, both moving and renaming use the <span>mv</span> command.

Syntax:

  • <span>mv <oldName> <newName></span>
  • <span>mv file1 file2... dest</span>
# Rename
mv a.jpg b.jpg
# Move
mv a.jpg images/
mv a.jpg b.jpg images/

# If the target folder already has a file with the same name, prompt
mv -i a.jpg images

# Only move files that are not already in dstDir
mv -u dirSrc/*  dstDir

If the object being moved is a directory, it has the same characteristics as the <span>cp</span> command.

# List all folders starting with f folder1 folder2
ls -d folder*

# Move folder1 into folder2
mv folder1  folder2
# Because folder_bk does not exist, the result is that folder1 is renamed to folder_bk
mv folder1  folder_bk

Viewing Directory Contents

The <span>ls</span> command is the most commonly used command for viewing directories. Personally, I find <span>ls</span> very powerful and can meet most of our needs.

# Long list display, default sorted by filename
ls -l
# Show hidden files
ls -a
# Show file size (in KB, MB, etc. human-readable format)
ls -lh
# Display files sorted by modification time in descending order (latest first)
ls -lt
# Display files sorted by size in descending order (largest first)
ls -lS
# Reverse order display
ls -r
# Display files sorted by modification time in ascending order
ls -ltr
# Show all directories
ls -F | grep "/$"
# Show the directory itself starting with dir (without -d shows files within dir)
ls -d dir*
# Recursively display all contents under the directory
ls -R my_project
# Show the inode number of files or directories
ls -li
# Owner and group represented by id
ls -ln

The time displayed in long list format defaults to the file’s modification time mtime, which can be specified using the <span>--time</span> parameter, such as <span>atime</span>, <span>ctime</span>, or <span>mtime</span>, etc.

Viewing File Contents

The <span>cat</span> command is short for <span>concatenate</span> (meaning to connect), and we can also understand it as “cat” to view file contents.

Syntax: <span>cat [option] [file]...</span>

<span>cat</span> primarily outputs the contents of multiple files to standard output. If we provide only one file, it outputs the contents of that file to standard output.

# Merge output of multiple files
cat file1 file2
# Merge contents of multiple files into file3
cat file1 file2 > file3
# Output file contents
cat file1

<span>cat</span> command can output content with line numbers

# Show line numbers
cat -n file1
# Only non-empty lines with line numbers
cat -b file1

<span>cat</span> command can read data from files or keyboard input. For example, if we want to create a file but the content is long, we can use the <span>cat</span> command to input content from the keyboard and then output it to a file.

Press ctrl + d to end input

# Save content from keyboard input to a specified file
cat > daily.txt

<span>cat</span> has a reverse command called <span>tac</span>, which is also a command (Linux has many interesting similarities) that outputs file contents in reverse order, starting from the last line.

# Display file contents in reverse order
tac file

<span>less</span> is a powerful command for viewing file contents. When opening a file, it defaults to command mode (like vim), and specific usage can be found by pressing <span>h</span> for help.

<span>u</span> to scroll up, <span>d</span> to scroll down, <span>/</span> or <span>?</span> to search, <span>gg</span> to jump to the top, <span>G</span> to jump to the bottom

less file

less can open multiple files

  • <span>:e</span> then enter the new filename
  • <span>:p</span> previous file
  • <span>:n</span> next file

less has a bookmark feature

  • <span>m</span> then press any letter to mark the current position
  • Single quote + previous letter to locate the corresponding tag

less can display new content added to a file in real-time

ls -1 . > files.txt
less -F files.txt

# Open a new terminal
echo new content >> files.txt

<span>more</span> command is similar to <span>less</span> command, but <span>more</span> command can only scroll down, not up, so <span>more</span> command’s functionality is not as powerful as <span>less</span> command.

There is a saying: more is less, less is more, isn’t it full of philosophical meaning? 🙂

<span>more</span> command mainly displays file contents in chunks, and <span>-5</span> indicates displaying the first 5 lines; because when the output content is long, scrolling back can be troublesome.

more file
more -5 index.html
cat order.tsx | more -5

head displays the first few lines/characters from the top of a file, and the <span>-n</span> parameter is used to specify the position of the last line, supporting negative numbers, indicating the last few lines.

# From the beginning to the 10th line
head -n 10 file
# Same as above
head -10 file
# From the beginning to the last 3rd line (the first line always starts from the 1st line, the last line uses negative numbers, indicating the last few lines)
head -n -3 file
# Display the first 5 characters
head -c 5 file

tail displays the last few lines/characters of a file, and the <span>-n</span> parameter is used to specify the position of the first line, supporting negative numbers, indicating the first few lines.

tail -n 3 file
tail -3 file
# Monitor new lines written to the file
tail -f file
# Terminate tail when the process ends
tail -f file --pid 3012
# Continuously try to open a file that does not exist yet
tail -f notExistFile --retry

Viewing File Types

Syntax: <span>file <filename></span>

file hello.txt
# MIME format file type information
file -i hello.txt
# Key-value format output file --help for instructions
file -N *.txt

Counting File Information

The <span>wc</span> command is used to count the number of lines, words, characters, etc., in a file.

Syntax: <span>wc [options] [file...]</span>

# Count lines, words, and characters
wc hello.txt
# Number of lines
wc -l hello.txt
# Number of words
wc -w hello.txt
# Number of characters
wc -c hello.txt
# Length of the longest line
wc -L hello.txt

Finding Files and Directories

The <span>find</span> command is one of the most commonly used and important commands in Linux systems, allowing you to search based on file name, file size, modification time, permissions, and other attributes.

find /etc -name init-tab
# Supports filename expansion
find /etc -name "*.conf"
# Case insensitive filename search
find . -iname server.js
# Find the directory named tmp in the current directory
find . -type d -name tmp
# Find all php files in the current directory
find . -type f -name "*.php"
# Limit search depth
find . -maxdepth 2 -type f -name "*.js"
# Find dotfile
find . -type f -name ".*"

# The default command executed after finding a file is echo, which can be specified with -exec to execute a command, {} represents the matched filename
# Find log files and delete them
find /tmp -type f -name "*.log" -exec rm -f {} \;

# ! before condition options indicates negation
# Find non-txt files in the current directory
find . -type f ! -name "*.txt"

# () for condition grouping
# Find txt and sh files
find . -type f \( -name "*.txt" -o -name "*.sh" \)

# Find files in the current directory with permissions not equal to 777
find . -type f ! -perm 777
# Find read-only files
find . -type f ! -perm /a+w
# Find executable files
find . -type f -perm /a+x

# Find empty files
find . -type f -empty
# Find empty directories
find . -type d -empty

# Files owned by root
find /tmp -user root
# Files belonging to the sudo group
find /tmp -group sudo

# Files modified 3 days ago
find ~ -type f -mtime 3
# [-5, -3] Files modified 3 days ago within 5 days
# +3 -> 3+ 3 means the past 3 days, + means greater than
# -5 -> 5- 5 means the past 5 days, - means less than
find ~ -type f -mtime +3 -mtime -5

# Files modified within the last hour
find . -type f -cmin -60
# Files accessed within the last hour
find . -type f -amin -60

# Files of size 50M
find . -type f -size 50M
# Files greater than 50M and less than 100M
find . -type f -size +50M -size -100M

Creating Soft Links and Hard Links

The <span>ln</span> command is used to create soft links or hard links.

  • Soft links (also known as symbolic links) can point to files or directories. When reading or writing to the symbolic file, the system will convert it to operate on the source file or directory. However, when deleted, only the link file is deleted; the link file and the source file have their own inode and are different files.
  • Hard links have multiple file paths pointing to the same file (same inode). It actually creates another file path pointing to the same inode. Hard links can only point to files, and a file is truly deleted only when the hard link count is 0.

Syntax:

  • <span>ln <src> <dstLink></span>
  • <span>ln -s <src> <dstLink></span>
# By default, a hard link is created
ln hello.txt /tmp/hello.txt
# -s specifies to create a soft link
ln -s images /tmp/images

File and Directory Permissions

Permissions for three roles

  • Owner <span>u</span>
  • Group <span>g</span>
  • Other users <span>o</span>

The execute permission (x) for directories controls whether users can open the directory.

Syntax:

<span>chmod [ugoa] [+-=][rwxug] file...</span>

chmod a+x hi.js
chmod a=rx hi.js
chmod o-x hi.js
chmod 777 hi.js

# Set owner permissions to be the same as group permissions
chmod u=g hi.js
# Set owner permissions to be the same as other users' permissions
chmod u=o hi.js
# Recursively modify permissions for all files in the directory
chmod -R 664 dir
# Modify permissions for all subdirectories
find . -type d -exec chmod -R 775 {} \;

Modify the owner/group of files/directories

Syntax:

  • <span>chown <user> dir</span>
  • <span>chgrp <group> dir</span>
# Modify both owner and group
chown alice:pan codes

# Recursively modify the owner of all files
chown -R alice src

chgrp pan codes
# Only modify the user group
chown :lucy images

# Modify the owner of the soft link, which actually modifies the source file
chown alice:lucy images_symlink
# Modify the owner of the soft link
chown -h alice:lucy images_symlink

# Change from owner alice to new owner
chown --from=alice lucy:lucy images

Set user and group permission bits

  • <span>setuid</span> sets the user identifier, allowing users to execute files with owner permissions
  • <span>setgid</span> sets the group identifier, allowing users to execute files with group permissions

Note: If the <span>setuid</span> permission bit is set on a file and the file owner is root, any user can execute that file with root permissions

<span>stat</span> command is very useful for viewing complete information about a file, including permissions and modification time, etc.

Check if <span>setuid</span> and <span>setgid</span> permission bits are set

  • <span>ls -l</span>
  • <span>stat filename</span>
ls -l /usr/bin/passwd
stat /usr/bin/passwd

# Set uid
chmod u+s hello.sh
# 4 represents setting uid, same as above
chmod 4775 hello.sh
# Set gid
chmod g+s hello.sh
# 2 represents setting gid, same as above
chmod 2775 hello.sh
# Remove uid/gid permission bits
chmod 0775 hello.sh

Text Processing

Text Sorting

The <span>sort</span> command sorts the lines of text in alphabetical order.

# Sort lines in alphabetical order within a file
sort filename
# Sort lines in numerical order within a file
sort -n filename
# Sort lines in numerical descending order within a file
sort -nr filename

# Remove duplicate lines
sort -u filename

# Can sort the contents of multiple files
sort -u data data2

# Comma-separated into multiple fields, sort by the 2nd field
sort -t ',' -k2 data
sort -t ',' -k2 -n -r data

Text Deduplication

The <span>uniq</span> command removes duplicate lines and must be used in conjunction with the <span>sort</span> command.

Syntax:<span>uniq [options] [input_file]</span>

sort data | uniq
# Count duplicate lines
sort data | uniq -c
# Only show duplicate lines, deduplication shows only once
sort data | uniq -d
# Show duplicate lines without deduplication
sort data | uniq -D
sort data | uniq -d -c
# Only show non-duplicate lines, opposite of -d
sort data | uniq -u
# Only judge duplicates by the first 2 characters
sort data | uniq -w 2
# Show duplicate lines
sort data | uniq -w 2 -D
# Skip the first 3 characters for comparison, opposite of -w
sort data | uniq -s 3
# Split by spaces into multiple columns, skip the first column
uniq -f 1 text

Deleting and Replacing Characters

Syntax: <span>tr [OPTION]... SET1 [SET2]</span><span>tr</span> command is used to translate, delete, and compress repeated characters.<span>tr set1 set2</span> replaces characters in set1 with corresponding characters in set2.

# Convert lowercase to uppercase
echo linuxShell | tr lin LIN  
# Convert all characters to uppercase
echo how are you | tr a-z A-Z 

# {} convert to ()
echo {linuxShell} > source.txt
# Redirect both input and output simultaneously
tr '{}' '()' < source.txt > result.txt 
cat result.txt

# Convert spaces to tabs
echo "it is a long time" | tr [:space:] "\t"

# Compress repeated characters
echo "hello----world" | tr -s "-" " " # hello world

# Delete specified characters

# Delete lowercase letters
echo "LongTimeAgo" | tr -d a-z  
# Delete numbers
echo "hello123" | tr -d 0-9
echo "it is time 2:30" | tr -d [:digit:] 
# Delete non-numeric characters
echo "it is time 2:30" | tr -c -d [:digit:] 

Text Search

The <span>grep</span> command is a very powerful text query command that can be used to search text or files, displaying only matching lines.

Syntax: <span>grep [OPTIONS] PATTERN [FILE...]</span>

  • <span>grep pattern file</span>
  • <span>grep -e pattern -f file</span>
grep alice /etc/passwd

# Ignore case
grep -i one content.txt 

# Search directory, recursively find files containing specified content
grep -r console  src/utils 

# Only show names of files containing specified content
grep -r -l console src/utils 

# Word match, does not match gone
grep -w one content.txt 

# Match count
grep -c one content.txt 

# Show line numbers
grep -n one content.txt 

# Reverse, show non-matching lines
grep -v one content.txt 

File Comparison

The <span>diff</span> command compares the differences between two files.

Syntax: <span>diff [option] file1 file2</span>

# Ignore whitespace differences, compare files
diff -w file1 file2 
# Side-by-side format, show differences
diff -y file1 file2 
# -w ignore whitespace
diff -y -w file1 file2 
# -W specify column width
diff -y -W 100 file1 file2 
# Show differences in a top-bottom comparison format
diff -cw file1 file2 

Other Common Commands

Viewing Hostname

The <span>hostname</span> command is used to view/modify the system’s hostname.

# View system hostname
hostname 
# Modify hostname, only temporary, will revert on reboot
sudo hostname my-notebook 
# Read hostname from file
sudo hostname -F host.txt 

Viewing Logged-in Users

The <span>w</span> and <span>who</span> commands

# More detailed
w  
# Less information
who 
# System boot time
who -b 
# Show system login processes
who -l 
# Show users associated with the current standard input
who -m 
# Show system run level
who -r 

Show system uptime

Syntax:<span>uptime [option]</span>

Viewing System Information

Syntax: <span>uname [option]</span>

# All information
uname -a 
# Show kernel name
uname 
# Show hostname, same as hostname
uname -n 
# Kernel release version
uname -r 
# System hardware name
uname -m 
# Processor information
uname -p 
# Hardware platform
uname -i 
# Show help
uname --help

Displaying and Setting Date

Syntax: <span>date [option] [+format]</span>

# Display current date in default format
date 

# Display specified date in default format
date --date '20/2/2012' 
date --date='1 Oct 2012'
date --date='next week'
date --date='last month'
date --date='2 months ago'
date --date='last day'

# Read multiple dates from file and display in default format
date -f date.txt 

# Modify system date/time, does not set the date
sudo date --set 'last day' 

# UTC time
date -u 
stat host.txt

# Print the last modification time of a file
date -r host.txt 

# Specify format
date +%Y-%m-%d 

Viewing User ID

Syntax: <span>id [options] [username]</span>

# Display current user uid gid username group name and which groups belong to
id 
id -u

# View uid of specified user
id -u root 

# Display username
id -un 

# View gid of the group to which the user belongs
id -g
# View group name of the group to which the user belongs
id -gn 

# View which groups the user belongs to
id -G 
id -Gn
id -Gn alice

Summary

As a developer, I find that mastering Linux commands really allows me to work more efficiently.

When I need to process a large number of log files, using grep to quickly search for keywords is much faster than manually searching. Sometimes I need to rename dozens of files, using mv with wildcards can accomplish it all at once, no longer needing to manually rename each one.

What impresses me the most is when I need to analyze issues on the server, using tail -f to view logs in real-time and using find to quickly locate problem files, these commands combined become my “problem troubleshooting toolbox,” helping me quickly locate and resolve issues.

Especially when deploying projects on cloud servers, without a graphical interface, I rely entirely on command-line operations. If I didn’t understand these commands, I wouldn’t be able to complete the deployment work.

To be honest, I initially found the command line very dull, but once I got used to it, I realized how powerful it really is. Now I use these commands every day; they have become indispensable tools in my work. Learning these commands is something that everyone who wants to go further in technology must do.

Leave a Comment