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
clearcommand or theCtrl + Lshortcut 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,lsis the command name, and-lis the option, which specifies the format for displaying files in the directory.cd /root:cdis the command name, and/rootis the parameter.touch 1.txt 2.txt 3.txt:touchis 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 (dfor directory,-for file), permissions, owner, file size, etc., e.g.,ls -l.ls -al: Combines-aand-lto show all files and directories (including hidden ones) with detailed information.ls -al /etc: Here,/etcis 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/localdirectory.
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 theprofilefile in the/etcdirectory.cat -n /etc/profile: View the contents of theprofilefile 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
qorCtrl + 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 theprofilefile by default.tail -20 /etc/profile: Displays the last 20 lines of theprofilefile.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 namedkathyin the current directory.mkdir -p kathy/test: Creates a nested directorytestinsidekathy. Ifkathydoes 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 thekathydirectory if it is empty.rmdir -p kathy/test: Deletes thetestsubdirectory insidekathy, and deleteskathyif 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 theitcastdirectory and all its contents, with confirmation.rm -rf itcast/: Deletes theitcastdirectory and all its contents without confirmation.rm -f hello.txt: Deletes thehello.txtfile 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.txtto theitcastdirectory.cp hello.txt ./hi.txt: Copieshello.txtto the current directory and renames it tohi.txt.cp -r itcast/ ./itheima/: Copies theitcastdirectory and its contents to theitheimadirectory.
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.txttohi.txt.mv hi.txt itheima/: Moveshi.txtto theitheimadirectory.
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
.tarsuffix: Only packaged, not compressed. - Files with a
.tar.gzsuffix: 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 the01directory into a file named01.tar.tar -zcvf 01.tar.gz 01: Package and compress the01directory into a file named01.tar.gz.tar -xvf 01.tar: Unpack the01.tarfile into the current directory. The default extracted directory name will be01.tar -zxvf 01.tar.gz: Decompress and unpack the01.tar.gzfile 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:
vimis an enhanced version ofvi, offering additional features like syntax highlighting, making it more convenient for editing. In practical use,vimis more commonly used.- To use the
vimcommand, 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,
vimwill open it; if it does not exist, a new file will be created.
Example:vim HelloWorld.java. vimoperates 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,
ggto move to the start,Gto 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
ESCto 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.javain 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.javain the/itcastdirectory 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 stringHelloin theHelloWorld.javafile.grep hello *.java: Search for all occurrences of the stringhelloin all.javafiles in the current directory.