Summary of Common Linux Commands

Whether you are a programming novice or a seasoned professional, it is recommended that you save this common Linux command manual.

Whether you are a backend developer or a frontend developer, you will inevitably have to deal with the Linux system. It could be setting up a virtual machine environment while learning, maintaining services in the company’s testing environment, or troubleshooting production issues on an online server.

This requires us to be proficient in using the Linux command line, and the related common commands may be forgotten if not used frequently. Here, I have collected some common commands for easy reference when needed, and you are welcome to add more (the operations mentioned here are based on the CentOS system).

If you have not yet installed a Linux virtual machine, you can refer to this article:

A Guide to Setting Up a Linux Virtual Machine for Developers

2021-01-31

Summary of Common Linux Commands

File Management

Directory Operations

Change Directory cd

View Directory ls

  • -l List detailed information about files or directly ll
  • -a List all files and directories in the current directory, including hidden ones (a means all)

Create Directory mkdir

  • -p Create a directory; if the parent directory does not exist, create it (p means parent)

Output Information echo

Print file to command line (view file) cat

Change file owner chown

Change file group chgrp

Download file wget

Find a string in a text file grep

Count lines, words, and characters in text wc

Partially display more/less

Find file find / -name 'auto.cnf'

Create empty file touch

Copy file cp

Move or rename mv

Delete file rm

  • -r Recursively delete, can delete subdirectories and files

  • -f Force delete

Delete empty directory rmdir

Display directory structure in tree format (needs tree package) tree

Show current directory pwd

Create link file ln

Page display of text file content more、less

Show head and tail of file content head、tail

Vim Operations

Enter editor vi/vim, vim has three modes: command mode, insert mode, edit mode. Use ESC or i or : to switch modes.

  • Enter insert mode i
  • Exit insert mode esc
  • Save: type w afterward
  • Exit: type q afterward
  • Exit without saving: type q! afterward
  • Show line numbers set number
  • Search keyword /xxxx press n to jump to the next, shift+n for the previous
  • Copy the line where the cursor is and paste yyp
  • h (move left one character ←), j (next line ↓), k (previous line ↑), l (move right one character →)

Packing and Compressing Commands

Pack and compress targzipzip2

  • -c Archive files
  • -x Extract files
  • -z gzip compress files
  • -j bzip2 compress files
  • -v Show compression or decompression process (v means view)
  • -f Use archive name

Example:

Only pack, do not compress: tar -cvf /home/abc.tar /home/abc

Pack and compress using gzip: tar -zcvf /home/abc.tar.gz /home/abc

Pack and compress using bzip2: tar -jcvf /home/abc.tar.bz2 /home/abc

If you want to decompress, just replace “c” in the above commands tar -cvf / tar -zcvf / tar -jcvf with “x”.

Linux Pipeline

Use the standard output of one command as the standard input of another command. This means combining several commands, where the latter command uses the result of the previous command.

Example: grep -r "close" /home/* | more to search for files containing “close” in all files under the home directory and output in pages.

File Permission Management

Three Basic Permissions

R: Read, numerical representation is 4

W: Write, numerical representation is 2

X: Execute, numerical representation is 1

[root@VM-16-2-centos ~]# ll
total 597952
-rw------- 1 root root  12387614 Aug 29  2021 apache-zookeeper-3.7.0-bin.tar.gz
-rw-r--r-- 1 root root 113304268 May  3 12:22 jdk-8u281-linux-x64.rpm

As shown above, the permissions of the file jdk-8u281-linux-x64.rpm are -rw-r--r--, a total of ten characters, divided into four segments.

  • The first character “-” indicates a normal file; this position may also show l for links; d indicates a directory

  • The second, third, and fourth characters rw- represent the permissions of the current owner, which numerically is 4 + 2 = 6

  • The fifth, sixth, and seventh characters r-- represent the permissions of the current group, which numerically is 4

  • The eighth, ninth, and tenth characters r-- represent the permissions of other users, which numerically is 4

Thus, the permission to operate this file is numerically represented as 644.

Change Permissions

Change permissions: sudo chmod [u owner g group o other users a all users] [+ add permission – remove permission] [r w x] directory name

For example: if a file named filename has permissions -rw-r----x, change the permission value to -rwxrw-r-x, which is numerically 765.

sudo chmod u+x g+w o+r filename can also be represented numerically as sudo chmod 765 filename

Running Programs

Command Line Running

Run ./filename

Exit ctrl+c

Run in Background

Run nohup command >out.file 2>&1 &

Exit ps -ef |grep keyword |awk '{print $2}'|xarg kill -9

Run as a Service

Set to start at boot systemctl enable

Start systemctl start

Stop systemctl stop

System Related

System Management Commands

Show detailed information about specified files, more detailed than ls stat

Show online logged-in users who

Show current operating user whoami

Show hostname hostname

Show system information uname

Dynamically display information about the processes consuming the most resources top

Show instantaneous process status ps \\ ps -aux

Check directory size du -h /home (shows directory information with units)

Check disk size df -h (shows disk information with units)

Check network status ifconfig

Test network connectivity ping

Show network status information netstat

If you don’t remember a command, check the documentation, e.g., man grep

[root@VM-16-2-centos ~]# man grep
GREP(1)                    General Commands Manual                       GREP(1)
NAME
       grep, egrep, fgrep - print lines matching a pattern

SYNOPSIS
       grep [OPTIONS] PATTERN [FILE...]
       grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]
……

Clear screen clear

Rename command alias, e.g., alias showmeit="ps -aux", to remove use unaliax showmeit

Kill process kill, you can first use ps or top to check the process id, then use the kill command to terminate the process. kill -9 force delete

Firewall

Check firewall status firewall-cmd --state

Stop firewall systemctl stop firewalld.service

Disable firewall startup systemctl disable firewalld.service

Shutdown and Restart

Shutdown shutdown -h now

  • -r Restart

  • -h Shutdown without rebooting

  • now Shutdown immediately

Restart reboot

Shutdown halt

Network Configuration

Check network card information ifconfig

Network configuration /etc/sysconfig/network-script/ifcfg-eth0

Configure network card /etc/udev/rules.d/70-persistent-net.rules

User Management

Create user useradd

Set password passwd

  • Store group accounts /etc/group

  • System user configuration file /etc/passwd

Store user account passwords /etc/shadow

Store user group account passwords /etc/gshadow

Username useradd

Username userdel

Username adduser

Group name groupadd

Group name groupdel

Set password for root passwd root

su root

su - root

System environment variables /etc/profile

User environment variables bash_profile

User environment variables .bashrc

su user switch user, load configuration file .bashrc

su - user switch user, load configuration file /etc/profile, load bash_profile

Change file owner and group

sudo chown [-R] owner[:group] {File|Directory}

-rw-r--r-- 1 root root 113304268 May  3 12:22 jdk-8u281-linux-x64.rpm

For example, taking jdk-8u281-linux-x64.rpm as an example. It belongs to user root, group root.

To switch the owner and group of this file, you can use the command.

sudo chown daley:java jdk-8u281-linux-x64.rpm

Installing Software

Download RPM Installation Package

Install rpm -i jdk-XXX_linux-x64_bin.rpm

Search rpm -qa | grep jdk

List rpm -qa | more

Ubuntu dpkg method

Search dpkg -I | grep jdk

List dpkg -I | more

Install dpkg -i jdk-XXX_linux-x64_bin.deb

YUM Method

Search yum search jdk

Install yum install java-11-openjdk.x86_64

Remove yum erase java-11-openjdk.x86_64

Configuration file /etc/yum.repos.d/CentOS-Base.repo

Ubuntu apt-get method

Search apt-cache search jdk

Install apt-get install openjdk-9-jdk

Remove apt-get purge openjdk-9-jdk

Configuration file /etc/apt/sources.list

Download Compressed File Method

Edit .bashrc

Configure environment variables

  • Open environment variable file vi /etc/profile

  • Configure environment variable export JAVA_HOME=/root/jdk-XXX_linux-x64

  • export PATH=$JAVA_HOME/bin:$PATH

  • Refresh configuration source /etc/profile

Finally, everyone is welcome to ask questions and communicate.

If you find this helpful, feel free tolike, star 🌟 orshare!

Leave a Comment