Common Linux Commands
Command: ls
Abbreviation for list, the ls command not only allows you to view files contained in a Linux folder, but also to view file permissions (including directory, folder, and file permissions), view directory information, and so on.
Commonly used parameters with:
ls -a lists all files in a directory, including hidden files starting with. Hidden files starting with .
ls -A lists all files except . and ..
ls -r lists files in reverse order
ls -t Sort files by modification time
ls -S Sort by file size
ls -h displays files in easy-to-read size
ls -l lists file permissions, owner, size, and other information in addition to the file name.
Command: cd
Description: Switch the current directory to dirName.
Example:
# Enter the desired directory
cd /
# Enter "home" directory.
cd ~
# Go to the last working path
cd -
# Use the arguments from the previous command as cd arguments.
cd !$
Command: pwd
View the current working directory path.
Example:
# View current path
pwd
# View the actual path of a soft link
pwd -P
Command: mkdir
The mkdir command: creates a folder.
Example:
# Create a folder named t in the current working directory
mkdir t
# Create a directory with path test/t1/t in the tmp directory, if it does not exist, create
mkdir -p /tmp/test/t1/t
Command: touch
The touch command creates a file.
Example:
# Create a file named test in the current working directory
touch test
Command: rm
Deletes one or more files or directories in a directory. rm does not delete directories if the - r option is not used. If you use rm to delete a file, you can usually still restore the file rm [option] file .
Example:
# Delete any .log file; ask for confirmation on each one before deletion
rm -i *.log
# Delete the test subdirectory and all files in the subdirectory without confirming each one.
rm -rf test
# Delete files starting with -f
rm -- -f*
Command: mv
The mv command moves a file or modifies a file name, depending on the type of the second argument (e.g., directory, move the file; if it is a file, recommand the file). When the second parameter is a directory, you can just multiple files separated by spaces as the first parameter, move multiple files to the directory specified by parameter 2.
Example:
# Rename the file test.log to test1.txt
mv test.log test1.txt
# Move the files log1.txt, log2.txt, log3.txt to the root test3 directory.
mv llog1.txt log2.txt log3.txt /test3
# Rename the file file1 to file2, if file2 already exists, ask if you want to overwrite it
mv -i log1.txt log2.txt
# Move all files in the current folder to the previous directory
mv * ... /
Command: cp
The cp command: copies a source file to a destination file, or copies multiple source files to a destination directory.
Example:
# Copy a.txt to the test directory, keep the original file time, if the original file exists, prompt whether to overwrite or not
cp -ai a.txt test
# Suggest a link for a.txt (shortcut)
cp -s a.txt link_a.txt
Command: cat
The cat command has three main functions.
- display the entire file at once: cat filename
- from the keyboard to create a file: cat > filename can only create new files, can not edit existing files.
- merge several files into one: cat file1 file2 > file -b outputs line numbers for non-empty lines -n outputs all line numbers
Example:
# Input the contents of log2012.log into log2013.log with line numbers.
cat -n log2012.log log2013.log
# Append the contents of log2012.log and log2013.log to log.log with line numbers (without blank lines)
cat -b log2012.log log2013.log log.log
# Generate a new file using here doc
cat >log.txt <<EOF
>Hello
>World
>PWD=$(pwd)
>EOF
ls -l log.txt
cat log.txt
Hello
World
PWD=/opt/soft/test
Command: head
head is used to display the beginning of the file to the standard output, the default head command prints the first 10 lines of the corresponding file.
Commonly used parameters: -n<line count>
The number of lines to display (a negative line count means counting forward from the end).
Example:
#(1) Display the first 20 lines of the 1.log file.
head 1.log -n 20
#(2) Display the first 20 bytes of 1.log file.
head -c 20 log2014.log
#(3) Display the last 10 lines of t.log
head -n -10 t.log
Command: tail
The tail command is used to display the content at the end of the specified file, and processed as input information when no file is specified. Commonly used to view log files.
Common parameters:
-f Cyclic read (often used to view incremental log files)
-n<line count>
show lines (back to front)
Example:
tail -f /tmp/1.log
tail -n 100 /tmp/1.log
Commands: find & which
In linux, if you want to find a file, but you don't know where you put it, you can use some of the following commands to search for it:
# which: See the location of the executable file.
which gcc
# which searches for the location of a system command in the path specified by PATH and returns the first search result. Using the which command, you can see whether a particular system command exists and exactly which location of the command was executed.
# find actually searches the hard disc for the name of the query file.
find /home/hpcadmin -name vasp.sh
# find searches for the location of a file in a specified path and returns the first search result. Using the find command, you can see whether a file exists and where it exists.
Command: chmod
Used to change the access permissions of a file or directory on the linux system.
Each file or directory has three groups of access permissions, each group is represented by three bits, respectively, the file owner's read, write and execute permissions; with the owner of the same group of users read, write and execute permissions; other users in the system read, write and execute permissions.
# Take the file log2012.log as an example:
-rw-r--r-- 1 root root 296K 11-13 06:03 log2012.log
# The first column has 10 positions, the first character specifies the file type. In the usual sense, a directory is also a file. If the first character is a dash, it means it is a non-directory file. If it is a d, it is a directory. Starting from the second character to the tenth there are 9 characters, groups of 3 characters, which indicate 3 groups of user permissions on a file or directory. The permission characters are crossed out for empty permissions, r for read-only, w for write, and x for executable.
# Common parameters:
-c Report processing information when changes are made.
-R Processes all files in the specified directory and its subdirectories.
# Permission scopes:
u: current user of the directory or file
g: the current group of the directory or file
o: Users or groups other than the current user or group of the directory or file.
a: All users and groups
# Permission designator:
r: read permission, indicated by the number 4
w: Write permission, indicated by the number 2
x: execute permission, represented by the number 1
- r: read permission, denoted by the number 4 w: write permission, denoted by the number 2 x: execute permission, denoted by the number 1
s: special privilege
Example:
# Increase the execute permissions of the file t.log for all users.
chmod a+x t.log
# Revoke all permissions, then give the owner readable permissions, and output the processing information
chmod u=r t.log -c
# Assign read, write, and execute (7) permissions to the file's owner, read and execute (5) permissions to the file's group, and execute (1) permissions to other users.
chmod 751 t.log -c (or: chmod u=rwx,g=rx,o=x t.log -c)
# Add read permissions to all files in the test directory and its subdirectories
chmod u+r,g+r,o+r -R text/ -c
Command: tar
Used to compress and decompress files. tar itself does not compress files, it only packs them, the compression and decompression is done by calling other functions.
# Common parameters:
-c Creates a new tar file.
-f Specifies the zip file.
-r Add files to an existing tarball.
-u Add changed or existing files to the archive.
-x Extract files from the archive
-t Show contents of zip file
-z Support gzip compression
-j Supports bzip2 compression
-Z Supports compress to decompress files
-v Display the operation process
Example:
# Pack all files into a tarball
tar -cvf log.tar 1.log,2.log or tar -cvf log.*
# Pack all files and directories under /etc into the specified directory with gz compression
tar -zcvf /tmp/etc.tar.gz /etc
# View the contents of the file you just packed (be sure to add z, as it was compressed using gzip)
tar -ztvf /tmp/etc.tar.gz
# To zip /home, /etc but not /home/dmtsai
tar --exclude /home/dmtsai -zcvf myfile.tar.gz /home/* /etc
Command: chown
The chown command changes the owner of the specified file to the specified user or group, the user can be a username or user ID, the group can be a group name or group ID, and the file is a space-separated list of files whose permissions are to be changed, with wildcards supported.
# Common parameters:
-c Displays information about the changed section
-R Processes the specified directory and subdirectories
Example:
# Change the owner and group and display the change information
chown -c mail:mail log2012.log
# Change the file group
chown -c :mail t.log
# Change folder and subdirectory owner and group to mail
chown -cR mail: test/
Command: df
Display disc space usage. Gets information about how much space is taken up on the hard disc and how much space is left.
# Common parameters:
-a List of all filesystems
-h Display information in a readable format
-i Display inode information
-k blocks of 1024 bytes
-l Show local discs only
-T List file system types
Example:
# Show disc usage
df -l
# List all file systems and their types in an easy-to-read manner
df -haT
Command: du
The du command also looks at used space, but unlike the df command, the Linux du command looks at the space used by file and directory disks
# Command Format:
du [options] [file]
# Common parameters:
-a Displays the size of all files in a directory
-k Displays file sizes in kilobytes (KB)
-m Displays file sizes in MB
-g Display file sizes in gigabytes.
-h Display file sizes in an easy-to-read format
-s Displays totals only
-c or --total displays the sum of all directories or files in addition to the size of individual directories or files
Example:
# Display folder and subfolder sizes in an easy-to-read format
du -h scf/
# Display the size of all files in a folder in an easy-to-read format.
du -ah scf/
# Show the size of several files or directories in terms of disk space, and also the sum of them
du -hc test/ scf/
# Output the space used by each subdirectory of the current directory.
du -hc --max-depth=1 scf/
Command: date
Displays or sets the system date and time.
Example:
# Display the current time
date
# Display the next day
date +%Y%m%d --date="+1 day" //Show the date of the next day.
# The -d parameter is used
date -d 'nov 22' 22 November is a Wednesday this year.
date -d '2 weeks' date 2 weeks from now
date -d 'next monday' (next Monday's date)
date -d next-day +%Y%m%d (tomorrow's date) or: date -d tomorrow +%Y%m%d
date -d last-day +%Y%m%d (yesterday's date) or: date -d yesterday +%Y%m%d
date -d last-month +%Y%m(what month was last month)
date -d next-month +%Y%m%d (what month is next)
Command: grep
A powerful text search command, grep (Global Regular Expression Print).
# Command Format:
grep [option] pattern file|dir
# Command format: grep [option] pattern file|dir
-A n --after-context Displays n lines after the matching character.
-B n --before-context Displays n lines before the matching character.
-C n --context displays n lines before and after matching characters
-c --count Counts the number of columns that match the style.
-i Ignore case
-l List only the names of files whose content matches the specified style.
-f Reads keywords from a file
-n Display the number of lines in the file with matching text.
-R Recursive folder search
# Rules for grep: ^ # Anchor the start of the line.
^ # Anchor the start of a line e.g. '^grep' matches all lines that start with grep.
$ # Anchor end of line e.g. 'grep$' matches all lines ending in grep.
. # Match a non-newline character e.g., 'gr.p' matches gr followed by an arbitrary character, then p.
* # Match zero or more previous characters e.g. '*grep' matches all lines with grep immediately after one or more spaces.
. * # Used together to represent arbitrary characters.
[] # Matches a specified range of characters, e.g. '[Gg]rep' matches grep and grep.
[^] # Match a character not in the specified range, e.g. '[^A-FH-Z]rep' matches lines that do not start with a letter containing A-R and T-Z, immediately following rep.
\(. \) # Marks the matching character, e.g. '\(love\)', love is marked as 1.
\< # Anchor the start of a word, e.g.:'\<grep' matches a line containing a word starting with grep.
\> # Anchor the end of the word, e.g., 'grep\>' matches lines containing words ending in grep.
x\{m\} # Repeat character x, m times, e.g. '0\{5\}' matches lines containing 5 o's.
x\{m,\} # Repeat character x, at least m times, e.g.: 'o\{5,\}' matches a line with at least 5 o's.
x\{m,n\} # Repeat character x, at least m times, no more than n times, e.g.: 'o\{5,10\}' matches lines with 5 - 10 o's.
\w # Matches literal and numeric characters, i.e., [A-Za-z0-9], e.g., 'G\w*p' matches a line with G followed by zero or more literal or numeric characters, then p.
\W # The inverted form of \w matches one or more non-word characters, such as a dot period.
\b # Word lock character, e.g.: '\bgrep\b' matches grep only.
Example:
# Find the specified process
ps -ef | grep svn
# Find the specified number of processes
ps -ef | grep svn -c
# Read keywords from a file
cat test1.txt | grep -f key.log
# Recursively find lines starting with grep from a folder, and list only files
grep -lR '^grep' /tmp
# Find lines with non-x switches
grep '^[^x]' test.txt
# Show lines containing the ed or at character
grep -E 'ed|at' test.txt
Command: ps
The ps(process status) command, used to view the current running process status, one-time view, if you need dynamic continuous results use top
# linux processes have five states.
1. running (running or waiting in the running queue)
2. interrupted (sleeping, blocked, waiting for a condition to form or receiving a signal)
3. uninterruptible (not waking up and not running when signalled, the process must wait until an interrupt occurs)
4. deadlocked (the process is terminated, but the process descriptor exists until the parent process is released by calling the wait4() system call)
5. stop (the process receives SIGSTOP, SIGSTP, SIGTIN, SIGTOU signal to stop running after running)
# The ps tool identifies five status codes for a process.
D uninterruptible sleep (usually IO)
R runnable (on run queue)
S interrupted sleeping
T stopped traced or stopped
Z defunct ("zombie") process
# Command Parameters:
-A show all processes
a defunct ("zombie") process # Command parameters: -A show all processes
a show all processes -a show all processes in the same terminal
c Show real names of processes
e Show environment variables
f Show relationships between processes
r Show processes running in the current terminal
-aux Show all processes that contain other uses
Example:
# Display environment variables and inter-process relationships for all current processes
ps -ef
# Show all current processes
ps -A
# Use grep to find a process
ps -aux | grep apache
# Find the PID numbers associated with the cron and syslog services.
ps aux | grep '(cron|syslog)'
Command: top
Display information about the processes currently running on the system, including process IDs, memory usage, CPU usage, etc.
# Commonly used parameters:
-c Show full process command
-s secret mode
-p <process number> Specify the process to display
-n <times> Cycle through the processes
示例:
14:06:23 up 70 days, 16:44, 2 users, load average: 1.25, 1.32, 1.35
Tasks: 206 total, 1 running, 205 sleeping, 0 stopped, 0 zombie
Cpu(s): 5.9%us, 3.4%sy, 0.0%ni, 90.4%id, 0.0%wa, 0.0%hi, 0.2%si, 0.0%st
Mem: 32949016k total, 14411180k used, 18537836k free, 169884k buffers
Swap: 32764556k total, 0k used, 32764556k free, 3612636k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
28894 root 22 0 1501m 405m 10m S 52.2 1.3 2534:16 java
# The first five lines are an overall statistical information area for the current system situation.
# The first line, task queue information, is the result of the uptime command, with the following parameters:
14:06:23 — Current system time
up 70 days, 16:44 - the system has been running for 70 days, 16 hours and 44 minutes (the system has not been rebooted during this period!)
2 users - There are currently 2 users logged in to the system
load average: 1.15, 1.42, 1.44 - The three numbers after the load average are the load conditions for 1 minute, 5 minutes, and 15 minutes.
The load average data is calculated by checking the number of active processes every 5 seconds and calculating the value according to a specific algorithm. If this number is divided by the number of logical CPUs, and the result is higher than 5, the system is overloaded.
# The second line, Tasks - Tasks (processes), describes the information as follows:
The system has 206 processes, of which 1 is running, 205 are asleep, 0 are stopped, and 0 are zombies.
# The third line, cpu status information, describes the attributes as follows:
5.9% us - Percentage of CPU occupied by user space.
3.4% sy - Percentage of CPU occupied by kernel space.
0.0% ni - Percentage of CPU occupied by processes that have changed priorities
90.4% id - Percentage of free CPU
0.0% wa - Percentage of CPU occupied by IO waits
0.0% hi - Percentage of CPU occupied by Hardware IRQs
0.2% si - Percentage of CPU used by Software Interrupts
# Note: Here the CPU usage ratio is different from windows concept, you need to understand the knowledge of linux system user space and kernel space!
# The fourth line, memory status, has the following information:
32949016k total - total physical memory (32GB)
14411180k used - total memory in use (14GB)
18537836k free - total free memory (18GB)
169884k buffers - amount of cached memory (169M)
# The fifth line, swap swap partition information, is described as follows:
32764556k total - total amount of swap partition (32GB)
0k used - total amount of swap used (0K)
32764556k free - total amount of swap partition free (32GB)
3612636k cached - total amount of swap area cached (3.6GB)
# Sixth, empty line.
# Seventh line below: status monitoring for each process (task), item column information is described below:
PID - process id
USER - process owner
PR - process priority
NI - nice value. Negative values indicate high priority, positive values indicate low priority
VIRT - total amount of virtual memory used by the process, in kilobytes (kb).
RES - the amount of physical memory used by the process that has not been swapped out, in kilobytes RES=CODE+DATA
SHR - Shared memory size in kb.
S - process state. d=uninterruptible sleep state r=running s=sleep t=track/stop z=zombie process
%CPU - Percentage of CPU time used since last update
%MEM - percentage of physical memory used by the process
TIME+ - Total amount of CPU time used by the process in 1/100th of a second
COMMAND - Process name (command name/command line)
# top interactive commands
h Display top interactive command help information
c Switch between displaying the command name and the full command line
m Sort by memory usage
P Sort by percentage of CPU usage
T Sort by time/accumulated time
W Write current settings to ~/.toprc file
o or O Changes the order of displayed items
Command: kill
Sends the specified signal to the corresponding process. Unspecified models send SIGTERM (15) to terminate the specified process. If you cannot terminate the process, use the "-KILL" parameter, which sends the signal SIGKILL(9) to forcibly terminate the process, use the ps command or the jobs command to view the process number. root users will affect the user's processes, non-root users can only affect their own processes.
# Common parameters:
-l signals, if you don't add the signal number parameter, use "-l" to list all the signal names.
-a When dealing with the current process, there is no restriction on the correspondence between the command name and the process number.
-p Specifies that the kill command only prints the process number of the relevant process without sending any signals.
-s Specifies to send a signal
-u Specifies the user.
Example:
# First use ps to find process pro1, then kill it with kill
kill -9 $(ps -ef | grep pro1)
Command: free
Display system memory usage, including physical memory, swap memory and kernel buffer memory.
# command arguments:
-b Display memory usage in bytes.
-k Display memory usage in kilobytes.
-m Display memory usage in mb.
-g Display memory usage in gb.
-s <interval seconds> display memory continuously
-t Display memory usage in total
Example:
#(1) Display memory usage
free
free -k
free -m
#(2) Display memory usage information as a sum
free -t
#(3) Query memory usage periodically
free -s 10