SoFunction
Updated on 2025-03-10

Share several commonly used commands for Linux development

Some commonly used commands in Linux

/cd/pwd directory operation command, used to view and switch directories

1.1 ls

ls List all files in the current directory

ls -l: List the details of the file

ls -a: List all files in the current directory, including hidden files

1.2 cd

cd /data/logs Switch to the data/logs directory

cd …: Switch to the previous directory

cd ~: Switch to user home directory

1.3 pwd

pwd displays the absolute path to the current directory

pwd -L: Displays the logical path, that is, the actual path pointed to by the symbolic link (default option)

pwd -P: Displays the physical path, that is, the actual path of the directory tree, without following any symbolic links

/touch/rm/cp/mv file operation commands, used to create, delete, copy and move files

2.1 mkdir

mkdir [Directory Name]: Create a new directory

2.2 touch

touch [File name]: Create an empty file

2.3 rm

rm [File]: Delete the specified file

rm -r [Catalog]: Recursively delete the specified directory and its contents

2.4 cp

cp [source file] [target file]: Copy file
cp -r [source directory] [destination directory]: Recursively copy the directory

2.5 mv

mv [source file] [target file]: Move/rename file

/less/head/tail - Command to view the contents of the file. less can be viewed on pages.

3.3 cat view file content

cat [File]: Display file contents from the first line

3.4 less pagination view file content

less [File]: View file contents by paging

You can browse the content in the following ways:

  • Spacebar - Turn down a page
  • B key - turn up a page
  • G-key - Skip to the end of the file
  • g key - Skip to the beginning of the file
  • /String - Search downward strings in document
  • n-key - Repeat the previous search (related to/or?)
  • q key - Exit
  • u - scroll forward half screen
  • d - scroll back half screen
  • y - Scroll forward one line
  • b - Return to the previous screen

3.5 head

head file name: Display the first 10 lines of the file

head -n 20 File name: Display the first 20 lines of the file

Head main parameters:

-n Number of rows: Specify how many rows are viewed before, the default is 10
-c Number of bytes: View the beginning of the file by number of bytes
-q:Hidden file name
-v: Show file name

3.6 tail

tail file name: Display the last 10 lines of the file

tail -n 20 file name: display the last 20 lines of the file

tail main parameters:

-n Number of rows: Specify how many rows to view, default is 10

Search within the file for content matching the specified regular expression or string

grep [Options] 'String' filename
grep 'error' /var/log/syslog // existsyslogFind in log files'error'Matching rows。

Common options for grep include:

  • i: case insensitive
  • -n: Show matching rows and row numbers
  • -C n: Show the context of n lines before and after matching lines
  • -v: Show all lines that do not contain matching text
  • -w: Whole word matching
  • -A n: Show matching rows after n rows
  • -B n: Show n lines before matching row
  • -r: Recursively search for files in directory

Powerful file search commands

find [Search path] [Options]
find /home -name "*.txt" #exist/homeFind all in the directory.txtdocument

Find has many practical options, commonly used ones include:-

  • -name: Search by file name
  • -size: Find by file size
  • -type: Look up by file type (such as f represents a normal file)
  • -atime/-mtime: Search by file access/modification time
  • -user/-group: Search by file owner/group group
  • -exec: Execute the specified command on the found file
  • -delete: Delete the matching file

Commands to package and compress files

6.1 Pack the file

tar -cvf  file1 file2 #Packaging filestar -tvf  #View the contents of the packaged file

6.2 Compress the file

tar -zcvf  file1 file2 #Compression Packagingtar -zxvf  #Decompress

6.3 Other common parameters

  • -c: Create a packaged file
  • -v: Show the running process
  • -f: Specify the packaged file name
  • -z:gzip compression/decompression
  • -j:bzip2 compression/decompression
  • -x: Unpack or decompress
  • -t: View packaged file content

/nano - Text editor under Linux, used to modify configuration files

7.1 vim

vim file name

Vim has three working modes:

  • Normal mode: used for navigation and command input, press Esc to enter
  • Editing mode: used for text editing, press i to enter
  • Command line mode: used for saving, exit, etc., press: Enter

Some basic tips for using vim:

  • i - Enter edit (insert) mode at the cursor
  • h/j/k/l - move the cursor right on the lower left
  • :w - Save the file but not exit vim
  • :wq - Save the file and exit vim
  • :q! - Forced exit without saving the file
  • dd - delete the entire line
  • u - Undo the previous operation
  • /Text - Search for text

Vim supports a large number of shortcut key combinations, which can greatly improve text editing efficiency. Commonly used ones include:

  • ciw - Change words
  • d2w - Delete 2 words
  • y3j - Copy 3 lines of text

indentation

7.2 nano

  • Open file:nano file name
  • Search: Ctrl+W, enter the word to search
  • Number of positioning rows: Ctrl+C displays cursor position
  • Cut text: Ctrl+K
  • Copy text: Ctrl+6
  • Paste text: Ctrl+U
  • Undo the previous operation: Alt+U
  • Save file: Ctrl+O
  • Exit nano:Ctrl+X

/kill command to view and kill processes

8.1 ps

ps: Shows the process running in the current terminal

ps aux: To view all processes, you can use aux parameters

Common parameters used by the ps command include:

-a: Show all processes
-u: Show the process of the specified user
-x: Show all processes, not distinguished by terminals
-e: Show all process information

-f: Show full information
-C: Display process according to command name

ps -C java # Checkjavaprocess

Common fields in ps output:

  • PID:Process
  • IDTTY: Terminal number
  • STAT: Process Status
  • TIME: Takes up CPU time
  • COMMAND: Process name

8.2 kill

kill PID: The PID here is the process ID of the process to be terminated. This will send a SIGTERM signal to the process, requesting the process to be terminated.

kill -9 PID: Forced termination with -9 parameter

The kill command supports sending a variety of signals, commonly used signals include:

  • SIGTERM (15): Request to terminate the process
  • SIGKILL (9): Forced termination of process
  • SIGSTOP (17): Pause the process
  • SIGCONT (18): Continue to run the stopped process

/htop command to view system resource usage

9.1 top

top [Options]

The top command is used in Linux systems to display the resource usage of processes in real time, which is equivalent to the task manager of Windows. Running the top command directly in the terminal will enter the prompt interface, displaying the CPU, memory, swap partition and other resource usage of all processes in real time.

Top command options:

-bOperate in batch mode
-cShow full command
-dScreen refresh interval time
-I Ignore the failure process
-sConfidential mode
-SCumulative mode
-i<time>  设置刷新间隔time
-u<username> 指定username
-p<Process number> Specify the process
-n<frequency> 循环显示的frequency

Commonly used shortcut keys in top state:

hShow shortcut key help
kTerminate a process
i open/Ignore idle and dead processes
qExit the program
r Reschedule a process's priority
S  Switch to cumulative mode
sChange refresh interval time,Unit seconds
f,F Add or delete items from the current display
o,O Change the order of displayed items
l Switch to display load average and startup time information
m  Switch to display memory information
t Switch to display the process andCPUStatus information
cToggle display command name and full command line
M  Sort by memory usage size
Paccording toCPUUsage sorts (Default sort)
Taccording to时间/Sorting the cumulative time
wWrite the current settings to~/.toprcIn the file
1展open多核cpushow

The information field displayed by the top command is meaning:

top - Current system time
up   How long has the system been powered on?
user  Current number of users
load average cpuAverage load,The three values ​​are,1minute,5minute,15minute
Tasks  Current number of processes in the system,total:Total number of processes,running:Number of running processes,sleeping:Number of sleep processes,stopped:Number of processes stopped,zombie:Number of zombie processes
%Cpu(s) cpuUsage rate,us:User usagecpuHundred percent,sy:System kernel usagecpuHundred percent,id:The restcpuHundred percent
Mem   Memory usage information,total:Total memory size,free:Free memory,used:Used memory,buff/cache:The cached memory size
Swap  Virtual memory information
PID processid
USER process所有者
PR Priority
NI nicevalue,负value表示高Priority,正value表示低Priority
VIRT   process使用的虚拟内存总量
RES   process使用的物理内存大小
SHR   Shared memory size
S  process状态,D:Uninterruptible sleep state,R:run,S:Sleep,T:track/stop,Z:僵尸process
%CPU  process使用的CPUPercentage of use
%MEM  process使用的物理内存百分比
TIME+  process使用的CPUTotal time
COMMAND Command name

9.2 htop is similar to top

  • htop has some improvements over top:
  • The interface is more user-friendly, using colors to distinguish process states, and visualization is stronger
  • Support mouse operation
  • Supports tree-shaped process structure, can expand/collapse processes and child processes
  • Supports highlighting the sorting columns when sorting
  • Support horizontal scrolling terminal browsing process information
  • Supports search and filtering processes

Some of the main interactive keys of htop:

F1 - help
F2 - show/Hide process tree
F3/F4 - Search process
F5/F6 - Sort columns 
F7/F8 - Increase/Reduce columns
F9 - Terminate the process
F10 - quithtop

/df command to view disk space usage

10.1 du

du - Displays all files and subdirectories in the current directory
du -h - Display size in human-readable form(GB,MB,KB)
du -sh - Show the current directory total size
du -ah - Displays all files and directory sizes in the specified directory
du -lh - Show only the total size directory,No file displayed
du -s * - Displays the total size of each subdirectory in the current directory

10.2 df

df - View disk space usage for all file systems
df -h - Display disk space usage in readable format
df -H - Show disk usage for all file systems,Including temporary mounts
df -T - Show file system type
df -i - showinodeUsage information
df -a - show所有的磁盘,Including unmounted

Display parameter description:

Filesystem: The file system path displayed
1K-blocks: The total size of the file system, in units of 1KB
Used: The space used by the file system
Available: The remaining free space in the file system
Use %(Use%): Percentage of used space
Mounted on: file system mount point

Common file system types include:

ext4 - LinuxThe most common file systems
xfs - High-performance file system
nfs - Network file system
vfat - WindowsCommon file systems
iso9660 - CD/DVDMirror file system
tmpfs - Temporary memory-based file system
devtmpfs - equipment tmpfs File system

/telnet/curl/wget Network testing and calling commands

11.1 ping

ping [Host Name/IP]: Test whether the network is smooth

11.2 telnet

telnet [server] [port]
telnet  80 #Log in to Baidu80port

Some important uses of telnet include:

  • Test network connectivity
  • Debug client-server communication
  • Manual access to SMTP, POP3, IMAP and other services
  • Remote login to the host

11.3 curl

curl 	#Send GET requestcurl -d "data=test"  #Send POST requestcurl -O / #Download filecurl -v  #Display communication process

11.4 wget

wget [URL]: Download files from the Internet

Command to obtain administrator permissions

sudo [Order]: 在Order前加上sudoExecute with administrator privileges

Timed task management commands

Common usage of crontab:

  • Edit crontab tasks
crontab -e

This opens the crontab file for editing, allowing the user to set the commands to be executed regularly.

List crontab tasks

crontab -l

This lists all crontab tasks set by the current user.

  • Delete all crontab tasks
crontab -r

This will delete all crontab tasks for the current user.

  • crontab file format
# Time-sharing day, moon and weekly order* * * * * command

Each asterisk represents a unit of time. From left to right are: minutes, hours, date, month, week (0-6,0 means Sunday). For example:

# Execute the backup script at 8 o'clock every day0 8 * * * /path/to/

System service management commands

Common usage of chkconfig is as follows:

  • Check the service's startup status at different run levels
chkconfig --list [Service name]
  • Set the service to start automatically at the specified run level
chkconfig --level [Running Level] [Service name] on
  • Turn off the service automatically starts at the specified run level
chkconfig --level [Running Level] [Service name] off 
  • Setting up service automatic start at all run levels
chkconfig [Service name] on
  • Turn off the service automatically starts at all run levels
chkconfig [Service name] off

Linux run levels include 0 to 6, etc., usually 3 or 5 are the default levels.

Firewall configuration commands

Some common uses of iptables are as follows:

  • Check out the existing iptables rules
iptables -L
  • Open the port
iptables -A INPUT -p tcp --dport 80 -j ACCEPT

This rule allows 80-port TCP packets.

  • Block IP
iptables -A INPUT -s 192.168.0.1 -j DROP

This rule prohibits packets from 192.168.0.1.

  • Save rules
iptables-save > /etc/

Save the iptables rules and are still valid after restarting.

  • Delete all rules
iptables -F

16. scp/rsync - remote copy file command

16.1 scp

scp [Options] [Source File] [Target path]

in,OptionsIt can be one of the following commonly used options:

  • -r: Copy the directory and its contents.
  • -p: Keep the modification time, access time and permissions of the original file.
  • -v: Display detailed transmission information.

Source FileThe path to the file or directory that needs to be transferred can be a local path or a path on the remote host. The format of the remote path is[user@]host:file

Target pathThe destination path for transmission can be a local path or a path on a remote host.

Here are some example usages:

  • Copy files from local to remote host:scp user@remote:/path/to/destination
  • Copy files from a remote host to local:scp user@remote:/path/to/ /path/to/destination
  • Copy the directory and its contents:scp -r directory user@remote:/path/to/destination

16.2 rsync

rsync [Options] Source path Target path

in,OptionsIt can be one of the following commonly used options:

  • -r: Recursively synchronize the directory and its contents.
  • -a: Archive mode, which means transferring files in a recursive manner and maintaining file attributes (such as permissions, owners, etc.).
  • -v: Display detailed transmission information.
  • -z: Use compression algorithm for transmission to reduce the amount of data transmission.
  • --delete: Delete files on the target path that are inconsistent with the source path.

Source pathThe path to the file or directory that needs to be synchronized can be a local path or a path on the remote host. The format of the remote path is[user@]host:file

Target pathThe target path for synchronization can be a local path or a path on a remote host.

Here are some example usages:

  • Synchronize files or directories locally:rsync -avz source/ destination/
  • Sync from local to remote host:rsync -avz source/ user@remote:/path/to/destination/
  • Sync from remote host to local:rsync -avz user@remote:/path/to/source/ destination/

Change file permissions

chmod [Options] model document

in,OptionsIt can be one of the following commonly used options:

-R: Permission to recursively modify the directory and its contents.-v: Show modified permissions.-c: Show prompt message only when changes occur.

Here are some example usages:

  • The permission to modify the file is read-only:chmod 444
  • The permission to modify the file is readable and writable:chmod u+rw
  • The permission to recursively modify the directory and its content is that only the owner has read and write execution permissions, while other users have read and execute permissions:chmod -R 750 directory/

Written at the end

The above is the detailed content of several commonly used commands for development of Liunx. For more information about commonly used commands for Liunx, please pay attention to my other related articles!