linux实操总结——用户管理、实用指令总结

1.开机、重启和用户登录注销

1.1 关机和重启

  • shutdown –h now 立该进行关机
  • shudown -h 1 1 分钟后会关机了
  • shutdown –r now 立即重启
  • shutdown –r 1 1分钟后重启
  • halt 立即关机
  • reboot 立即重启
  • sync 把内存的数据同步到磁盘

不管是重启系统还是关闭系统,首先要运行sync命令,把内存中的数据写到磁盘中,防止数据丢失。

1.2 登录和注销

  • 登录时尽量少用root帐号登录,因为它是系统管理员,最大的权限,避免操作失误。可 以利用普通用户登录,登录后再用”su -用户名’命令来切换成系统管理员身份。
  • 在提示符下输入 logout 即可注销用户,注意只在远程连接使用有效。

2.用户管理

Linux系统是一个多用户多任务的操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系统。
在这里插入图片描述
注意:每个用户可以属于多个组。

2.1 添加用户

[root@hadoop ~]# useradd xm

在这里插入图片描述

  1. 当创建用户成功后,会自动的创建和用户同名的家目录;
  2. 也可以通过制定目录创建用户指定的家目录。
[root@hadoop ~]# useradd -d /home/dog xq

在这里插入图片描述

2.2 指定或修改用户密码

# xm指定(修改)密码,由于密码简单所以下面提示无效,我们这里强制执行了000000
[root@hadoop ~]# passwd xm

在这里插入图片描述

2.3 删除用户

2.3.1 删除xm用户,但保留家目录

在这里插入图片描述

2.3.2 删除xq用户以及家目录

在这里插入图片描述

2.4 查询用户信息

# uid用户id,gid组id,组:组名
[root@hadoop ~]# id root
uid=0(root) gid=0(root) 组=0(root)
[root@hadoop ~]# id xm
id: xm:无此用户
[root@hadoop ~]# whoami
root

2.5 用户组

类似于角色,系统可以对有共性的多个用户进行统一的管理。

2.5.1 创建和删除组

#创建组和删除组
[root@hadoop ~]# groupadd wudang
[root@hadoop ~]# groupdel wudang

2.5.2 创建用户的时候直接指定组

增加一个用户 zwj, 直接将他指定到 wudang

[root@hadoop ~]# groupadd wudang
[root@hadoop ~]# useradd -g wudang zwj
[root@hadoop ~]# id zwj
uid=500(zwj) gid=500(wudang) 组=500(wudang)

在这里插入图片描述

2.5.3 修改用户组

创建一个shaolin组,将zwj修改到shaolin

[root@hadoop ~]# groupadd shaolin
[root@hadoop ~]# usermod -g shaolin zwj
[root@hadoop ~]# id zwj
uid=500(zwj) gid=501(shaolin) 组=501(shaolin)

在这里插入图片描述

3.实用指令

3.1 运行级别说明

0 :关机
1 :单用户【找回丢失密码】
2:多用户状态没有网络服务
3:多用户状态有网络服务
4:系统未使用保留给用户
5:图形界面
6:系统重启
常用运行级别是3和5 ,要修改默认的运行级别可改文件
/etc/inittab的id:5:initdefault:这一行中的数字
命令:init [012356]

在这里插入图片描述
通过init 来切换不同的运行级别,比如从 5切换到3 , 然后关机。

# 默认是级别5,图形页面
[root@hadoop 桌面]# vim /etc/inittab

在这里插入图片描述

init 3
init 5
init 0

如果想开机启动就是3,那么需要修改 vim /etc/inittab最后id为3。那么重启后就是级别3而不是图形页面5.

假设我们的root密码忘记了,请问如何找回密码。
思路:进入到单用户模式,然后修改root密码。因为进入单用户模式,root 不需要密码就可以登录。(前提是服务器上操作,不能远程)

重启–输入enter进入下面界面
在这里插入图片描述
输入e进入下面-选择kernel内核–输入e进入下面页面
在这里插入图片描述
在这里插入图片描述
空格输入1–进入单用户模式如下图–输入b
在这里插入图片描述
进入单用户模式
![在这里插入图片描述](https://img-blog.csdnimg.cn/c4cb558bfb6f40f9b00d6a5f7893ce23.png
修改密码如2.2所示
在这里插入图片描述

3.2 帮助指令

3.2.1 man 获得帮助信息

[root@hadoop ~]# man ls

3.2.2 help获取帮助信息

[root@hadoop ~]# help cd

3.3 文件目录命令

3.3.1 pwd指令

在这里插入图片描述

3.3.2 ls指令

ls [选项] [目录或是文件]
• 常用选项
-a :显示当前目录所有的文件和目录,包括隐藏的。
-l :以列表的方式显示信息
-al:隐藏加列表
在这里插入图片描述
在这里插入图片描述

3.3.3 mkdir指令

  • mkdir指令用于创建目录
  • -p :创建多级目录
[root@hadoop ~]# mkdir /home/dog

在这里插入图片描述

[root@hadoop ~]# mkdir -p  /home/animal/tigger

在这里插入图片描述

3.3.4 rmdir指令

  • rmdir指令删除空目录
  • 如果需要删除非空目录,需要使用 rm -rf 要删除的目录
[root@hadoop ~]# rmdir /home/dog
[root@hadoop ~]# ls /home
animal  xm  zwj

[root@hadoop ~]# rmdir /home/animal
rmdir: 删除 "/home/animal" 失败: 目录非空
[root@hadoop ~]# rm -rf /home/animal
[root@hadoop ~]# cd /home
[root@hadoop home]# ls
xm  zwj

3.3.5 touch指令

touch指令创建空文件
在这里插入图片描述

3.3.6 cp指令

  • cp 指令拷贝文件到指定目录
  • -r :递归复制整个文件夹
  • 强制覆盖不提示的方法:\cp

案例1: 将 /home/aaa.txt 拷贝到 /home/bbb 目录下

[root@hadoop ~]# touch /home/aaa.txt
[root@hadoop ~]# ls /home
aaa.txt  xm  zwj
[root@hadoop ~]# mkdir /home/bbb
[root@hadoop ~]# ls /home
aaa.txt  bbb  xm  zwj
[root@hadoop ~]# cp /home/aaa.txt /home/bbb/
[root@hadoop ~]# ls /home/bbb
aaa.txt

案例2: 递归复制整个文件夹,将/home/test 整个目录拷贝到 /home/zwj目录

[root@hadoop ~]# mkdir /home/test
[root@hadoop ~]# cp -r /home/test/ /home/zwj/
[root@hadoop ~]# ls /home/zwj
test

案例3:强制覆盖不提示的方法\cp

[root@hadoop ~]# ls /home/test/
[root@hadoop ~]# touch /home/test/a.txt /home/test/b.txt
[root@hadoop ~]# ls /home/test/
a.txt  b.txt
[root@hadoop ~]# \cp -r /home/test/ /home/zwj/
[root@hadoop ~]# cp -r /home/test/ /home/zwj/
cp:是否覆盖"/home/zwj/test/b.txt"? y
cp:是否覆盖"/home/zwj/test/a.txt"? y
[root@hadoop ~]# \cp -r /home/test/ /home/zwj/
[root@hadoop ~]# ls /home/zwj/test/
a.txt  b.txt

3.3.7 rm指令

  • rm 指令删除文件或目录
  • -r :递归删除整个文件夹
  • -f : 强制删除不提示

案例1: 将 /home/aaa.txt 删除

[root@hadoop ~]# rm /home/aaa.txt
rm:是否删除普通空文件 "/home/aaa.txt"?y
[root@hadoop ~]# touch /home/aaa.txt
[root@hadoop ~]# rm -f /home/aaa.txt
[root@hadoop ~]# ls /home
bbb  test  xm  zwj

案例2: 递归删除整个文件夹 /home/bbb

[root@hadoop ~]# rm /home/bbb
rm: 无法删除"/home/bbb": 是一个目录
[root@hadoop ~]# rm -r /home/bbb
rm:是否进入目录"/home/bbb"? no
[root@hadoop ~]# rm -rf /home/bbb
[root@hadoop ~]# ls /home/
test  xm  zwj

3.3.8 mv指令

  • mv 移动或重命名文件、目录
  • 同一地址下移动就是重命名
  • 不同地址下功能为移动

案例1: 将 /home/aaa.txt 文件 重新命名为 pig.txt

[root@hadoop home]# ls
aaa.txt  test  xm  zwj
[root@hadoop home]# mv aaa.txt pig.txt
[root@hadoop home]# ls
pig.txt  test  xm  zwj

案例2:将 /home/pig.txt 文件 移动到 /home/test 目录下

# 注意test需要写全路径才有效
[root@hadoop home]# mv pig.txt /home/test/
[root@hadoop home]# ls /home/test/
a.txt  b.txt  pig.txt

3.3.9 cat指令

  • cat 查看文件内容
  • -n :显示行号
  • cat 只能浏览文件,而不能修改文件,为了浏览方便,一般会带上管道命令 | more

案例1: /ect/profile 文件内容,并显示行号
在这里插入图片描述
但是会直接退出,因此用管道命令增加分页浏览

# 空格翻页
[root@hadoop etc]# cat -n profile | more

在这里插入图片描述

3.3.10 more指令

more指令是一个基于VI编辑器的文本过滤器,它以全屏幕的方式按页显示文本文件的内容。more指令中内置了若干快捷键,详见操作说明
在这里插入图片描述
案例: 采用more查看文件/etc/profile

[root@hadoop etc]# more profile

3.3.11 less指令

  • less查看日志文件
  • less指令用来分屏查看文件内容,它的功能与more指令类似,但是比more指令更加强大,支持各种显示终端。less指令在显示文件内容时,并不是一次将整个文件加载之后才显示,而是根据显示需要加载内容,对于显示大型文件具有较高的效率。
    在这里插入图片描述
    采用less查看一个大文件文件/pig.txt
[root@hadoop ~]# less /home/pig.txt 

3.3.12 > 指令 和 >> 指令

  • 输出重定向:>
  • 追加: >>
  • ls -l >文件名称 (功能描述:列表的内容写入文件a.txt中(覆盖写,先清空再写入))
[root@hadoop home]# ls -l
总用量 16
-rw-r--r--. 1 root root    213 2月  16 23:52 a.txt
drwxr-xr-x. 2 root root   4096 2月  16 23:11 test
drwx------. 4 zwj  wudang 4096 2月  15 18:07 xm
drwx------. 5 zwj  wudang 4096 2月  16 22:30 zwj
[root@hadoop home]# ls -l  /home/>  /home/a.txt  # 当前路径操作可省略路径
[root@hadoop home]# more a.txt
总用量 12
-rw-r--r--. 1 root root      0 2月  17 21:29 a.txt
drwxr-xr-x. 2 root root   4096 2月  16 23:11 test
drwx------. 4 zwj  wudang 4096 2月  15 18:07 xm
drwx------. 5 zwj  wudang 4096 2月  16 22:30 zwj
[root@hadoop home]# ls -l > a.txt
[root@hadoop home]# more a.txt
总用量 12
-rw-r--r--. 1 root root      0 2月  17 21:29 a.txt
drwxr-xr-x. 2 root root   4096 2月  16 23:11 test
drwx------. 4 zwj  wudang 4096 2月  15 18:07 xm
drwx------. 5 zwj  wudang 4096 2月  16 22:30 zwj

注意:如果a.txt文件不存在就会创建该文件再写入。

  • ls -al >>文件 (功能描述:列表的内容(包括隐藏)追加到文件aa.txt的末尾)
[root@hadoop home]# ls -al >> a.txt
[root@hadoop home]# more a.txt
总用量 12
-rw-r--r--. 1 root root      0 2月  17 21:29 a.txt
drwxr-xr-x. 2 root root   4096 2月  16 23:11 test
drwx------. 4 zwj  wudang 4096 2月  15 18:07 xm
drwx------. 5 zwj  wudang 4096 2月  16 22:30 zwj
总用量 24
drwxr-xr-x.  5 root root   4096 2月  17 21:28 .
dr-xr-xr-x. 25 root root   4096 2月  16 18:15 ..
-rw-r--r--.  1 root root    215 2月  17 21:29 a.txt
drwxr-xr-x.  2 root root   4096 2月  16 23:11 test
drwx------.  4 zwj  wudang 4096 2月  15 18:07 xm
drwx------.  5 zwj  wudang 4096 2月  16 22:30 zwj
  • cat 文件1 > 文件2 (功能描述:将文件1的内容覆盖到文件2)
[root@hadoop home]# cat /etc/profile > a.txt
[root@hadoop home]# more a.txt
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
.....
  • echo “内容”>> 文件

案例1: 将 /home 目录下的文件列表写入到 /home/info.txt 中
在这里插入图片描述
案例2: 将当前日历信息 追加到 /home/mycal 文件中

[root@hadoop home]# cal
      二月 2022     
日 一 二 三 四 五 六
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28

[root@hadoop home]# cal >> /home/mycal
[root@hadoop home]# more mycal
      二月 2022     
日 一 二 三 四 五 六
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28

3.3.13 echo指令

echo输出内容到控制台。

案例: 使用echo 指令输出环境变量

[root@hadoop home]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

案例: 使用echo 指令输出 hello,world

[root@hadoop home]# echo "hello,word"
hello,word

3.3.14 head指令

  • head用于显示文件的开头部分内容,默认情况下head指令显示文件的前10行内容
  • head 文件 (功能描述:查看文件头10行内容)
  • head -n 5 文件 (功能描述:查看文件头5行内容,5可以是任意行数)

案例: 查看/etc/profile 的前面5行代码

[root@hadoop home]# head -n 5 /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

3.3.15 tail指令

  • tail用于输出文件中尾部的内容,默认情况下tail指令显示文件的前10行内容。
  • tail 文件(功能描述:查看文件头10行内容)
  • tail -n 5 文件 (功能描述:查看文件头5行内容,5可以是任意行数)
  • tail -f 文件 (功能描述:实时追踪该文档的所有更新)

案例1: 查看/etc/profile 最后5行的代码

[root@hadoop home]# tail -n 5 /etc/profile
    fi
done

unset i
unset -f pathmunge

案例2: 实时监控 mydate.txt , 看看到文件有变化时,是否看到, 实时的追加日期

cal >> /home/mydata.txt
[root@hadoop home]# more mydata.txt
      二月 2022     
日 一 二 三 四 五 六
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28

[root@hadoop home]# tail -f mydata.txt
      二月 2022     
日 一 二 三 四 五 六
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28

总用量 24
-rw-r--r--. 1 root root   1796 2月  17 21:43 a.txt
-rw-r--r--. 1 root root    142 2月  17 21:46 mycal
-rw-r--r--. 1 root root    142 2月  17 22:19 mydata.txt
drwxr-xr-x. 2 root root   4096 2月  16 23:11 test
drwx------. 4 zwj  wudang 4096 2月  15 18:07 xm
drwx------. 5 zwj  wudang 4096 2月  16 22:30 zwj

在这里插入图片描述

3.3.16 ln指令

  • 软链接也成为符号链接,类似于windows里的快捷方式,主要存放了链接其他文件的路径
  • ln -s [原文件或目录] [软链接名] (功能描述:给原文件创建一个软链接)

案例1: 在/home 目录下创建一个软连接 linkToRoot,连接到 /root 目录

# 当我们使用pwd指令查看目录时,仍然看到的是软链接所在目录。
[root@hadoop home]# ln -s /root linktoroot
[root@hadoop home]# ls
a.txt  linktoroot  mycal  mydata.txt  test  xm  zwj
[root@hadoop home]# cd linktoroot/
[root@hadoop linktoroot]# pwd
/home/linktoroot

案例2: 删除软连接 linkToRoot

[root@hadoop home]# rm -rf linktoroot
[root@hadoop home]# ls
a.txt  mycal  mydata.txt  test  xm  zwj

3.3.17 history指令

  • 查看已经执行过历史命令,也可以执行历史指令

案例1: 显示所有的历史命令

[root@hadoop home]# history
    1  ifconfig
    2  hostnames
    3  hostname
    4  vim /etc/hostname
    5  setup
    6  ifconfig
    7  set +o history–;export LANG="en_US.UTF-8";export LANGUAGE="en_US.UTF-8";top
    8  ll
    9  cd opt/
   10  ll
     ......

案例2: 显示最近使用过的10个指令。

[root@hadoop home]# history 10
  155  ls
  156  cd /linktoroot
  157  cd linktoroot/
  158  pwd
  159  cd ..
  160  rm -rf linktoroot
  161  ls
  162  historty
  163  history
  164  history 10

案例3:执行历史编号为161的指令

[root@hadoop home]# !161
ls
a.txt  mycal  mydata.txt  test  xm  zwj

3.4 时间日期类

3.4.1 date指令

  • date指令-显示当前日期或设置日期
  • date (功能描述:显示当前时间)
  • date +%Y (功能描述:显示当前年份)
  • date +%m (功能描述:显示当前月份)
  • date +%d (功能描述:显示当前是哪一天)
  • date “+%Y-%m-%d %H:%M:%S”(功能描述:显示年月日时分秒)
  • date -s 字符串时间
    (注意大小写)
[root@hadoop home]# date
2022年 02月 18日 星期五 15:12:58 CST
[root@hadoop home]# date +%Y
2022
[root@hadoop home]# date +%m
02
[root@hadoop home]# date +%d
18
[root@hadoop home]# date "+%Y-%m-%d %H:%M:%S"
2022-02-18 15:16:59
[root@hadoop home]# date "+%Y年%m月%d日 %H:%M:%S"
2022年02月18日 15:17:39

# 设置系统当前时间 , 比如设置成 2020-11-11 11:22:22
[root@hadoop home]# date -s "2020-11-11 11:11:11"
2020年 11月 11日 星期三 11:11:11 CST
[root@hadoop home]# date
2020年 11月 11日 星期三 11:11:20 CST

3.4.2 cal指令

  • 查看日历指令
  • cal [选项] (功能描述:不加选项,显示本月日历)
# 显示当前日历
[root@hadoop home]# cal
      二月 2022     
日 一 二 三 四 五 六
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28

# 显示2022年日历
[root@hadoop home]# cal 2022
                               2022                               

        一月                   二月                   三月        
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
                   1          1  2  3  4  5          1  2  3  4  5
 2  3  4  5  6  7  8    6  7  8  9 10 11 12    6  7  8  9 10 11 12
 9 10 11 12 13 14 15   13 14 15 16 17 18 19   13 14 15 16 17 18 19
16 17 18 19 20 21 22   20 21 22 23 24 25 26   20 21 22 23 24 25 26
23 24 25 26 27 28 29   27 28                  27 28 29 30 31
30 31
        四月                   五月                   六月        
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
                1  2    1  2  3  4  5  6  7             1  2  3  4
 3  4  5  6  7  8  9    8  9 10 11 12 13 14    5  6  7  8  9 10 11
10 11 12 13 14 15 16   15 16 17 18 19 20 21   12 13 14 15 16 17 18
17 18 19 20 21 22 23   22 23 24 25 26 27 28   19 20 21 22 23 24 25
24 25 26 27 28 29 30   29 30 31               26 27 28 29 30
........

3.5 搜索查找类

3.5.1 find指令

  • find指令将从指定目录向下递归地遍历其各个子目录,将满足条件的文件或者目录显示在终端。
    在这里插入图片描述
    案例1: 按文件名:根据名称查找/home 目录下的hello.txt文件
[root@hadoop home]# touch hello.txt
[root@hadoop home]# ls
a.txt  hello.txt  mycal  mydata.txt  test  xm  zwj
[root@hadoop home]# find /home -name hello.txt
/home/hello.txt

案例2:按拥有者:查找/opt目录下,用户名称为 nobody的文件和root的文件

[root@hadoop home]# find /opt -user nobody
[root@hadoop home]# find /opt -user root
/opt
/opt/rh
/opt/vmware-tools-distrib
/opt/vmware-tools-distrib/installer
/opt/vmware-tools-distrib/installer/vgauth.conf
/opt/vmware-tools-distrib/installer/upstart-job.conf

案例3:查找整个linux系统下大于20m的文件(+n 大于 -n小于 n等于)

[root@hadoop home]# find / -size +20M
.....
[root@hadoop home]# find / -size 20M
find: “/proc/123487/task/123487/fd/5”: 没有那个文件或目录
find: “/proc/123487/task/123487/fdinfo/5”: 没有那个文件或目录
find: “/proc/123487/fd/5”: 没有那个文件或目录
find: “/proc/123487/fdinfo/5”: 没有那个文件或目录
/media/CentOS_6.8_Final/Packages/foomatic-db-ppds-4.0-7.20091126.el6.noarch.rpm
/media/CentOS_6.8_Final/Packages/oxygen-icon-theme-4.3.4-2.el6.noarch.rpm
/usr/share/anthy/anthy.dic

案例4:查找linux下所有txt文件

[root@hadoop ~]# find / -name *.txt
/opt/vmware-tools-distrib/doc/open_source_licenses.txt
/home/a.txt
/home/hello.txt
/home/zwj/test/b.txt
/home/zwj/test/a.txt
/home/mydata.txt
/home/test/b.txt
/home/test/pig.txt
/home/test/a.txt
.........

3.5.2 locate指令

  • locaate指令可以快速定位文件路径。locate指令利用事先建立的系统中所有文件名称及路径 的locate数据库实现快速定位给定的文件。Locate指令无需遍历整个文件系统,查询速度较快。
  • 为了保证查询结果的准确度,管理员必须定期更新locate时刻。
  • 由于locate指令基于数据库进行查询,所以第一次运行前,必须使用updatedb指令创建locate数据库
#请使用locate 指令快速定位 hello.txt 文件所在目录
[root@hadoop ~]# updatedb
[root@hadoop ~]# locate hello.txt
/home/hello.txt

3.5.3 grep指令和管道符号|

grep 过滤查找 , 管道符,“|”,表示将前一个命令的处理结果输出传递给后面的命令处理。
在这里插入图片描述

# 请在 hello.txt 文件中,查找 "yes" 所在行,并且显示行号
[root@hadoop home]# cat hello.txt | grep -n yes
1:yes 
4:yes
6:yes
10:yes
[root@hadoop home]# cat hello.txt | grep -ni yes
1:yes 
4:yes
6:yes
7:Yes
8:YES
10:yes

3.6 压缩和解压类

3.6.1 gzip/gunzip 指令

  • gzip 用于压缩文件, gunzip 用于解压的
  • gzip 文件 (功能描述:压缩文件,只能将文件压缩为*.gz文件)
  • gunzip 文件.gz (功能描述:解压缩文件命令)

案例1: gzip压缩, 将 /home下的 hello.txt文件进行压缩

[root@hadoop home]# ls
a.txt  hello.txt  mycal  mydata.txt  test  xm  zwj
[root@hadoop home]# gzip hello.txt
[root@hadoop home]# ls
a.txt  hello.txt.gz  mycal  mydata.txt  test  xm  zwj
注意:压缩后不保留原始文件

案例2: gunzip压缩, 将 /home下的 hello.txt.gz 文件进行解压缩

[root@hadoop home]# gunzip hello.txt.gz 
[root@hadoop home]# ls
a.txt  hello.txt  mycal  mydata.txt  test  xm  zwj

3.6.2 zip/unzip

  • zip 用于压缩文件, unzip 用于解压的,这个在项目打包发布中很有用的
  • -r:递归压缩,即压缩目录
  • -d<目录> :指定解压后文件的存放目录

案例1: 将 /home下的 所有文件进行压缩成 mypackage.zip

[root@hadoop home]# zip -r mypackage.zip /home/
  adding: home/ (stored 0%)
  adding: home/xm/ (stored 0%)
  adding: home/xm/.bash_logout (stored 0%)
  adding: home/xm/.gnome2/ (stored 0%)
  adding: home/xm/.mozilla/ (stored 0%)
  adding: home/xm/.mozilla/extensions/ (stored 0%)
  adding: home/xm/.mozilla/plugins/ (stored 0%)
  adding: home/xm/.bashrc (deflated 21%)
  adding: home/xm/.bash_history (deflated 17%)
  adding: home/xm/.bash_profile (deflated 19%)
  adding: home/a.txt (deflated 52%)
  adding: home/hello.txt (deflated 15%)
  adding: home/zwj/ (stored 0%)
  adding: home/zwj/.bash_logout (stored 0%)
  adding: home/zwj/.gnome2/ (stored 0%)
  adding: home/zwj/.mozilla/ (stored 0%)
  adding: home/zwj/.mozilla/extensions/ (stored 0%)
  adding: home/zwj/.mozilla/plugins/ (stored 0%)
  adding: home/zwj/.bashrc (deflated 21%)
  adding: home/zwj/.bash_profile (deflated 19%)
  adding: home/zwj/test/ (stored 0%)
  adding: home/zwj/test/b.txt (stored 0%)
  adding: home/zwj/test/a.txt (stored 0%)
  adding: home/mycal (deflated 26%)
  adding: home/mydata.txt (deflated 47%)
  adding: home/test/ (stored 0%)
  adding: home/test/b.txt (stored 0%)
  adding: home/test/pig.txt (stored 0%)
  adding: home/test/a.txt (stored 0%)
[root@hadoop home]# ls
a.txt  hello.txt  mycal  mydata.txt  mypackage.zip  test  xm  zwj

案例2: 将 mypackge.zip 解压到 /home/test 目录下

[root@hadoop home]# ls /home/test/
a.txt  b.txt  pig.txt
[root@hadoop home]# unzip -d /home/test/ mypackage.zip
Archive:  mypackage.zip
   creating: /home/test/home/
   creating: /home/test/home/xm/
 extracting: /home/test/home/xm/.bash_logout  
 ........
[root@hadoop home]# ls /home/test/
a.txt  b.txt  home  pig.txt

3.6.3 tar指令

  • tar 指令 是打包指令,打包后的文件是 .tar.gz 的文件。
  • 指定解压的那个目录实现要存在才能成功,否则会报错。
    在这里插入图片描述
    案例1: 压缩多个文件,将 /home/test/a.txt 和 /home/test/b.txt 压缩成 a.tar.gz
[root@hadoop home]# cd /home/test/
[root@hadoop test]# ls
a.txt  b.txt  home  pig.txt
[root@hadoop test]# tar -zcvf a.tar.gz a.txt b.txt
a.txt
b.txt
[root@hadoop test]# ls
a.tar.gz  a.txt  b.txt  home  pig.txt

案例2: 将/home 的文件夹 压缩成 myhome.tar.gz

[root@hadoop home]# tar -zcvf myhome.tar.gz /home/
/home/
/home/mypackage.zip
/home/xm/
/home/xm/.bash_logout
/home/xm/.gnome2/
/home/xm/.mozilla/
/home/xm/.mozilla/extensions/
/home/xm/.mozilla/plugins/
/home/xm/.bashrc
..........
[root@hadoop home]# ls
a.txt  hello.txt  mycal  mydata.txt  myhome.tar.gz  mypackage.zip  test  xm  zwj

案例3: 将 a.tar.gz 解压到当前目录

[root@hadoop test]# ls
a.tar.gz home  pig.txt
[root@hadoop test]# tar -zxvf a.tar.gz
a.txt
b.txt
[root@hadoop test]# ls
a.tar.gz  a.txt  b.txt  home  pig.txt

案例4:将myhome.tar.gz解压到/home/test/目录下

[root@hadoop home]# ls /home/test
a.tar.gz  a.txt  b.txt  pig.txt
[root@hadoop home]# tar -zxvf myhome.tar.gz -C /home/test/
home/
home/mypackage.zip
home/xm/
.........
[root@hadoop home]# ls /home/test/
a.tar.gz  a.txt  b.txt  home  pig.txt
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值