1, 指定格式显示当前时间, 格式: 2017-11-09 10:20:30
- [root@cent6OS ~]#date "+%Y-%m-%d %H:%M:%S"
- 2017-11-19 19:04:14
date -d "-6 day" "+%F %T" 指定格式显示六天前的日期
date -d "yesterday"
date -d "10 month"
date "+%s" 距离 1970 年 1 月 1 号的秒数时间
`echo date "+%s" `/ 3600/24 | bc 将秒数转化为天数
2, 显示前天是星期几
今天星期三 两天前是星期一 0 或 7-6 0 或 7 表示星期日
- [root@centos7 ~]#date -d "2 day ago" "+%u"
- 6
设置当前日期为 2017-11-09 16:26:00
date -s "16:26:00 2017-11-09" "+%Y-%m-%d %H:%M:%S"
3, 在本机字符终端登录时, 除显示原有信息外, 再显示当前登录终端号, 主机名和当前时间
登录的终端号 时间 主机名
Nano /etc/issue
The hostname is \n 主机名
Time is \t 时间
TTy is \l 登录终端
4, 今天 18:30 自动关机, 并提示用户
shutdown -P(power off) "18:30"
shutdown -h(halt) now 立即关机
shutdown -r +2 2 分钟后重启
二, 文件管理
1, 显示 / var 目录下所有以 l 开头, 以一个小写字母结尾, 且中 间出现至少一位数字的文件或目录
ls -d /var/1*[0-9]*[[:lower:]]
2, 显示 / etc 目录下以任意一位数字开头, 且以非数字结尾的 文件或目录
ls -d /etc/[0-9]*[^[:digit:]] tuolefu ^
3, 显示 / etc / 目录下以非字母开头, 后面跟了一个字母及其 它任意长度任意字符的文件或目录
ls -d [^[:alpha:]][[:alpha:]]*
4, 显示 / etc / 目录下所有以 rc 开头, 并后面是 0-6 之间的数 字, 其它为任意字符的文件或目录
ls -d /etc/rc[0-6]*
5, 显示 / etc 目录下, 所有以. d 结尾的文件或目录
l s -d /etc/*.d
6, 显示 / etc 目录下, 所有. conf 结尾, 且以 m,n,r,p 开头的文 件或目录
ls -d [mnrp]*.conf
7, 只显示 / root 下的隐藏文件和目录
ls -d /root/.[^.]*
ls -aI /root/[^.]*
8, 只显示 / etc 下的非隐藏目录
ls -d /etc/[^.]*/
总结
* 显示普通文件 压缩文件 可执行程序
*/ 只显示目录
(1) 如何创建 / testdir/dir1/x, /testdir/dir1/y,
/testdir/dir1/x/a, /testdir/dir1/x/b,
/testdir/dir1/y/a, /testdir/dir1/y/b
mkdir -pv /testdir/dir1{x,y}/{a,b}
(2) 如何创建 / testdir/dir2/x, /testdir/dir2/y,
/testdir/dir2/x/a, /testdir/dir2/x/b
mkdir /testdir/dir2/{x/{a,b},y} -pv
(3) 如何创建 / testdir/dir3, /testdir/dir4, /testdir/dir5, /testdir/dir5/dir6, /testdir/dir5/dir7
mkdir -pv /testdir/dir{3,4,5/dir{6,7}}
三, 重定向与管道
1, 将 / etc/issue 文件中的内容转换为大写后保存至 / tmp/issue.out 文件中
tr 'a-z' 'A-Z' </etc/issue> /tmp/issue.out
2, 将当前系统登录用户的信息转换为大写后保存至 / tmp/who.out 文件中
who | tr [a-z] [A-Z]> /tmp/who.out
3, 一个 linux 用户给 root 发邮件, 要求邮件标题为 "help", 邮件正文如下: Hello, I am 用户名, The system version is here,please help me to check it ,thanks!
操作系统版本信息
Mail -s "help" root <<EOF
>Hello, I am `whoami` | $USER ,The system version is here,please help me to check it ,thanks!
> Uname -r 系统内核信息
>Cat /etc/centos-release 操作系统版本信息
> EOF
4, 将 / root / 下文件列表, 显示成一行, 并文件名之间用空格隔开
ls/root/ | tr '\n' ' '
5, 计算 1+2+3+..+99+100 的总和
- [root@centos7 ~]#seq --separator="+" 1 100 | bc
- 5050
- [root@centos7 ~]#seq -s '+' 1 100 | bc
- 5050
- [root@centos7 ~]#echo {1..100} | tr """+" | bc
- 5050
- [root@centos7 ~]#
6, 删除 Windows 文本文件中的'^M'字符
tr -d '\r' <win.txt> linux.txt
tr -d '\15' win2.txt> linux2.txt
7, 处理字符串 "xt.,l 1 jr#!$mn 2 c*/fe 3 uz 4", 只保留其中的数字和空格
[root@centos7 ~]#echo 'xt.,l 1 jr#!$mn 2 c*/fe 3 uz 4' | tr -dc '[0-9] \n'
'xt.,l 1 jr#!$mn 2 c*/fe 3 uz 4' 单引号认为!$ 为普通字符 , 不认为它是上个命令的最后一个参数
8, 将 PATH 变量每个目录显示在独立的一行
echo $PATH | tr ':' '\n'
9, 将指定文件中 0-9 分别替代成 a-j
tr [0-9] [a-j] < testfile.txt
10, 将文件中每个单词 (由字母组成) 显示在独立的一行, 并无空行
tr -sc 'a-z 大 A 到 Z' '\n' < /etc/centos-release
四, 用户组和权限管理
1, 创建用户 gentoo, 附加组为 bin 和 root, 默认 shell 为 / bin/csh, 注释信息为 "Gentoo Distribution"
Useradd -G bin,root -s /bin/csh -c "Gentoo Distribution" gentoo
2, 创建下面的用户, 组和组成员关系
名字为 admins 的组
用户 natasha, 使用 admins 作为附属组
用户 harry, 也使用 admins 作为附属组
用户 sarah, 不可交互登录系统, 且不是 admins 的成员, natasha,harry,sarah 密码都是 centos
- groupadd admins;
- useradd -G admins natasha; useradd -G admins harry;
- useradd -s /sbin/nologin sarah ;
- echo centos | passwd --stdin natasha;
- echo centos | passwd --stdin harry;
- echo centos | passwd --stdin sarah;
3, 当用户 xiaoming 对 / testdir 目录无执行权限时, 意味着无法做哪些操作?
不能 cd 进入该目录 目录下的文件什么都不能做(即使文件有 777 最高权限)
4, 当用户 xiaoqiang 对 / testdir 目录无读权限时, 意味着无法做哪些操作?
不能查看目录的文件列表
5, 当用户 wangcai 对 / testdir 目录无写权限时, 该目录下的只读文件 file1 是否可修改和删除?
file1 文件是不能删除的, 不能修改
6, 当用户 wangcai 对 / testdir 目录有写和执行权限时, 该目录下的只读文件 file1 是否可修改和删除?
file1 文件可以删除, 但是不能修改
7, 复制 / etc/fstab 文件到 / var/tmp 下, 设置文件所有者为 wangcai 读写权限, 所属组为 sysadmins 组有读写权限, 其他人无权限
- cp /etc/fstab /var/tmp/ ;
- chmod
- u=rw,g=rw,o= fstab
- chown
- wangcai:sysadmins fstab
8, 误删除了用户 haha 的家目录, 请重建并恢复该用户家目录及相应的权限属性, 所有者
- cp -r /etc/skel /home/haha (复制目录并改名)
- chown -R haha:haha /home/haha
- chmod 700 /home/haha
haha 用户的所有者和所有组为 haha, 所以需更改
9, 在 / testdir/dir 里创建的新文件自动属于 g1 组, 组 g2 的成员如: alice 能对这些新文件有读写权限, 组 g3 的成员如: tom 只能对新文件有读权限, 其它用户 (不属于 g1,g2,g3) 不能访问这个文件夹.
- [root@centos7 app]#mkdir /testdir/dir
- [root@centos7 app]#groupadd g1
- [root@centos7 app]#groupadd g2
- [root@centos7 app]#groupadd g3
- [root@centos7 app]#chgrp g1 /testdir/dir
- [root@centos7 app]#chmod g+s /testdir/dir
创建用户并添加到相应组
[root@centos7 ~]#setfacl -m d:g:g2:rw,d:g:g3:r,o::- /testdir/dir (新创建目录不需要加 - R 递归设置默认权限)
sed 文本处理
1, 删除 centos7 系统 / etc/grub2.cfg 文件中所有以空白开头的行行首的空白字符
[root@centos7 ~]$ sed -nr 's@^([[:space:]]+)(.*)@\2@p' /etc/grub2.cfg
2, 删除 / etc/fstab 文件中所有以 #开头, 后面至少跟一个空白字符的行的行首的 #和空白字符
[root@centos7 ~]$ sed -nr 's@(^#[[:space:]])(.*)@\2@p' /etc/fstab
3, 在 centos6 系统 / root/install.log 每一行行首增加 #号
[root@centos6 ~]# sed -nr 's@.*@#\0@p' /root/install.log
4, 在 / etc/fstab 文件中不以 #开头的行的行首增加# 号
[root@centos6 ~]# sed -nr 's@^[^#].*@#\0@p' /etc/fstab
5, 处理 / etc/fstab 路径, 使用 sed 命令取出其目录名和基名
- [root@centos6 ~]# echo "/etc/fstab" |sed -nr 's@(.*/)([^/]+)/?@\2@p'
- fstab
- [root@centos6 ~]# echo "/etc/fstab/" |sed -nr 's@(.*/)([^/]+)/?@\2@p'
- fstab
- [root@centos6 ~]# echo "/etc/" |sed -nr 's@(.*/)([^/]+)/?@\1@p'
6, 利用 sed 取出 ifconfig 命令中本机的 IPv4 地址
[root@centos7 ~]$ ifconfig ens33 |sed -nr '2s@.*inet (.*) netmask.*@\1@p'
v7, 统计 centos 安装光盘中 Package 目录下的所有 rpm 文件的以. 分隔倒数第二个字段的重复次数
root@centos7 ~]$ ls /misc/cd/Packages/ |sed -nr 's@.*\.([^.]*)\.rpm@\1@p' |sort |uniq -c
- 2141 i686
- 3076 noarch
- 4374 x86_64
8, 统计 / etc/init.d/functions 文件中每个单词的出现次数, 并排序(用 grep 和 sed 两种方法分别实现)
sed 方法
[root@centos7 ~]#sed -r 's/[^[:alpha:]]/\n/g'
/etc/init.d/functions | sed '/^$/d' | uniq -c | sort -n | wc -l
grep 方法(注意非正则符号前加斜杠)
egrep -o "[[:alpha:]]+" /etc/init.d/functions | uniq -c | sort | wc -l
9, 将文本文件的 n 和 n+1 行合并为一行, n 为奇数行
- [root@centos7 ~]#seq 1 10 | sed 'N;s/\n//'
- 1 2
- 3 4
- 5 6
- 7 8
- 9 10
- [root@centos7 ~]#seq 1 10 | xargs -n3 (设置最多的参数个数)
- 1 2 3
- 4 5 6
- 7 8 9
- 10
来源: http://www.bubuko.com/infodetail-2569665.html