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}.txtCreate 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 fileRename a file
Rename file.txt to readme.md
mv file.txt readme.mdMove a directory
Move the directory music/ to the directory media/
mv music media/
# Or write it as
mv music/ media/musicRename 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 contentCreate a directory
Create music/ directory
mkdir music
# Create a series of folders
mkdir -p media/music/rockView information
File and directory size
du -sh node_modules/File information
stat -x file # MacOS
stat file # LinuxFile 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.txtDirectory 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 folderDisplay 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 # WindowsOpen a file in any program
open -a appName fileDelete
Delete a file
Delete file.txt
rm file.txtDelete a directory
Delete music/ directory
rm -r musicUnzip
Compress the entire directory
Compress the directory music/ to archive.zip
zip -r archive.zip musicUnzip a file
Unzip archive.zip
unzip archive.zipTake 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.zipSearch
Find old files
Find all files that were last modified 5 days ago
find folder -mtime +5Retrieve file contents
grep -i "music" file.txtgrep can retrieve specific content in a file, some common supporting command line parameters:
-i: case sensitive-A/-B/-C <N>: display the context,-Ameans the next N lines, -B means the first N lines,-Cmeans 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_nameNetwork
Server response
curl -i https://pengzhanbo.cnCheck 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 443Domain name DNS configuration
dig pengzhanbo.cnDomain name owner and registration information
whois pengzhanbo.cnHotkeys
Ctrl + AJump to the beginning of the command line you are currently editingCtrl + EJump to the end of the command line you are currently editingCtrl + LClear the screen, similar to the clear commandCtrl + Uclears the content before the cursor in the line (clears the entire line at the end of the line)Ctrl + His the same as backspaceCtrl + Rallows you to search for previously used command line recordsCtrl + Cforce stop the current programCtrl + Dexit the current shell (shell/command line interface)Ctrl + Zsuspend the currently running program, use fg to resume runningCtrl + Wdelete the word before the cursorCtrl + Kclear the content after the cursor in the lineCtrl + Tswap the two characters before the cursorEsc + Tswap the two words before the cursorAlt + Fmove the cursor to the next word in the lineAlt + Bmove the cursor to the previous word in the lineTabautomatically 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