跟散仙学shell命令(一)

linux系统的重要性不言而喻了,好多项目都要部署到linux服务器上运行,学好shell命令和编程会对我们开发人员如虎添翼,所以很有必要学习,熟悉一番。

既然是学习,就从最基础的开始,一步一步循序渐进。

1,man命令是一个帮助我们,快速掌握某个命令的使用方法的命令,当然前提时我们安装了man命令模块,man命令的用法,man command,后面跟自己不了解的命令,如果你的系统没有安装,先执行命令,yum install man* 安装。用法如下:

[root@h1 ~]# man ls
LS(1)                            User Commands                           LS(1)

NAME
       ls - list directory contents

SYNOPSIS
       ls [OPTION]... [FILE]...

DESCRIPTION
       List information about the FILEs (the current directory by default).  Sort entries alphabetically if none of -cftuvSUX nor --sort.

       Mandatory arguments to long options are mandatory for short options too.

       -a, --all
              do not ignore entries starting with .

       -A, --almost-all
              do not list implied . and ..

       --author
              with -l, print the author of each file

       -b, --escape
              print octal escapes for nongraphic characters

       --block-size=SIZE
              use SIZE-byte blocks.  See SIZE format below

       -B, --ignore-backups
              do not list implied entries ending with ~

       -c     with  -lt:  sort by, and show, ctime (time of last modification of file status information) with -l: show ctime and sort by name otherwise: sort
              by ctime

       -C     list entries by columns

       --color[=WHEN]
              colorize the output.  WHEN defaults to ‘always’ or can be ‘never’ or ‘auto’.  More info below

       -d, --directory
              list directory entries instead of contents, and do not dereference symbolic links

       -D, --dired
              generate output designed for Emacs’ dired mode

       -f     do not sort, enable -aU, disable -ls --color

       -F, --classify
              append indicator (one of */=>@|) to entries

       --file-type
              likewise, except do not append ‘*’

       --format=WORD
              across -x, commas -m, horizontal -x, long -l, single-column -1, verbose -l, vertical -C
:

2,cd命令,cd命令时linux目录下,切换路径的命令,主要用法是用来浏览文件系统,和遍历目录所需.
用法(一)cd  绝对路径
例如cd /etc/profile.d,cd /home等等
用法(二)cd  相对路径
例如cd ../ 上一级目录 cd ./当前目录 cd ../../上一级的上一级

3,ls命令,显示当前目录下所有文件盒目录

[root@h1 ~]# ls
abc1.txt         count.txt            hivesrc        install.log         jdk1.7.0_25            login        tsethadoop
anaconda-ks.cfg  hadoop-2.2.0.tar.gz  initserver.sh  install.log.syslog  jdk-7u25-linux-x64.gz  setlimit.sh
[root@h1 ~]# 

ls -F参数,可以给目录后面加/号,更容易区别文件和目录,注意是大写的-F
[root@h1 ~]# ls -F
abc1.txt         count.txt            hivesrc/       install.log         jdk1.7.0_25/            login/       tsethadoop/
anaconda-ks.cfg  hadoop-2.2.0.tar.gz  initserver.sh  install.log.syslog  jdk-7u25-linux-x64.gz*  setlimit.sh
[root@h1 ~]# 

ls -F -R 可以递归打印某个目录下所有嵌套文件等多级路径。
ls -F -a 可以显示系统隐藏的文件比如我们的公钥文件.ssh/等
[root@h1 ~]# ls -F -a
./        anaconda-ks.cfg  .bash_profile  .cshrc               initserver.sh       jdk1.7.0_25/            .m2/            .ssh/        .viminfo
../       .bash_history    .bashrc        hadoop-2.2.0.tar.gz  install.log         jdk-7u25-linux-x64.gz*  .mysql_history  .tcshrc
abc1.txt  .bash_logout     count.txt      hivesrc/             install.log.syslog  login/                  setlimit.sh     tsethadoop/
[root@h1 ~]# 

ls -l可以以换行格式,显示更详细的信息。
[root@h1 ~]# ls -F -l
总用量 328444
-rw-r--r--  1 root   root 143775368 7月  28 19:30 abc1.txt
-rw-------. 1 root   root      1087 6月  13 19:06 anaconda-ks.cfg
-rw-r--r--  1 root   root        52 7月  31 19:46 count.txt
-rw-r--r--. 1 root   root  96183833 6月   9 17:27 hadoop-2.2.0.tar.gz
drwxr-xr-x  3 root   root      4096 7月  29 04:47 hivesrc/
-rw-r--r--. 1 root   root      2111 6月  16 13:10 initserver.sh
-rw-r--r--. 1 root   root      7995 6月  13 19:06 install.log
-rw-r--r--. 1 root   root      3384 6月  13 19:06 install.log.syslog
drwxr-xr-x. 8 search  143      4096 6月   6 2013 jdk1.7.0_25/
-rwx------. 1 root   root  96316511 11月 20 2013 jdk-7u25-linux-x64.gz*
drwxr-xr-x  3 root   root      4096 7月   9 04:08 login/
-rw-r--r--  1 root   root      1048 6月  19 03:31 setlimit.sh
drwxr-xr-x  3 root   root      4096 6月  20 02:51 tsethadoop/

散仙在这里解释一下,这几个东西分别代表的含义,
1,第一列第一个字符目录(d),文件(-),字符文件(c),块文件(b)
2,第一列第二个rw,drwx,等等,代表文件权限,这个散仙会在后面的文章里介绍
3,第二列数字,代表硬链接总数(??不知道硬链接是啥?,没关系,下文会介绍)
4,第三列,代表用户名
5,第四列代表组名
6,第五列文件的大小
7,第六列,文件的修改时间
8,第七列,具体的文件或目录

更人性化的输出大小ls -F -l -h 命令将字节转为M来显示
[root@h1 ~]# ls -F -l -h
总用量 321M
-rw-r--r--  1 root   root 138M 7月  28 19:30 abc1.txt
-rw-------. 1 root   root 1.1K 6月  13 19:06 anaconda-ks.cfg
-rw-r--r--  1 root   root   52 7月  31 19:46 count.txt
-rw-r--r--. 1 root   root  92M 6月   9 17:27 hadoop-2.2.0.tar.gz
drwxr-xr-x  3 root   root 4.0K 7月  29 04:47 hivesrc/
-rw-r--r--. 1 root   root 2.1K 6月  16 13:10 initserver.sh
-rw-r--r--. 1 root   root 7.9K 6月  13 19:06 install.log
-rw-r--r--. 1 root   root 3.4K 6月  13 19:06 install.log.syslog
drwxr-xr-x. 8 search  143 4.0K 6月   6 2013 jdk1.7.0_25/
-rwx------. 1 root   root  92M 11月 20 2013 jdk-7u25-linux-x64.gz*
drwxr-xr-x  3 root   root 4.0K 7月   9 04:08 login/
-rw-r--r--  1 root   root 1.1K 6月  19 03:31 setlimit.sh
drwxr-xr-x  3 root   root 4.0K 6月  20 02:51 tsethadoop/
[root@h1 ~]# 

这么多命令是不是很烦,试试这个ll这个命令,散仙经常用的,一个加强命令
[root@h1 ~]# ll
总用量 328444
-rw-r--r--  1 root   root 143775368 7月  28 19:30 abc1.txt
-rw-------. 1 root   root      1087 6月  13 19:06 anaconda-ks.cfg
-rw-r--r--  1 root   root        52 7月  31 19:46 count.txt
-rw-r--r--. 1 root   root  96183833 6月   9 17:27 hadoop-2.2.0.tar.gz
drwxr-xr-x  3 root   root      4096 7月  29 04:47 hivesrc
-rw-r--r--. 1 root   root      2111 6月  16 13:10 initserver.sh
-rw-r--r--. 1 root   root      7995 6月  13 19:06 install.log
-rw-r--r--. 1 root   root      3384 6月  13 19:06 install.log.syslog
drwxr-xr-x. 8 search  143      4096 6月   6 2013 jdk1.7.0_25
-rwx------. 1 root   root  96316511 11月 20 2013 jdk-7u25-linux-x64.gz
drwxr-xr-x  3 root   root      4096 7月   9 04:08 login
-rw-r--r--  1 root   root      1048 6月  19 03:31 setlimit.sh
drwxr-xr-x  3 root   root      4096 6月  20 02:51 tsethadoop
[root@h1 ~]# 

解释一下,有些命令时不同的linu系统,演化而来,不用的linux分支演化可能不支持,散仙的命令是在centos下,使用的。

按文件大小输出并排序ll -S -h 这个在运维时候,很有用,用来查找大文件
[root@h1 ~]# ll -S -h
总用量 321M
-rw-r--r--  1 root   root 138M 7月  28 19:30 abc1.txt
-rwx------. 1 root   root  92M 11月 20 2013 jdk-7u25-linux-x64.gz
-rw-r--r--. 1 root   root  92M 6月   9 17:27 hadoop-2.2.0.tar.gz
-rw-r--r--. 1 root   root 7.9K 6月  13 19:06 install.log
drwxr-xr-x  3 root   root 4.0K 7月  29 04:47 hivesrc
drwxr-xr-x. 8 search  143 4.0K 6月   6 2013 jdk1.7.0_25
drwxr-xr-x  3 root   root 4.0K 7月   9 04:08 login
drwxr-xr-x  3 root   root 4.0K 6月  20 02:51 tsethadoop
-rw-r--r--. 1 root   root 3.4K 6月  13 19:06 install.log.syslog
-rw-r--r--. 1 root   root 2.1K 6月  16 13:10 initserver.sh
-rw-------. 1 root   root 1.1K 6月  13 19:06 anaconda-ks.cfg
-rw-r--r--  1 root   root 1.1K 6月  19 03:31 setlimit.sh
-rw-r--r--  1 root   root   52 7月  31 19:46 count.txt


按文件名,排序ll -X
[root@h1 ~]# ll -X
总用量 328444
drwxr-xr-x  3 root   root      4096 7月  29 04:47 hivesrc
drwxr-xr-x  3 root   root      4096 7月   9 04:08 login
drwxr-xr-x  3 root   root      4096 6月  20 02:51 tsethadoop
drwxr-xr-x. 8 search  143      4096 6月   6 2013 jdk1.7.0_25
-rw-------. 1 root   root      1087 6月  13 19:06 anaconda-ks.cfg
-rw-r--r--. 1 root   root  96183833 6月   9 17:27 hadoop-2.2.0.tar.gz
-rwx------. 1 root   root  96316511 11月 20 2013 jdk-7u25-linux-x64.gz
-rw-r--r--. 1 root   root      7995 6月  13 19:06 install.log
-rw-r--r--. 1 root   root      2111 6月  16 13:10 initserver.sh
-rw-r--r--  1 root   root      1048 6月  19 03:31 setlimit.sh
-rw-r--r--. 1 root   root      3384 6月  13 19:06 install.log.syslog
-rw-r--r--  1 root   root 143775368 7月  28 19:30 abc1.txt
-rw-r--r--  1 root   root        52 7月  31 19:46 count.txt


ll -i显示文件或目录的索引节点inode,第一列的参数
[root@h1 ~]# ll -i
总用量 328444
655837 -rw-r--r--  1 root   root 143775368 7月  28 19:30 abc1.txt
659103 -rw-------. 1 root   root      1087 6月  13 19:06 anaconda-ks.cfg
674329 -rw-r--r--  1 root   root        52 7月  31 19:46 count.txt
660638 -rw-r--r--. 1 root   root  96183833 6月   9 17:27 hadoop-2.2.0.tar.gz
663837 drwxr-xr-x  3 root   root      4096 7月  29 04:47 hivesrc
660644 -rw-r--r--. 1 root   root      2111 6月  16 13:10 initserver.sh
654083 -rw-r--r--. 1 root   root      7995 6月  13 19:06 install.log
654084 -rw-r--r--. 1 root   root      3384 6月  13 19:06 install.log.syslog
659106 drwxr-xr-x. 8 search  143      4096 6月   6 2013 jdk1.7.0_25
659105 -rwx------. 1 root   root  96316511 11月 20 2013 jdk-7u25-linux-x64.gz
663800 drwxr-xr-x  3 root   root      4096 7月   9 04:08 login
663149 -rw-r--r--  1 root   root      1048 6月  19 03:31 setlimit.sh
663148 drwxr-xr-x  3 root   root      4096 6月  20 02:51 tsethadoop
[root@h1 ~]# 



ls命令还支持,文件过滤,熟悉正则的同学应该都很熟悉
ls -l i* 代表显示以i开头文件或目录
[root@h1 ~]# ls -l i*     
-rw-r--r--. 1 root root 2111 6月  16 13:10 initserver.sh
-rw-r--r--. 1 root root 7995 6月  13 19:06 install.log
-rw-r--r--. 1 root root 3384 6月  13 19:06 install.log.syslog

intest:
总用量 0
-rw-r--r-- 1 root root 0 7月  31 21:19 abc.txt




更多命令语法支持,请使用我们亲爱的man命令,还记得用法么?  好吧man ls这个命令。


3,touch命令,用来创建一个新文件,还可以改变文件的访问时间,在实际开发中使用较少,请使用man命令,来学习即可。


4,cp 命令 用法: cp source  destination,实际开发使用较多,
例子: cp test.txt test2.txt 拷贝当前的test.txt并新命令一个test2.txt
cp test.txt /home 拷贝test.txt到home目录下,名字不变
cp -a /abc/  /xyz 拷贝abc文件夹下所有内容到xyz下,xyz目录可以不用提前建立
cp -R /a /b  递归的拷贝文件或文件夹,到另一个目录下。


5,介绍下linux中的软链接和硬链接的区别,

软链接类似windows下的快捷方式,引用的文件还是原始文件,软链接,可以跨挂载的存储设备使用。命令: ln -s source  destination

硬链接软件开发中双写模式,建立硬链接的文件,只要向其中一个写入内容,所有的硬链接的内容都会改变,但是硬链接的文件是各自独立的,只能同一个挂载的存储设备下使用。
命令 ln -d source destination

实际开发中用的较多的是软链接:
下面的例子,演示了散仙对jdk所建立的一个软连接:
[root@h1 ~]# ll
总用量 328464
-rw-r--r--  1 root   root 143775368 7月  28 19:30 abc1.txt
-rw-------. 1 root   root      1087 6月  13 19:06 anaconda-ks.cfg
-rw-r--r--  1 root   root        52 7月  31 21:29 count2.txt
-rw-r--r--  1 root   root        52 7月  31 19:46 count.txt
-rw-r--r--. 1 root   root  96183833 6月   9 17:27 hadoop-2.2.0.tar.gz
-rw-r--r--  1 root   root         1 7月  31 21:25 hh.txt
drwxr-xr-x  3 root   root      4096 7月  29 04:47 hivesrc
-rw-r--r--. 1 root   root      2111 6月  16 13:10 initserver.sh
-rw-r--r--. 1 root   root      7995 6月  13 19:06 install.log
-rw-r--r--. 1 root   root      3384 6月  13 19:06 install.log.syslog
drwxr-xr-x  2 root   root      4096 7月  31 21:19 intest
drwxr-xr-x. 8 search  143      4096 6月   6 2013 jdk1.7.0_25
-rwx------. 1 root   root  96316511 11月 20 2013 jdk-7u25-linux-x64.gz
drwxr-xr-x  3 root   root      4096 7月  31 21:33 li
drwxr-xr-x  3 root   root      4096 7月   9 04:08 lo
drwxr-xr-x  3 root   root      4096 7月   9 04:08 login
-rw-r--r--  1 root   root      1048 6月  19 03:31 setlimit.sh
drwxr-xr-x  3 root   root      4096 6月  20 02:51 tsethadoop
[root@h1 ~]# ln -s jdk1.7.0_25/ jdk
[root@h1 ~]# ll
总用量 328464
-rw-r--r--  1 root   root 143775368 7月  28 19:30 abc1.txt
-rw-------. 1 root   root      1087 6月  13 19:06 anaconda-ks.cfg
-rw-r--r--  1 root   root        52 7月  31 21:29 count2.txt
-rw-r--r--  1 root   root        52 7月  31 19:46 count.txt
-rw-r--r--. 1 root   root  96183833 6月   9 17:27 hadoop-2.2.0.tar.gz
-rw-r--r--  1 root   root         1 7月  31 21:25 hh.txt
drwxr-xr-x  3 root   root      4096 7月  29 04:47 hivesrc
-rw-r--r--. 1 root   root      2111 6月  16 13:10 initserver.sh
-rw-r--r--. 1 root   root      7995 6月  13 19:06 install.log
-rw-r--r--. 1 root   root      3384 6月  13 19:06 install.log.syslog
drwxr-xr-x  2 root   root      4096 7月  31 21:19 intest
lrwxrwxrwx  1 root   root        12 7月  31 21:45 jdk -> jdk1.7.0_25/
drwxr-xr-x. 8 search  143      4096 6月   6 2013 jdk1.7.0_25
-rwx------. 1 root   root  96316511 11月 20 2013 jdk-7u25-linux-x64.gz
drwxr-xr-x  3 root   root      4096 7月  31 21:33 li
drwxr-xr-x  3 root   root      4096 7月   9 04:08 lo
drwxr-xr-x  3 root   root      4096 7月   9 04:08 login
-rw-r--r--  1 root   root      1048 6月  19 03:31 setlimit.sh
drwxr-xr-x  3 root   root      4096 6月  20 02:51 tsethadoop
[root@h1 ~]# 



6. 重命名一个文件使用命令mv
用法mv source destination

重命名目录或文件都可以,举例子如下:
mv a.txt b.txt 重名名a文件为b文件名字

具体详细的用法,请使用man mv查看,退出按q即可!














猜你喜欢

转载自qindongliang.iteye.com/blog/2098634