每天2个Linux命令 cd mkdir


20170714 每天2个Linux命令 cd命令

cd命令用来切换工作目录至dirname。 其中dirName表示法可为绝对路径或相对路径。若目录名称省略,则变换至使用者的home directory(也就是刚login时所在的目录)。另外,~也表示为home directory的意思,.则是表示目前所在的目录,..则表示目前目录位置的上一层目录。


1.用法:

cd (选项) [目录]

2.功能:

切换当前目录至dirName

3.选项:

(1) -p    如果要切换到的目标目录是一个符号连接,直接切换到符号连接指向的目标目录

(2) -L    如果要切换的目标目录是一个符号的连接,直接切换到字符连接名代表的目录,而非符号连接所指向的目标目录。

(3) -     当仅实用"-"一个选项时,当前工作目录将被切换到环境变量"OLDPWD"所表示的目录。

4.实例:

(1) cd 进入用户主目录;cd ~ 进入用户主目录;

[sunjimeng@localhost ~]$ cd /        /*刚开启terminal终端,是用户的主目录*/   /* “cd /” 进入根目录*/
[sunjimeng@localhost /]$ cd          /*cd命令默认进入用户的主目录*/
[sunjimeng@localhost ~]$             /*默认的主目录是这样的“~”*/
[sunjimeng@localhost ~]$ cd /        /*"cd"命令和"cd ~"可以达到一样的功能*/
[sunjimeng@localhost /]$ cd ~
[sunjimeng@localhost ~]$ 

(2) cd - 返回进入此目录之前所在的目录;

复制代码
[sunjimeng@localhost /]$ cd /home/sunjimeng                  /*由根目录进入用户目录*/
[sunjimeng@localhost ~]$ ll                                  //列出用户目录下的目录及文件
total 0
drwxr-xr-x. 2 sunjimeng sunjimeng 6 May  1 01:23 Desktop
drwxr-xr-x. 2 sunjimeng sunjimeng 6 May  1 01:23 Documents
drwxr-xr-x. 2 sunjimeng sunjimeng 6 May  1 01:23 Downloads
drwxr-xr-x. 2 sunjimeng sunjimeng 6 May  1 01:23 Music
drwxr-xr-x. 2 sunjimeng sunjimeng 6 May  1 01:23 Pictures
drwxr-xr-x. 2 sunjimeng sunjimeng 6 May  1 01:23 Public
drwxr-xr-x. 2 sunjimeng sunjimeng 6 May  1 01:23 Templates
drwxr-xr-x. 2 sunjimeng sunjimeng 6 May  1 01:23 Videos
[sunjimeng@localhost ~]$ cd Desktop       //选择进入Desktop文件夹
[sunjimeng@localhost Desktop]$ cd -      //"cd -"命令功能:(1)输出之前的目录名称;(2)返回之前的目录。
/home/sunjimeng 

(3) cd .. 返回上级目录(若当前目录为“/“,则执行完后还在“/“;”..”为上级目录的意思);cd ../.. 返回上两级目录;

[sunjimeng@localhost /]$ cd ..                       //在根目录下返回上一级目录,依然是根目录
[sunjimeng@localhost /]$ cd /home/sunjimeng/Desktop  //进入自定义文件夹
[sunjimeng@localhost Desktop]$ cd ..                 //返回上一级 /home/sunjimeng,即用户主目录
[sunjimeng@localhost ~]$ cd ../..                    //返回上两级目录
[sunjimeng@localhost /]$

(4) cd !$ 把上个命令的参数作为cd参数使用。

[sunjimeng@localhost ~]$ cd /home/sunjimeng/Desktop  //进入用户主目录的桌面文件夹
[sunjimeng@localhost Desktop]$ cd !$     //cd !$功能:(1)打印这个命令的解释;(2)执行这个命令
cd /home/sunjimeng/Desktop
[sunjimeng@localhost Desktop]$ 
[sunjimeng@localhost Desktop]$ cd ..
[sunjimeng@localhost ~]$ cd !$
cd ..
[sunjimeng@localhost home]$

20170714 每天2个Linux命令 mkdir命令命令

mkdir命令用来创建目录。


(1)用法:

用法: mkdir [选项]... 目录...

(2)功能:

功能: 若指定目录不存在则创建目录

该命令创建由dirname命名的目录。如果在目录名的前面没有加任何路径名,则在当前目录下创建由dirname指定的目录;<br/>
如果给出了一个已经存在的路径,将会在该目录下创建一个指定的目录。
在创建目录时,应保证新建的目录与它所在目录下的文件没有重名。

(3)选项参数

   1)-Z:      设置安全上下文,当使用SELinux时有效

   2) -m:<目标属性>或-mode<目标属性>    建立目录的同时设置目录的权限

   3) -p或--parents   若所要建立目录的上层目录目前尚未建立,则会一并建立上层目录

   4) -version       显示版本信息

(4)实例

1)[sunjimeng@localhost Documents]$ mkdir mainDir 在当前文件夹下创建一个新文件

[sunjimeng@localhost Documents]$ ll            //列出当前目录下的文件及文件夹
total 0
[sunjimeng@localhost Documents]$ mkdir mainDir //新建一个文件
[sunjimeng@localhost Documents]$ ll            //查看
total 0
drwxrwxr-x. 2 sunjimeng sunjimeng 6 May  1 02:49 mainDir

2)[sunjimeng@localhost Documents]$ mkdir -v secondDir 在当期那文件夹下下创建一个新文件,并输出提示信息

[sunjimeng@localhost Documents]$ mkdir -v secondDir
mkdir: created directory ‘secondDir’
[sunjimeng@localhost Documents]$ 

3)[sunjimeng@localhost Documents]$ mkdir -p thirdDir/{test1,test2,test3}
在当前文件夹下创建一个新文夹,而且包含多个子文件夹复制代码

[sunjimeng@localhost Documents]$ mkdir -p thirdDir/{test1,test2,test3}  //新建包含多个子文件夹的文件夹
[sunjimeng@localhost Documents]$ ll   //查看当前工作目录下有的文件及文件夹,以详细信息输出
total 0
drwxrwxr-x. 2 sunjimeng sunjimeng  6 May  1 02:49 mainDir
drwxrwxr-x. 2 sunjimeng sunjimeng  6 May  1 02:52 secondDir
drwxrwxr-x. 5 sunjimeng sunjimeng 42 May  1 02:57 thirdDir
[sunjimeng@localhost Documents]$ cd thirdDir  //进入目录
[sunjimeng@localhost thirdDir]$ ll    //查看目录下的子目录
total 0
drwxrwxr-x. 2 sunjimeng sunjimeng 6 May  1 02:57 test1
drwxrwxr-x. 2 sunjimeng sunjimeng 6 May  1 02:57 test2
drwxrwxr-x. 2 sunjimeng sunjimeng 6 May  1 02:57 test3
[sunjimeng@localhost thirdDir]$ 
[sunjimeng@localhost Documents]$ ll                                 //查看Document文件夹下的文件及文件夹
total 0
drwxrwxr-x. 2 sunjimeng sunjimeng  6 May  1 02:49 mainDir
drwxrwxr-x. 2 sunjimeng sunjimeng  6 May  1 02:52 secondDir
drwxrwxr-x. 5 sunjimeng sunjimeng 42 May  1 03:01 thirdDir
[sunjimeng@localhost Documents]$ mkdir thirdDir/{test4,test5,test6}//虽然已经存在了thirdDir文件夹,但丝毫不影响这个操作
[sunjimeng@localhost Documents]$ ll                      
total 0
drwxrwxr-x. 2 sunjimeng sunjimeng  6 May  1 02:49 mainDir
drwxrwxr-x. 2 sunjimeng sunjimeng  6 May  1 02:52 secondDir
drwxrwxr-x. 8 sunjimeng sunjimeng 78 May  1 03:01 thirdDir
[sunjimeng@localhost Documents]$ cd thirdDir
[sunjimeng@localhost thirdDir]$ ll                                //因为新建的重名文件夹下的子文件夹集将可以添加到已有重名文件夹下
total 0
drwxrwxr-x. 2 sunjimeng sunjimeng 6 May  1 02:57 test1
drwxrwxr-x. 2 sunjimeng sunjimeng 6 May  1 02:57 test2
drwxrwxr-x. 2 sunjimeng sunjimeng 6 May  1 02:57 test3
drwxrwxr-x. 2 sunjimeng sunjimeng 6 May  1 03:01 test4
drwxrwxr-x. 2 sunjimeng sunjimeng 6 May  1 03:01 test5
drwxrwxr-x. 2 sunjimeng sunjimeng 6 May  1 03:01 test6

4)[sunjimeng@localhost Documents]$ mkdir -m 700 /home/sunjimeng/Document 在指定路径下创建文件夹,并且只有文件主有读、写和执行权限,其他人无权问。

复制代码
[sunjimeng@localhost Documents]$ mkdir -m 700 /home/sunjimeng/Document
[sunjimeng@localhost Documents]$ cd /home/sunjimeng
[sunjimeng@localhost ~]$ ll
total 0
drwxr-xr-x. 2 sunjimeng sunjimeng  6 May  1 01:23 Desktop
drwx------. 2 sunjimeng sunjimeng  6 May  1 03:07 Document     //这一项即为所新建的文件夹
drwxr-xr-x. 5 sunjimeng sunjimeng 51 May  1 02:57 Documents
drwxr-xr-x. 2 sunjimeng sunjimeng  6 May  1 01:23 Downloads
drwxr-xr-x. 2 sunjimeng sunjimeng  6 May  1 01:23 Music
drwxr-xr-x. 2 sunjimeng sunjimeng  6 May  1 01:23 Pictures
drwxr-xr-x. 2 sunjimeng sunjimeng  6 May  1 01:23 Public
drwxr-xr-x. 2 sunjimeng sunjimeng  6 May  1 01:23 Templates
drwxr-xr-x. 2 sunjimeng sunjimeng  6 May  1 01:23 Videos
复制代码

5)[sunjimeng@localhost Document]$ mkdir -pm 750 bin/os_1 在当前目录中建立bin和bin下的os_1目录,权限设置为文件主可读、写、执行,同组用户可读和执行,其他用户无权访问

复制代码
[sunjimeng@localhost Document]$ mkdir -pm 750 bin/os_1
[sunjimeng@localhost Document]$ ll
total 0
drwxrwxr-x. 3 sunjimeng sunjimeng 17 May  1 03:13 bin
[sunjimeng@localhost Document]$ cd bin
[sunjimeng@localhost bin]$ ll
total 0
drwxr-x---. 2 sunjimeng sunjimeng 6 May  1 03:13 os_1
[sunjimeng@localhost bin]$  

6)[sunjimeng@localhost Document]$ mkdir –version 显示mkdir的版本信息

复制代码
[sunjimeng@localhost Document]$ mkdir --version
mkdir (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by David MacKenzie.

7)[sunjimeng@localhost Document]$ mkdir –parents Father/Child 与 mkdir -p Father/Child的效果是一样的 同理 -m等同于–mood

复制代码
[sunjimeng@localhost Document]$ mkdir --parents Father/Child
[sunjimeng@localhost Document]$ ll
total 0
drwxrwxr-x. 3 sunjimeng sunjimeng 17 May  1 03:13 bin
drwxrwxr-x. 3 sunjimeng sunjimeng 18 May  1 03:21 Father
[sunjimeng@localhost Document]$ cd Father
[sunjimeng@localhost Father]$ ll
total 0
drwxrwxr-x. 2 sunjimeng sunjimeng 6 May  1 03:21 Child
[sunjimeng@localhost Father]$ 

文章作者: 邓滔
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 邓滔 !
评论
  目录