ls
1.压缩与解压缩
// 创建test目录及文件file1-3
[root@localhost ~]# ls
anaconda-ks.cfg test tt.txt
[root@localhost ~]# cd test/
[root@localhost test]# ls
file1.txt file2.txt file3.txt
[root@localhost test]#
(1):使用 tar 命令对文件进行打包压缩与解压缩:
[root@localhost ~]# tar cvf test.tar test/
test/
test/file3.txt
test/file2.txt
test/file1.txt
[root@localhost ~]# ls
anaconda-ks.cfg test test.tar tt.txt
[root@localhost ~]#
// 可看到,test目录已经被打包为test.tar格式;
(2):使用 gzip 方式对文件进行压缩,并指定压缩名为 tar_gzip.tar.gz:
[root@localhost test]# gzip -c file1.txt > tar_gzip.tar.gz
[root@localhost test]# ll
total 12
-rw-r--r--. 1 root root 30 Jun 3 12:25 file1.txt
-rw-r--r--. 1 root root 30 Jun 3 12:25 file2.txt
-rw-r--r--. 1 root root 0 Jun 3 12:24 file3.txt
-rw-r--r--. 1 root root 47 Jun 13 22:42 tar_gzip.tar.gz
[root@localhost test]#
// -c 的作用就是用来保留原文件不被删除的;
(3):使用bzip2方式对文件夹进行压缩,并指定压缩名为tar_bzip2.tar.bz2:
[root@localhost test]# bzip2 -c file2.txt > tar_bzip2.tar.bz2
[root@localhost test]# ll
total 16
-rw-r--r--. 1 root root 30 Jun 3 12:25 file1.txt
-rw-r--r--. 1 root root 30 Jun 3 12:25 file2.txt
-rw-r--r--. 1 root root 0 Jun 3 12:24 file3.txt
-rw-r--r--. 1 root root 56 Jun 13 22:44 tar_bzip2.tar.bz2
-rw-r--r--. 1 root root 47 Jun 13 22:42 tar_gzip.tar.gz
[root@localhost test]#
(4):使用xz方式对文件进行压缩,并指定压缩名为tar_xz.tar.xz:
[root@localhost test]# xz file3.txt > tar_xz.tar.xz
[root@localhost test]# ll
total 20
-rw-r--r--. 1 root root 30 Jun 3 12:25 file1.txt
-rw-r--r--. 1 root root 30 Jun 3 12:25 file2.txt
-rw-r--r--. 1 root root 32 Jun 3 12:24 file3.txt
-rw-r--r--. 1 root root 56 Jun 13 22:44 tar_bzip2.tar.bz2
-rw-r--r--. 1 root root 47 Jun 13 22:42 tar_gzip.tar.gz
-rw-r--r--. 1 root root 0 Jun 13 22:46 tar_xz.tar.xz
(5):新建文件file1.txt,file2.txt,file3.txt
对文件file1.txt和file2.txt,进行压缩(使用gzip方式),排除file3.txt(即不对file3进行压缩)并指定压缩名为tar_file.tar.gz:
//方法一:
[root@localhost test]# tar -czf tar_file.tar.gz file[!3].txt //按照指定名字打包
[root@localhost test]# ls
file1.txt file2.txt file3.txt tar_file.tar.gz
[root@localhost test]# tar -tf tar_file.tar.gz //查看包中的文件
file1.txt
file2.txt
[root@localhost test]#
//方法二:
[root@localhost test]# tar -czf tar.gz file*.txt --exclude file3.txt
[root@localhost test]# ls
file1.txt file2.txt file3.txt tar.gz
[root@localhost test]# tar -tf tar.gz
file1.txt
file2.txt
[root@localhost test]#
(6):新建文件file4.txt,将file4.txt添加到 tar_file.tar.gz 中,查看 tar_file.tar.gz 中有哪些文件及目录(不解压,只查看):
[root@localhost test]# ls
file1.txt file2.txt file3.txt tar_filr.tar.gz
[root@localhost test]# touch file4.txt
[root@localhost test]# tar -rvf tar_filr.tar.gz file4.txt
file4.txt
[root@localhost test]# tar -tf tar_filr.tar.gz
file1.txt
file2.txt
file4.txt
[root@localhost test]#
// 注意,只有用cvf打包才可以使用rvf来进行添加文件,也就是打包可以添加文件,压缩不能使用rvf来进行添加文件;
(7):解压tar_gzip.tar.gz到指定目录tar_test(没有这个目录就创建),解压tar_xz.t.ar.xz;
[root@localhost test]# mkdir tar_test
[root@localhost test]# tar -xvf tar_filr.tar.gz -C tar_test
file1.txt
file2.txt
file3.txt
file4.txt
[root@localhost test]# ls
file1.txt file2.txt file3.txt file4.txt tar_filr.tar.gz tar_test
[root@localhost test]# ls ./tar_test/
file1.txt file2.txt file3.txt file4.txt
[root@localhost test]#
[root@localhost test]# xz tar_xz.tar.xz file*.txt
[root@localhost test]# ls
file1.txt.xz file3.txt.xz tar_filr.tar.gz
file2.txt.xz file4.txt.xz tar_test
[root@localhost test]# unxz file*
[root@localhost test]# ls
file1.txt file2.txt file3.txt file4.txt tar_filr.tar.gz tar_test
[root@localhost test]#
2.在Linux上的/root目录创建一个Linux.txt,在windows上创建windows.txt
(1)
通过sftp 的get和put命令,将windows上的windows.txt推送到Linux上;
通过sftp 的get和put命令,将Linux上的tt.txt推送到windows上;
sftp> pwd
Remote working directory: /root
sftp> lpwd
Local working directory: c:\users\administrator
sftp> lcd C:\Users\Administrator\.local
sftp> lpwd
Local working directory: c:\users\administrator\.local
sftp> cd /root/test
sftp> pwd
Remote working directory: /root/test
sftp> put windows.txt
Uploading windows.txt to /root/test/windows.txt
windows.txt 100% 0 0.0KB/s 00:00
sftp> EXIT
C:\Users\Administrator>sftp root@192.168.188.133
root@192.168.188.133's password:
Connected to 192.168.188.133.
sftp> lpwd
Local working directory: c:\users\administrator
sftp> lcd C:\Users\Administrator\.local
sftp> cd /root/test
sftp> get tt.txt
Fetching /root/test/tt.txt to tt.txt
/root/test/tt.txt 100% 10 4.9KB/s 00:00
sftp>
(2):使用rz上传文件windows.txt到Linux上
使用sz下载文件linux.txt到windows上;
3.环境变量,普通变量
(1)创建普通变量local_data=1并访问;
[root@localhost ~]# local_data=1
[root@localhost ~]# echo $local_data
1
(2)创建环境变量ROOT_DATA=root,只有root用户可以访问到;
// 当前是在root中
[root@localhost ~]# export ROOT_DATA=root
[root@localhost ~]# echo $ROOT_DATA
root
[root@localhost ~]#
// 切换到普通用户中,我的是rhcsa
[rhcsa@localhost ~]$ echo $ROOT_DATA
[rhcsa@localhost ~]$
(3)创建环境变量USER_DATA=user,只有普通用户可以访问到;
// 普通用户中
[rhcsa@localhost ~]$ USER_DATA=user
[rhcsa@localhost ~]$ echo $USER_DATA
user
[rhcsa@localhost ~]$
// root中
[root@localhost ~]# echo $USER_DATA
[root@localhost ~]#
(4)创建环境变量DATA=all,root用户和普通用户都可以访问到;
// 想要作为全局变量需要在.bashrc文件中进行配置,并用export进行导出
// root中
[root@localhost ~]# vim ./.bashrc
[root@localhost ~]# source ./.bash_profile
[root@localhost ~]# echo $DATA
all
[root@localhost ~]#
// 普通用户中
[root@localhost ~]# echo $DATA
all
[root@localhost ~]#
4.别名
(1)创建3个文件test1.txt,test2.txt,test3.txt,使用find查找test1.txt,test2.txt,test3.txt;
[root@localhost ~]# touch test{1..3}.txt
[root@localhost ~]# ll
total 4
-rw-------. 1 root root 1736 Jun 3 10:05 anaconda-ks.cfg
drwxr-xr-x. 3 root root 146 Jun 14 15:44 test
-rw-r--r--. 1 root root 0 Jun 14 16:29 test1.txt
-rw-r--r--. 1 root root 0 Jun 14 16:29 test2.txt
-rw-r--r--. 1 root root 0 Jun 14 16:29 test3.txt
[root@localhost ~]#
[root@localhost ~]# find . -name "test*.txt"
./test1.txt
./test2.txt
./test3.txt
[root@localhost ~]#
(2):使用别名:将上边命令命名为myfind,取消别名
[root@localhost ~]# alias myfind=find
[root@localhost ~]# myfind . -name "test*.txt"
./test1.txt
./test2.txt
./test3.txt
[root@localhost ~]# unalias myfind
[root@localhost ~]# myfind . -name "test*.txt"
-bash: myfind: command not found
[root@localhost ~]#
5.查看最近使用的10条历史命令
[root@localhost ~]# history 10
773 alias find
774 alias myfind=find
775 alias find
776 myfind . -name "test*.txt"
777 alias a=pwd
778 a
779 unalias find
780 unalias myfind
781 myfind . -name "test*.txt"
782 history 10
[root@localhost ~]#
6.在一行上执行两个命令,打印123和从root切换到普通用户
[root@localhost ~]# echo 123;su rhcsa
123
[rhcsa@localhost root]$
7.通配符使用:
(1)创建3个文件 file1,file2,file3
1.*去匹配3个文件
2.?匹配3个文件
3.[]匹配file1和file3
4.[^]匹配file2
5.[ ! ]匹配file2
6.{}匹配file1和file3
[root@localhost test]#
[root@localhost test]# ll
total 0
-rw-r--r--. 1 root root 0 Jun 16 14:46 file111.txt
-rw-r--r--. 1 root root 0 Jun 13 22:52 file1.txt
-rw-r--r--. 1 root root 0 Jun 13 22:52 file2.txt
-rw-r--r--. 1 root root 0 Jun 13 22:52 file3.txt
-rw-r--r--. 1 root root 0 Jun 16 14:46 fileabc.txt
[root@localhost test]# ls file*
file111.txt file1.txt file2.txt file3.txt fileabc.txt
[root@localhost test]# ls file?.txt
file1.txt file2.txt file3.txt
[root@localhost test]# ls file[1,3].txt
file1.txt file3.txt
[root@localhost test]# ls file[1..3].txt
file1.txt file3.txt
[root@localhost test]# ls file[^1].txt
file2.txt file3.txt
[root@localhost test]# ls file[!1].txt
file2.txt file3.txt
[root@localhost test]# ls file[!3].txt
file1.txt file2.txt
[root@localhost test]# ls file{1..3}.txt
file1.txt file2.txt file3.txt
[root@localhost test]# ls file{1,3}.txt
file1.txt file3.txt
8.引号的使用举例:
无引号,单引号,双引号,反引号,$()
[root@localhost test]# date=123
[root@localhost test]# echo $date
123
[root@localhost test]# echo '$date'
$date
[root@localhost test]# echo "$date"
123
[root@localhost test]# echo `$date` // 这里报错是因为反引号一般用于命令
-bash: 123: command not found
[root@localhost test]# echo `pwd` // 反引号中内容为命令
/root/test
[root@localhost test]# echo $($date) //$()的作用和反引号类似,所以内容都为命令
-bash: 123: command not found
[root@localhost test]# echo $(pwd) // 命令执行结果
/root/test
[root@localhost test]#
9.Linux中用户和组:
(1)linux中用户的类型:
用户类型:超级用户,系统用户,普通用户;
(2)linux中用户组的类型:
用户组类型:基本组,附加组,系统组;
(3)linux中存储用户信息的文件是哪个?且其中的字段是什么意思?
[root@localhost ~]# tail -3 /etc/passwd
rhcsa:x:1000:1000:rhcsa:/home/rhcsa:/bin/bash
user1:x:1001:1001::/home/user1:/bin/bash
user2:x:1002:1002::/home/user2:/bin/bash
[root@localhost ~]#
以rhcsa为例子:一共分为七个字段,每个字段以冒号隔开:
第一个字段为账号名称;
第二个字段为密码,显示为X说明是经过了shadow 保护的;
第三个字段的1000表示的是用户ID,这个是分范围的,0为root用户,1~999是系统用户,>=1000是普通用户,每创建一个用户默认是在最大的数上加一,rhcsa是第一个普通用户,所以表示为1000;
第四个字段的1000表示的是用户组ID,若没有指定ID,则默认和用户ID一样,此时系统就会建立一个和用户名想同的基本组,基本组只容纳一个用户,当加入其他的用户的时候,则基本组就变成了附加组(可容纳多个用户,组中的每个用户都有组所拥有的的权利);
第五个字段代表的是用户个人资料,可以记录用户的电话,邮箱等个人信息;
第六个字段代表的是用户的家目录,一般默认的家目录在 /home/username 下,这里的usernname是用户名,
第七个字段代表的是用的登录后激活的shell类型,一般默认为Bash Shell;
(4)linux中存储组信息的文件是哪个?且其中的字段是什么意思?
[root@localhost ~]# tail -3 /etc/group
testgroup2:x:1011:
user1:x:1001:
user2:x:1002:
[root@localhost ~]#
以user1为例:
第一个字段代表的是,组的名字;
第二个字段代表的是密码;
第三个字段代表的是用户组ID;
第四个字段代表的是代表附加组成员的,若有附加组在这里显示;
10.创建下列用户,组和组成员资格:
1.创建名为sysmgrs 的组
[root@localhost ~]# groupadd sysmgrs
[root@localhost ~]# tail -1 /etc/group
sysmgrs:x:1012:
[root@localhost ~]#
2.创建用户natasha 同时指定sysmgrs作为natasha的附加组
[root@localhost ~]# useradd natasha -G sysmgrs
[root@localhost ~]# tail -1 /etc/passwd
natasha:x:1003:1003::/home/natasha:/bin/bash
[root@localhost ~]#
3.创建用户harry 同时指定sysmgrs作为harry的附加组
[root@localhost ~]# useradd harry -G sysmgrs
[root@localhost ~]# tail -1 /etc/passwd
harry:x:1004:1004::/home/harry:/bin/bash
[root@localhost ~]#
4.创建用户sarah 指定shell类型为/sbin/false(无权访问系统上的交互式shell)且不是sysmgrs 的成员
[root@localhost ~]# useradd sarah -s /sbin/false
[root@localhost ~]# tail -1 /etc/passwd
sarah:x:1005:1013::/home/sarah:/sbin/false
[root@localhost ~]#
5.设置natasha 、 harry 和 sarah的密码都是123
[root@localhost ~]# passwd natasha
Changing password for user natasha.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@localhost ~]# passwd harry
Changing password for user harry.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@localhost ~]# passwd sarah
Changing password for user sarah.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@localhost ~]#
6.创建用户lockuser,并指定家目录为/home/lock,然后锁定该用户
[root@localhost ~]# useradd -d /home/lock lockuser
[root@localhost ~]# tail -1 /etc/passwd
lockuser:x:1006:1006::/home/lock:/bin/bash
[root@localhost ~]# usermod lockuser -L
7.创建用户limituser,gid为1555,userid为1666,让其密码在10天后过期
[root@localhost /]# useradd limituser -g 1555 -u 1666 // 用户组必须是以及存在的
[root@localhost /]# tail -1 /etc/passwd
limituser:x:1666:1555::/home/limituser:/bin/bash
[root@localhost /]# passwd limituser -n 10
Adjusting aging data for user limituser.
passwd: Success
[root@localhost /]#
8.解锁lockuser,并设定下次登录时必须修改密码
[root@localhost /]# usermod limituser -U
[root@localhost /]# passwd limituser -e
Expiring password for user limituser.
passwd: Success
[root@localhost /]#
9.让natasha具备修改harry密码的权限(sudo)
visudo
Host_Alias RHCSA=lwz
User_Alias USER11=natasha
Cmnd_ Alias CHPASS=/usr/ bin/passwd harry
USER RCHSA= (root)CHPASs
Host_Alias A = localhost
User_Alias B = natasha
Cmnd_Alias CD = /usr/bin/passwd harry
B A=(root) NOPASSWD:CD
[root@localhost ~]# visudo
[root@localhost ~]# su natasha
[natasha@localhost root]$ sudo passwd harry
Changing password for user harry.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[natasha@localhost root]$
10.创建用户testuser并设置密码,修改用户名为normaluser
[root@localhost ~]# useradd testuser -p "123"
[root@localhost ~]# usermod testuser -l normaluser
[root@localhost ~]# tail -1 /etc/passwd
normaluser:x:1667:1667::/home/testuser:/bin/bash
[root@localhost ~]#
11.删除lockuser
[root@localhost ~]# userdel -r lockuser