Linux基本操作实验(5)

平常在LINUX上进行操作,操作最多接触的就是SHELL,而在SHELL中最多接触的就是文件,我们通常需要不停的查找、查看、编辑等。文件在LINUX中命名是有一定规则的。如果快速的定位到一个文件名。或者说模糊匹配一个文件名,应该怎么做。比喻说我们只记得曾经写过一个search****fILE.c 这个文件,但不知道具体的名字,那这个时候需要用到的知道,就是模糊匹配,在LINUX中对文件名有通配符和元字符之说,通配符和元字符与正则表达式中相关符号可能相近,但表示的意义是不一样,需要注意。这里总结如下:

* 匹配文件名中的任何字符串,包括空字符串,或者当前目录下所有文件 ls app*
ls *.doc
ls cl*.sed
cd cron.w*
列出所有以app开头的文件
列出以.doc结尾的所有文件
列出以cl中间任意字符(个数不定)并以.sed结尾的字符
进入以cron.w*开头的目录,注意只能一个,如果多个会失败。
匹配文件名中的单个字符串 ls ??R*
ls conf??.log
ls f??*s
列出R前有任意两个字符的,R后有任意多个字符的文件
列出以conf开头,中间两个任意字 符,后以.log结尾
列出以f开头,中间任意两个字符,后接任意多个字符并以s结尾的文件
[...] 匹配[]中包含的任意字符 ls [io]*
ls log.[0-9]*
 
[!...] 不匹配[]中!后的任意字符 ls log.[!0-9]*  

这些通配符与正则中有一些不一样,需要注意。

[root@windriver-machine shtest]# ls *3*
1234  1234.txt

123abc:
123abc.txt
[root@windriver-machine shtest]# ls *abc
123abc.txt
[root@windriver-machine shtest]# ls *abc*
123abc.txt
[root@windriver-machine shtest]# ls -latr *abc*
total 8
-rwxr-xr-x 1 windriver windriver    0 2011-08-06 21:24 123abc.txt
drwsr-sr-x 2 windriver windriver 4096 2011-08-06 21:24 .
drwxrwxr-x 3 windriver windriver 4096 2011-08-07 02:15 ..
[root@windriver-machine shtest]# ll
total 28
lrwxrwxrwx 1 root      root         8 2011-08-06 22:07 1234 -> 1234.txt
-rw-r--r-- 1 root      root         0 2011-08-07 01:43 1234.txt
drwsr-sr-x 2 windriver windriver 4096 2011-08-06 21:24 123abc
-rw-rw-r-- 2 windriver windriver   74 2011-08-06 22:11 errors
-rwxrwxr-x 1 windriver windriver  120 2011-08-06 01:44 ex1.sh
-rw-rw-r-- 2 windriver windriver   74 2011-08-06 22:11 newerr
-rw-rw-r-- 1 windriver windriver 1640 2011-08-06 19:13 output
-rw-r--r-- 1 root      root        31 2011-08-07 02:14 testoutput
-rw-r--r-- 1 root      root       169 2011-08-07 02:15 testoutput2
[root@windriver-machine shtest]# ll -d *abc*
drwsr-sr-x 2 windriver windriver 4096 2011-08-06 21:24 123abc
[root@windriver-machine shtest]#
[root@windriver-machine shtest]# ls ????r?
errors  newerr
[root@windriver-machine shtest]# ls [e]*
errors  ex1.sh
[root@windriver-machine shtest]# ls [0-9][0-9][0-9][A-Za-z]*
123abc.txt
[root@windriver-machine shtest]#

学习了如何模糊匹配文件,这里我们继续学习几个常用的文件操作命令。

【1】echo echo可以向屏幕输出。 echo [-e] [-n] STRING 其中STRING是要输出的字符串,其中可以包含Shell变量名、转义符等,一般用双引号括起来。缺省情况下echo不解释STRING中的转义符,加上-e 才可,缺省情况,echo 自动输出NEWLINE,加上-n 不输出换行符NEWLINE。

[root@windriver-machine shtest]# echo "\007you home is $HOME,you are connected on `tty`"
\007you home is /root,you are connected on /dev/pts/0
[root@windriver-machine shtest]# echo -e "\007you home is $HOME,you are connected on `tty`"
you home is /root,you are connected on /dev/pts/0
[root@windriver-machine shtest]# echo Hello world
Hello world
[root@windriver-machine shtest]# echo "Hello world"
Hello world
[root@windriver-machine shtest]# w
22:36:57 up 87 days, 23:16,  1 user,  load average: 0.00, 0.00, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
windrive pts/0    :2.0             Sat19    1.00s  0.28s  6.56s gnome-terminal
[root@windriver-machine shtest]# echo -e "Here is a tab\there are two tabs\t\tOK"
Here is a tab   here are two tabs               OK
[root@windriver-machine shtest]# echo "\"/dev/rmt0\""
"/dev/rmt0"
[root@windriver-machine shtest]# echo "The log files have all been done" >myfile
[root@windriver-machine shtest]# cat myfile
The log files have all been done
[root@windriver-machine shtest]#

【2】read 可以使用read语句从键盘或者文件中的某一行文本中读入信息,并将其赋给一个变量。

[root@windriver-machine shtest]# read -p "How old r u" age
How old r u32
[root@windriver-machine shtest]# echo $age
32
[root@windriver-machine shtest]# read -p "some words?" -a words
some words?I am a boy
[root@windriver-machine shtest]# echo ${word[*]}

[root@windriver-machine shtest]# echo ${words[*]}
I am a boy
[root@windriver-machine shtest]# read -p "Passowrd:" -s passwd
Passowrd:[root@windriver-machine shtest]# echo $passwd
teetesetbpadasdf

[root@windriver-machine shtest]# read -n
-bash: read: -n: option requires an argument
read: usage: read [-ers] [-u fd] [-t timeout] [-p prompt] [-a array] [-n nchars] [-d delim] [name ...]
[root@windriver-machine shtest]# read -t 5 auth
sdfasdfas
[root@windriver-machine shtest]# echo $auth
sdfasdfas
[root@windriver-machine shtest]# read -n 1 key
1[root@windriver-machine shtest]#
[root@windriver-machine shtest]# echo $key
1
[root@windriver-machine shtest]# read -dq -p "imput something end with q:" menu
imput something end with q:wobiq[root@windriver-machine shtest]# echo $menu
wobi
[root@windriver-machine shtest]# read -e myfile
hello
[root@windriver-machine shtest]# cat myfile
The log files have all been done
[root@windriver-machine shtest]# read -e myfile
ll
[root@windriver-machine shtest]# read -e myfile  testfile

[root@windriver-machine shtest]#

【3】cat 显示文件内容,它显示内容与more和head命令不同,它不会分页显示,而是全显示,当然可以通过管道输出给more或head,less等。

image

[root@windriver-machine shtest]# cat myfile
The log files have all been done
[root@windriver-machine shtest]# read -e myfile
ll
[root@windriver-machine shtest]# read -e myfile  testfile

[root@windriver-machine shtest]# ls
1234  1234.txt  123abc  errors  ex1.sh  myfile  newerr  output  testoutput  testoutput2
[root@windriver-machine shtest]# cp myfile myfile1
[root@windriver-machine shtest]# cat myfile
The log files have all been done
[root@windriver-machine shtest]# cat myfile myfile1
The log files have all been done
The log files have all been done
[root@windriver-machine shtest]# cat
Hello world
Hello world
hello world
hello world
[root@windriver-machine shtest]# cat >myfile2
This is great
[root@windriver-machine shtest]# cat myfile2
This is great
[root@windriver-machine shtest]# cat -n /etc/passwd
     1  root:x:0:0:root:/root:/bin/bash
     2  bin:x:1:1:bin:/bin:/sbin/nologin
     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4  adm:x:3:4:adm:/var/adm:/sbin/nologin
     5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     6  sync:x:5:0:sync:/sbin:/bin/sync
     7  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
     8  halt:x:7:0:halt:/sbin:/sbin/halt
     9  mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10  news:x:9:13:news:/etc/news:

[root@windriver-machine shtest]# cat -n myfile
     1  The log files have all been done
[root@windriver-machine shtest]# cat -n myfile1 >myfile
myfile   myfile1  myfile2 
[root@windriver-machine shtest]# cat -n myfile1 >myfile3
[root@windriver-machine shtest]# cat myfile3
     1  The log files have all been done
[root@windriver-machine shtest]# vi myfile1
[root@windriver-machine shtest]# cat -n myfile1
     1  The log files have all been done
     2
     3
     4  This is other line to do verify work
[root@windriver-machine shtest]# cat -b myfile1
     1  The log files have all been done

     2  This is other line to do verify work
[root@windriver-machine shtest]# cat myfile2
This is great
[root@windriver-machine shtest]# cat /dev/null >myfile2
[root@windriver-machine shtest]# cat myfile2
[root@windriver-machine shtest]#

【4】tee  会从标准输入设备读取数据,并将其内容输出到标准输出设备,同时保存成文件。 �Ca 附加到现有文件中。-i 忽略中断信号。

[root@windriver-machine shtest]# cat myfile1|tee myfile1.bak1 myfile1.bak2 myfile1.bak3
The log files have all been done

This is other line to do verify work
This is tab\tcontrol
[root@windriver-machine shtest]# ll
total 52
lrwxrwxrwx 1 root      root         8 2011-08-06 22:07 1234 -> 1234.txt
-rw-r--r-- 1 root      root         0 2011-08-07 01:43 1234.txt
drwsr-sr-x 2 windriver windriver 4096 2011-08-06 21:24 123abc
-rw-rw-r-- 2 windriver windriver   74 2011-08-06 22:11 errors
-rwxrwxr-x 1 windriver windriver  120 2011-08-06 01:44 ex1.sh
-rw-r--r-- 1 root      root        33 2011-08-09 22:39 myfile
-rw-r--r-- 1 root      root        94 2011-08-09 23:08 myfile1
-rw-r--r-- 1 root      root        94 2011-08-09 23:12 myfile1.bak1
-rw-r--r-- 1 root      root        94 2011-08-09 23:12 myfile1.bak2
-rw-r--r-- 1 root      root        94 2011-08-09 23:12 myfile1.bak3
-rw-r--r-- 1 root      root         0 2011-08-09 23:06 myfile2
-rw-r--r-- 1 root      root        40 2011-08-09 23:05 myfile3
-rw-rw-r-- 2 windriver windriver   74 2011-08-06 22:11 newerr
-rw-rw-r-- 1 windriver windriver 1640 2011-08-06 19:13 output
-rw-r--r-- 1 root      root        31 2011-08-07 02:14 testoutput
-rw-r--r-- 1 root      root       169 2011-08-07 02:15 testoutput2
[root@windriver-machine shtest]#

【5】exec 系统环境都会被清除,重新启动一个shell,这个通常是用来执行一个脚本命令。

【6】|和&gt; &gt;&gt; < &lt;&lt; &  管道和重定向,这些前面已经说过,不在重复。

【7】date �Cs 设置系统时间 data �Cs 11/21/08  date �Cs 22:12:00,设置时间可以两次设置,也可一次性设置 date 112122202008.30  格式 月 日 时 分 年.秒 。虚拟终端时间与CMOS时间可能不一样,CMOS时间可以使用hwclock �Cshow 或clock --show来显示。

[windriver@windriver-machine shtest]$ date
Tue Aug  9 23:26:04 CST 2011
[windriver@windriver-machine shtest]$ date -s 08/08/2011
date: cannot set date: Operation not permitted
Mon Aug  8 00:00:00 CST 2011
[windriver@windriver-machine shtest]$ su - root
Password:
[root@windriver-machine ~]# date -s 08/08/2011
Mon Aug  8 00:00:00 CST 2011
[root@windriver-machine ~]#
[root@windriver-machine ~]#
[root@windriver-machine ~]# date -s 23:28:00
Mon Aug  8 23:28:00 CST 2011

[root@windriver-machine ~]#
[root@windriver-machine ~]#
[root@windriver-machine ~]# date
Mon Aug  8 23:28:03 CST 2011
[root@windriver-machine ~]# hwclock
Tue 09 Aug 2011 11:40:28 PM CST  -0.009747 seconds
[root@windriver-machine ~]# hwclock --show
Tue 09 Aug 2011 11:42:25 PM CST  -0.000300 seconds
[root@windriver-machine ~]# clock --show
Tue 09 Aug 2011 11:42:28 PM CST  -0.000329 seconds
[root@windriver-machine ~]# date
Mon Aug  8 23:30:23 CST 2011
[root@windriver-machine ~]#

设置硬件时钟 hwclock �Cset -date=”09/17/2003 13:26:00”

同步系统时钟与硬件时钟  hwclock �C-hctosys

clock �C-hctosys  表示硬件时钟同步到系统时钟或者

hwclock �C-systohc 或clock �Csystohc 或者hwclock -w写入硬件时钟CMOS

[root@windriver-machine ~]# date
Mon Aug  8 23:30:23 CST 2011
[root@windriver-machine ~]# hwclock --hctosys

[root@windriver-machine ~]#
[root@windriver-machine ~]#
[root@windriver-machine ~]#
[root@windriver-machine ~]#
[root@windriver-machine ~]# date
Tue Aug  9 23:45:47 CST 2011
[root@windriver-machine ~]# date 080923312011.30
Tue Aug  9 23:31:30 CST 2011
[root@windriver-machine ~]#
[root@windriver-machine ~]#
[root@windriver-machine ~]# clock --show
Tue 09 Aug 2011 11:46:54 PM CST  -0.000331 seconds
[root@windriver-machine ~]# hwclock -w
[root@windriver-machine ~]# hwclock --show
Tue 09 Aug 2011 11:32:01 PM CST  -0.000321 seconds
[root@windriver-machine ~]# date
Tue Aug  9 23:32:08 CST 2011
[root@windriver-machine ~]#

【8】&&和|| 将多个shell命令同时执行。&&表明前一个命令执行成功,而||通常意味着前一个命令执行失败。

[root@windriver-machine shtest]# sort myfile1 >myfile4 && echo "if you are seeing this, then sort is ok"
if you are seeing this, then sort is ok
[root@windriver-machine shtest]# cp /etc/passwd passwd.bak && echo "if you are seeing this,the cp is ok"
if you are seeing this,the cp is ok

[root@windriver-machine shtest]# cp /etc/passwd passwd.bak ||  echo "if you are seeing this,the cp is ok"
cp: overwrite `passwd.bak'? y
[root@windriver-machine shtest]#

你可能感兴趣的:(linux,职场,休闲)