Home > Article > Operation and Maintenance > Introduction to common terminal commands under mac
First of all, let me introduce absolute paths and relative paths.
(Learning video sharing: Introduction to Programming)
Absolute path
As we all know, when we usually use computers, we need to find the files we need. The location of the file must be known, and the way to indicate the location of the file is the path. For example, as long as we see this path: c:/website/img/photo.jpg, we will know that the photo.jpg file is img in the website directory of the c drive. in the subdirectory. In this way, the path that completely describes the file location is an absolute path. We don't need to know any other information to determine the location of the file based on the absolute path. (It can also be considered that everything starting with "/" is an absolute path)
Relative path
The so-called relative path, as the name suggests, is relative to the target location. No matter where you put these files, as long as their relative relationship does not change, there will be no error. In addition, we use "../" to represent the upper-level directory, "../../" to represent the upper-level directory, and so on. (It can also be considered that all paths that do not start with "/" are relative paths)
Give a chestnut
Example 1
For example, there are two files in the ABC folder of your C drive 1 and 2 (they are both under the ABC file), if you want file 1 to tell the location of file 2
(that is, the path), then it has two representation methods:
1. Absolute path: C:\ABC\2
Because it indicates that file 2 is under the ABC file on drive C, starting from the largest directory on drive C.
2. Relative path: 2
Because file 1 and file 2 are both Under C:\ABC, so the "C:\ABC" in front of their paths are the same, so there is no need to show it.
Example 2
For example, the ABC folder on the C drive has 1 file and another DEF folder, and there are 2 files 1 and 2 under the DEF file. The file paths are:
(both absolute paths)
C:\ABC\1
C:\ABC\DEF\2
If file 1 is used to represent the path of file 2
* Absolute path: C:\ABC\DEF\2
* Relative path: DEF\2 (because C:\ABC in front of files 1 and 2 If this path is the same, there is no need to write it out).
Commonly used mac terminal operation command commands
1. Display the path of the current directory
Display the path of the current directory (allowing you to view the current path in real time)
pwd copy code
2. View the contents of the current directory (allowing you to view the contents of the current path in real time)
ls parameter directory name
Parameters: - w displays Chinese, -l details, -a includes hidden files,
If each line starts with "d", it is a folder, and if it starts with "-", it is a file.
LS Display all files or folders in the current directory
LS directory displayed in all files or folders in this directory
LS space -A display hidden file
LS spaces- l Display detailed information in the current directory
ls space -R Display subdirectory files in the current directory
ls space -w Display Chinese files in the current directory
ls space -la Display all files
Example: Take a look What is in the driver directory: ls /System/Library/Extensions
3. Switching paths (also called conversion directories) means going from one path to another path
cd directory name
cd . cd ../.. /. . Represents that returning the previous three -level directory
CD space The next directory indicates that it will enter the next directory from the current path
CD space ~ indicate from the current directory to the user root directory
CD space / indicate from the current directory from the current directory Enter the root directory
cd Space ./ means entering from the current directory to the current directory (a dot "." indicates the current)
Example: I want to take a walk in the driver directory: cd /System/Library/Extensions
4. Create a new directory (can also be understood as creating a folder)
mkdir directory name
mkdir space folder name means creating a folder
mkdir space absolute path means creating a folder with the same function as above,
except that the above is created through a relative path, below It is created through an absolute path.
Example: Create a backup directory backup under the driver directory: mkdir /System/Library/Extensions/backup
5. Delete the directory (can also be understood as deleting files Folder)
rmdir directory name
rmdir space folder name means deleting a folder
rmdir space absolute path means deleting a folder The function is the same as the above, except that the above is deleted through a relative path ,The following is deletion through absolute path
Example: Delete a file called new on the desktop MacBook-Air:Desktop admin$ rmdir new
6. Create a new file
touch File name
Example: Create a backup file backup in the current directory: touch backup.txt
7. Delete file
rm Parameter file parameter-rf Represents recursion and force , be sure to use it with caution, if you execute rm -rf /
your system will be gone
rm spaces file name and format means deleting a file
rm -r folder means forced deletion of the folder Folder containing files
(When the folder contains files, the folder cannot be deleted by using "rmdir space folder name". You can only force deletion by using "rm -f folder name")
rm -rf folder means forcibly deleting the folder containing files in the folder
(Same function as "rm -r folder name")
Note: rm can delete multiple files instead of just one file , but the paths of the two files must be separated by spaces.
Example: Want to delete the driver cache: rm -rf /System/Library/Extensions.kextcache rm -rf /System/Library/Extensions.mkext
8. Copy
cp parameter source file target file
Example: I want to copy the Natit.kext of the desktop to the driver directory cp -R /User/username/Desktop/Natit. kext /System/Library/Extensions
Parameter R indicates recursive operation on the directory. kext looks like a file in the graphical interface, but is actually a folder.
Back up all files in the driver directory to desktop backup: cp -R /System/Library/Extensions/
/User/username/Desktop/backup
9. Move files And rename the file
mv source file target file
mv the path to which the file or folder needs to be moved (the path here can be a relative path or an absolute
path);mv a.c space b.c means rename a.c to b.c
Example: want to move AppleHDA.Kext to the desktop:
mv /System/Library/Extensions/AppleHDA.kext /User/username/Desktop
10. Text editing
vim file name
vim file name indicates the content of the edited file
Example:
vim /System/Library/Extensions/Natit.kext/Info.plist
After editing, use Ctrl + O to save, Ctrl + ## * Input in "command mode":
:w Save the current file
:q Exit editing, if the file is not saved, you need to use force mode
:q! Force exit without saving changes
: wq combine commands, save and exit
* Move in "command mode":
h left
j down
k up
l right
11. Clear screen
clear
open
* The open command can open applications in the terminal, and -n can be used to open multiple identical applications. For example, you can use the following command to open a new Safari window
open -n /Applications/Safari.app/
Related recommendations:
macos system
The above is the detailed content of Introduction to common terminal commands under mac. For more information, please follow other related articles on the PHP Chinese website!