标签:linux bash
bash基本特性及linux基本命令
1、命令历史
history
环境变量:
HISTSIZE:命令历史记录的条数;
HISTFILE:~/.bash_history;
HISTFILESIZE:命令历史文件记录历史的条数;
history -d OFFSET
-c
history #: 显示历史中最近的#条命令;
[root@master ~]# history 6 1003 HISSIZE 1004 history 1005 ifcfg 1006 history # 1007 history 10 1008 history 6
-a: 手动追加当前会话缓冲区的命令历史至历史文件中;
调用历史中的命令:
!#: 重复执行第#条指令;
!!: 执行上一条
[root@master ~]# !! ls a f1 hello.txt Public WorldCount.jar anaconda-ks.cfg file01 HTTP_20130313143750.dat Templates y? a.txt file02 install.log test y?.pub Desktop hello install.log.syslog test1.tar zookeeper.out Documents hello.link Music test.tar Downloads hello.ls Pictures Videos
!string
调用上一条命令的最后一个参数:
!$:
ESC, .
Alt+.
控制命令历史的记录方式:
环境变量:HISTCONTROL
[root@master ~]# echo $HISTCONTROL ignoredups
ignoredups:忽略重复的命令;连续且相同方为“重复”;
ignorespace:忽略所有以空白开头的命令;
ignoreboth:ignoredups, ignorespace;
export HISTCONTROL="ignorespace"
export HISTCONTROL="ignoredups"
修改环境变量值的方式:export 变量名="值"
变量赋值:把赋值符号后面的数据存储于变量名指向内存空间;
2、命令补全
bash执行命令:
内部命令:
外部命令:bash根据PATH环境变量定义的路径,自左而右在每个路径搜寻以给定命令名命名的文件,第一次找到即为要执行的命令;
直接补全:Tab,用户给定的字符串只有一条惟一对应的命令;
以用户给定的字符串为开头对应的命令不惟一,则再次Tab会给出列表;
3、 路径补全
把用户给出的字符串当做路径开头,并在其指定上级目录下搜索以指定的字符串开头的文件名;
如果惟一:则直接补全;
否则:再次Tab,给出列表;
4、命令行展开
~: 展开为用户的主目录
~USERNAME:展开为指定用户的主目录
{}:可承载一个以逗号分隔的列表,并将其展开为多个路径
/tmp/{a,b} = /tmp/a, /tmp/b
/tmp/{tom,jerry}/hi = /tmp/tom/hi, /tmp/jerry/hi
[root@master ~]# mkdir -p /tmp/{a,b}/{c,d}
[root@master ~]# ll /tmp/a/ c/ d/ [root@master ~]# ll /tmp/a/
5、命令的执行结果状态
成功
失败
0:成功
1-255:失败
bash使用特殊变量$?保存最近一条命令的执行状态结果:
查看命令执行是对还是错
[root@master ~]# echo $?
[root@master ~]# mkdir /aaaaaa/aaa mkdir: cannot create directory `/aaaaaa/aaa‘: No such file or directory
[root@master ~]# echo $? 1
下面是成功的
[root@master ~]# echo $? 0
程序执行有两类结果:
程序的返回值;
程序的执行状态结果;
下面是linux基本命令。
(1)目录管理类命令:
cd, pwd, ls
mkdir, rmdir, tree
mkdir [options] /path/to/somewhere
-p: 存在于不报错,且可自动创建所需的各目录,递归创建目录
[root@master ~]# mkdir -p /root/a/a/a/a
-v: 显示详细信息
[root@master ~]# mkdir -v /root/t mkdir: created directory `/root/t‘
[root@master ~]# mkdir -p -v /tmp/{a,b,v}/{c,d},{a,b} mkdir: created directory `/tmp/a/c,a‘ mkdir: created directory `/tmp/a/c,b‘ mkdir: created directory `/tmp/a/d,a‘ mkdir: created directory `/tmp/a/d,b‘ mkdir: created directory `/tmp/b/c,a‘ mkdir: created directory `/tmp/b/c,b‘ mkdir: created directory `/tmp/b/d,a‘ mkdir: created directory `/tmp/b/d,b‘ mkdir: created directory `/tmp/v‘ mkdir: created directory `/tmp/v/c,a‘ mkdir: created directory `/tmp/v/c,b‘ mkdir: created directory `/tmp/v/d,a‘ mkdir: created directory `/tmp/v/d,b‘
-m MODE: 创建目录时直接指定权限;
tree基本用法:
[root@master ~]# yum install tree Loaded plugins: fastestmirror, refresh-packagekit, security Setting up Install Process Loading mirror speeds from cached hostfile * base: centos.ustc.edu.cn * extras: centos.ustc.edu.cn * updates: centos.ustc.edu.cn Resolving Dependencies --> Running transaction check ---> Package tree.x86_64 0:1.5.3-3.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved ======================================================================================== Package Arch Version Repository Size ======================================================================================== Installing: tree x86_64 1.5.3-3.el6 base 36 k Transaction Summary ======================================================================================== Install 1 Package(s) Total download size: 36 k Installed size: 65 k Is this ok [y/N]: y Downloading Packages: tree-1.5.3-3.el6.x86_64.rpm | 36 kB 00:00 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : tree-1.5.3-3.el6.x86_64 1/1 Verifying : tree-1.5.3-3.el6.x86_64 1/1 Installed: tree.x86_64 0:1.5.3-3.el6 Complete!
[root@master ~]# tree /root/a /root/a ├── a │ └── a │ └── a ├── test │ └── install.log ├── test1.tar └── test.tar
4 directories, 3 files
-d: 只显示目录
-L level:指定显示的层级数目
-P pattern: 只显示由指定pattern匹配到的路径;
rmdir:删除空目录
[root@master ~]# mkdir /root/bbbb
[root@master ~]# rmdir /root/bbbb
rmdir [OPTION]... DIRECTORY...
-v: 显示过程;
练习:
(1) 如何创建/tmp/x/y1, /tmp/x/y2, /tmp/x/y1/a, /tmp/x/y1/b, /tmp/x/y2/a, /tmp/x/y2/b
mkdir /tmp/x/{y1,y2}/{a,b}
(2) 如何创建x_m, y_m, x_n, y_n
mkdir {x,y}_{m,n}
(3) 如何创建/tmp/bin, /tmp/sbin, /tmp/usr, /tmp/usr/bin, /tmp/usr/sbin
mkdir -p /tmp/{bin,sbin,usr/{bin,sbin}}
(2)文本文件查看类命令:
cat, tac
more, less, tail, head
more
more [OPTIONS...] FILE...
-d: 显示翻页及退出提示
[root@master ~]# more /usr/local/zookeeper-3.4.6/conf/log4j.properties # Define some default values that can be overridden by system properties zookeeper.root.logger=INFO, CONSOLE zookeeper.console.threshold=INFO zookeeper.log.dir=. zookeeper.log.file=zookeeper.log zookeeper.log.threshold=DEBUG zookeeper.tracelog.dir=. zookeeper.tracelog.file=zookeeper_trace.log # # ZooKeeper Logging Configuration # # Format is "<default threshold> (, <appender>)+ # DEFAULT: console appender only log4j.rootLogger=${zookeeper.root.logger} # Example with rolling log file #log4j.rootLogger=DEBUG, CONSOLE, ROLLINGFILE
注意:按着b 查看前面的
less
less [OPTIONS...] FILE...
head
head [OPTION]... [FILE]...
默认显示10行
[root@master ~]# head /etc/rc.d/rc.sysinit #!/bin/bash # # /etc/rc.d/rc.sysinit - run once at boot time # # Taken in part from Miquel van Smoorenburg‘s bcheckrc. # HOSTNAME=$(/bin/hostname) set -m
-c #: 指定获取前#字节
[root@master ~]# head -c 11 /etc/rc.d/rc.sysinit #!/bin/bash
-n #: 指定获取前#行
[root@master ~]# head -n 10 /etc/rc.d/rc.sysinit #!/bin/bash # # /etc/rc.d/rc.sysinit - run once at boot time # # Taken in part from Miquel van Smoorenburg‘s bcheckrc. # HOSTNAME=$(/bin/hostname) set -m
-#:
上面等价于
[root@master ~]# head -10 /etc/rc.d/rc.sysinit #!/bin/bash # # /etc/rc.d/rc.sysinit - run once at boot time # # Taken in part from Miquel van Smoorenburg‘s bcheckrc. # HOSTNAME=$(/bin/hostname) set -m
tail 与head命令类似
tail [OPTION]... [FILE]...
-c #: 指定获取后#字节
-n #: 指定获取后#行
-#:
-f: 跟踪显示文件新追加的内容;
(3)文件的时间戳管理工具:
touch
文件:metadata, data
查看文件状态:stat
[root@master ~]# echo a >> a.txt
[root@master ~]# stat a.txt File: `a.txt‘ Size: 4 Blocks: 8 IO Block: 4096 regular file Device: 802h/2050d Inode: 140905 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2016-06-30 06:38:00.929058651 -0700 Modify: 2016-07-04 07:09:20.470056778 -0700 Change: 2016-07-04 07:09:20.470056778 -0700
三个时间戳:
access time:访问时间,简写为atime,读取文件内容
modify time: 修改时间, mtime,改变文件内容(数据)
change time: 改变时间, ctime,元数据发生改变
touch命令:
touch [OPTION]... FILE...
-a: only atime
-m: only mtime
-t STAMP:
[[CC]YY]MMDDhhmm[.ss]
-c: 如果文件不存,则不予创建
[root@master ~]# touch a.txt
[root@master ~]# stat a.txt File: `a.txt‘ Size: 4 Blocks: 8 IO Block: 4096 regular file Device: 802h/2050d Inode: 140905 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2016-07-04 07:11:53.289055027 -0700 Modify: 2016-07-04 07:11:53.289055027 -0700 Change: 2016-07-04 07:11:53.289055027 -0700
总结:细节决定成败!一定要好好练习哦,linux基础非常重要!
本文出自 “梁小明的博客” 博客,请务必保留此出处http://7038006.blog.51cto.com/7028006/1795746
标签:linux bash
原文地址:http://7038006.blog.51cto.com/7028006/1795746