[使用帮助]
显示当前日期,格式: 2016-08-08 10:20:30
[root@CentOS7 ~]# date +"%F %H:%M:%S" 2016-07-31 23:15:12
显示前天是星期几
[root@CentOS7 ~]# date -d ‘-2 day‘ +%w 6
设置当前日期为2008-08-08 08:08
[root@CentOS7 ~]# date 080808082008 Fri Aug 8 08:08:00CST 2008
今天17: 30自动关机
[root@CentOS7 ~]# shutdown –h 17:30
字符终端登录时,显示当前登录终端号,主机名和当前时间
[root@CentOS7 ~]# cat /etc/issue CentOS release 6.8 (Final) Kernel \r on an \m The tty is: \l The hostname is: \n The time is: \t
[文件管理]
显示/var目录下所有以l开头,以一个小写字母结尾,且中间出现至少一位数字的文件或目录。
[root@CentOS7 ~]# ls /var/l*[0-9]*[[:lower:]] -rw-r--r--. 1 root root 0 Jul 28 15:07 /var/l34j -rw-r--r--. 1 root root 0 Jul 28 15:07 /var/l55h
显示/etc目录下以任意一位数字开头,且以非数字结尾的文件或目录。
[root@CentOS7 ~]# ll /etc/[[:digit:]]*[^[:digit:]] -rw-r--r--. 1 root root 0 Jul 28 15:23 /etc/13f
显示/etc/目录下以非字母开头,后面跟了一个字母及其它任意长度任意字符的文件或目录。
[root@CentOS7 ~]# ll/etc/[^[:alpha:]][[:alpha:]]* -rw-r--r--. 1 root root 0 Jul 28 15:27 /etc/1fg1
显示/etc目录下所有以m开头以非数字结尾的文件或目录。
[root@CentOS7 ~]# ll -d /etc/m*[^[:digit:]] -r--r--r--. 1 root root 33 Jul 21 11:27 /etc/machine-id -rw-r--r--. 1 root root 111 Nov 20 2015 /etc/magic -rw-r--r--. 1 root root 1968 Dec 17 2014 /etc/mail.rc -rw-r--r--. 1 root root 5122 Nov 21 2015 /etc/makedumpfile.conf.sample -rw-r--r--. 1 root root 5171 Jun 10 2014 /etc/man_db.conf drwxr-xr-x. 2 root root 6 Nov 21 2015 /etc/maven -rw-r--r--. 1 root root 936 Mar 6 2015 /etc/mke2fs.conf drwxr-xr-x. 2 root root 22 Jul 21 11:27 /etc/modprobe.d drwxr-xr-x. 2 root root 6 Nov 20 2015 /etc/modules-load.d -rw-r--r--. 1 root root 0 Jun 7 2013 /etc/motd lrwxrwxrwx. 1 root root 17 Jul 21 11:23 /etc/mtab -> /proc/self/mounts -rw-r--r--. 1 root root 2620 Jun 10 2014 /etc/mtools.conf drwxr-xr-x. 2 root root 6 Nov 20 2015 /etc/multipath -rw-r--r--. 1 root root 570 Nov 21 2015 /etc/my.cnf drwxr-xr-x. 2 root root 30 Jul 21 11:25 /etc/my.cnf.d
显示/etc目录下,所有以.d结尾的文件或目录。
[root@CentOS7 ~]# ll -d /etc/*.d drwxr-xr-x. 2 rootroot 4096 Jul 21 11:35 /etc/bash_completion.d drwxr-xr-x. 2 rootroot 6 Nov 20 2015 /etc/binfmt.d drwxr-xr-x. 2 rootroot 6 Nov 20 2015 /etc/chkconfig.d drwxr-xr-x. 2 rootroot 51 Jul 21 11:32 /etc/cron.d drwxr-xr-x. 2 rootroot 22 Jul 21 11:27 /etc/depmod.d drwxr-xr-x. 2 rootroot 6 Aug 6 2015/etc/dnsmasq.d drwxr-xr-x. 2 rootroot 30 Jul 28 20:22 /etc/dracut.conf.d drwxr-xr-x. 2 rootroot 6 Nov 20 2015 /etc/exports.d drwxr-xr-x. 2 rootroot 6 Sep 11 2015 /etc/gdbinit.d drwx------. 2 rootroot 4096 Jul 21 11:41 /etc/grub.d lrwxrwxrwx. 1 rootroot 11 Jul 21 11:24 /etc/init.d ->rc.d/init.d drwx------. 5 rootroot 4096 Jul 21 11:28 /etc/ipsec.d … …
显示/etc目录下,所有.conf结尾,且以m,n,r,p开头的文件或目录。
[root@CentOS7 ~]# ll /etc/[mnrp]*.conf -rw-r--r--.1 root root 5171 Jun 10 2014/etc/man_db.conf -rw-r--r--.1 root root 936 Mar 6 2015/etc/mke2fs.conf -rw-r--r--.1 root root 2620 Jun 10 2014/etc/mtools.conf -rw-r--r--.1 root root 3390 Nov 20 2015/etc/nfsmount.conf -rw-r--r--.1 root root 1717 Jul 30 21:07 /etc/nsswitch.conf -rw-r--r--.1 root root 91 Dec 3 2012/etc/numad.conf -rw-r--r--.1 root root 1362 Jun 10 2014/etc/pbm2ppa.conf -rw-r--r--.1 root root 6300 Jun 10 2014/etc/pnm2ppa.conf -rw-r--r--.1 root root 433 Sep 9 2015/etc/radvd.conf -rw-r--r--.1 root root 1787 Jun 10 2014/etc/request-key.conf -rw-r--r--.1 root root 138 Jul 31 21:14/etc/resolv.conf -rw-r--r--.1 root root 458 Nov 21 2015 /etc/rsyncd.conf -rw-r--r--.1 root root 3232 Sep 8 2015 /etc/rsyslog.conf
使用别名命令,每日将/etc/目录下所有文件,备份到/testdir/下独立的新目录下,并要求新目录格式为 backupYYYY-mm-dd ,备份过程可见
[root@CentOS7 ~]# alias tbak="cp -rpv /etc/ /testdir/backup`date +%F`" [root@CentOS7 ~]# alias tbak alias tbak=‘cp-rpv /etc/ /testdir/backup2016-07-28 [root@CentOS7 ~]# tbak … … [root@CentOS7 ~~]# ll -d /testdir/backup* drwxr-xr-x.6 root root 8192 Jul 30 09:10 /testdir/backup2016-07-28
先创建/testdir/rootdir目录,再复制/root所有下文件到该目录内,并要求保留原有权限
[root@CentOS7 ~]# mkdir /testdir/rootdir [root@CentOS7 ~]# ls -ld /root/ dr-xr-x---.15 root root 4096 Jul 31 22:40 /root/ [root@CentOS7 ~]# cp -prv /root/ /testdir/rootdir … … [root@CentOS7 ~]# ls -ld /testdir/rootdir/ dr-xr-x---. 15 root root 4096 Jul 31 22:40 /testdir/rootdir/
如何创建/testdir/dir1/x,/testdir/dir1/y,/testdir/dir1/x/a, /testdir/dir1/x/b,/testdir/dir1/y/a,/testdir/dir1/y/b
[rootCentOS7 ~]# mkdir -pv/testdir/dir1/{x,y}/{a,b} mkdir: created directory ‘/testdir/dir1’ mkdir: created directory ‘/testdir/dir1/x’ mkdir: created directory ‘/testdir/dir1/x/a’ mkdir: created directory ‘/testdir/dir1/x/b’ mkdir: created directory ‘/testdir/dir1/y’ mkdir: created directory ‘/testdir/dir1/y/a’ mkdir: created directory ‘/testdir/dir1/y/b’ [root@localhost testdir]# tree /testdir/dir1/ /testdir/dir1/ ├── x │ ├── a │ └── b └── y ├──a └──b 6 directories, 0 files
如何创建/testdir/dir2/x,/testdir/dir2/y,/testdir/dir2/x/a, /testdir/dir2/x/b
[root@CentOS7 ~]# mkdir -pv /testdir/dir2/{y,x/{a,b}} mkdir: created directory ‘/testdir/dir2’ mkdir: created directory ‘/testdir/dir2/y’ mkdir: created directory ‘/testdir/dir2/x’ mkdir: created directory ‘/testdir/dir2/x/a’ mkdir: created directory ‘/testdir/dir2/x/b’ [root@CentOS7 ~]# tree /testdir/dir2 /testdir/dir2 ├── x │ ├── a │ └── b └── y 4 directories, 0 files
如何创建/testdir/dir3, /testdir/dir4,/testdir/dir5,/testdir/dir5/dir3, /testdir/dir5/dir4
[root@CentOS7 ~]# mkdir -pv /testdir/dir{3,4,5/dir{6,7}} mkdir: created directory ‘/testdir/dir3’ mkdir: created directory ‘/testdir/dir4’ mkdir: created directory ‘/testdir/dir5’ mkdir: created directory ‘/testdir/dir5/dir6’ mkdir: created directory ‘/testdir/dir5/dir7’ [root@CentOS7 ~]# tree /testdir/ /testdir/dir3 /testdir/dir4 /testdir/dir5 ├── dir6 └── dir7 2 directories, 0 files
[重定向和管道]
将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中
[root@CentOS7 ~]# tr ‘a-z‘ ‘A-Z‘ < /etc/issue >/tmp/issue.out [root@CentOS7 ~]# cat /etc/issue tmp/issue.out CentOS release7.2 (Final) Kernel \r onan \m This systemis: \s The time is:\t The hostnameis: \n The tty is: \l CENTOS RELEASE7.2 (FINAL) KERNEL \R ONAN \M THIS SYSTEMIS: \S THE TIME IS:\T THE HOSTNAMEIS: \N THE TTY IS: \L
将当前系统登录用户的信息转换为大写后保存至/tmp/who.out文件中
[root@CentOS7 ~]# w | tr ‘a-z‘ ‘A-Z‘ > /tmp/who.out [root@CentOS7 ~]# cat /tmp/who.out 12:51:51 UP 20:18, 9 USERS, LOAD AVERAGE: 0.00, 0.03, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT ROOT :0 :0 WED21 ?XDM? 11:45 0.27S GDM-SESSION-WORKER[PAM/GDM-AUTOLOGIN] ROOT PTS/0 :0 WED21 18:21M 1.63S 0.36S -BASH ROOT PTS/1 :0 THU11 16:38M 0.17S 0.00S SYSTEMCTL --HELP ROOT TTY2 THU20 16:36M 12.49S 0.00S XINIT /ETC/X11/XINIT/XINITRC --/USR/BIN/X :2 VT2 -KEEPTTY -AUTH /ROOT/.SERVERAUTH.52239 ROOT PTS/2 :2 THU20 16:26M 0.10S 0.10S BASH ROOT PTS/3 :2 THU20 16:26M 0.02S 0.02S BASH ROOT PTS/4 :2 THU20 9:11 0.10S 0.10S BASH ROOT PTS/6 10.1.250.75 08:29 7.00S 0.09S 0.00S W ROOT PTS/7 10.1.250.75 08:29 4:21M 0.04S 0.02S -BASH
一个linux用户给root发邮件,要求邮件标题为”help”,邮件正文如下:
Hello, I am 用户名,the system versionis here,please help me to
check it ,thanks!
操作系统版本信息
[root@CentOS7 ~]# mail -s "help" root <<end > Hello, Iam `whoami`, the system version is here, please help me to check it, thanks! > `cat /etc/centos-release` > end [root@CentOS7 ~]# mail Heirloom Mailversion 12.5 7/5/10. Type ? for help. "/var/spool/mail/root":11 messages 1 new 2 unread >N 11root Sat Jul 3016:47 19/714 "help" & 11 Message 11: From root@CentOS7 Sat Jul 30 16:47:45 2016 Return-Path:<root@CentOS7 > X-Original-To:root Delivered-To:root@CentOS7 Date: Sat, 30Jul 2016 16:47:45 +0800 To:root@CentOS7 Subject: help User-Agent:Heirloom mailx 12.5 7/5/10 Content-Type:text/plain; charset=us-ascii From:root@CentOS7 (root) Status: R Hello, I amroot, the system version is here, please help me to check it, thanks! CentOS Linuxrelease 7.2.1511 (Core)
将/root/下文件列表,显示成一行,并文件名之间用空格隔开
[root@CentOS7 ~]# ls /root/ | tr ‘\n‘ ‘ ‘ 1 121 21 all.txt anaconda-ks.cfg Desktop Documents Downloads f1file1 filetype h.1 -h.bak initial-setup-ks.cfg issue Music Pictures PublicTemplates test test2 test.f tr Videos VMwareTools-10.0.0-2977863.tar.gzvmware-tools-distrib who.out
file1文件的内容为:”1 2 3 45 6 7 8 9 10” 计算出所有数字的总和
[root@CentOS7 ~]# cat file1 1 2 3 4 5 6 7 8 9 10 [root@CentOS7 ~]# cat file1 | tr ‘ ‘ ‘+‘ 1+2+3+4+5+6+7+8+9+10 [root@CentOS7 ~]# cat file1 | tr ‘ ‘ ‘+‘ | bc 55
删除Windows文本文件中的‘^M‘字符
[root@CentOS7 ~]# cat -v /root/Desktop/1.txt | tr -d ‘^M‘ >/root/Desktop/1 [root@CentOS7 ~]# cat -v /root/Desktop/1.txt 1^M 2^M 3^M 4^M 5[root@CentOS7 ~]# cat -v /root/Desktop/1 1 2 3 4 5
处理字符串“ xt.,l 1 jr#!$mn2 c*/fe3 uz4”,只保留其中的数字和空格
[root@CentOS7 ~]# tr -dc ‘[:digit:][:space:]‘ xt.,l 1 jr#!$mn2 c*/fe3 uz4 1 2 3 4
将PATH变量每个目录显示在独立的一行
[root@CentOS7 ~]# echo $PATH /usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin [root@localhost boot]# echo $PATH | tr ‘:‘ ‘\n‘ /usr/local/bin /usr/local/sbin /usr/bin /usr/sbin /bin /sbin /root/bin
删除指定文件的空行
[root@CentOS7 ~]# cat test Hello world! China! good! [root@CentOS7 ~]# tr -s ‘\n‘ < test Hello world! China! good!
将文件中每个单词(字母)显示在独立的一行,并无空行
[root@CentOS7 ~]# cat test Hello world! China! good! world line user root # $ [root@CentOS7 ~]# cat test | tr ‘ ‘ ‘\n‘ | tr -s ‘\n‘ Hello World! China! good! world line user root # $
[用户组和权限管理]
创建用户gentoo,附加组为bin和root,默认shell为/bin/csh,注释信息为"Gentoo Distribution":
[root@CentOS7 ~]# useradd gentoo -G bin,root -s /bin/csh -c"Gentoo Distribution" [root@CentOS7 ~]# getent passwd Gentoo gentoo:x:1001:1001:Gentoo Distribution:/home/gentoo:/bin/csh [root@CentOS7 ~]# id gentoo uid=1001(gentoo) gid=1001(gentoo) groups=1001(gentoo),0(root),1(bin)
创建下面的用户、组和组成员关系:
名字为admins 的组
用户natasha,使用admins 作为附属组
用户harry,也使用admins 作为附属组
用户sarah,不可交互登录系统,且不是admins 的成员,natasha,harry,sarah密码都是centos
[root@CentOS7 ~]# groupadd admins [root@CentOS7 ~]# getent group admins admins:x:1002:harry [root@CentOS7 ~]# useradd -G admins Natasha [root@CentOS7 ~]# id natasha uid=1004(natasha) gid=1005(natasha)groups=1005(natasha),1002(admins) [root@CentOS7 ~]# useradd -G admins harry [root@CentOS7 ~]# id harry uid=1002(harry) gid=1003(harry) groups=1003(harry),1002(admins) [root@CentOS7 ~]# useradd -s /bin/nologin sarah [root@CentOS7 ~]# getent passwd sarah sarah:x:1003:1004::/home/sarah:/bin/nologin [root@CentOS7 ~]# echo centos | passwd --stdin natasha Changing password for user natasha. passwd: all authentication tokens updated successfully. [root@CentOS7 ~]# echo centos | passwd --stdin harry Changing password for user harry. passwd: all authentication tokens updated successfully. [root@CentOS7 ~]# echo centos | passwd --stdin sarah Changing password for user sarah. passwd: all authentication tokens updated successfully.
设置完用户的密码后进入到字符界面测试一下,natasha和harry均能正常登录,sarah的登录shell设置为nologin,所以当输入完成用户名和密码后又重新返回到登录界面了。
本文出自 “Linux路上” 博客,请务必保留此出处http://dreamlinuxc.blog.51cto.com/5733156/1832987
原文地址:http://dreamlinuxc.blog.51cto.com/5733156/1832987