Linux基礎練習


顯示當前時間,格式: 2016-06-18 10:20:30

[root@CentOS7  ~]# date +"%F %H:%M:%S"

2016-07-31 23:15:12

 

顯示前天是星期幾

[root@CentOS7  ~]# date -d '-2 day' +%w

6

 

設置當前日期爲2019-08-07 06:05:10

[root@CentOS7  ~]# date 080808082008

Fri Aug  8 08:08:00CST 2008

 


字符終端登錄時,顯示當前登錄終端, 主機名和當前時間

[root@localhost ~]# 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

wKioL1fJp2nC8jBLAAA-wJp7iv4365.png-wh_50 

 

今天1730自動關機

[root@localhost ~]# shutdown –h 17:30

 

 

顯示/var目錄下所有以l開頭,以一個小寫字母結尾,且中間出現至少一位數字的文件或目錄

[root@localhost ~]# ls -l /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@localhost ~]# ll /etc/[[:digit:]]*[^[:digit:]]

-rw-r--r--. 1 root root 0 Jul 28 15:23 /etc/13f

 

顯示/etc/目錄下以非字母開頭,後面跟了一個字母及其它任意長度任意字符的文件或目錄

[root@localhost etc]# ll /etc/[^[:alpha:]][[:alpha:]]*

-rw-r--r--. 1 root root 0 Jul 28 15:27 /etc/1fg1

 

顯示/etc目錄下所有以m開頭以非數字結尾的文件或目錄

[root@localhost etc]# 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@localhost etc]# ll -d  /etc/*.d

drwxr-xr-x.  2 root root 4096 Jul 21 11:35 /etc/bash_completion.d

drwxr-xr-x.  2 root root    6 Nov 20  2015 /etc/binfmt.d

drwxr-xr-x.  2 root root    6 Nov 20  2015 /etc/chkconfig.d

drwxr-xr-x.  2 root root   51 Jul 21 11:32 /etc/cron.d

drwxr-xr-x.  2 root root   22 Jul 21 11:27 /etc/depmod.d

drwxr-xr-x.  2 root root    6 Aug  6  2015 /etc/dnsmasq.d

drwxr-xr-x.  2 root root   30 Jul 28 20:22 /etc/dracut.conf.d

drwxr-xr-x.  2 root root    6 Nov 20  2015 /etc/exports.d

drwxr-xr-x.  2 root root    6 Sep 11  2015 /etc/gdbinit.d

drwx------.  2 root root 4096 Jul 21 11:41 /etc/grub.d

lrwxrwxrwx.  1 root root   11 Jul 21 11:24 /etc/init.d -> rc.d/init.d

drwx------.  5 root root 4096 Jul 21 11:28 /etc/ipsec.d

… …

 

顯示/etc目錄下,所有.conf結尾,且以m,n,r,p開頭的文件或目錄

[root@localhost etc]# 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

 

 


定義別名命令baketc,每天將/etc/目錄下所有文件,備份到/testdir獨立的子目錄下, 並要求子目錄格式爲 backupYYYY-mm-dd ,備份過程可見

[root@localhost ~]# alias baketc="cp -rpv /etc/ /testdir/backup`date +%F`"

[root@localhost ~]# alias baketc

alias baketc='cp -rpv /etc/ /testdir/backup2016-07-28

[root@localhost ~]# baketc

`/etc/' -> `/testdir/backup2016-09-02/etc'

`/etc/dracut.conf' -> `/testdir/backup2016-09-02/etc/dracut.conf'

`/etc/rc.sysinit' -> `/testdir/backup2016-09-02/etc/rc.sysinit'

`/etc/ld.so.cache' -> `/testdir/backup2016-09-02/etc/ld.so.cache'

`/etc/dbus-1' -> `/testdir/backup2016-09-02/etc/dbus-1'

`/etc/dbus-1/system.d' -> `/testdir/backup2016-09-02/etc/dbus-1/system.d'

`/etc/dbus-1/system.d/Upstart.conf' -> `/testdir/backup2016-09-02/etc/dbus-1/system.d/Upstart.

conf'

`/etc/hosts' -> `/testdir/backup2016-09-02/etc/hosts'

......

[root@localhost ~]# ll -d /testdir/backup*

drwxr-xr-x. 6 root root 8192 Jul 30 09:10 /testdir/backup2016-07-28

 

創建/testdir/rootdir目錄並複製/root所有下文件到該目錄內,要求保留原有權限

[root@localhost ~]# mkdir /testdir/rootdir

[root@localhost ~]# ls -ld /root/

dr-xr-x---. 15 root root 4096 Jul 31 22:40 /root/

[root@localhost ~]# cp -prv /root/ /testdir/rootdir

… …

[root@localhost ~]# 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

[root@localhost testdir]# 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@localhost testdir]# 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@localhost testdir]# tree /testdir/dir2

/testdir/dir2

├── x

│   ├── a

│   └── b

└── y

4 directories, 0 files

 

如何創建/testdir/dir3, /testdir/dir4, /testdir/dir5,/testdir/dir5/dir6, /testdir/dir5/dir7

[root@localhost testdir]# 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@localhost testdir]# 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 release 7.2 (Final)

Kernel \r on an \m

This system is: \s

The time is: \t

The hostname is: \n

The tty is: \l

 

CENTOS RELEASE 7.2 (FINAL)

KERNEL \R ON AN \M

THIS SYSTEM IS: \S

THE TIME IS: \T

THE HOSTNAME IS: \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 version is here,please help me to

check it ,thanks!

操作系統版本信息

[root@CentOS7 ~]# mail -s "help" root <<end

> Hello, I am `whoami`, the system version is here, please help me to check it, thanks!

> `cat /etc/centos-release`

> end

 

[root@localhost ~]# mail

Heirloom Mail version 12.5 7/5/10.  Type ? for help.

"/var/spool/mail/root": 11 messages 1 new 2 unread

>N 11 root                  Sat Jul 30 16:47  19/714   "help"

& 11

Message 11:

From [email protected]  Sat Jul 30 16:47:45 2016

Return-Path: <[email protected]>

X-Original-To: root

Delivered-To: [email protected]

Date: Sat, 30 Jul 2016 16:47:45 +0800

To: [email protected]

Subject: help

User-Agent: Heirloom mailx 12.5 7/5/10

Content-Type: text/plain; charset=us-ascii

From: [email protected] (root)

Status: R

 

Hello, I am root, the system version is here, please help me to check it, thanks!

CentOS Linux release 7.2.1511 (Core)

 

/root/下文件列表,顯示成一行,並文件名之間用空格隔開

[root@localhost ~]# ls /root/ | tr '\n' ' '

1 121 21 all.txt anaconda-ks.cfg Desktop Documents Downloads f1 file1 filetype h.1 -h.bak initial-setup-ks.cfg issue Music Pictures Public Templates test test2 test.f tr Videos VMwareTools-10.0.0-2977863.tar.gz vmware-tools-distrib who.out

 

file1文件的內容爲: ”1 2 3 4 5 6 7 8 9 10” 計算出所有數字的總和

[root@localhost ~]# cat file1

1 2 3 4 5 6 7 8 9 10

[root@localhost ~]# cat file1 | tr ' ' '+'

1+2+3+4+5+6+7+8+9+10

[root@localhost ~]# cat file1 | tr ' ' '+' | bc

55

 

刪除Windows文本文件中的‘^M’字符

[root@localhost ~]# cat -v /root/Desktop/1.txt | tr -d '^M' > /root/Desktop/1

[root@localhost ~]# cat -v /root/Desktop/1.txt  

1^M

2^M

3^M

4^M

5[root@localhost ~]# cat -v /root/Desktop/1  

1

2

3

4

5

 

處理字符串“ xt.,l 1 jr#!$mn 2 c*/fe 3 uz 4”,只保留其中的數字和空格

[root@localhost ~]# tr -dc '[:digit:][:space:]'

xt.,l 1 jr#!$mn2 c*/fe3 uz4

 1 2 3 4

 

PATH變量每個目錄顯示在獨立的一行

[root@localhost boot]# 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!

 

將指定文件中0-9分別替代成a-j

 

將文件中每個單詞(字母)顯示在獨立的一行,並無空行

[root@localhost testdir]# cat test

Hello world!

 

China!

 

 

good!

world line user root # $

[root@localhost ~]# cat test | tr  ' ' '\n' | tr -s '\n'

Hello

World!

China!

good!

world

line

user

root

#

$

 

創建用戶gentoo,附加組爲binroot,默認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 的成員,natashaharrysarah密碼都是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.

 

設置完用戶的密碼後進入到字符界面測試一下,natashaharry均能正常登錄,sarah的登錄shell設置爲nologin,所以當輸入完成用戶名和密碼後又重新返回到登錄界面了。

wKioL1fJqRWzvdtEAAEbZJIniiI595.png-wh_50

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章