`
qindongliang1922
  • 浏览: 2171481 次
  • 性别: Icon_minigender_1
  • 来自: 北京
博客专栏
7265517b-f87e-3137-b62c-5c6e30e26109
证道Lucene4
浏览量:117083
097be4a0-491e-39c0-89ff-3456fadf8262
证道Hadoop
浏览量:125430
41c37529-f6d8-32e4-8563-3b42b2712a50
证道shell编程
浏览量:59504
43832365-bc15-3f5d-b3cd-c9161722a70c
ELK修真
浏览量:71021
社区版块
存档分类
最新评论

跟散仙学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即可!














1
1
分享到:
评论
1 楼 di1984HIT 2015-01-26  
xuexile,xiexie

相关推荐

    Android执行shell命令(转)

    可以创建一个包含多个命令的shell脚本(如:`script.sh`),然后通过`adb push`将脚本推送到设备上,再用`adb shell sh script.sh`执行脚本。 6. 源码级操作 对于开发者而言,有时需要在源码层面执行shell命令,...

    Linux下Shell命令解释步骤程序实现

    Linux Shell 命令解释是 Linux 操作系统中最基本也是最重要的一部分,它负责处理用户与操作系统之间的交互动作,并且给出相应的操作系统的输出结果。 Shell 基本上是一个命令解释器,类似于 DOS 下的 command.com。...

    shell命令解释器

    **Shell命令解释器**是Linux操作系统中的核心组件之一,它是一个用户与系统交互的界面,让用户能够通过输入命令来执行各种系统操作。在Linux的世界里,shell不仅是一个命令行接口,还是一个强大的脚本编程语言,允许...

    shell命令shell命令shell命令

    Shell不仅是一个命令解释器,也是一个编程环境,可以编写脚本来实现复杂的任务自动化。 在描述中反复提到了"shell命令",这暗示了我们要关注的是如何使用和理解各种shell命令。以下是一些常见的和重要的shell命令...

    shell命令常用shell命令

    Shell 既是一种命令语言,又是一种程序设计语言。 Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务。 Ken Thompson 的 sh 是第一种 Unix Shell,Windows Explorer 是...

    java调用shell命令.pdf

    Java 调用 Shell 命令 在 Java 中调用 Shell 命令是非常常见的操作,特别...Java 调用 Shell 命令是一种非常有用的技术,能够帮助我们实现自动化任务和系统集成。但是,我们需要注意安全性和权限问题,以免出现问题。

    shell命令文档 中文版

    在IT领域,Shell命令是Linux和Unix操作系统中的一个强大工具,它允许用户通过文本界面与操作系统进行交互。这本书“shell命令文档 中文版”提供了一个全面的指南,涵盖了所有基本到高级的Shell命令,这对于系统管理...

    android执行shell命令

    - **命令语法**:Android设备上的shell环境通常是 BusyBox 提供的一个精简版,因此并非所有Linux命令都可用。在编写shell脚本或命令时,要确保命令在Android环境下是兼容的。 - **日志输出**:通过`logcat`命令可以...

    Android程序中(APK程序)执行Adb shell 命令

    Android Debug Bridge(ADB)就是一个强大的工具,允许开发者从计算机上对连接的Android设备进行各种操作,包括发送shell命令。本篇将深入探讨如何在APK程序中执行Adb shell命令以及它与高级语言之间的调用关系。 ...

    shell命令的用法

    Shell 命令是 Linux 操作系统中最基本也是最重要的工具之一,它提供了强大的命令行接口,能够帮助用户快速高效地完成各种任务。了解 shell 命令的用法是每一个 Linux 用户的必备知识。 1. cp 命令 cp 命令的功能是...

    Android-ExecuteShell是一个可以方便的执行shell命令的Android开源库

    ExecuteShell是一个专为Android开发者设计的开源库,它使得在Android应用程序中执行shell命令变得轻而易举。在Android系统中,有时我们需要执行一些低级的操作,如文件管理、系统调试或者权限控制,这时就需要借助...

    ubuntu_shell命令学习

    ubuntu_shell命令学习

    c++builder的Android程序中执行Shell命令

    本单元实现了一个伪的Shell,可以用于在Android程序中执行Shell命令,如果需要Root权限,则在执行前,调用AskForRoot方法来获取用户的Root授权,如果不再需要在Root账号下执行命令,则调用ExitRoot方法来退出Root...

    shell命令解析器

    Shell命令解析器是Linux操作系统中的一个核心组件,它充当用户与操作系统内核之间的接口,使得用户可以通过命令行界面执行各种操作。在本文中,我们将深入探讨shell命令解析器的工作原理,以及如何使用C语言编程来...

    android 给app授权 执行shell命令

    总的来说,给Android应用授予Root权限并执行shell命令是一个涉及设备修改、权限管理、代码实现和错误处理的复杂过程。务必谨慎操作,因为这可能影响设备安全性和稳定性。对于非专业人士,建议只在充分了解风险和必要...

    文件管理及常见SHELL命令应用

    SHELL命令是操作系统提供的一种交互式命令行接口。SHELL命令可以用于执行各种操作,例如文件管理、进程管理、网络管理等。常见的SHELL命令包括cd、ls、mkdir、rm、cp、mv等。例如,cd命令用于切换当前目录,ls命令...

    jenkins shell 命令,自动部署命令.txt

    放在Post Steps 的shell命令 ,一键自动部署(2021最新版)

    shell命令学习资料

    本文将深入探讨Shell命令的学习资料,帮助读者掌握这一基础且强大的技能。 首先,Shell是一种命令行解释器,它允许用户通过文本输入执行系统命令。常见的Shell有Bash(Bourne-Again SHell),它是大多数Linux发行版...

    在python 中实现运行多条shell命令

    `shell=True`表示通过shell来执行命令,而`&&`则是用来连接两个命令,确保前一条命令成功后才会执行下一条命令。 - **注意**:`shell=True`可能会带来安全风险,因为它允许注入shell命令。在处理用户提供的数据时要...

Global site tag (gtag.js) - Google Analytics