linux刚开始问世时没有图形界面,所有操作全靠命令完成服务器维护都通过远程SSH客服端来完成。在职场中,后端程序员,必须掌握一些linux常用的命令。linux发行版的命令大约有200多个,但是常用的只有10多个技巧:常用命令用多了就记住了。
1 常用的linux基本命令
2 linux终端命令的格式
2.1 command [-options] [parameter]
command:命令名;
[-options] :选项,用来对命令进行控制,也可以省略 [ ]表示可选例如rm要删除目录需要额外设置。例如rm命令在删除文件时直接使用rm 123.txt就可以将123.txt这个文件删除,如果要删除目录的时候就需要在rm后面加上-r ,这里的-r就是命令格式中的[-options]了,如:rm -r aaa就能将aaa目录删除。
[parameter]:传给命令的参数,可以是零个、一个或多个
cd 目录名称(目录名称就是参数)
touch 文件名(文件名就是参数)
rm 文件名(文件名就是参数)
2.2 终端命令帮助命令
(1)--help
使用 command --help可以得到系统对command的提示信息。比如使用mkdir --help时:
mkdir --help
Usage: mkdir [OPTION]... DIRECTORY...
Create the DIRECTORY(ies), if they do not already exist.
Mandatory arguments to long options are mandatory for short options too.
-m, --mode=MODE set file mode (as in chmod), not a=rwx - umask
-p, --parents no error if existing, make parent directories as needed
-v, --verbose print a message for each created directory
-Z set SELinux security context of each created directory
to the default type
--context[=CTX] like -Z, or if CTX is specified then set the SELinux
or SMACK security context to CTX
--help display this help and exit
--version output version information and exit
(2)man
这里的man是manue的缩写,使用command man时可以显示命令的使用手册,比如使用man mkdir:
man mkdir
MKDIR(1) User Commands MKDIR(1)
NAME
mkdir - make directories
SYNOPSIS
mkdir [OPTION]... DIRECTORY… #这里的directory没有中括号,表示必须要的参数
DESCRIPTION
Create the DIRECTORY(ies), if
they do not already exist.
Mandatory arguments to long op‐
tions are mandatory for short op‐
tions too.
-m, --mode=MODE
set file mode (as in
chmod), not a=rwx - umask
-p, --parents
no error if existing, make
parent directories as
needed
Manual page mkdir(1) line 1/78 30% (press h for help or q to quit)
使用man时显示的信息比较多,可以通过下面的操作方法来查看这些信息:
3 文件和目录常用命令
3.1 cd命令
(1)绝对路径和相对路径
绝对路径:在输入路径时,最前面是/或者~,表示从根目录或者家目录开始的具体目录位置,无论我们工作目录在哪里都可以通过绝度路径切换到目标路径,比如当前目录是Desktop,要想转换到Documents这个目录下,那么就必须使用绝对路径,因为Documents并没有在Desktop下面,Docuents是在/home/lxy下面,要从Desktop转到Documents有两种方式,第一种方式:
lxy@ubuntu:~/Desktop$ cd /home/lxy/Documents
lxy@ubuntu:~/Documents$
第二种方式:
lxy@ubuntu:~/Desktop$ cd ~/Documents
lxy@ubuntu:~/Documents$
上面的~ 相当于就是第一种方式中的/home/lxy
相对路径:在输入路径时,最前面不是/或者~,表示从当前目录开始的具体目录位置,相对于当前目录而言的路径,当前目录发生变化了那么相对路径就会发生变化。比如当前目录是Desktop,在Desktop下面有一个“Python学习”的目录,如果要转换到这个目录下,那么直接输入cd Python学习 就可以。
lxy@ubuntu:~/Desktop$ cd Python学习
lxy@ubuntu:~/Desktop/Python学习$