某校linux课实验(一)

来源:互联网 发布:浙江省选修课网络课程 编辑:程序博客网 时间:2024/05/13 18:26

0x00

某老师linux课的实验题,结果发现自己用了这些年linux还是对find、sort不太熟悉,还有awk,觉得不应该沉溺于ubuntu之类的图形界面之中,切切实实地用shell command来用linux才是正道。实验使用Centos6.4 minimal……

P.S.所以所有linux实验都只会会在这两个系列的部分社区发行版进行。如:ubuntu,debian,centos6,rhel5。默认环境Centos minimal(300来m镜像)

0x01

1.root帐号登录,查看/tmp目录,如果/tmp目录下没有子目录myshare,则建立该目录。

 # touch /tmp/myshare 
  # mkdir /tmp/myshare 无法覆盖,应该与 rm -rf /tmp/myshare 一起用

2.创建帐号testuser。

  # useradd testuser  
    # passwd testuser   密码是:fuckwindows

3.把myshare目录及其目录下的所有文件和子目录的拥有者该为testuser,工作组改为users。

    # chown -R testuser:users /tmp/myshare



4、切换至testuser帐号。进入/tmp/myshare目录,采用vim编辑器编写以上程序,程序名称为hello.sh:
#!/bin/bashecho "app start"echo -efunc (){  echo "hello world!"}funcecho -eecho "app end"


 # su testuser   # vi /tmp/myshare/hello.s 


5.保存hello.sh后,给予hello.sh拥有者可读、可写和可执行的权限,同组可读可执行,其他人可执行权限。
  
  $ chmod 751 hello.sh 

6.输入./hello.sh,观察程序输出的效果。

  $ ./hello.sh


7.进入testuser的用户主目录,在这个目录下创建hello.sh的软链接,同时拷贝hello.sh到该目录下并改名为hello.sh.bak,要求拷贝时保留文件属性值。

  $ ln -s /tmp/myshare/hello.sh     $cp -p /tmp/myshare/hello.sh ./hello.sh.bak


8.退出testuser帐号,回到root帐号,从/开始查找后缀名为.conf的所有文件,把输出结果重定向到testuser帐号的主目录下的output.txt文件。

  $ su root    # find / -name "*.conf" >output.txt

9.在上一步操作的.conf文件中找出文件容量最大的和最小那个,并把这两个文件的容量大小输出到output.txt文件中。

    # find / -name "*.conf" -exec ls -l {} \;|cut -d' ' -f5|sort -n|head -1  # find / -name "*.conf" -exec ls -l {} \;|cut -d' ' -f5|sort -nr|head -1


10.统计出系统中有多少个用户帐号,把数量输出到output.txt文件中。

  # cat /etc/passwd|cut -d: -f1|wc -l>>output.txt

11.把output.txt文件转换为windows记事本可正规打开的格式。
  这里比较坑爹,装unix2dos不行,然后就转为tr了,理解一下mac,win,unix的文件格式就知道 \r \r\n \n,那用tr好了

  # tr '\n' '\r\n' <output.txt> output1.txt 前面的是unix文件

12.tar打包压缩testuser帐号主目录下的所有文件。 

   # tar -zcvf ./testuser.tar.gz /home/testuser


13.用U盘把上一步打包压缩文件拷贝到U盘上。
  #fdisk -l找到u盘  # mkdir /mnt/udisk&&mount -t vfat /dev/sdb1 /mnt/udisk  # cp testuser.tar.gz /mnt/udisk  # umount /mnt/udisk


14、执行userdel -r testuser,执行rm -fr myshare



0x03 后续:

很多人发现第九题的用不了,多数是rhel5.x系列的,补充一下

# find / -name "*.conf" -exec ls -l {} \;|sort -t' ' -k5 -n|cut -d' ' -f5|head -1# find / -name "*.conf" -exec ls -l {} \;|sort -t' ' -k5 -nr|cut -d' ' -f5|head -1

如果先用cut再用sort,会发现有三行为空,是因为cut有个很严重的缺陷,在处理多个空格时候会非常麻烦



原创粉丝点击