Linux 常用命令解析和Bash Shell使用示例脚本演示



摘要

Linux命令是基于文本格式输入输出的一种程序,依照Unix哲学中强调的程序功能简单,输入宽松,输出严谨,各种程序组合能够具有更强大的功能,而具有这样的灵活性的主要原因是Linux规定程序的输入输出必须坚持文件流格式。即文本格式,而这就是Linux系统的核心之中的一个。

对于Bash,即Shell的一种。为如今主流Linux发行版本号默认的命令行解释器,是一种功能强大的工具。能够实现对Linux支持的程序命令的组合。从而实现强大功能。类似于Window系统的bat文件,Bash具有更为强大的功能,通过Bash能够实现自己主动化的程序设计,功能运行甚至系统启动,而这些都要归功于Unix的设计理念。

本篇博文是基于笔者的经验之谈,仅供学习參考之用,存在疏漏还请留言指正,不胜感激~

-----------------------------------
Linux
命令具体解释-----------------------------------

文件夹相关命令

显示当前文件夹下的文件详情
ls
仅显示文件名称。ll‘ls
-alF’
命令的别称,通过 alias| grep’ll’命令能够查看:

[email protected]:~/test$ ls
t1  test1
[email protected]:~/test$ ll
total 12
drwxr-xr-x  3 king king 4096 2014-08-23 18:26 ./
drwxr-xr-x 39 king king 4096 2014-08-23 18:26 ../
-rw-r--r--  1 king king    0 2014-08-23 18:26 t1
drwxr-xr-x  2 king king 4096 2014-08-23 18:26 test1/
[email protected]:~/test$ alias | grep  'll'
alias ll='ls -alF'

另外,假设想通过经典的树形文件夹显示。可使用额外的tree命令(非内部命令),详细操作例如以下,
-L 2指的是以当前文件夹为根文件夹,显示文件夹结构到第二层:

[email protected]:~$ tree
The program 'tree' is currently not installed.  You can install it by typing:
sudo apt-get install tree
[email protected]:~$ sudo apt-get install

[email protected]:~/test$ tree -L 2
.
|-- t1
`-- test1
    `-- t2
1 directory, 2 files

创建删除文件夹操作。使用mkdirrmdir两个命令。对于删除,假设文件夹非空,可使用rm
–rf DirName
来实现:

[email protected]:~/test$ ls
t1  test1
[email protected]:~/test$ mkdir test2
[email protected]:~/test$ rmdir test1
rmdir: failed to remove `test1': Directory not empty
[email protected]:~/test$ rm -rf test1
[email protected]:~/test$ ls
t1  test2
[email protected]:~/test$ rmdir test2/
[email protected]:~/test$ ls
t1

tar命令使用

经常使用于打包,压缩和解压,使用和參数相关,当中 c指的是打包,x是提取,z指的是gzip压缩。j指的是bzip压缩。v显示解压过程。C是指定文件夹。这里指要解压到的文件夹。将文件夹test1打包,注意后接的參数顺序,显示打包的名字,后才是文件夹名

[email protected]:~/test$ tar -cvf test.tar test1/
test1/
test1/t2
[email protected]:~/test$ ls
t1  test1  test.tar

将打包的文件提取到指定文件夹下。-C实现
[email protected]:~/test$ tar -xvf test.tar -C ./test1/
test1/
test1/t2
[email protected]:~/test$ ls ./test1/
t2  test1

gzip压缩与解压
[email protected]:~/test$ tar -czvf test.tar.gz test1/
[email protected]:~/test$ tar -xzvf test.tar.gz

bzip2压缩与解压
[email protected]:~/test$ tar -cjvf test.tar.bz test1/
tar -xjvf test.tar.bz test1/

文件相关

显示文件内容

包含 cat,more,less,head,tail,nl等内部命令可实现,但稍微不同

cat  将文件串联输出到stdout,一般输出在终端。经常使用參数包含–n,功能和nl类似即同一时候输出行号。

more       可用于浏览超过一页或者超过终端显示长度的文件内容,通过空格键进行翻页,Enter键可行阅览,Q键退出,文本显示在终端

less    
可用于浏览超过一页或者超过终端显示长度的文件内容。通过空格键进行翻页,Enter键可行阅览。上下箭头可前进或者后退,Q键退出。文本显示在独立开启的模式下

head        后接參数–n 10。当然10能够更改,指的是显示文本的前10行

tail            后接參数 -n 10,同上,指的是显示文本的后10行

nl              类似于
cat -n

[email protected]:~/test$ cat t1
hello
world
!!!

END
gujinjin
[email protected]:~/test$ head -n 1 t1
hello
[email protected]:~/test$ tail -n 2 t1
END
gujinjin
[email protected]:~/test$ nl t1
     1	hello
     2	world
     3	!!!

     4	END
     5	gujinjin
[email protected]:~/test$ cat -n t1
     1	hello
     2	world
     3	!!!
     4
     5	END
     6	gujinjin
[email protected]:~/test$ more t1
hello
world
!!!

END
gujinjin

文本流处理与使用

文本流模式是Linux的核心思想之中的一个。因而命令能够组合形成更强大的功能。处理文本的命令非常多。这里笔者主要介绍此时此刻想到的,不足还请见谅!

这里要提一下 awk命令,即文本处理器,比較强大和奇妙,由贝尔实验室的A。W,K三位搞出来的,入门还是相当easy的,这里有一个陈皓老师的文章。公布于酷壳网(CoolShell.cn)。网址例如以下。感兴趣的能够看下:

http://coolshell.cn/articles/9070.html

特殊符号介绍
“|” 管道符,连接一个程序的输出和还有一个程序的输入通路
“>”“>>” 重定向,输出到指定文件。差别是前者输出并涵盖文件原有内容,后者输出加入到文件尾部

grep		(global search regular expression(RE) and print out the line), 參数 –o 指的是仅输出匹配对象,不输出完整行
演示样例: 输出IP地址
[email protected]:~/test$ ifconfig | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'
192.168.229.200
192.168.229.255
255.255.255.0
127.0.0.1
255.0.0.0

cut  顾名思义,即将输入文本进行移除处理并输出
演示样例,将IP地址分段,将上述输出存入 ip.log文件
參数解释: -s 不输出不包括指定分隔符的行。 -d 指定分隔符, -f 输出指定的段,可多个,如 –f1,3 表示输出1,3 段
[email protected]:~/test$ cat ip.log | cut -s -d. -f1
192
192
255
127
255

sort 即排序,这里经常使用两个參数,即 –n 基于数值大小排序,一般升序; -r 反向,即reverse
加n和不加还是有点差别的,这里为了展示这个差别,对ip.log进行略微改动,注意差别,事实上即当做数值处理和字符串处理的差别:
[email protected]:~/test$ cat ip.log | cut -s -d. -f1 | sort
127
192
192
20
255
255
[email protected]:~/test$ cat ip.log | cut -s -d. -f1 | sort -n
20
127
192
192
255
255
[email protected]:~/test$ cat ip.log | cut -s -d. -f1 | sort -nr
255
255
192
192
127
20

uniq  唯一,这里经常使用一个參数 –c, 用于计数,这里指上下行同样的
[email protected]:~/test$ cat ip.log | cut -s -d. -f1 | sort | uniq -c
      1 127
      2 192
      1 20
      2 255

awk  使用初探。 -F 后接分隔符, $1代表第一个分段。详细使用方法參见上述说明的连接。
[email protected]:~/test$ awk  -F. '$1>127 && $1<255 {print $0}' ip.log
192.168.229.200
192.168.229.255

文本处理演示样例

统计历史命令使用次数最多的前10个

[email protected]:~/test$ history | awk '{print $2}' | sort | uniq -c | sort -nr | head -n 10
    255 ls
    168 clear
     79 cd
     64 history
     60 sh
     48 sudo
     46 cat
     22 vim
     20 clea
     19 tree

哈哈哈,发现了啥,clea竟然有20次。可见clear命令常常打错啊~

权限相关

权限管理体系在Linux中非常完好,这也是Linux非常少受到黑客攻击的原因之中的一个。一般经常使用的三个命令,即改边文件权限的
chmod, chown, chgrp

chmod 变更文件权限,一般分为可读。可写,可运行3种,即 r - 4, w - 2,x - 1, - - 0,同一时候结合文件全部者 u – User, g – Group, o – Other, a – All 用户群使用, 下述三个命令等效
[email protected]:~/test/test1$ chmod ugo=rwx t2
[email protected]:~/test/test1$ chmod a=rwx t2
[email protected]:~/test/test1$ chmod 777 t2
[email protected]:~/test/test1$ ll t2
-rwxrwxrwx 1 king king 10240 2014-08-23 19:27 t2*

chown 变更文件全部者,也能够改变用户组
[email protected]:~/test/test1$ sudo chown root t2
[email protected]:~/test/test1$ ll t2
-rwxrwxrwx 1 root king 10240 2014-08-23 19:27 t2*

[email protected]:~/test/test1$ sudo chown root:root t2
[email protected]:~/test/test1$ ll t2
-rwxrwxrwx 1 root root 10240 2014-08-23 19:27 t2*

chgrp 改变文件所在的用户组
[email protected]:~/test/test1$ sudo chgrp root t2
[email protected]:~/test/test1$ ll t2
-rwxrwxrwx 1 king root 10240 2014-08-23 19:27 t2*

网络相关

ifconfig  查看网络配置信息

netstat  提供TCP连接,TCP、UDP监听,进程内存管理,路由表信息等,同一时候也能够查看端口信息。

经常使用參数 –r 输出路由表信息, -i 网络接口 interface信息,-a 显示全部socket
[email protected]:~/test/test1$ netstat -a | grep tcp | head -n 1
tcp        0      0 *:ssh                   *:*                     LISTEN

lsof  list open files,列出当前系统打开文件工具,一般在root权限下使用。如查看 80端口的相关进程信息:
[email protected]:~# lsof -i:80
COMMAND  PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
apache2  987     root    3u  IPv4   4423      0t0  TCP *:www (LISTEN)
apache2 3899 www-data    3u  IPv4   4423      0t0  TCP *:www (LISTEN)
apache2 3900 www-data    3u  IPv4   4423      0t0  TCP *:www (LISTEN)
apache2 3901 www-data    3u  IPv4   4423      0t0  TCP *:www (LISTEN)
apache2 3902 www-data    3u  IPv4   4423      0t0  TCP *:www (LISTEN)
apache2 3903 www-data    3u  IPv4   4423      0t0  TCP *:www (LISTEN)

nc	NetCat,网络工具,功能强大。可用于端口监測
參数说明: -4 仅IPv4。 -6 仅IPv6。 -v 输出运行过程。 -w  同意延迟时间(s) 。 -z 仅扫描监听守护进程不发送消息。 -u UDP协议默觉得TCP
[email protected]:~$ nc -v -w 2 -z 192.168.229.200 76-80
nc: connect to 192.168.229.200 port 76 (tcp) failed: Connection refused
nc: connect to 192.168.229.200 port 77 (tcp) failed: Connection refused
nc: connect to 192.168.229.200 port 78 (tcp) failed: Connection refused
nc: connect to 192.168.229.200 port 79 (tcp) failed: Connection refused
Connection to 192.168.229.200 80 port [tcp/www] succeeded!

------------------------
Bash Shell详细实现演示样例------------------------

这里列举几个经常使用的小样例。算是对Shell语法的基本回想吧

对指定时间内某作者改动文件指定字符串的查看

#!/bin/bash
res=`ls -l | awk '$3=="king" && $6=="2014-08-23" && $7<"22:10" && NR!=1 {print $8}'`
#echo $res
for var in $res;do
        r=`cat ./$var | grep -n -w 'test' >> mod.log`
        #echo $var
        if [ $? -ne 0 ];then
                echo "Execute CMD error!!!"
                exit 1
        fi
done
exit 0

[email protected]:~/Shell$ sudo sh p1.sh
[email protected]:~/Shell$ cat mod.log
1:this is a test this is a test
6:this is a test this is a test

对启动某个进程后台执行,然后完毕操作后关闭进程

#!/bin/bash

DIR=/home/king/CPPFile/Socket
echo $DIR

if [ -e $DIR/server ];then
        $DIR/server & > info.log 2>&1
        if [ $? -ne 0 ];then
                echo "Execute Wrong!"
                exit 1
        fi
        sleep 2
        pid=$!
        echo $pid
        kill -9 $pid
        if [ $?

-ne 0 ];then
                echo "fail to kill pid"
                exit 2
        fi
fi
exit 0

[email protected]:~/CPPFile/Socket$ sudo sh test.sh
/home/king/CPPFile/Socket
16028

版权声明:本文博主原创文章。博客,未经同意不得转载。

时间: 2024-08-07 08:37:41

Linux 常用命令解析和Bash Shell使用示例脚本演示的相关文章

Linux 常用命令解析及Bash Shell脚本用法示例

 摘要 Linux 命令是基于文本格式输入输出的一种程序,按照Unix哲学中强调的程序功能简单,输入宽松,输出严谨,各种程序组合可以具有更强大的功能,而具有这种灵活性的主要原因是Linux规定程序的输入输出必须坚持文件流格式,即文本格式,而这就是Linux系统的核心之一. 对于Bash,即Shell的一种,为现在主流Linux发行版本默认的命令行解释器,是一种功能强大的工具,可以实现对Linux支持的程序命令的组合,从而实现强大功能.类似于Window系统的bat文件,Bash具有更为强大的

linux常用命令整理(五):shell基础

大家好,我是会唱歌的程序猿------ 最近在学习linux,闲暇之余就把这些基本的命令进行了整理,希望大家能用的上,整理的的目的是在忘了的时候翻出来看看^?_?^,前后一共分为五个部分: linux基本命令整理(一):常用命令 地址:http://www.cnblogs.com/devinCat/p/7247824.html linux基本命令整理(二):用户.用户组.文件系统和网络 地址:http://www.cnblogs.com/devinCat/p/7247847.html linux

Linux常用命令及bash特性(1)

Linux简单使用(1) Linux常用命令介绍 linux命令是对Linux系统进行管理的命令.对于Linux系统来说,无论是中央处理器.内存.磁盘驱动器.键盘.鼠标,还是用户等都是文件,Linux系统管理的命令是它正常运行的核心. linux命令在系统中有两种类型:内置Shell命令和Linux命令.可以使用help.man和info命令获得帮助. * help提供内部命令的帮助:man和info提供外部命令的帮助. Linux常用命令 pwd命令:以绝对路径的方式显示当前的工作目录: [[

linux常用命令整理(四):软件包管理和shell基础

大家好,我是会唱歌的程序猿------ 最近在学习linux,闲暇之余就把这些基本的命令进行了整理,希望大家能用的上,整理的的目的是在忘了的时候翻出来看看^?_?^,前后一共分为五个部分: linux基本命令整理(一):常用命令 地址:http://www.cnblogs.com/devinCat/p/7247824.html linux基本命令整理(二):用户.用户组.文件系统和网络 地址:http://www.cnblogs.com/devinCat/p/7247847.html linux

Linux常用命令(十三)基础网络设置

Linux常用命令(十三)基础网络设置 本篇内容实验环境为Redhat 6.5系统 一.查看及测试网络 查看及测试网络配置时管理Linux网络服务的第一步.其中本篇中大多数命令以普通用户权限就可以完成操作.   1.查看网络配置 1.1).使用ifconfig命令查看网络接口地址 主机的网络接口卡(网卡)通常称为"网络接口".在Linux系统中,使用ifconfig命令可以查看网络接口的地址配置信息. 查看活动的网络接口设备 当ifconfig命令不带任何选项和参数时,将显示当前主机中

Linux常用命令(echo、date、ls、cd、history、cat)

一.linux常用命令有很多今天我们来总结一下常用的入门命令: 1.linux下关机命令:poweroff.init 0.halt.shutdown -h now 2.linux下重启命令:reboot.init 6.shutdown -r now 3.shutdown命令: 格式:shutdown  options TIME 其中options有以下几个: -r:执行重启 -c:取消shutdown命令 -h:执行关机 其中TIME有以下几个: now:表示现在 +m:相对时间表示法,从命令提

Linux 常用命令汇总

Linux 常用命令汇总 1. 显示日期与时间:date 可修改显示日期格式,如:date +%Y/%m/%d %H:%M à2016/12/25 17:05(%Y:年:%m:月:%d:日:%H:24小时制:%M:分) date –d 参数后可加:today/yesterday/tomorrow 来显示今天.昨天.明天日期 2. 显示日历:cal(默认显示本月月历) cal 2001 可显示2001年整年日历:cal 10 2001 可具体显示某月月历 3.  计算器:bc 4. Tab键:用在

Linux常用命令(一)

1.命令格式: 命令 -选项 参数 如:ls -la /usr ls: 显示文件和目录列表(list) 常用参数: -l (long) -a (all) -t (time) 2.命令的分类 内部命令:属于Shell解析器的一部分 cd 切换目录(change directory) pwd 显示当前工作目录(print working directory) help 帮助 外部命令:独立于Shell解析器之外的文件程序 ls 显示文件和目录列表(list) mkdir 创建目录(make dire

linux常用命令技巧

原文地址 这篇文章来源于Quroa的一个问答<What are some time-saving tips that every Linux user should know?>-- Linux用户有哪些应该知道的提高效率的技巧.我觉得挺好的,总结得比较好,把其转过来,并加了一些自己的理解. 首先,我想告诉大家,在Unix/Linux下,最有效率技巧的不是操作图形界面,而是命令行操作,因为命令行意味着自动化.如果你看过<你可能不知道的Shell>以及<28个Unix/Linu