您的位置:首页 > 运维架构 > Linux

LINUX 常用指令学习

2020-02-17 11:44 585 查看

目录

0 查找find

1 别名alias

2 变量的设置 

3 常用的系统变量

4 通配符及组合按键

5 指令之间的分隔符(;&||)

6 输出重定向(>,>>,1>,2>)

7 管道

 

通过本问还能了解到

env,set,export

unset,unalias

$?,$RANDOM

last 

cut,wc,uniq,sort

 

0 查找

通过这个格式,可以查找到文件内容为{}里面的数据

find ~ -type f -exec grep -n abs '{}' ';' -print

 

 

1 别名(alias)

一、设置方法

alias是一个比较好用的指令,用于对指令进行重新命名,方便记忆和使用

例如:我们要打开系统的网络配置文件就需要输入较长的指令(虽然vi指令不长,但是文件路径有点深并不适合不好记忆)

[root@xiaoftest ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0

我们给这串指令起一个别名’eth0',是不是很方便呢?

[root@xiaoftest ~]# alias eth0='vi /etc/sysconfig/network-scripts/ifcfg-eth0 '

这样下一次访问只需要输入eth0即可达到一样的效果

二、查看

直接输入alias即可查看用户目前已经配置可以用的别名

[root@xiaoftest ~]# alias
alias cp='cp -i'
alias eth0='vi /etc/sysconfig/network-scripts/ifcfg-eth0 '   //这是我们刚才定义的别名
alias l.='ls -d .* --color=tty'
alias ll='ls -l --color=tty'
alias ls='ls --color=tty'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@xiaoftest ~]#

 三、取消设置别名

unalias

 

2 变量与变量的设置

一、变量的使用

个人认为变量其实是与别名相似,变量常常用来保存比较深的路径,其实也可以用来保存指令的

变量名='需要定义的值'

[root@xiaoftest /]# test='ls -al'
[root@xiaoftest /]# $test
total 182
drwxr-xr-x  24 root root  4096 Jan  1 16:48 .
drwxr-xr-x  24 root root  4096 Jan  1 16:48 ..
-rw-r--r--   1 root root     0 Jan  1 16:48 .autofsck

另外变量还可以向后追加

[root@xiaoftest /]# name='sunfan'
[root@xiaoftest /]# echo $name
sunfan
[root@xiaoftest /]# name="$name"test   //先引用$name后再追加test
[root@xiaoftest /]# echo $name
sunfantest            //追加test成功
[root@xiaoftest /]#

先定义变量name的值是sunfan ,然后通过对变量的引用($name)再附加test赋值给name

name的值就变成了sunfantest. (注意 1变量的赋值一定在等号前后不能有空格 2引用变量的时候一定要用双引号)

在引用变量后面添加值后再赋值给这个变量的方法常常用于对环境变量的配置,

如果我要在环境变量里写入JAVA_HOME="XXXXXX", 这样就会很方便--->PATH="$PATH":XXXXXX

 

二、查看系统变量

env 显示目前系统中主要的变量

set 显示系统中全部的变量内容

export 如果不接变量的话,则打印所有的变量同set

如果接变量的话,则将用户变量添加至环境变量中去

[root@xiaoftest ~]# name='test' //不使用export
[root@xiaoftest ~]# env |grep name //环境变量中去查询不到name
[root@xiaoftest ~]# echo $name
test
[root@xiaoftest ~]# export name="testtest" //使用export赋值
[root@xiaoftest ~]# echo $name
testtest
[root@xiaoftest ~]# env |grep name  //环境变量中能够查询到name
name=testtest //结果

 三、取消设置变量

unset

 

3 常用的系统变量

? 变量(常用)

echo $? 用于输出上一个指令执行是否错误,说明没有错误则显示0,如果是错误的话,就显示错误码

[root@xiaoftest /]# ls
bin   dev  home    lib         media  mnt  proc  sbin     sfcd  sys  usr
boot  etc  initrd  lost+found  misc   opt  root  selinux  srv   tmp  var
[root@xiaoftest /]# echo $?
0       //上一句指令执行成功
[root@xiaoftest /]# lls
-bash: lls: command not found
[root@xiaoftest /]# echo $?
127   //上一句指令执行失败,错误码127
[root@xiaoftest /]#

 RANDOM

[root@xiaoftest ~]# echo $RANDOM
12015

 

 4 通配符及组合按键

 

 

5 指令之间的分隔符

command1;command2

指令1和指令2都会执行

[root@xiaoftest sfcd]# ls;ls
d
d

command1&&command2

指令1成功的情况下指令2才会执行

[root@xiaoftest sfcd]# ls&&ls
d
d

command1||command2

指令1失败的情况下指令2才会执行

[root@xiaoftest sfcd]# lll||ls
-bash: lll: command not found
d

 

6 输出重定向

>,>> 都是屏幕输出到文件 ,>与>>的区别在于 >>是追加在原有文本内容的后面,而>则是覆盖

1> 成功的信息输入

2> 报错的信息输入

下面给三个例子

成功的信息输入到a 失败的信息输入到b

[root@xiaoftest sfcd]# lll 1>>a 2>>b
[root@xiaoftest sfcd]# lll 1>>a 2>>b
[root@xiaoftest sfcd]# lll 1>>a 2>>b
[root@xiaoftest sfcd]# cat a b
-bash: lll: command not found
-bash: lll: command not found
-bash: lll: command not found
[root@xiaoftest sfcd]#

成功和失败的信息都输入到c(注意同文件的失败信息输出是>&1)

[root@xiaoftest sfcd]# lll 1>>c 2>&1 //报错
[root@xiaoftest sfcd]# lll 1>>c 2>&1 //报错
[root@xiaoftest sfcd]# lll 1>>c 2>&1 //报错
[root@xiaoftest sfcd]# ll 1>>c 2>&1 //成功
[root@xiaoftest sfcd]# cat c
-bash: lll: command not found
-bash: lll: command not found
-bash: lll: command not found
total 12
-rw-r--r--  1 root root  0 Jan  1 20:09 a
-rw-r--r--  1 root root 90 Jan  1 20:09 b
-rw-r--r--  1 root root 90 Jan  1 20:11 c
-rwxrwxrwx  1 root root  3 Jan  1 14:57 d
[root@xiaoftest sfcd]#

成功的信息输入到a 失败的丢到垃圾桶

[root@xiaoftest sfcd]# ll 1>>e 2>>/dev/null
[root@xiaoftest sfcd]# lll 1>>e 2>>/dev/null
[root@xiaoftest sfcd]# cat e
total 12
-rw-r--r-- 1 root root 0 Jan 1 20:09 a
-rw-r--r-- 1 root root 90 Jan 1 20:09 b
-rw-r--r-- 1 root root 267 Jan 1 20:11 c
-rwxrwxrwx 1 root root 3 Jan 1 14:57 d
-rw-r--r-- 1 root root 0 Jan 1 20:13 e

 

 7 管道

对命令的结果进行一步一步的过滤及筛选

例如command1|command2|command3...

command1的结果传递给command2,command1进行过滤后传递给command3...最后输出

...悲剧 ,我本想是看grep reboot的结果命令少打了

[root@xiaoftest sfcd]# last |reboot //这第二个命令直接重启了,悲剧。

重新来!

[root@xiaoftest ~]# last|grep reboot
reboot   system boot  2.6.9-42.EL      Wed Jan  1 20:18          (00:00)
reboot   system boot  2.6.9-42.EL      Wed Jan  1 19:57          (00:20)
reboot   system boot  2.6.9-42.EL      Wed Jan  1 17:41          (02:35)
reboot   system boot  2.6.9-42.EL      Wed Jan  1 16:48          (00:52) 

在第一个命令结束后再根据第一个命令获得了reboot的记录
[root@xiaoftest ~]# last|grep reboot|wc -l
4
在第二个命令结束后再根据第二个命令获得了reboot的具体次数统计

 

 管道的常用指令

cut 用于筛选文档

[root @test /root ]# cut -d "分隔字符" [-cf] fields
参数说明:
-d  :后面接的是用来分隔的字符,默认是『空格符』
-c  :后面接的是『第几个字符』
-f  :后面接的是第几个区块?
范例:
[root @test /root]# cat /etc/passwd | cut -d ":" -f 1
将 passwd 这个档案里面,每一行里头的 : 用来作为竖线,
而列出第一个区块!也就是姓名所在啦!
[root @test /root]# last | cut -d " " -f1
以空格符为分隔,并列出第一个区间!
[root @test /root]# last | cut -c1-20
将 last 之后的数据,每一行的 1-20 个字符取出来!

-c 的使用

[root@xiaoftest ~]# last|cut -c1-15 //输出1-15个字符
root     pts/0
reboot   system
root     pts/0
reboot   system

-d -f的使用 -d是采用分隔符 -f用来读取第N个区间的(注意:这里是整体的划分,如本用例就是左右的整体划分)

[root@xiaoftest sfcd]# cat user
Mary/password
Peter/password
kittey/password

xiaoLi/password
xiaoming/password

test/password
test2/password
[root@xiaoftest sfcd]# cat user |cut -d "/" -f1 //第一区间
Mary
Peter
kittey

xiaoLi
xiaoming

test
test2
[root@xiaoftest sfcd]# cat user |cut -d "/" -f2 //第二区间
password
password
password

password
password

password
password

 

wc 用于统计

[root @test /root ]# wc [-lmw]
参数说明:
-l   :多少行
-m   :多少字符
-w   :多少字?
范例:
[root @test /root]# cat /etc/passwd | wc -l
这个档案里头有多少行?
[root @test /root]# cat /etc/passwd | wc -w
这个档案里头有多少字!?

 

uniq 用于去重

[root@xiaoftest sfcd]# last |cut -d " " -f 1 |uniq //使用了去重
root
reboot
root
reboot

 

[root@xiaoftest sfcd]# last |cut -d " " -f 1  //没有使用去重
root
root
root
reboot
root
reboot

 sort 排序(更加有利于去重与uniq配合使用较好)

[root@xiaoftest sfcd]# last |cut -d " " -f 1 |sort |uniq //先排序后去重

reboot
root
wtmp
[root@xiaoftest sfcd]#

 

 

 

 

 

转载于:https://www.cnblogs.com/sunfan1988/p/3500725.html

  • 点赞
  • 收藏
  • 分享
  • 文章举报
ackus42682 发布了0 篇原创文章 · 获赞 0 · 访问量 135 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: