Linux基础命令练习
1、 在home下创建一个以自己名字首字母开始的文件夹
root@ubuntu:/home# mkdir Gsang
root@ubuntu:/home# ls
Gsang ubuntu
# 要切换到root
2、进入此文件夹
ubuntu@ubuntu:/home$ cd Gsang/
ubuntu@ubuntu:/home/Gsang$
3、在该文件夹下直接创建 AAA/BBB 文件夹
root@ubuntu:/home/Gsang# mkdir -p AAA/BBB
root@ubuntu:/home/Gsang# ls
AAA
# -p 选项递归创建目录
4、继续在创建CCC DDD文件夹
root@ubuntu:/home/Gsang# mkdir CCC DDD
root@ubuntu:/home/Gsang# ls
AAA CCC DDD
5、进入CCC文件夹 在此文件夹中创建3个文件 1.txt 2.txt 3.txt
root@ubuntu:/home/Gsang# cd CCC
root@ubuntu:/home/Gsang/CCC# ls
root@ubuntu:/home/Gsang/CCC# touch 1.txt 2.txt 3.txt
root@ubuntu:/home/Gsang/CCC# ls
1.txt 2.txt 3.txt
6、用vim编辑器修改1.txt 内容为hello world 修改2.txt 内容为nihao 修改3.txt内容为111111
root@ubuntu:/home/Gsang/CCC# vim 1.txt
root@ubuntu:/home/Gsang/CCC# vim 2.txt
root@ubuntu:/home/Gsang/CCC# vim 3.txt
7、将三个文件拷贝到 AAA/BBB文件夹下
root@ubuntu:/home/Gsang/CCC# cp 1.txt /home/Gsang/AAA/BBB/
root@ubuntu:/home/Gsang/CCC# cp 2.txt /home/Gsang/AAA/BBB/
root@ubuntu:/home/Gsang/CCC# cp 3.txt /home/Gsang/AAA/BBB/
8、回到AAA文件夹下,创建文件4.txt 5.txt 修改内容分别为shaoxing和yuanpei
root@ubuntu:/home/Gsang/AAA# touch 4.txt
root@ubuntu:/home/Gsang/AAA# touch 5.txt
root@ubuntu:/home/Gsang/AAA# vim 4.txt
root@ubuntu:/home/Gsang/AAA# vim 5.txt
9、将AAA文件夹下的BBB 4.txt 5.txt压缩一个文件夹叫1.tar.gz
root@ubuntu:/home/Gsang/AAA# tar -zcvf 1.tar.gz 4.txt 5.txt BBB
10、将1.tar.gz移动到DDD文件夹下
root@ubuntu:/home/Gsang/AAA# mv 1.tar.gz /home/Gsang/DDD/
11、进入DDD文件夹,把1.tar.gz进行解压
root@ubuntu:/home/Gsang/DDD# tar -zxvf 1.tar.gz
root@ubuntu:/home/Gsang/DDD# ls
1.tar.gz 4.txt 5.txt BBB
12、查看4.txt 5.txt 以及BBB文件夹下的文件内容是否和原来的一样
root@ubuntu:/home/Gsang/DDD# cat 4.txt
shaoxing
root@ubuntu:/home/Gsang/DDD# cat 5.txt
yuanpei
root@ubuntu:/home/Gsang/DDD# ls BBB
1.txt 2.txt 3.txt
13、进入DDD/BBB文件夹 查看各个文件的大小以及详细信息
root@ubuntu:/home/Gsang/DDD/BBB# ls -l
total 12
-rw-r--r-- 1 root root 11 Jul 13 06:26 1.txt
-rw-r--r-- 1 root root 6 Jul 13 06:26 2.txt
-rw-r--r-- 1 root root 7 Jul 13 06:26 3.txt
14、查看DDD文件夹的大小
root@ubuntu:/home/Gsang# ls -lh DDD/
total 16K
-rw-r--r-- 1 root root 255 Jul 13 06:29 1.tar.gz
-rw-r--r-- 1 root root 9 Jul 13 06:27 4.txt
-rw-r--r-- 1 root root 8 Jul 13 06:27 5.txt
drwxr-xr-x 2 root root 4.0K Jul 13 06:26 BBB
15、在DDD文件夹下新建一个1.log日志文件
root@ubuntu:/home/Gsang/DDD# touch 1.log
16、用tail命令监听日志文件,并开启另一个终端,用重定向向日志文件输出hello world,并查看监听日志的终端是否显示
root@ubuntu:/home/Gsang/DDD# tail -f 1.log
# -f为持续监听
hello world
root@ubuntu:/home/Gsang/DDD# echo 'hello world' >> 1.log
17、在最上层的文件夹(自己新建的文件夹)下搜索所有3.txt的文件,并显示搜索结果
root@ubuntu:/home/Gsang# find -name 3.txt
./DDD/BBB/3.txt
./CCC/3.txt
./AAA/BBB/3.txt
18、继续搜索所有.txt结尾的文件
root@ubuntu:/home/Gsang# find -name *.txt
./DDD/5.txt
./DDD/BBB/1.txt
./DDD/BBB/2.txt
./DDD/BBB/3.txt
./DDD/4.txt
./CCC/1.txt
./CCC/2.txt
./CCC/3.txt
./AAA/5.txt
./AAA/BBB/1.txt
./AAA/BBB/2.txt
./AAA/BBB/3.txt
./AAA/4.txt
19、搜索所有文件中含有hello world字符串的文件
root@ubuntu:/home/Gsang# grep -r "helloworld" /home/Gsang/
/home/Gsang/DDD/BBB/1.txt:helloworld
/home/Gsang/CCC/1.txt:helloworld
/home/Gsang/AAA/BBB/1.txt:helloworld
# grep “字符串” 文件名
# grep “字符串” 文件1 文件2 文件3
# grep -r “字符串” 目录
# -r 递归查找目录中的文件
20、把DDD删除
root@ubuntu:/home/Gsang# rm -rf DDD
root@ubuntu:/home/Gsang# ls
AAA CCC
# -f 即使原档案属性设为唯读,亦直接删除,无需逐一确认。
# -r 将目录及以下之档案亦逐一删除。
文件权限练习
1、创建用户组A,B
root@ubuntu:/home/Gsang# groupadd A
root@ubuntu:/home/Gsang# groupadd B
2、创建用户111 222为A组 333为B组
root@ubuntu:/home/Gsang# useradd -g A -m 111
root@ubuntu:/home/Gsang# useradd -g A -m 222
root@ubuntu:/home/Gsang# useradd -g B -m 333
# -g 指定所属用户组,-m 指定创建的用户名
3、以root用户登录,在home下新建一个文件夹www 并把文件夹的权限设置为rwxrwxrwx
root@ubuntu:/home# mkdir www
root@ubuntu:/home# ls -l
total 24
drwxr-xr-x 2 111 A 4096 Jul 13 07:53 111
drwxr-xr-x 2 222 A 4096 Jul 13 07:54 222
drwxr-xr-x 2 333 B 4096 Jul 13 07:54 333
drwxr-xr-x 4 root root 4096 Jul 13 06:56 Gsang
drwxr-xr-x 21 ubuntu ubuntu 4096 Jul 6 07:31 ubuntu
drwxr-xr-x 2 root root 4096 Jul 13 07:55 www
root@ubuntu:/home# chmod 777 www
root@ubuntu:/home# ls -l
total 24
drwxr-xr-x 2 111 A 4096 Jul 13 07:53 111
drwxr-xr-x 2 222 A 4096 Jul 13 07:54 222
drwxr-xr-x 2 333 B 4096 Jul 13 07:54 333
drwxr-xr-x 4 root root 4096 Jul 13 06:56 Gsang
drwxr-xr-x 21 ubuntu ubuntu 4096 Jul 6 07:31 ubuntu
drwxrwxrwx 2 root root 4096 Jul 13 07:55 www
4、进入www文件夹
root@ubuntu:/home# cd www
5、创建一个文件文件1.txt 权限设置为rwxrw-r–
root@ubuntu:/home/www# touch 1.txt
root@ubuntu:/home/www# chmod 764 1.txt
root@ubuntu:/home/www# ls -l
total 0
-rwxrw-r-- 1 root root 0 Jul 13 08:02 1.txt
把1.txt 修改文件所有者为222 文件所有组为B
root@ubuntu:/home/www# chown 222 1.txt
root@ubuntu:/home/www# chgrp B 1.txt
root@ubuntu:/home/www# ls -l
total 0
-rwxrw-r-- 1 222 B 0 Jul 13 08:02 1.txt
root@ubuntu:/home/www#
切换为111 222 和333分别对1.txt 验证权限
#为用户设置密码
#su 用户名
#查看文件
6.给1.txt文件增加一个其他用户可以执行的权限
# 权限分为3组,一组三个字符 r w x,分别为owner/group/others的rwx权限
root@ubuntu:/home/www# chmod 765 1.txt
root@ubuntu:/home/www# ls -l
total 0
-rwxrw-r-x 1 222 B 0 Jul 13 08:02 1.txt
并进行测试
7.切换root用户 创建一个AAA文件夹 并在AAA文件夹下创建1.txt 2.txt
root@ubuntu:/home# mkdir AAA
root@ubuntu:/home# ls
111 222 333 AAA Gsang ubuntu www
8.修改文件夹的权限为rwxr-xr-x 并把文件夹的内的子文件等全部修改 把文件所有者设置为333 文件所有组也设置为B
并验证
root@ubuntu:/home# chmod -R 755 AAA
root@ubuntu:/home# ls -l
total 28
drwxr-xr-x 2 111 A 4096 Jul 13 07:53 111
drwxr-xr-x 2 222 A 4096 Jul 13 07:54 222
drwxr-xr-x 2 333 B 4096 Jul 13 07:54 333
drwxr-xr-x 2 root root 4096 Jul 13 08:11 AAA
drwxr-xr-x 4 root root 4096 Jul 13 06:56 Gsang
drwxr-xr-x 21 ubuntu ubuntu 4096 Jul 6 07:31 ubuntu
drwxrwxrwx 2 root root 4096 Jul 13 08:02 www
root@ubuntu:/home# ls AAA/ -ls
total 0
0 -rwxr-xr-x 1 root root 0 Jul 13 08:11 1.txt
0 -rwxr-xr-x 1 root root 0 Jul 13 08:11 2.txt
root@ubuntu:/home# chgrp -R B AAA
root@ubuntu:/home# chown -R 222 AAA
root@ubuntu:/home# ls -l AAA/
total 0
-rwxr-xr-x 1 222 B 0 Jul 13 08:11 1.txt
-rwxr-xr-x 1 222 B 0 Jul 13 08:11 2.txt