Command-Line Interface
This article only collects and records commonly used command lines and their use in specific scenarios, and does not introduce them in detail.
Copy
Copy a file
Copy file.txt
to documents/
cp file.txt documents/
### Copy a directory
Copy the entire `music/` directory to the `media/` directory
```sh
cp -a music media/
# Or write
cp -a music/ media/music/
Create a file copy
Create a copy file.bak.txt
from file.txt
cp file.txt file.bak.txt
# Or write
cp file{,.bak}.txt
Create a directory copy
Create a copy from music/
cp -a music/ media/
# If the media directory does not exist
cp -a music media/
Move
Move a file
Write Move file.txt
to documents/
mv file.txt documents/
# Do not ignore the `/` after document, otherwise it will be regarded as a renamed file
Rename a file
Rename file.txt
to readme.md
mv file.txt readme.md
Move a directory
Move the directory music/
to the directory media/
mv music media/
# Or write it as
mv music/ media/music
Rename a directory
Rename the directory music/
to media/
mv music/ media/
Merge directory files
Merge the images/
directory into the images2/
directory
# -a is equivalent to -rlptgoD , means archive, files with the same name will be overwritten
rsync -a images/ images2/
Create
Create a file
Create file.txt
touch file.txt # If the file exists, update its permissions and modification time
# Or use
> file.txt # If the file exists, clear the file content
Create a directory
Create music/
directory
mkdir music
# Create a series of folders
mkdir -p media/music/rock
View information
File and directory size
du -sh node_modules/
File information
stat -x file # MacOS
stat file # Linux
File content
View file content
cat file.txt
# If the file is too large, use `less` Let's view the content one page at a time
less file.txt
Directory files
View the files in the directory
ls folder
# -l: Display in list format. -a: Display all files including hidden files. -la Combine the above two options.
ls -la folder
# -r: Display in reverse order. -t: Sort by modification time. -h: Display size in human-readable format.
ls -alrth folder
Display the file tree of all files and subdirectories in the directory
tree folder # Linux
find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g' # MacOS
# You can also install the `tree` command line tool on MacOS using `brew install tree`
Open a file
Open a file with the default program
xdg-open file # Linux
open file # MacOS
start file # Windows
Open a file in any program
open -a appName file
Delete
Delete a file
Delete file.txt
rm file.txt
Delete a directory
Delete music/
directory
rm -r music
Unzip
Compress the entire directory
Compress the directory music/
to archive.zip
zip -r archive.zip music
Unzip a file
Unzip archive.zip
unzip archive.zip
Take a quick look at a compressed file
Take a quick look at the files in the compressed file
zipinfo archive.zip
# Or
unzip -l archive.zip
Search
Find old files
Find all files that were last modified 5 days ago
find folder -mtime +5
Retrieve file contents
grep -i "music" file.txt
grep
can retrieve specific content in a file, some common supporting command line parameters:
-i
: case sensitive-A/-B/-C <N>
: display the context,-A
means the next N lines, -B means the first N lines,-C
means the N lines before and after-E
: use regular expressions to match-v
: invert (output non-matching lines)-l
: only output file names that can match the content-F
: do not treat the search content as a regular expression-r
: recursively match the contents of all files in the directory-o
: Output only the matching parts (not the whole line)-a
: Search binary files too, instead of ignoring them!
Force quit the program
killall program_name
Network
Server response
curl -i https://pengzhanbo.cn
Check domain name/address connection
Check whether a domain name or a certain port of the address can be connected
nc -vz pengzhanbo.cn 443
nc -vz 1.1.1.1 443
Domain name DNS configuration
dig pengzhanbo.cn
Domain name owner and registration information
whois pengzhanbo.cn
Hotkeys
Ctrl + A
Jump to the beginning of the command line you are currently editingCtrl + E
Jump to the end of the command line you are currently editingCtrl + L
Clear the screen, similar to the clear commandCtrl + U
clears the content before the cursor in the line (clears the entire line at the end of the line)Ctrl + H
is the same as backspaceCtrl + R
allows you to search for previously used command line recordsCtrl + C
force stop the current programCtrl + D
exit the current shell (shell/command line interface)Ctrl + Z
suspend the currently running program, use fg to resume runningCtrl + W
delete the word before the cursorCtrl + K
clear the content after the cursor in the lineCtrl + T
swap the two characters before the cursorEsc + T
swap the two words before the cursorAlt + F
move the cursor to the next word in the lineAlt + B
move the cursor to the previous word in the lineTab
automatically completes the name of the file/directory
MacOS
!! # execute the previous command again
sudo !! # execute the previous command as an administrator
!<word> # Prefix the command line with a specific command line and execute the previous command
!<word>:p # Display the previous command with a prefix, but do not execute it
<space>command # Execute the command, but do not save it to the history
echo "ls -l" | at midnight # Execute the command at a specific time
caffeinate -u -t 3600 # Prevent your mac from sleeping for the next hour
ls -lhs # Sort the files in a directory by size
qlmanage -p <file> # Call "Quick View" from the command line
top -o vsize # See what is slowing down your mac