Linux | Red Hat Certification | IT Technology | Operations Engineer
👇Join our technical exchange QQ group with the note 【Official Account】 for quicker access
1. What is Linux
1. System Programming & Network Programming: Linux itself provides some APIs for programmers to call to complete more complex programming tasks (such as file operations, multithreading, socket programming, etc.). However, due to Java’s cross-platform features, this part of the functionality has already been encapsulated by Java itself (such as stream objects, Thread objects, Socket objects, etc.). Therefore, we do not need to learn this part.
2. Deploying Java Web Projects: For our self-written web programs to be accessible to other users, we need to publish them on a server. This is the focus of our learning next.
Terminal software is a type of tool software that can establish a network connection with a remote host, allowing operations on the host.
The commands used in the following explanations are Xshell commands.
2. Common Linux Commands
2.1 ls
Syntax: ls [options] [directory or file]
Function: For directories, this command lists all subdirectories and files under the directory. For files, it will list the file names and other information.
Common options:
-a List all files in the directory, including hidden files starting with .. -d Display the directory like a file instead of showing its contents. For example: ls -d specified_directory -k Display file size in kilobytes. ls -alk specified_file -l List detailed information about the file. -r Reverse sort the directory. -t Sort by time. -R List files in all subdirectories (recursive).
Example:
ls -l
2.2 pwd
Syntax: pwd
Function: Display the current directory of the user.
Example:
pwd
2.3 cd
In the Linux system, files and directories on the disk form a directory tree, where each node is a directory or file.
Syntax: cd directory_name
Function: Change the working directory. Change the current working directory to the specified directory.
Example:
# Go back to the parent directory
cd ..
# Enter the user home directory
cd ~
# Return to the last accessed directory
cd -
2.4 touch
Syntax: touch [options]… file…
Function: The touch command can change the date and time of a document or directory, including access time and modification time, or create a new file if it does not exist.
Example:
touch test.txt
2.5 cat
Syntax: cat [options] [file]
Function: View the contents of the target file.
Common options:
-n Number all output lines.
cat test.txt
2.6 mkdir
Syntax: mkdir [options] dirname…
Function: Create a directory named “dirname” in the current directory.
Common options: -p, --parents can be a path name. If some directories in the path do not exist, adding this option will automatically create those non-existent directories, i.e., it can create multiple levels of directories at once.
Example:
# Recursively create multiple directories
mkdir -p test/test1
2.7 rm
Syntax: rm [-f -i -r -v] [dirName/dir]
Function: Delete files or directories.
Common options: -f Force delete files or directories, even if the file attributes are read-only (i.e., write-protected).
-i Ask for confirmation before deleting each file.
-r Delete the directory and all files under it.
Example:
rm test.txt
3. Important Linux Commands
3.1 cp
Syntax: cp [options] source_file_or_directory target_file_or_directory
Function: Copy files or directories.
Note: The cp command is used to copy files or directories. If more than two files or directories are specified at once, and the last destination is an existing directory, it will copy all previously specified files or directories into this directory. If multiple files or directories are specified, and the last destination is not an existing directory, an error message will appear.
Common options:
-f or --force Forcefully copy files or directories, regardless of whether the destination file or directory already exists.
-i or --interactive Ask the user before overwriting files.
-r Recursively process, including all files and subdirectories in the specified directory. If the source file or directory is not a directory or symbolic link, treat it as a regular file.
-R or --recursive Recursively process, including all files and subdirectories in the specified directory.
Example:
cp test1.txt test2.txt
3.2 mv
Syntax: mv [options] source_file_or_directory target_file_or_directory
Function:
Depending on the type of the second parameter (whether it is a target file or target directory), the mv command will rename the file or move it to a new directory. When the second parameter is a file, the mv command renames the file. At this point, there can only be one source file (it can also be the source directory name), which will rename the given source file or directory to the specified target file name. When the second parameter is an existing directory name, multiple source files or directory parameters can be specified, and the mv command will move all specified source files to the target directory.
Common options:
-f: Force, meaning if the target file already exists, it will overwrite directly without asking.
-i: If the target file (destination) already exists, it will ask whether to overwrite!
Example:
mv test1.txt test2.txt
3.3 tail
Syntax: tail [required parameters] [optional parameters] [file]
Function: Used to display the end content of the specified file. If no file is specified, it will process the input information. Commonly used to view log files.
Options:
-f Continuously read
-n <number> Display the specified number of lines.</number>
Example:
tail -10 test1.txt
3.4 vim
vim is a well-known text editor. You can use vim for text editing.
vim is similar to Windows Notepad, but with more powerful functionality.
1) Create a file/Open a file
vim [filename]
2) Enter insert mode: After opening the file with vim, it defaults to normal mode. In normal mode, the keys on the keyboard represent special function shortcuts. (For example, pressing ‘j’ does not input the letter ‘j’, but moves the cursor down one line.) You need to enter insert mode to edit text. Use the ‘i’ key to enter insert mode. (The bottom left corner will show –INSERT–). Then you can edit normally like in Notepad.
3) Save: You cannot save the file in insert mode; you need to return to normal mode. Press Esc to return to normal mode. In normal mode, input :w, and then press Enter to save the file.
4) Exit: You cannot exit in insert mode; you need to return to normal mode. When exiting, there are the following situations:
① If the file is not modified: input :q, and then press Enter to exit.
② If the file is modified and saved: use :wq to save and exit simultaneously.
③ If the file is modified but you do not want to save: use :wq! to force exit.
3.5 grep
Syntax: grep [options]… [file]…
Function: Used to check whether a specified string is included in the file and display the corresponding line.
Options:
-n <number> Display the specified number of lines.
-w Full word matching. Requires the entire word to be exactly the same in order to match, not just part of a word.
-r Recursive search. Can search all files in multiple levels of directories.
--color Highlight the matched results.
--include Specify which files to search.
--exclude Specify which files to exclude.</number>
Example:
grep "hello" Hello.java
3.6 ps
Syntax: ps [options]…
Function: Used to view the processes running on the current system.
Options:
a Display all processes of a terminal in a user-oriented format.
x Display all programs, not just those in the session.
e Display all processes, including system daemons.
f Display full-format output.
Example:
ps aux # Display all processes on the system
ps aux | grep "process_name"
ps aux | grep "process_id"
3.7 netstat
Syntax: netstat [options]…
Function: View the network status on the system.
Options:
-a Display all sockets that are listening or not.
-n Display addresses in numeric form instead of resolving host, port, or username.
-p Display the PID and name of the process that owns the socket.
Example:
netstat -anp
netstat -anp | grep "process_name"
netstat -anp | grep "port_number"
For course inquiries, add: HCIE666CCIE
↑ Or scan the QR code above ↑
What technical points and content do you want to see?
You can leave a message below to let us know!