Common Linux Commands
Common Commands
Command | English | Function |
---|---|---|
ls | list | View the contents (files/directories) of the current directory |
pwd | print work directory | View the current directory |
cd [directory name] | change directory | Switch directories |
touch [file name] | touch | Create a new file if it does not exist |
mkdir [directory name] | make directory | Create a directory |
rm [file name] | remove | Delete a specified file |
Usage Tips:
- Use the Tab key for auto-completion.
- Press the Tab key twice to get operation suggestions.
- Use the up and down arrow keys to quickly recall previously used commands.
- Use the
clear
command or theCtrl + L
shortcut to clear the screen.
Linux Command Format
1 | command [-options] [parameter] |
Explanation:
command
: The name of the command.[-options]
: Optional flags to modify the command behavior (can be omitted).[parameter]
: Parameters passed to the command (can be none, one, or multiple).
Note:
- Square brackets (
[ ]
) indicate optional elements. - There should be spaces between the command name, options, and parameters.
Examples:
ls
: This is the command name.ls -l
: Here,ls
is the command name, and-l
is the option, which specifies the format for displaying files in the directory.cd /root
:cd
is the command name, and/root
is the parameter.touch 1.txt 2.txt 3.txt
:touch
is the command name, used to create files, followed by multiple parameters separated by spaces.
File and Directory Commands
1. ls
Command
Function: Displays the contents of a specified directory.
Syntax:
1 | ls [-a or -l or -al] dir |
Explanation:
-a
: Shows all files and directories, including hidden ones (those starting with.
), e.g.,ls -a
.-l
: Displays detailed information about files and directories, such as type (d
for directory,-
for file), permissions, owner, file size, etc., e.g.,ls -l
.ls -al
: Combines-a
and-l
to show all files and directories (including hidden ones) with detailed information.ls -al /etc
: Here,/etc
is the directory to be listed.
Note:
The ls -l
command can be simplified to ll
.
2. cd
Command
Function: Switches the current working directory.
Syntax:
1 | cd [dirName] |
Special Cases:
~
: Represents the user’s home directory..
: Represents the current directory...
: Represents the parent directory of the current directory.
Examples:
cd ..
: Switch to the parent directory.cd ~
: Switch to the user’s home directory.cd /usr/local
: Switch to the/usr/local
directory.
3. cat
Command
Function: Displays the contents of a file.
Syntax:
1 | cat [-n] fileName |
Explanation:
-n
: Numbers all output lines starting from 1.
Examples:
cat /etc/profile
: View the contents of theprofile
file in the/etc
directory.cat -n /etc/profile
: View the contents of theprofile
file with line numbers.
4. more
Command
Function: Displays file contents in a paginated format, which is more convenient than cat
for long files.
Syntax:
1 | more fileName |
Instructions:
- Press Enter: Scroll down one line.
- Press Space: Scroll down one screen.
- Press
b
: Go back one screen. - Press
q
orCtrl + C
: Exitmore
.
Example:more /etc/profile
: Displays the profile
file in a paginated format.
5. tail
Command
Function: Displays the last part of a file.
Syntax:
1 | tail [-f] fileName |
Explanation:
-f
: Dynamically reads and displays the last part of the file, useful for monitoring log files.
Examples:
tail /etc/profile
: Displays the last 10 lines of theprofile
file by default.tail -20 /etc/profile
: Displays the last 20 lines of theprofile
file.tail -f ~/my.log
: Dynamically reads and displays the last part ofmy.log
. You can add content to the file in another terminal window usingecho 'text' >> my.log
, and the changes will appear in real time.
6. mkdir
Command
Function: Creates directories.
Syntax:
1 | mkdir [-p] dirName |
Explanation:
-p
: Ensures parent directories are created if they don’t already exist, allowing the creation of nested directories.
Examples:
mkdir kathy
: Creates a directory namedkathy
in the current directory.mkdir -p kathy/test
: Creates a nested directorytest
insidekathy
. Ifkathy
does not exist, it will also be created.
7. rmdir
Command
Function: Deletes empty directories.
Syntax:
1 | rmdir [-p] dirName |
Explanation:
-p
: If the parent directory becomes empty after deleting the subdirectory, it will also be removed.
Examples:
rmdir kathy
: Deletes thekathy
directory if it is empty.rmdir -p kathy/test
: Deletes thetest
subdirectory insidekathy
, and deleteskathy
if it becomes empty.rmdir kathy*
: Deletes all empty directories starting withkathy
.
8. rm
Command
Function: Deletes files or directories, including non-empty ones.
Syntax:
1 | rm [-rf] name |
Explanation:
-r
: Recursively deletes directories and their contents.-f
: Force delete without confirmation.
Examples:
rm -r itcast/
: Deletes theitcast
directory and all its contents, with confirmation.rm -rf itcast/
: Deletes theitcast
directory and all its contents without confirmation.rm -f hello.txt
: Deletes thehello.txt
file without confirmation.
Copying Command cp
Function: Copies files or directories.
Syntax:
1 | cp [-r] source dest |
Explanation:
-r
: Required for copying directories. Not needed for files.
Examples:
cp hello.txt itcast/
: Copieshello.txt
to theitcast
directory.cp hello.txt ./hi.txt
: Copieshello.txt
to the current directory and renames it tohi.txt
.cp -r itcast/ ./itheima/
: Copies theitcast
directory and its contents to theitheima
directory.
Moving Command mv
Function: Renames files or directories or moves them to another location.
Syntax:
1 | mv source dest |
Examples:
mv hello.txt hi.txt
: Renameshello.txt
tohi.txt
.mv hi.txt itheima/
: Moveshi.txt
to theitheima
directory.
Packaging and Compression Command: tar
Function: Used for packaging, unpacking, compressing, and decompressing files.
Installation:
On CentOS, the tar
compression and decompression tool is not pre-installed. If the tar
command is missing, you will see the error -bash: tar: Command Not Found
. To resolve this, install tar
using:
1 | yum install -y tar |
Syntax:
1 | tar [-zcxvf] output_file_name [files_to_process] |
- Files with a
.tar
suffix: Only packaged, not compressed. - Files with a
.tar.gz
suffix: Packaged and compressed.
Options:
-z
: Use gzip for compression or decompression.-c
: Create a new package file (packaging).-x
: Extract files from a package file (unpacking).-v
: Display the command execution process (verbose).-f
: Specify the name of the package file.[files]
: Specify which files to package.
Examples:
tar -cvf 01.tar 01
: Package the01
directory into a file named01.tar
.tar -zcvf 01.tar.gz 01
: Package and compress the01
directory into a file named01.tar.gz
.tar -xvf 01.tar
: Unpack the01.tar
file into the current directory. The default extracted directory name will be01
.tar -zxvf 01.tar.gz
: Decompress and unpack the01.tar.gz
file into the current directory (commonly used).tar -zxvf 01.tar.gz -C /another_directory
: Decompress and unpack the file into a specified directory.
Text Editing Command vi/vim
1. vi
Command
Function: The vi
command is a text editor provided by Linux systems, used for editing file contents, similar to Notepad in Windows.
Syntax:
1 | vi fileName |
Notes:
vim
is an enhanced version ofvi
, offering additional features like syntax highlighting, making it more convenient for editing. In practical use,vim
is more commonly used.- To use the
vim
command, you may need to install it first:1
yum install vim
2. vim
Command
Function: Used for editing file contents. Essentially, vim
is a text editor.
Syntax:
1 | vim fileName |
Notes:
- If the specified file exists,
vim
will open it; if it does not exist, a new file will be created.
Example:vim HelloWorld.java
. vim
operates in three modes: Command Mode, Insert Mode, and Last Line Mode. You need to be aware of the current mode while usingvim
.
vim Modes Explanation
Command Mode:
- Used for viewing file content and moving the cursor (e.g., using arrow keys,
gg
to move to the start,G
to move to the end). - This is the default mode when opening a file with
vim
. - To switch to other modes, you must first return to Command Mode.
- Used for viewing file content and moving the cursor (e.g., using arrow keys,
Insert Mode:
- Used for editing file content.
- Switch from Command Mode to Insert Mode by pressing
i
,a
, oro
. Once in Insert Mode, you will see[insert]
at the bottom. - Press
ESC
to return to Command Mode.
Last Line Mode:
- Used for tasks like searching for content, displaying line numbers, or exiting.
- Switch to Last Line Mode from Command Mode by pressing
:
or/
. /text
: Search for “text” in the file. (Exit withq!
after searching.):wq
: Save and exit.:q!
: Exit without saving.:set nu
: Display line numbers.
Search Command
1. find
Command
Function: Search for files in a specified directory.
Syntax:
1 | find dirName -option fileName |
Examples:
find . -name "*.java"
: Search for all files ending with.java
in the current directory and its subdirectories.
Here:.
: Refers to the current directory.-name
: Specifies the search criteria based on file names.*.java
: Uses a wildcard to match files ending with.java
.
find /itcast -name "*.java"
: Search for all files ending with.java
in the/itcast
directory and its subdirectories.
2. grep
Command
Function: Search for specific text content in a specified file.
Syntax:
1 | grep word fileName |
word
: The text to search for.fileName
: The file to search in.
Examples:
grep Hello HelloWorld.java
: Find all occurrences of the stringHello
in theHelloWorld.java
file.grep hello *.java
: Search for all occurrences of the stringhello
in all.java
files in the current directory.