1. Directory processing command (above) Create directory command
mkdir -p 【目录名】
-P is used to recursively create
mkdir -p outerdir/innerdirdesktop Jeff$ ls$RECYCLE.BINLearning_PythonThumbs.dbouterdir实习小结.md A*算法SSH设置myBooks毕业规范
Toggle Directory Command
cd 【目录名】
cd ~and cd back home directory
cd -Go back to your last directory
cd ..Back to the top level directory
cd .Go to current directory
Also control+L clear screen shortcut keys
Absolute path and relative path
The following is the directory structure of Mac OS X
jeffsmacbook-Pro:~Jeff$ pwd/users/jeffjeffsmacbook-Pro:~ jeff$ CD. jeffsmacbook-pro:Users jeff$ CD. jeffsmacbook-pro:/jeff$ lsapplications home library installer.failurerequests network net system private users sbin Volumes tmp bin usr cores var dev user information etc
Double-click Tab Directory/Command completion 2. Directory processing command (bottom) Delete empty directory
rmdir【目录名】
Delete a non-empty directory or file
rm -rf 【目录/文件】
-rDelete Directory
-fForced
Simply rm or rm -r would you really want to delete files or directories?
Do not hit rm -rf / will erase all files under the root directory!!!
Copy command
cp 【选项】【原文件/目录】【目标目录】
-rCopy Directory
-pJoint file attribute replication
-dWakahara file is a linked file, copy the link property
-aEquivalent to-PDR, guaranteed to be identical to the original file attributes!
/Users/Jeff/Documents //复制japan目录下的目录到文稿目录下cp -r japan /Users/Jeff/Documents //复制japan目录到文稿目录下
Besides, it ll 's an ls -l order!
Cut or Rename command
mv 【原文件或目录】【目标目录】
In the same directory is renamed
3. Additional help command Options Help
命令 --helpSuch asls --help
Shell Internal Command Help
help shell内部命令
Used to whereis cd determine if a shell internal command is help cd used to get internal command help
Detailed command Help
info 命令It is not recommended!!
-回车Go to Child help page
-uUp into the upper page
-nNext, go to the next Help section.
-pPrevious into the previous Help section
-qQuit quitting
4. Output redirection standard input and output
| Equipment |
Device file name |
File Descriptor |
type |
| Keyboard |
/dev/stdin |
0 |
Standard input |
| Display |
/dev/sdtout |
1 |
Standard output |
| Display |
/dev/sdteer |
2 |
Standard error Output |
Output redirection
ipconfig > ~/desktop/test.logFor example, save the content in a log file
Redirects to a file as long as the command has output
Special attention, error output greater than the number and the file can not have spaces!!!
Correct and error outputs are saved simultaneously
The 3 and 4 functions are the same! Recommended 3,4,5!
Especially this file /dev/null , called the "black Hole", if the command is redirected here, the system simply discards
Input redirect
wc【选项】【文件名】
Options:
-cNumber of statistics bytes
-wCount the number of words
-lNumber of rows of statistics
This example is first introduced here--the wc < test.log wc Original command input is the keyboard, now redirected to the file, of course, does not need<
Another example is---------------- wc << ddy it will require you to keep typing, knowing that it will ddy stop and do the relevant statistics, and that the actual work will not be used
5. Wildcard with wildcard characters
| wildcard characters |
function |
| ? |
Match an arbitrary character |
| * |
Match 0 or any number of any character, which can match anything |
| [] |
Matches any one of the characters in the brackets. For example [ABC] represents a certain match for a character, or a or B or C |
| [-] |
Matches any one of the characters in the brackets-representing a range. For example, [A-z] means matching a lowercase letter |
| [^] |
Logical non, which means that the match is not a character within the brackets. For example [^0-9] stands for matching a character that is not a number |
Other special symbols in bash
| symbols |
function |
| ‘‘ |
Single quotation marks. All special symbols in single quotes, such as "$" and "'" (anti-quotes) have no special meaning |
| "" |
Double quotation marks. Special symbols have no special meaning in double quotes, but "$", "" "and" \ "are exceptions, with special meanings for" value of the calling variable "," Reference command ", and" escape character " |
| `` |
Anti-quote. The contents of the inverted quotation mark are system commands, which are executed first in bash. As with $ (), it is recommended to use $ () because the anti-quote is very easy to read. |
| $() |
As with anti-quotes, used to refer to system commands |
| # |
In the shell script, #开头的行代表注释 |
| $ |
The value that is used to invoke the variable, such as the value of the variable name that needs to be called, is $name to get the value of the variable. |
| \ |
The escape character, followed by the special symbol after \ will lose special meaning, stale ordinary characters. such as \$ will output the "$" symbol, not as a variable reference |
Example
aa=123echo $aa # output 123echo ' $aa ' # output $aaecho $aa "# output 123aa= ' ls ' # first execute, then assign value, Easy to mistake, not recommended echo $aa # output ls output aa=$ ( LS) # recommended echo $aa # output ls output echo \ $aa # let special symbol lose function # output $AA
6. File Search Command File Search command
locate 文件名Search by file name in the background database, faster
This database is /var/lib/mlocate , its update rate by default is once a day, Linux version of different, name is different, Mac under/var/db/locate.datebase
You can use updatedb commands to force updates to the database, under Mac sudo /usr/libexec/locate.updatedb , depending on the number of files changed since the last update, the speed is different
Search by file name only
Follow /etc/updatedb.conf the filter rules in the configuration file, so even if you update the database, you may not be able to locate to the files you are looking for.
command to search for commands
whereis【选项】命令名
-bFind executable files only
-mFind only Help files
Mac doesn't use these two options
which 命令名
Can see aliases, Mac does not have this function
cdCommands are brought by the shell, not found, whereis and which can be found are later installed commands
PATH environment variable
Linux PATH seeks commands from defined paths, all commands do not bother to hit absolute paths
echo $PATHView paths, : separating
File Search Command
find【搜索范围】【搜索条件】
-nameSearch by file name
-inameFile names are case insensitive
Wildcard characters in Linux
#find是在系统当中搜索符合条件的文件名. If a match is required, wildcard matches are used, and wildcards are exactly matched.
* Match any content (any number of characters)
? Match any one character
[] matches any one of the characters in brackets
such as find desktop/ -name "ab[cd]" matching ABC or ABD
Search by Owner
-userSearch by Owner, for example, find /root -user root to find the owner root file in the root directory
-nouseris to find files that have no owner, such as find /root -nonuser files that do not have an owner in the root directory
Search by Time
find 【搜索范围】 -(X)time (+/-)时间
If you find /root -mtime +10 are looking for a file that was modified 10 days ago
+10Files modified 10 days ago
10Documents modified on the day of 10 days
-10Files modified within 10 days
atimeTime to access the file
ctimeTime to change file properties
mtimeTime to modify file contents
Search by size
find 【搜索范围】-size (+/-)X(k/M)
Lowercase k is KB, uppercase M is MB
If you find /root -size +10k are looking for files larger than 10kB in the root directory
Follow the I node to search
find 【搜索范围】-inum 【i节点号】
ls -i 【文件名】just the opposite.
Multi-Criteria Query
find /root -size +20k -a -size -50kFind Files of 20~50kb size
find /root -size -20k -o -size +50kFind files that are less than 20 or larger than 50kB
find /root -size +20k -a -size -50k -exec ls -lh {}\;Find 20~50kb files, and list more information
-exec/-ok 命令 {} \;Used to continue processing the searched file
find /root -inum 606838 -exec rm -rf {} \;Find the I-node number 606838 file and delete
7. Compress commands 1
The most common compression formats in Linux are:. zip. gz. bz2. tar.gz. tar.bz2
. zip Format compression
zip 压缩文件名 原文件#压缩文件
zip -r 压缩文件名 源目录#压缩目录
JeffsMacBook-Pro:~ Jeff$ zip ~/desktop/install.zip ~/desktop/install.log adding: Users/Jeff/desktop/install.log (stored 0%)JeffsMacBook-Pro:~ Jeff$ ls -l ~/desktop-rw-r--r-- 1 Jeff staff 0 4 13 18:35 install.log-rw-r--r-- 1 Jeff staff 210 4 15 23:07 install.zip
. zip Format Decompression
unzip 压缩文件#在哪个目录下操作就解压到那个目录下
. gz format Compression
gzip 源文件#压缩后源文件会被删除
gzip -c 源文件 > 压缩文件#源文件被保留
Such asgzip -c cangls > cangls.gz
gzip -r 目录#只能压缩目录下所有的子文件, but cannot compress the directory
You can use wildcard characters to handle multiple compressed files uniformly, such asrm -rf *.zip
. gz Format Decompression
gzip -d 压缩文件
Equivalent gunzip 压缩文件 , the source compressed file will be deleted
. bz2 format Compression
bzip2 源文件#压缩后源文件会被删除
bzip2 -k 源文件#源文件被保留
This command cannot compress the directory!
. bz2 Format Decompression
bzip2 -d 压缩文件You can use the -k preserve source file
Equivalent to the bunzip2 压缩文件 same available-k
8. Compress command 2 To resolve the inconvenience of the. GZ and. bz2 compressed catalogs by packaging. Tar
Common compression formats: .tar.gz and.tar.bz2
Packaging commands
tar -cvf 打包文件名 源文件
-cPackaged
-vDisplay process
-fSpecify the file name after packaging
Such astar -cvf longzls.tar longzls
Then compress the packaged files
Unpacking commands
tar -xvf 打包文件
-x 解打包
Such astar -xvf longzls.tar
. tar.gz format
tar -zcvf 压缩包名.tar.gz 源文件Compression
tar -zxvf 压缩包名.tar.gzUnzip
tar -ztvf 压缩包名.tar.gzViewing the contents of a compressed package
. tar.bz2 format
tar -jcvf 压缩包名.tar.bz2 源文件Compression
tar -jxvf 压缩包名.tar.bz2Unzip
tar -jtvf 压缩包名.tar.bz2Viewing the contents of a compressed package
You can 解压缩命令 add the -C 指定目录 extracted files to a different directory after adding them.
You can 压缩命令 compress multiple files together in a space at the source file, such astar -zcvf jp.tar.gz japan install.log
9. User Login View command to view user login information
w 用户名(You can do this without adding a user name)
Command output:
- User: Username for login
- TTY: Login Terminal
- From: Which IP address to log on from
- [Email protected]: Login time
- Idle: User idle time
- JCPU: Refers to the time taken by all processes that the terminal is connected to. This time does not include past background job time, but includes the time taken by the currently running background job
- PCPU: Refers to the time occupied by the current process
- What: commands that are currently running
who 用户名and w the same, just simpler (no user name is allowed)
Command output:
- -User Name
- -Login Terminal
- -Logon time (login source IP address)
View switch Restart information for all users in the past
lastThe default is /var/log/wtmp to read the file data, which is a binary file that prevents human modification
Command output:
- -User Name
- -Login Terminal
- -Login IP
- -Logon Hours
- -Exit time (online time)
View last login information for all users
lastlogRead /var/log/lastlog file data by default
10. String Search Command
grep 【选项】"字符串" 文件名
#在文件中查找字符串
-iIgnoring case because Linux is strictly case sensitive
-vSearch for files that do not contain strings
grep needs to 正则表达式 be matched using
Linux Basics (11-20)