您的位置:首页 > 运维架构 > Linux

Linux——简单介绍

2016-05-04 15:32 387 查看

Introduction

许多人通过一个图形用户界面(graphical user interface)与计算机交互。GUI如下:



在GUI之前,人们是通过命令行界面(command-line interface)与计算机进行交互的,它也可以称为一个shell或终端。对于一个编程任务,命令行界面比GUI更快更强大。 几乎所有的程序员和数据科学家都广泛的使用终端,并且认为能与它进行交互是一个至关重要的技能。每个系统的终端都稍微有点不同,它们使用不同的命令和语法。比如linux和OSX,因为他们都是基于unix操作系统,所以他们的终端非常相似。但是Windows操作系统与上面两个的终端就截然不同,因为命令都是不同的。由于使用linux较为广泛,因此以下的任务都是运行在Ubuntu 14.04上。



Filesystem

~$ pwd
/home/dq


当我们在终端中输入pwd时,它提示我们现在所在的文件夹。终端能够在目录之间进行切换。它与图形界面不同的是图形界面是点击文件夹,而命令行界面是输入一个命令。当学会使用终端后,在终端中进行目录导航将比图形界面快很多。

我们把根目录描述为斜杠/,home目录是在根目录下的,而dq目录是home目录的子目录。

当输入cd / 表示进入到根目录

~$ cd /
~$


Absolute Vs Relative Paths

当输入/时这是切换到根目录,任何路径以/开头都是绝对路径。绝对路径是与文件的根目录相关的。无论你现在在哪个目录,当你输入cd /home/dq都将进入到dq目录。

另一方面,相对路径是相对你现在所处的目录,不能以 / 开头。如果你现在在home目录下,你输入cd dq将切换到 /home/dq目录下。如果你在 / 根目录下输入cd dq,此时会报错,因为在当前目录下不存在dq这个目录。

/$ cd home
/home$


此时利用相对路径进入到home目录中,由于home是在根目录下,因此home文件也叫根文件夹。

Getting Back To The Dq Folder

现在我们处在home文件夹中,我们需要进入到dq中,由于dq就在home文件夹下,采用相对路径即可:

/home$ cd dq
~$


Users

现在我们以用户dq的身份进入到这个习题中,我们可以通过whoami命令来查询我是谁:

~$ whoami
dq
~$


The Home Directory

每个用户都有一个home目录,而且每一个home目录都在/home这个目录下。比如现在我是dq用户,那么我的home目录就是/home/dq.输入cd ~将自动切换到当前用户的home目录:

~$ cd ~
~$


Making A Directory

可以通过mkdir命令来新建一个目录。由于有相对路径和绝对路径的区别,因此如果你输入mkdir test,那么这个test目录就在当前目录下,因为输入的是相对路径。如果你输入mkdir /home/dq/test,这是绝对路径,因为开头是根目录 / ,因此test就在/home/dq目录下。

~$ mkdir test
~$


Command Options

我们可以通过一个破折号 - 加入一些参数来修改命令的行为,比如在mkdir后面添加 -v表示“冗长”模式,并打印输出新建的文件夹。

~$ mkdir -v test2
mkdir: created directory ‘test2’
~$


Checking Possible Options

大部分命令都提供了–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 nee
ded
-v, --verbose     print a message for each created directory
-Z, --context=CTX  set the SELinux security context of each created
directory to CTX
--help     display this help and exit
--version  output version information and exit

Report mkdir bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils 'mkdir invocation'
~$


Listing The Contents Of A Directory

到目前为止,在我的主目录下创建了两个文件夹,我们可以通过ls -l来查看dq目录下的文件:

~$ ls -l
total 8
drwxr-xr-x 2 dq dq 4096 May  4 07:21 test
drwxr-xr-x 2 dq dq 4096 May  4 07:21 test2
~$


Removing A Directory

rmdir可以用来删除一个文件夹

~$ rmdir test2
~$ rmdir --help
~$ rmdir --help
Usage: rmdir [OPTION]... DIRECTORY...
Remove the DIRECTORY(ies), if they are empty.

--ignore-fail-on-non-empty
ignore each failure that is solely because a directory
is non-empty
-p, --parents   remove DIRECTORY and its ancestors; e.g., 'rmdir -p a/
b/c' is
similar to 'rmdir a/b/c a/b a'
-v, --verbose   output a diagnostic for every directory processed
--help     display this help and exit
--version  output version information and exit

Report rmdir bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils 'rmdir invocation'
~$
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: