Linux的常见命令

Linux的常见命令

1.创建和删除目录

创建目录(文件夹)是命令 “mkdir” 语法如下

[root@rh1 ~]# mkdir dir

[root@rh1 ~]# mkdir -p dir1/dir2

这里的意思是在"dir1"下创建"dir2",-p的意思是,如果"dir1"不存在,则会把"dir1"也创建出来

删除目录的命令是 “rmdir”,语法如下

[root@rh1 ~]# rmdir dir

如果目录 “dir1"中还有"dir2”,执行命令删除"dir1"

[root@rh1 ~]# rmdir dir1
rmdir: 删除 'dir1' 失败: 目录非空

因为目录"dir1"中还有目录"dir2",所以rmdir无法直接删除目录"dir1",需要把目录中的内容全部清除才可以删除。这里可以利用后面讲的"rm -rf"命令来删除

[root@rh1 ~]# rm -rf dir1

这里的 -r 选项的意思是递归,如同剥洋葱,一层一层地剥;-f 选项的意思是强制。

2.cd的用法

cd的主要作用是切换到其他目录,用法如下

cd 路径

这里的路径可以是相对路径也可以是绝对路径,如果没有明确地指定路径,则是当前了路径,如

[root@rh1 ~]# cd test
[root@rh1 test]# 

创建一个测试用的目录

[root@rh1 /]# mkdir -p /11/22/33/44/55/66/77/88

查看这个目录的结构

[root@rh1 /]# tree 11
11
└── 22
    └── 33
        └── 44
            └── 55
                └── 66
                    └── 77
                        └── 88

7 directories, 0 files

进入"88",命令如下

[root@rh1 /]# cd /11/22/33/44/55/66/77/88
[root@rh1 88]#

不管在那个目录下,只要输入"cd",就可以切换到家目录

[root@rh1 88]# cd
[root@rh1 ~]#

直接输入cd命令,等用于输入cd ~命令,这里的 ~ 是一个变量,表示当前用户的家目录。
也可以用~user表示user用户的家目录。

3.拷贝和复制

如果想要拷贝文件或目录,可以使用命令"cp",语法如下

cp 选项 /path1/xx /path2/yy

如果"/path2/yy"是一个目录,意思把"/path1/xx"拷贝到"/path2/yy"中。如果"/path2/yy"不存在或是一个文件,意思是把"/path1/xx"拷贝到"/path2"中,命名为"yy" 。

把"/etc/hosts"拷贝到"/opt"目录中。

[root@rh1 ~]# cp /etc/hosts /opt/

这里/opt是一个目录,那么这句话的意思是把/etc/hosts拷贝到/opt目录中。查看一下/opt中的内容。

[root@rh1 ~]# ls /opt
hosts

把"/etc/hosts"拷贝到/opt中,命令为xx。

[root@rh1 ~]# cp /etc/hosts /opt/xx
[root@rh1 ~]# ls /opt
hosts  xx

原来并不存在"/opt/xx",上面的操作是把"/etc/hosts"拷贝到"/opt"中,命名为xx。看下面的例子

先创建目录/opt/11,因为/opt/11是一个目录,所以这里把/etc/hosts拷贝到/opt/11中,而不是把/etc/hosts拷贝到/opt之后命名为11。

[root@rh1 ~]# mkdir /opt/11
[root@rh1 ~]# cp /etc/hosts /opt/11
[root@rh1 ~]# ls /opt
11  hosts  xx
[root@rh1 ~]# cp /etc/hosts /opt/xx
cp:是否覆盖'/opt/xx'? y

因为"/opt/xx"不是一个目录,所以这句话的意思是把"/etc/hosts"拷贝到"/opt"目录并命名为xx。因为"/opt/xx"已经存在了,所以会问是否覆盖,如果此时直接按【Enter】键,则是n的意思,即不覆盖。如果要覆盖必须输入“y”,按【Enter】键。
拷贝一个文件,相当于新创建了一个文件。除文件内容相同外,文件的时间显示的也是创建这个文件的时间。

[root@rh1 ~]# ls -l /etc/hosts /opt/xx
-rw-r--r--. 1 root root 158 9月  10 2018 /etc/hosts
-rw-r--r--. 1 root root 158 11月 30 11:19 /opt/xx

可以看到,时间不一样。

拷贝一个文件时,如果想把文件的属性一起拷贝过去,就需要加上-p选项。

[root@rh1 ~]# cp -p /etc/hosts /opt/xx
cp:是否覆盖'/opt/xx'? y
[root@rh1 ~]# ls -l /etc/hosts /opt/xx
-rw-r--r--. 1 root root 158 9月  10 2018 /etc/hosts
-rw-r--r--. 1 root root 158 9月  10 2018 /opt/xx

这样看起来,时间也一致了。

下面是是拷贝目录的用法,需要加上"-r"选项才行,"-r"表示递归的意思

[root@rh1 ~]# cp -r /etc ~

删除这个目录

[root@rh1 ~]# rm -rf etc/

记住,不要写成"rm -rf /etc/"了

如果拷贝目录,同时想保持目录属性不变,可以使用 -rp 选项,或者 -a 选项。-a 选项中包括一系列的其他选项如-r,-p选项。

[root@rh1 ~]# cp -a /etc/ ~

剪切所用的命令是"mv",语法如下。

mv 选项 /path1/xx /path2/yy

把"/opt/hosts"剪切到当前目录中,命令如下

[root@rh1 ~]# mv /opt/hosts ~
[root@rh1 ~]# ls /opt/
11  xx
[root@rh1 ~]

"mv"命令也可以重命名操作,如把/opt/下的xx重命名为yy。

[root@rh1 ~]# mv /opt/xx /opt/yy
[root@rh1 ~]# ls /opt/
11  yy
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值