Unix Shell 学习笔记(一)

Simple life posted @ Fri, 14 Aug 2009 19:44:23 +0800 in Unix Shell with tags unix Unix Shell , 4669 readers

        1、基础知识

        1.1 基本命令

        >显示时间和日期:date

$ date
Fri Aug 14 05:55:12 CEST 2009

        >查看登入系统用户:who 

$ who
applhdsk pts/0        Aug 14 05:36       
applhdsk pts/4        May  6 12:04 

      查看自己的信息:who am i 

$ who am i
applhdsk pts/0        Aug 14 05:36

        >显示字符:echo        

$echo one two   three    four
one two three four

         多个连续空格只显示一个,可以用来查看环境变量

$echo $ORACLE_HOME
/global/apps/HDSK/ora/8.0.6

        1.2 文件操作

        >列出文件清单:ls

$ls
1.txt  2.txt  3.txt
$ls -l
total 3
-rw-r--r--    1 applhdsk hdskdba        32 Aug 14 06:17 1.txt
-rw-r--r--    1 applhdsk hdskdba         0 Aug 14 06:13 2.txt
-rw-r--r--    1 applhdsk hdskdba         0 Aug 14 06:13 3.txt

        >显示文件内容: cat

$cat 1.txt
Sunday is the first day of week

        >统计文件中的单词数: wc

$wc 1.txt
      1       7      32 1.txt

        显示的3个数字一次是行数、单词数、字符数。后面接文件名。

        >命令选项

        显示文件行数: wc -l filename

$wc -l 1.txt
      1 1.txt

        统计文件字符数: wc -c filename

$wc -c 1.txt
     32 1.txt

         统计文件单词数: wc -w filename

$wc -w 1.txt
     7 1.txt

        >复制文件: cp

$cp 1.txt CopyOf1.txt
$cat CopyOf1.txt
Sunday is the first day of week

         >文件重命名: mv 

$ mv CopyOf1.txt newName.txt
$ cat newName.txt
Sunday is the first day of week

        >删除文件: rm 

$ rm newName.txt
$ ls -l
total 4
-rw-r--r--    1 applhdsk hdskdba        32 Aug 14 06:17 1.txt
-rw-r--r--    1 applhdsk hdskdba         0 Aug 14 06:13 2.txt
-rw-r--r--    1 applhdsk hdskdba         0 Aug 14 06:13 3.txt
$ rm 2.txt 3.txt
$ ls -l
total 4
-rw-r--r--    1 applhdsk hdskdba        32 Aug 14 06:17 1.txt

        1.3 目录操作

        >显示当前工作目录: pwd

$ pwd
/global/apps/XXX/joey

        >改变当前工作目录: cd

用法: cd 目录名
$ cd .
当前目录
$ cd ..
上级目录
$ cd
返回宿主目录

          >创建目录: mkdir

$ mkdir newDirectory
$ ls -l
total 8
-rw-r--r--    1 applhdsk hdskdba        32 Aug 14 06:17 1.txt
drwxr-xr-x    2 applhdsk hdskdba      4096 Aug 14 08:15 newDirectory

        >链接文件: ln

        格式: ln from to        

$ ln 1.txt 1-1.txt
$ cat 2.txt
$ ls -l
total 8
-rw-r--r--    2 applhdsk hdskdba        32 Aug 24 04:40 1-1.txt
-rw-r--r--    2 applhdsk hdskdba        32 Aug 24 04:40 1.txt
-rw-r--r--    1 applhdsk hdskdba         0 Aug 24 04:46 2.txt

         可以看到1.txt和1-1.txt紧靠在applhdsk前面的数字都为2,正常为1,表示没有其他链接的非目录文件,比如这里的2.txt,而1.txt和1-1.txt链接在一起,链接数为2,文件可以链接更多次。删除其中的一个不会影响另外一个。

$ rm 1-1.txt
$ ls -l
total 4
-rw-r--r--    1 applhdsk hdskdba        32 Aug 24 04:40 1.txt
-rw-r--r--    1 applhdsk hdskdba         0 Aug 24 04:46 2.txt

        可以看到, 文件1.txt的链接数变回了1。

        ln命令加-s选项, 可以建立符号化链接。符号化链接与普通链接的区别在于, 符号化连接指向初始文件, 如果初始文件被删除了, 该符号化链接就不在起作用。这点有点类似windows中的快捷方式。

$ ln -s 1.txt 1-2.txt
$ ls -l
lrwxrwxrwx    1 applhdsk hdskdba         5 Aug 24 04:56 1-2.txt -> 1.txt
-rw-r--r--    1 applhdsk hdskdba        32 Aug 24 04:40 1.txt
-rw-r--r--    1 applhdsk hdskdba         0 Aug 24 04:46 2.txt

        可以看到1-2.txt是指向1.txt, 其大小只为5, 实际上只记录的指向文件的路径, 不同与1.txt的大小32. 可以把ls命令的-L和-l选项一起使用, 以获得符号化文件列表所指向文件的详细信息。

$ ls -Ll
total 8
-rw-r--r--    1 applhdsk hdskdba        32 Aug 24 04:40 1-2.txt
-rw-r--r--    1 applhdsk hdskdba        32 Aug 24 04:40 1.txt
-rw-r--r--    1 applhdsk hdskdba         0 Aug 24 04:46 2.txt

        删除初始文件会使符号化链接无效, 虽然链接还存在。

$ rm 1.txt
$ ls -l
total 0
lrwxrwxrwx    1 applhdsk hdskdba         5 Aug 24 04:56 1-2.txt -> 1.txt
-rw-r--r--    1 applhdsk hdskdba         0 Aug 24 04:46 2.txt
$ more 1-2.txt
1-2.txt: No such file or directory

        该类型文件被称为"悬摆的符号化链接"。ln也可以把一串文件链接到一个目录之下。格式如下:ln files directory

        >删除目录: rmdir

        若目录非空, 并需要删除该目录下的所有文件, 可以添加-r选项。

$ mkdir dir-1
$ ls -l
total 4
lrwxrwxrwx    1 applhdsk hdskdba         5 Aug 24 04:56 1-2.txt -> 1.txt
-rw-r--r--    1 applhdsk hdskdba         0 Aug 24 04:46 2.txt
drwxr-xr-x    2 applhdsk hdskdba      4096 Aug 24 05:07 dir-1
$ rmdir dir-1
$ ls -l
total 0
lrwxrwxrwx    1 applhdsk hdskdba         5 Aug 24 04:56 1-2.txt -> 1.txt
-rw-r--r--    1 applhdsk hdskdba         0 Aug 24 04:46 2.txt

         1.4文件名替换

        *可以替换0个或者以上的字符, ?可以匹配一个字符。

$ ls -l
total 8
-rw-r--r--    1 applhdsk hdskdba        28 Aug 24 05:13 1.txt
-rw-r--r--    1 applhdsk hdskdba        37 Aug 24 05:13 2.txt
$ cat 1.txt 2.txt
Sunday is first day of week
Monday is the first work day of week
$ cat *
Sunday is first day of week
Monday is the first work day of week
$ echo *
1.txt 2.txt
$ echo * : *
1.txt 2.txt : 1.txt 2.txt
$ echo 1*
1.txt
$ echo ?.txt
1.txt 2.txt
$ cp 1.txt 12.txt
$ ls 1*.txt
1.txt 12.txt
$ ls 1?.txt
12.txt

         1.5 标准输入/输出及重定向

        典型的Unix命令:  标准输入->命令->标准输出。比如sort命令。ctrl + D表示结束标准输入。标准输出打印排序后的A、B、C、D、E。

$ sort
B
C
A
D
E
ctrl + D
A
B
C
D
E

        > 输出重定向 ">"

        如果命令正常情况下会将输出写入标准输出, 则在改命令后添加> file符号后, 命令的输入就会写入到file当中。

$ echo "Today is a sunny day." > today.txt
$ more today.txt
Today is a sunny day.

        > 输出重定向 ">>" 

        ">>"与">"的不同在于, 使用">"会将文件中原有的内容替换掉, 而使用">>"则是将命令的输出追加到文件原有内容后面。

$ echo "What about tommorrow? " >> today.txt
$ more today.txt
Today is a sunny day.
What about tommorrow? 

        >输入重定向"<"

$ wc -l today.txt
      2 today.txt
$ wc -l < today.txt
      2

        以上两条命令的执行结果有些许不同, 第一种情况下, 文件名和计数值一起显示出来了; 而第二种情况只显示了计数值。这反映了: 第一种情况wc命令知道输入是从today.txt文件中读取的; 而第二种情况, 他只知道是从标准输入中读取输入, 是shell把输入重定向了。

        1.6 管道 

$ who > users
$ wc -l < users
      3

        以上输出表明当前有3名用户登入了系统。还有另外一种方法来判断登入的用户数量而不需要使用文件。Unix系统可以把两条命令的效果链接起来, 这种链接称为管道, 它可以把一条命令的输出直接座位下一条命令的输入。管道效果用字符"|"实现, 它必须在两个命令中间, 因此要在who和wc命令之间建立管道, 只需要输入who | wc -l

$ who | wc -l
      3

          过滤器:

        Unix术语中的过滤器通常指能够从标准输入接受输入, 进行处理后, 把结果写入标准输出的任何程序。从定义可以看出, 过滤器可以用在管线中任何两个程序的中间。常用的过滤器有wc, cat, sort, grep等等。

        1.7 标准错误        

$ ls n*
ls: n*: No such file or directory
$ ls n* > foo
ls: n*: No such file or directory
$ more foo

          我们看到, 虽然我们对ls n*的输出结果进行了重定向写入到foo文件中, 但错误信息还是在标准输入终端打印出来了, 且foo文件为空。这是因为ls n*命令返回的不是标准输出, 而是标准错误。重定向标准错误, 要使用 “2>” 重定向符号。

$ ls n* 2> errors
$ more errors
ls: n*: No such file or directory

        1.8 有关命令的输入讨论

          >一行键入多个命令, 命令间用; 分隔。

$ echo "There're now totally ";who | wc -l; echo "users on system"
There're now totally 
      3
users on system

         >命令发送到后台。

         在命令后键入&符号, 该命令就会发送到后台执行而不占用终端, 用户可以继续输入下一条命令而不需要等待。

$ date > date.txt &
[1] 20814
$ more date.txt
Mon Aug 24 06:12:19 CEST 2009
[1]+  Done                    date >date.txt

        >ps 命令

$ date > date.txt &
[1] 21932
$ ps
  PID TTY          TIME CMD
29616 pts/4    00:00:00 bash
21942 pts/4    00:00:00 ps
[1]+  Done                    date >date.txt

          ps命令显示当前进程的运行状态。4列信息依次为进程标志号PID、来自进程的终端号TTY、进程已经运行的时间TIME以及进程的名称CMD。加上-f选项会显示更多进程的信息, 包括父进程的标志号PPID、进程启动时间STIME和命令参数。        

$ ps -f
UID        PID  PPID  C STIME TTY          TIME CMD
applhdsk 29616 29593  0 05:36 pts/4    00:00:00 -bash
applhdsk 24404 29616  0 06:17 pts/4    00:00:00 ps -f

 

 

 

     

 

        

 

 

 

        

 

 

 

 

 

 

 

 

 

  • No match
Avatar_small
لی ویوسوہ ہو یار said:
Mon, 24 May 2021 15:22:12 +0800

I think this is an informative post and it is very beneficial and knowledgeable. Therefore, I would like to thank you for the endeavors that you have made in writing this article. All the content is absolutely well-researched. Thanks... 123movies.com official site

Avatar_small
jackjohnny said:
Thu, 03 Jun 2021 15:41:10 +0800

Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our. indian visa application

Avatar_small
bath towel cap said:
Fri, 25 Jun 2021 19:27:19 +0800

Actually I read it yesterday but I had some thoughts about it and today I wanted to read it again because it is very well written.

Avatar_small
jackjohnny said:
Sun, 27 Jun 2021 22:22:52 +0800

Awesome and interesting article. Great things you've always shared with us. Thanks. Just continue composing this kind of post. Smm

Avatar_small
jackjohnny said:
Mon, 28 Jun 2021 17:17:08 +0800

Wow! Such an amazing and helpful post this is. I really really love it. It's so good and so awesome. I am just amazed. I hope that you continue to do your work like this in the future also voyance-telephone-gaia.com

Avatar_small
AAA said:
Wed, 18 May 2022 19:46:30 +0800

I am typically to blogging we actually appreciate your site content. This article has truly peaks my interest. I am about to bookmark your site and keep checking for brand spanking new information. How to Be a Credit Card Processor

 

=====================================

 

Great read! Will check back some other time to see if you add more to your updates. Merchant Services Sales Consultant

 

=======================================

 

My husband and i felt very glad Michael could deal with his investigations out of the ideas he obtained from your own weblog. It’s not at all simplistic to simply happen to be giving for free helpful tips that other folks have been selling. So we discover we’ve got the writer to give thanks to for that. Those explanations you have made, the simple blog navigation, the friendships your site give support to promote – it’s many spectacular, and it’s really aiding our son and the family believe that this concept is enjoyable, and that’s extraordinarily indispensable. Many thanks for everything! Credit Card Processing Sales

Avatar_small
AAA said:
Sat, 21 May 2022 03:03:30 +0800

You seem to be very professional in the way you write. 日本精品

 

============================

 

There are a couple of interesting points over time here but I don’t know if I see every one of them center to heart. There’s some validity but Let me take hold opinion until I take a look at it further. Excellent post , thanks and now we want much more! Added onto FeedBurner also 攝影器材


Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter