本篇继续学习linux的常用命令,上接linux学习笔记(一).
学习视频为Linux达人养成计划 I,十分感谢tony老师的无私奉献。
二、常用命令
…
16、帮助命令man
man [命令名称]
获取指定命令的帮助,man是manual的缩写,手册
只能查询非shell内部命令
例
arururururu@ubuntu:~/Desktop/linuxCommand$ man ls
LS(1)//这里的1是指所在section User Commands LS(1)
NAME //作用
ls - list directory contents
SYNOPSIS //用法
ls [OPTION]... [FILE]...
DESCRIPTION //选项说明
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is speci‐
fied.
Mandatory arguments to long options are mandatory for short options
too.
-a, --all
do not ignore entries starting with .
-A, --almost-all
do not list implied . and ..
--author
...
Manual page ls(1) line 1 (press h for help or q to quit)
1、执行man man
时可以看到man分为不同section
The table below shows the section numbers of the manual followed by the
types of pages they contain.
1 Executable programs or shell commands
2 System calls (functions provided by the kernel)
3 Library calls (functions within program libraries)
4 Special files (usually found in /dev)
5 File formats and conventions eg /etc/passwd
6 Games
7 Miscellaneous (including macro packages and conventions), e.g.
man(7), groff(7)
8 System administration commands (usually only for root)
9 Kernel routines [Non standard]
2、查看某个命令有哪些section
//ls只有一个section
arururururu@ubuntu:~/Desktop/linuxCommand$ man -f ls
ls (1) - list directory contents
//passwd命令有多个section
arururururu@ubuntu:~/Desktop/linuxCommand$ man -f passwd
passwd (1ssl) - compute password hashes
passwd (1) - change user password
passwd (5) - the password file
//也可以用whatis [命令]查看
arururururu@ubuntu:~/Desktop/linuxCommand$ whatis ls
ls (1) - list directory contents
arururururu@ubuntu:~/Desktop/linuxCommand$ whatis passwd
passwd (1ssl) - compute password hashes
passwd (1) - change user password
passwd (5) - the password file
//还可以用whereis [命令]查看
//.1.gz、.1ssl.gz、.1ssl.gz后缀就显示了所拥有的section
// /etc/passwd为配置文件,可用man 5 passwd查看相关说明
arururururu@ubuntu:~/Desktop/linuxCommand$ whereis passwd
passwd: /usr/bin/passwd /etc/passwd /usr/bin/X11/passwd /usr/share/man/man1/passwd.1.gz /usr/share/man/man1/passwd.1ssl.gz /usr/share/man/man5/passwd.1ssl.gz
3、浏览指定section的帮助手册
arururururu@ubuntu:~/Desktop/linuxCommand$ man 5 passwd
PASSWD(5) File Formats and Conversions PASSWD(5)
NAME
passwd - the password file
DESCRIPTION
/etc/passwd contains one line for each user account, with seven fields
delimited by colons (“:”). These fields are:
· login name
· optional encrypted password
· numerical user ID
· numerical group ID
· user name or comment field
· user home directory
· optional user command interpreter
...
4、查看和命令相关的所有帮助
//列出所有含有passwd字串的帮助文档
arururururu@ubuntu:~$ man -k passwd
chgpasswd (8) - update group passwords in batch mode
chpasswd (8) - update passwords in batch mode
Crypt::PasswdMD5 (3pm) - Provides interoperable MD5-based crypt() functions
fgetpwent_r (3) - get passwd file entry reentrantly
getpwent_r (3) - get passwd file entry reentrantly
gpasswd (1) - administer /etc/group and /etc/gshadow
grub-mkpasswd-pbkdf2 (1) - generate hashed password for GRUB
lppasswd (1) - add, change, or delete digest passwords.
pam_localuser (8) - require users to be listed in /etc/passwd
passwd (1) - change user password
passwd (1ssl) - compute password hashes
passwd (5) - the password file
passwd2des (3) - RFS password encryption
smbpasswd (5) - The Samba encrypted password file
smbpasswd (8) - change a user's SMB password
update-passwd (8) - safely update /etc/passwd, /etc/shadow and /etc/group
//也可以用apropos [命令]查看,
//作用:在一些特定的包含系统命令的简短描述的数据库文件里查找关键字,然后把结果送到标准输出
//忘记某些命令具体名称时可以使用
arururururu@ubuntu:~$ apropos passwd
chgpasswd (8) - update group passwords in batch mode
chpasswd (8) - update passwords in batch mode
Crypt::PasswdMD5 (3pm) - Provides interoperable MD5-based crypt() functions
fgetpwent_r (3) - get passwd file entry reentrantly
getpwent_r (3) - get passwd file entry reentrantly
gpasswd (1) - administer /etc/group and /etc/gshadow
grub-mkpasswd-pbkdf2 (1) - generate hashed password for GRUB
lppasswd (1) - add, change, or delete digest passwords.
pam_localuser (8) - require users to be listed in /etc/passwd
passwd (1) - change user password
passwd (1ssl) - compute password hashes
passwd (5) - the password file
passwd2des (3) - RFS password encryption
smbpasswd (5) - The Samba encrypted password file
smbpasswd (8) - change a user's SMB password
update-passwd (8) - safely update /etc/passwd, /etc/shadow and /etc/group
17、其他帮助命令
1、help
[命令] --help
获取命令选项的帮助
例
arururururu@ubuntu:~$ ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
Mandatory arguments to long options are mandatory for short options too.
-a, --all do not ignore entries starting with .
-A, --almost-all do not list implied . and ..
--author with -l, print the author of each file
-b, --escape print C-style escapes for nongraphic characters
--block-size=SIZE scale sizes by SIZE before printing them. E.g.,
'--block-size=M' prints sizes in units of
1,048,576 bytes. See SIZE format below.
...
2、shell内部命令帮助
help [shell命令]
获取shell内部命令帮助,使用whereis命令确定是否shell内部命令
例
//无命令的执行位置,是shell内部命令
arururururu@ubuntu:~$ whereis cd
cd:
//有执行位置,不是内部命令
arururururu@ubuntu:~$ whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz
arururururu@ubuntu:~$ help cd
cd: cd [-L|[-P [-e]] [-@]] [dir]
Change the shell working directory.
Change the current directory to DIR. The default DIR is the value of the
HOME shell variable.
The variable CDPATH defines the search path for the directory containing
DIR. Alternative directory names in CDPATH are separated by a colon (:).
A null directory name is the same as the current directory. If DIR begins
with a slash (/), then CDPATH is not used.
If the directory is not found, and the shell option `cdable_vars' is set,
the word is assumed to be a variable name. If that variable has a value,
its value is used for DIR.
Options:
-L force symbolic links to be followed: resolve symbolic links in
DIR after processing instances of `..'
-P use the physical directory structure without following symbolic
links: resolve symbolic links in DIR before processing instances
of `..'
-e if the -P option is supplied, and the current working directory
cannot be determined successfully, exit with a non-zero status
-@ on systems that support it, present a file with extended attributes
as a directory containing the file attributes
The default is to follow symbolic links, as if `-L' were specified.
`..' is processed by removing the immediately previous pathname component
back to a slash or the beginning of DIR.
Exit Status:
Returns 0 if the directory is changed, and if $PWD is set successfully when
-P is used; non-zero otherwise.
3、info
info [命令]
详细命令帮助
- 回车 进入子帮助页面(带有*号标记)
- u 进入上层页面
- n 进入下一个帮助小节
- p 进入上一个帮助小节
- q 退出
例
arururururu@ubuntu:~$ info ls
File: coreutils.info, Node: ls invocation, Next: dir invocation, Up: Directo\
ry listing
10.1 `ls': List directory contents
==================================
The `ls' program lists information about files (of any type, including
directories). Options and file arguments can be intermixed
arbitrarily, as usual.
For non-option command-line arguments that are directories, by
default `ls' lists the contents of directories, not recursively, and
omitting files with names beginning with `.'. For other non-option
arguments, by default `ls' lists just the file name. If no non-option
argument is specified, `ls' operates on the current directory, acting
as if it had been invoked with a single argument of `.'.
By default, the output is sorted alphabetically, according to the
locale settings in effect.(1) If standard output is a terminal, the
output is in columns (sorted vertically) and control characters are
output as question marks; otherwise, the output is listed one per line
and control characters are output as-is.
--zz-Info: (coreutils.info.gz)ls invocation, 58 lines --Top---------------------
Welcome to Info version 5.2. Type h for help, m for menu item.
//按下n进入10.2
File: coreutils.info, Node: dir invocation, Next: vdir invocation, Prev: ls \
invocation, Up: Directory listing
10.2 `dir': Briefly list directory contents
===========================================
`dir' is equivalent to `ls -C -b'; that is, by default files are listed
in columns, sorted vertically, and special characters are represented
by backslash escape sequences.
*Note `ls': ls invocation.
//按下p回到10.1,与初始页面相同
//按下u回到上层页面
File: coreutils.info, Node: Directory listing, Next: Basic operations, Prev:\
Operating on characters, Up: Top
10 Directory listing
********************
This chapter describes the `ls' command and its variants `dir' and
`vdir', which list information about files.
* Menu:
* ls invocation:: List directory contents.
* dir invocation:: Briefly ls.
* vdir invocation:: Verbosely ls.
* dircolors invocation:: Color setup for ls, etc.
//在dircolors invocation:: 一行按下回车
File: coreutils.info, Node: dircolors invocation, Prev: vdir invocation, Up:\
Directory listing
10.4 `dircolors': Color setup for `ls'
======================================
`dircolors' outputs a sequence of shell commands to set up the terminal
for color output from `ls' (and `dir', etc.). Typical usage:
eval "$(dircolors [OPTION]... [FILE])"
If FILE is specified, `dircolors' reads it to determine which colors
to use for which file types and extensions. Otherwise, a precompiled
database is used. For details on the format of these files, run
`dircolors --print-database'.
To make `dircolors' read a `~/.dircolors' file if it exists, you can
put the following lines in your `~/.bashrc' (or adapt them to your
favorite shell):
d=.dircolors
test -r $d && eval "$(dircolors $d)"
18、压缩与解压缩命令
1、常用压缩格式
.zip .gz .bz2 .tar.gz .tar.bz2
2、.zip格式压缩与解压缩
zip [压缩文件名] 源文件
压缩文件
zip -r [压缩文件名] 源目录
压缩目录
unzip [压缩文件名]
解压缩.zip文件
例
//压缩文件
arururururu@ubuntu:~/Desktop/linuxCommand$ ls
test1 test1~ test2 testFind testLink
arururururu@ubuntu:~/Desktop/linuxCommand$ zip test1.zip test1
adding: test1 (stored 0%)
arururururu@ubuntu:~/Desktop/linuxCommand$ ls
test1 test1~ test1.zip test2 testFind testLink
//压缩目录
arururururu@ubuntu:~/Desktop/linuxCommand$ ls test2
test
arururururu@ubuntu:~/Desktop/linuxCommand$ zip -r test2.zip test2
adding: test2/ (stored 0%)
adding: test2/test (stored 0%)
arururururu@ubuntu:~/Desktop/linuxCommand$ ls
test1 test1~ test1.zip test2 test2.zip testFind testLink
//删除源文件
arururururu@ubuntu:~/Desktop/linuxCommand$ rm test1
arururururu@ubuntu:~/Desktop/linuxCommand$ rm -r test2
arururururu@ubuntu:~/Desktop/linuxCommand$ ls
test1~ test1.zip test2.zip testFind testLink
//解压
arururururu@ubuntu:~/Desktop/linuxCommand$ unzip test1.zip
Archive: test1.zip
extracting: test1
arururururu@ubuntu:~/Desktop/linuxCommand$ unzip test2.zip
Archive: test2.zip
creating: test2/
extracting: test2/test
arururururu@ubuntu:~/Desktop/linuxCommand$ ls
test1 test1~ test1.zip test2 test2.zip testFind testLink
2、gz格式压缩与解压缩
gzip [源文件]
压缩为.gz格式的压缩文件,源文件会消失
gzip -c [源文件] > [压缩文件]
压缩为.gz格式,源文件保留
gzip -r 目录
压缩目录下所有的子文件,但是不能压缩目录
gzip -d [压缩文件]
解压文件
gunzip [压缩文件]
解压文件
例
arururururu@ubuntu:~/Desktop/linuxCommand$ ls
test1 test1~ test2 testFind testLink
//压缩test1为gz格式文件,test1文件消失
arururururu@ubuntu:~/Desktop/linuxCommand$ gzip test1
arururururu@ubuntu:~/Desktop/linuxCommand$ ls
test1~ test1.gz test2 testFind testLink
//删除test1.gz
arururururu@ubuntu:~/Desktop/linuxCommand$ rm test1.gz
//压缩test1,将压缩结果写到标准输出
arururururu@ubuntu:~/Desktop/linuxCommand$ gzip test1 -c
�4
]test1+I-.1�*�uJ/�
�0�n�y)`�Of^6��,�.arururururu@ubuntu:~/Desktop/linuxCommand$ ls
test1 test1~ test2 testFind testLink
//压缩test1,将结果写到test1.zip
arururururu@ubuntu:~/Desktop/linuxCommand$ gzip -c test1 > test1.gz
arururururu@ubuntu:~/Desktop/linuxCommand$ ls
test1 test1~ test1.gz test2 testFind testLink
//压缩目录下的所有子文件
arururururu@ubuntu:~/Desktop/linuxCommand$ ls test2
test
arururururu@ubuntu:~/Desktop/linuxCommand$ gzip test2 -r
arururururu@ubuntu:~/Desktop/linuxCommand$ ls
test1 test1~ test1.gz test2 testFind testLink
arururururu@ubuntu:~/Desktop/linuxCommand$ ls test2
test.gz
//解压test1.gz文件
arururururu@ubuntu:~/Desktop/linuxCommand$ rm test1
arururururu@ubuntu:~/Desktop/linuxCommand$ gzip -d test1
arururururu@ubuntu:~/Desktop/linuxCommand$ ls
test1 test1~ test2 testFind testLink
//解压test2目录下的test.gz文件
arururururu@ubuntu:~/Desktop/linuxCommand$ gunzip test2/test.gz
arururururu@ubuntu:~/Desktop/linuxCommand$ ls test2
test
//解压test2目录下的所有gz文件
arururururu@ubuntu:~/Desktop/linuxCommand$ touch test2/afile
arururururu@ubuntu:~/Desktop/linuxCommand$ touch test2/bfile
arururururu@ubuntu:~/Desktop/linuxCommand$ touch test2/cfile
arururururu@ubuntu:~/Desktop/linuxCommand$ gzip -r test2
arururururu@ubuntu:~/Desktop/linuxCommand$ ls test2
afile.gz bfile.gz cfile.gz test.gz
arururururu@ubuntu:~/Desktop/linuxCommand$ gunzip -r test2
arururururu@ubuntu:~/Desktop/linuxCommand$ ls test2
afile bfile cfile test
3、.bz2格式压缩与解压缩
bzip2 [源文件]
压缩为.bz2格式的压缩文件,源文件会消失
bzip2 -k [源文件]
压缩为.bz2格式,源文件保留
bzip2命令不能压缩目录
bzip2 -d [压缩文件]
解压缩,-k保留压缩文件
bunzip2 [压缩文件]
解压缩,-k保留压缩文件
例
//压缩test1文件为.bz2格式
arururururu@ubuntu:~/Desktop/linuxCommand$ ls
test1 test1~ test2 testFind testLink
arururururu@ubuntu:~/Desktop/linuxCommand$ bzip2 test1
arururururu@ubuntu:~/Desktop/linuxCommand$ ls
test1~ test1.bz2 test2 testFind testLink
//压缩test3文件并保留test3
arururururu@ubuntu:~/Desktop/linuxCommand$ touch test3
arururururu@ubuntu:~/Desktop/linuxCommand$ bzip2 -k test3
arururururu@ubuntu:~/Desktop/linuxCommand$ ls
test1~ test1.bz2 test2 test3 test3.bz2 testFind testLink
//解压test1.bz2文件,不保留压缩文件
arururururu@ubuntu:~/Desktop/linuxCommand$ bzip2 -d test1.bz2
arururururu@ubuntu:~/Desktop/linuxCommand$ ls
test1 test1~ test2 test3 test3.bz2 testFind testLink
//解压test2.bz2文件并保留压缩文件
arururururu@ubuntu:~/Desktop/linuxCommand$ rm test3
arururururu@ubuntu:~/Desktop/linuxCommand$ bunzip2 test3.bz2 -k
arururururu@ubuntu:~/Desktop/linuxCommand$ ls
test1 test1~ test2 test3 test3.bz2 testFind testLink
4、打包命令和解打包命令
tar -cvf [打包文件名] [源文件]
打包文件
tar -xvf [打包文件名]
解打包文件
-c: 打包
-x: 解打包
-v: 显示过程
-f: 指定打包后的文件名
例
//打包testFind
arururururu@ubuntu:~/Desktop/linuxCommand$ ls testFind
abc Abc abcd abd efg root testLink
arururururu@ubuntu:~/Desktop/linuxCommand$ tar -cvf testFind.tar testFind
testFind/
testFind/abcd
testFind/Abc
testFind/abd
testFind/abc
testFind/efg
testFind/root
testFind/testLink/
testFind/testLink/test-hard
testFind/testLink/test1-so
testFind/testLink/test1~
testFind/testLink/today
testFind/testLink/test.s
testFind/testLink/test1
arururururu@ubuntu:~/Desktop/linuxCommand$ ls
test1 test1~ test2 test3 test3.bz2 testFind testFind.tar testLink
//再压缩为.gz格式
arururururu@ubuntu:~/Desktop/linuxCommand$ gzip testFind.tar
arururururu@ubuntu:~/Desktop/linuxCommand$ ls
test1 test1~ test2 test3 test3.bz2 testFind testFind.tar.gz testLink
//解打包
arururururu@ubuntu:~/Desktop/linuxCommand$ rm -rf testFind
arururururu@ubuntu:~/Desktop/linuxCommand$ tar -xvf testFind.tar.gz
testFind/
testFind/abcd
testFind/Abc
testFind/abd
testFind/abc
testFind/efg
testFind/root
testFind/testLink/
testFind/testLink/test-hard
testFind/testLink/test1-so
testFind/testLink/test1~
testFind/testLink/today
testFind/testLink/test.s
testFind/testLink/test1
arururururu@ubuntu:~/Desktop/linuxCommand$ ls
test1 test1~ test2 test3 test3.bz2 testFind testFind.tar.gz testLink
5、.tar.gz格式压缩与解压缩
tar -zcvf [压缩包目录] [源文件1 源文件2 ...]
打包为.tar格式,并压缩为.gz格式
-z: 压缩为.tar.gz格式
tar -zxvf [压缩包目录]
-x: 解压缩.tar.gz格式
tar -zxvf [压缩包目录] -C [解压目录]
解压到指定目录下
例
//打包压缩testFind文件夹为.tar.gz格式
arururururu@ubuntu:~/Desktop/linuxCommand$ ls
test1 test1~ test2 test3 test3.bz2 testFind testLink
arururururu@ubuntu:~/Desktop/linuxCommand$ tar -zcvf testFind.tar.gz testFind
testFind/
testFind/abcd
testFind/Abc
testFind/abd
testFind/abc
testFind/efg
testFind/root
testFind/testLink/
testFind/testLink/test-hard
testFind/testLink/test1-so
testFind/testLink/test1~
testFind/testLink/today
testFind/testLink/test.s
testFind/testLink/test1
arururururu@ubuntu:~/Desktop/linuxCommand$ ls
test1 test1~ test2 test3 test3.bz2 testFind testFind.tar.gz testLink
//然后解压
arururururu@ubuntu:~/Desktop/linuxCommand$ rm -r testFind
arururururu@ubuntu:~/Desktop/linuxCommand$ tar -zxvf testFind.tar.gz
testFind/
testFind/abcd
testFind/Abc
testFind/abd
testFind/abc
testFind/efg
testFind/root
testFind/testLink/
testFind/testLink/test-hard
testFind/testLink/test1-so
testFind/testLink/test1~
testFind/testLink/today
testFind/testLink/test.s
testFind/testLink/test1
arururururu@ubuntu:~/Desktop/linuxCommand$ ls
test1 test1~ test2 test3 test3.bz2 testFind testFind.tar.gz testLink
//压缩多个文件
arururururu@ubuntu:~/Desktop/linuxCommand$ tar -zcvf test.tar.gz test1 test1~
test1
test1~
arururururu@ubuntu:~/Desktop/linuxCommand$ ls
test1 test2 test3.bz2 testFind.tar.bz test.tar.gz
test1~ test3 testFind testLink
//解压到指定目录
arururururu@ubuntu:~/Desktop/linuxCommand$ tar -zxvf test.tar.gz -C testLink
test1
test1~
arururururu@ubuntu:~/Desktop/linuxCommand$ ls testLink
test1 test1~ test1-so test-hard
6、.tar.bz2格式压缩与解压缩
tar -jcvf [压缩包目录] [源文件1 源文件2 ...]
打包为tar格式,并压缩为.bz2格式
-z: 压缩为.tar.bz2格式
tar -jxvf [压缩包目录]
-x: 解压缩.tar.bz2格式
例
//打包压缩为.tar.bz格式文件
arururururu@ubuntu:~/Desktop/linuxCommand$ tar -jcvf testFind.tar.bz testFind
testFind/
testFind/abcd
testFind/Abc
testFind/abd
testFind/abc
testFind/efg
testFind/root
testFind/testLink/
testFind/testLink/test-hard
testFind/testLink/test1-so
testFind/testLink/test1~
testFind/testLink/today
testFind/testLink/test.s
testFind/testLink/test1
arururururu@ubuntu:~/Desktop/linuxCommand$ ls
test1 test1~ test2 test3 test3.bz2 testFind testFind.tar.bz testLink
//然后解压
arururururu@ubuntu:~/Desktop/linuxCommand$ rm -r testFind
arururururu@ubuntu:~/Desktop/linuxCommand$ tar -jxvf testFind.tar.bz
testFind/
testFind/abcd
testFind/Abc
testFind/abd
testFind/abc
testFind/efg
testFind/root
testFind/testLink/
testFind/testLink/test-hard
testFind/testLink/test1-so
testFind/testLink/test1~
testFind/testLink/today
testFind/testLink/test.s
testFind/testLink/test1
arururururu@ubuntu:~/Desktop/linuxCommand$ ls
test1 test1~ test2 test3 test3.bz2 testFind testFind.tar.bz testLink