Linux基础(2)-基础命令和bash的基础特性(1)
基础命令
命令历史
命令历史的管理
登陆 shell 时,会读取命令历史文件中记录下的命令: ~/.bash_history 。
登陆进 shell 后,新执行的命令只会记录在缓存中,这些命令会在用户退出时追加保存到命令历史文件中。
history使用
[root@zze ~]# history1 cd ~2 ll3 history
history:查看所有历史。
[root@zze ~]# history 23 history4 history 2
history #:查看最近 # 条历史。
[root@zze ~]# history -d 4 [root@zze ~]# history1 cd ~2 ll3 history4 history -d 45 history
history -d #:删除历史中指定的第 # 条命令。
[root@zze ~]# history -c [root@zze ~]# history1 history
history -c:清空历史。
快捷操作
[root@zze ~]# history1 history2 ll3 history [root@zze ~]# !2 ll total 20 -rw-------. 1 root root 1197 Jan 14 04:52 anaconda-ks.cfg -rw-r--r--. 1 root root 9458 Jan 14 04:52 install.log -rw-r--r--. 1 root root 3161 Jan 14 04:51 install.log.syslog
!#:调用历史中第 # 条命令。
[root@zze ~]# !l ll total 20 -rw-------. 1 root root 1197 Jan 14 04:52 anaconda-ks.cfg -rw-r--r--. 1 root root 9458 Jan 14 04:52 install.log -rw-r--r--. 1 root root 3161 Jan 14 04:51 install.log.syslog
!string:调用历史中最近一个以 string 开头的命令。
[root@zze ~]# !! ll total 20 -rw-------. 1 root root 1197 Jan 14 04:52 anaconda-ks.cfg -rw-r--r--. 1 root root 9458 Jan 14 04:52 install.log -rw-r--r--. 1 root root 3161 Jan 14 04:51 install.log.syslog
!!:上一条命令。
[root@zze ~]# ls -l /etc/profile -rw-r--r--. 1 root root 2008 Jan 14 05:28 /etc/profile [root@zze ~]# cat !$
!$:调用上一条命令的最后一个参数。
使用 ESC,. 可直接显示上一条命令参数。控制命令的记录方式
命令的记录方式通过环境变量 HISTCONTROL 控制,可选三个值:
ignoredups :忽略重复(连续且相同的命令)。
ignorespace :忽略以空白字符开头的命令。
ignoreboth :忽略以上两项。
修改环境变量值的方式: export 变量名="值" ,默认只对当前会话生效。[root@zze ~]# ls -l /etc/profile -rw-r--r--. 1 root root 2008 Jan 14 05:28 /etc/profile [root@zze ~]# history1 history
修改环境变量 HISTCONTROL
相关环境变量
HISTSIZE :命令历史记录的条数。
HISTFILE :命令历史文件路径, ~/.bash_history 。
HISTFILESIZE :命令历史文件记录历史的条数。
目录操作
切换目录-cd
[root@zze etc]# cd /etc/ [root@zze etc]# pwd /etc
cd [path]:切换到指定目录
[root@zze etc]# cd .. [root@zze /]#
cd ..:切换到上级目录
显示当前所在路径-pwd
[root@zze ~]# pwd /root
pwd:显示当前所在路径
显示文件-ls
[root@zze ~]# ls -a . .. anaconda-ks.cfg .bash_history .bash_logout .bash_profile .bashrc .cache .cshrc install.log install.log.syslog .mysql_history .mysql_secret .oracle_jre_usage .pip .rediscli_history .tcshrc
ls -a [path]:显示所有文件,包含隐藏文件,包括 "." 和 ".." 。
[root@zze ~]# ls -A anaconda-ks.cfg .bash_history .bash_logout .bash_profile .bashrc .cache .cshrc install.log install.log.syslog .mysql_history .mysql_secret .oracle_jre_usage .pip .rediscli_history .tcshrc
ls -A [path]:显示所有文件,包含隐藏文件,不包括 "." 和 ".." 。
[root@zze ~]# ls -l total 20 -rw-------. 1 root root 1197 Jan 14 04:52 anaconda-ks.cfg -rw-r--r--. 1 root root 9458 Jan 14 04:52 install.log -rw-r--r--. 1 root root 3161 Jan 14 04:51 install.log.syslog
ls -l [path]:长格式显示文件,不包含隐藏文件。
[root@zze ~]# ls -lh total 20K -rw-------. 1 root root 1.2K Jan 14 04:52 anaconda-ks.cfg -rw-r--r--. 1 root root 9.3K Jan 14 04:52 install.log -rw-r--r--. 1 root root 3.1K Jan 14 04:51 install.log.syslog [root@zze ~]# ll -h total 20K -rw-------. 1 root root 1.2K Jan 14 04:52 anaconda-ks.cfg -rw-r--r--. 1 root root 9.3K Jan 14 04:52 install.log -rw-r--r--. 1 root root 3.1K Jan 14 04:51 install.log.syslog
ls -h [path]:单位换算,通常和 "-l" 一起使用。
[root@zze local]# ls -ld /etc/ drwxr-xr-x. 55 root root 4096 Jan 14 06:35 /etc/ [root@zze local]# ll -d /etc/ drwxr-xr-x. 55 root root 4096 Jan 14 06:35 /etc/ [root@zze local]# ll -d drwxr-xr-x. 15 root root 4096 Jan 14 05:46 .
ls -d [path]:显示目录自身相关属性,通常和 "-l" 一起使用。
[root@zze local]# ls bin etc games include java lib lib64 libexec redis sbin share src tomcat [root@zze local]# ls -r tomcat src share sbin redis libexec lib64 lib java include games etc bin
ls -r [path]:反序显示。
[root@zze ~]# ls -R /root/ /root/: anaconda-ks.cfg install.log install.log.syslog
ls -R [path]:递归显示文件夹及子文件夹所有文件。
创建目录-mkdir
[root@zze ~]# mkdir testdir [root@zze ~]# ls anaconda-ks.cfg install.log install.log.syslog testdir
mkdir <path>:创建目录。
[root@zze testdir]# mkdir a/b mkdir: cannot create directory `a/b': No such file or directory [root@zze testdir]# mkdir -p a/b [root@zze testdir]# ls -R .: a./a: b
mkdir -p <path>:递归创建目录(不存在则创建)。
[root@zze testdir]# ll total 0 [root@zze testdir]# mkdir -pv a/b mkdir: created directory `a' mkdir: created directory `a/b'
mkdir -v <path>:显示详细信息。
[root@zze testdir]# mkdir -m 664 a [root@zze testdir]# ll total 4 drw-rw-r--. 2 root root 4096 Jan 15 11:51 a
mkdir -m <path>:创建目录时直接指定权限。
[root@zze testdir]# mkdir a/ a/b a/b/c [root@zze testdir]# tree a/ a/ └── b└── c
mkdir <path...>:同时创建多个目录
删除目录-rmdir
[root@zze testdir]# ls -R a/ a/: ba/b: [root@zze testdir]# rmdir a/ rmdir: failed to remove `a/': Directory not empty [root@zze testdir]# rmdir a/b/ [root@zze testdir]# rmdir a/ [root@zze testdir]# ll total 0
rmdir <path>:删除空目录。
[root@zze testdir]# rmdir -p a/b/c/ [root@zze testdir]# ll total 0
rmdir -p <path>:递归删除所有空目录。
树状显示目录结构-tree
[root@zze testdir]# tree a/ a/ └── b├── c└── test.txt2 directories, 1 file
tree <path>:树状显示目录及目录下的文件。
[root@zze testdir]# tree -d a/ a/ └── b└── c2 directories
tree -d <path>:树状显示目录结构,不包含文件。
[root@zze testdir]# tree -L 1 a/ a/ └── b
tree -L # <path>:树状显示仅 # 深度结构。
查看文本文件
正序输出文件内容-cat
[root@zze testdir]# cat test.txt 1 2 3 4 5 6 7 8 9
cat <filename>:正序查看指定文件。
反序输出文件内容-tac
[root@zze testdir]# tac test.txt 9 8 7 6 5 4 3 2 1
tac <filename>:反序查看指定文件。
按页查看-more
[root@zze ~]# more /etc/profile # /etc/profile# System wide environment and startup programs, for login setup # Functions and aliases go in /etc/bashrc# It's NOT a good idea to change this file unless you know what you # are doing. It's much better to create a custom.sh shell script in # /etc/profile.d/ to make custom changes to your environment, as this # will prevent the need for merging in future updates.pathmunge () {case ":${PATH}:" in*:"$1":*);;*)if [ "$2" = "after" ] ; thenPATH=$PATH:$1elsePATH=$1:$PATHfiesac }if [ -x /usr/bin/id ]; thenif [ -z "$EUID" ]; then# ksh workaroundEUID=`id -u`UID=`id -ru`fiUSER="`id -un`"LOGNAME=$USERMAIL="/var/spool/mail/$USER" fi# Path manipulation if [ "$EUID" = "0" ]; thenpathmunge /sbinpathmunge /usr/sbinpathmunge /usr/local/sbin elsepathmunge /usr/local/sbin afterpathmunge /usr/sbin afterpathmunge /sbin after fiHOSTNAME=`/bin/hostname 2>/dev/null` HISTSIZE=1000 if [ "$HISTCONTROL" = "ignorespace" ] ; thenexport HISTCONTROL=ignoreboth elseexport HISTCONTROL=ignoredups fiexport PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL --More--(63%)
more <filename>:按页查看指定文件。
[root@zze ~]# more -d /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 -mif [ -f /etc/sysconfig/network ]; then. /etc/sysconfig/network fi if [ -z "$HOSTNAME" -o "$HOSTNAME" = "(none)" ]; thenHOSTNAME=localhost fiif [ ! -e /proc/mounts ]; thenmount -n -t proc /proc /procmount -n -t sysfs /sys /sys >/dev/null 2>&1 fi if [ ! -d /proc/bus/usb ]; thenmodprobe usbcore >/dev/null 2>&1 && mount -n -t usbfs /proc/bus/usb /proc/bus/usb elsemount -n -t usbfs /proc/bus/usb /proc/bus/usb fi#remount /dev/shm to set attributes from fstab #669700 mount -n -o remount /dev/shm >/dev/null 2>&1 #remount /proc to set attributes from fstab #984003 mount -n -o remount /proc >/dev/null 2>&1. /etc/init.d/functionsPLYMOUTH= [ -x /bin/plymouth ] && PLYMOUTH=yes# Check SELinux status SELINUX_STATE= if [ -e "/selinux/enforce" ] && [ "$(cat /proc/self/attr/current)" != "kernel" ]; thenif [ -r "/selinux/enforce" ] ; thenSELINUX_STATE=$(cat "/selinux/enforce")else# assume enforcing if you can't read itSELINUX_STATE=1fi fiif [ -n "$SELINUX_STATE" -a -x /sbin/restorecon ] && __fgrep " /dev " /proc/mounts >/dev/null 2>&1 ; then/sbin/restorecon -R -F /dev 2>/dev/null fidisable_selinux() {echo $"*** Warning -- SELinux is active"
more -d <filename>:带提示信息查看,显示翻页及退出信息。
[root@zze temp]# more +5 test.txt 55555555555555555 666666666666666666666 77777777777777777777 88888888888888888888
more +# <filename>:从第 # 行开始查看。
- 操作方式:
- B :向前翻页,SPACE :向后翻页,RETURN :下一行,Q:退出。
- 比例显示:
- 按字符统计。
一旦翻完不可回转。
按页查看-less
按页查看,操作方式和 man 一致,因为 man 内部实际上也是使用 less 打开文本文件。
- 操作:
- Space,^V,^F:向文件尾部翻屏。
- b,^B:向文件首部翻屏。
- d,^D:向文件尾部翻半屏。
- u,^U:向文件首部翻半屏。
- Return,^N,e,^E,j,^J:向文件尾部翻一行。
- y,^Y,^P,k,^K:向文件首部翻一行。
- q:退出。
- #:跳转到第 # 行。
- 1G:回到文件首部。
- G:跳至文件尾部。
- /KEYWORD:以 KEYWORD 指定的字符串为关键字,从当前位置向文件尾部搜索,不区分大小写。n:下一个,N:上一个。
- ?KEYWORD:以 KEYWORD 指定的字符串为关键字,从当前位置向文件首部搜索,不区分大小写。n:下一个,N:上一个。
翻完可回转。
查看文件头部-head
[root@zze temp]# head test.txt 11111111111111111111111111111111111 22222222222222222222222222222222222 333333333333333 4444444444444444444 55555555555555555 666666666666666666666 77777777777777777777 88888888888888888888 9999999999 10
head <filename>:默认输出前 10 行。
[root@zze temp]# head -c 10 test.txt 1111111111
head -c # <filename>:指定输出前 # 字节。
[root@zze temp]# head -n 5 test.txt 11111111111111111111111111111111111 22222222222222222222222222222222222 333333333333333 4444444444444444444 55555555555555555
head [-n #|-#] <filename>:指定输出前 # 行。
查看文件尾部-tail
[root@zze temp]# tail test.txt 11 1222222222222222 1333333333333333 1444444444444 15 16 177777777 18 19 2000
tail <filename>:默认输出后 10 行。
[root@zze temp]# tail -c 10 test.txt 8 19 2000
tail -c # <filename>:指定输出后 # 字节。
[root@zze temp]# tail -n 5 test.txt 16 177777777 18 19 2000
tail [-n #|-#] <filename>:指定输出后 # 行。
[root@zze temp]# tail -f test.txt 11 1222222222222222 1333333333333333 1444444444444 15 16 177777777 18 19 2000 new line
tail -f <filename>:跟踪显示文件新追加的内容。
文件时间戳管理工具
数据
文件的数据分为元数据和内容数据,元数据可以看做文件的状态描述信息,内容数据则是文件的真实内容。
查看文件状态-stat
[root@zze temp]# stat test.txt File: `test.txt' Size: 292 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 1966102 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2019-01-13 09:57:08.339394446 +0800 Modify: 2019-01-13 09:57:08.339394446 +0800 Change: 2019-01-13 09:57:08.339394446 +0800
stat <filename>
- 三个时间戳:
-
access time (atime):访问时间,读取文件内容时。
modify time (mtime):修改时间,修改文件内容时。
change time (ctime):改变时间,元数据发生改变。
修改时间戳-touch
[root@zze temp]# stat test.txt File: `test.txt' Size: 292 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 1966102 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2019-01-13 09:57:08.339394446 +0800 Modify: 2019-01-13 09:57:08.339394446 +0800 Change: 2019-01-13 09:57:08.339394446 +0800 [root@zze temp]# touch test.txt [root@zze temp]# stat test.txt File: `test.txt' Size: 292 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 1966102 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2019-01-13 10:10:07.019393669 +0800 Modify: 2019-01-13 10:10:07.019393669 +0800 Change: 2019-01-13 10:10:07.019393669 +0800
touch [-t time] <filename>:修改 atime 和 mtime 为指定时间,不指定 time 则默认修改为当前时间。
[root@zze temp]# touch -m -t 201810101200.59 test.txt [root@zze temp]# stat test.txt File: `test.txt' Size: 292 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 1966102 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2019-01-13 10:10:07.019393669 +0800 Modify: 2018-10-10 12:00:59.000000000 +0800 Change: 2019-01-13 10:18:00.999393544 +0800
touch -m [-t time] <filename>:修改 mtime 为指定时间,不指定 time 则默认修改为当前时间。
[root@zze temp]# touch -a -t 201811101200.59 test.txt [root@zze temp]# stat test.txt File: `test.txt' Size: 292 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 1966102 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2018-11-10 12:00:59.000000000 +0800 Modify: 2018-10-10 12:00:59.000000000 +0800 Change: 2019-01-13 10:23:43.380393790 +0800
touch -a [-t time] <filename>:修改 atime 为指定时间,不指定 time 则默认修改为当前时间。
[root@zze temp]# ls test.txt [root@zze temp]# touch a.txt [root@zze temp]# ls a.txt test.txt
touch <filename>:当 filename 指定的文件不存在时则创建文件。
[root@zze temp]# touch -c a.txt [root@zze temp]# ls a.txt test.txt [root@zze temp]# touch -c b.txt [root@zze temp]# ls a.txt test.txt
touch -c <filename>:当 filename 指定的文件不存在时不创建文件。
time 的格式为 "yyyyMMddHHmmss.ss" ,例:“2019年1月1号2点35分34秒”为 "201901010235.34"。
bash的基础特性
补全-TAB键
命令补全
一次 TAB:如果用户给定的字符串只有一条唯一对应的命令,一次 TAB 则直接补全。
两次 TAB:如果用户给定的字符串开头对应的命令不唯一,再次 TAB 则会显示所有以该字符串开头的命令。
[root@zze ~]# cl clear clock clockdiff cloog
两次 TAB
路径补全
把用户给定的字符串当做路径开头,并在其指定目录下搜索以指定字符串开头的文件名。如果唯一,则直接补全,否则再次 TAB 显示列表。
命令行展开
[root@zze testdir]# cd ~ [root@zze ~]#
~:展开为当前用户主目录。
[root@zze testdir]# cd ~root/ [root@zze ~]#
~[user]:展开为 user 的主目录。
/tmp/{a,b} -> /tmp/a , /tmp/b /tmp/{a,b}/c -> /tmp/a/c , /tmp/b/c
{}:可承载一个逗号分隔的列表,并将其展开为多个路径。
[root@zze testdir]# mkdir tmp/x/{y1,y2}/{a,b} mkdir: cannot create directory `tmp/x/y1/a': No such file or directory mkdir: cannot create directory `tmp/x/y1/b': No such file or directory mkdir: cannot create directory `tmp/x/y2/a': No such file or directory mkdir: cannot create directory `tmp/x/y2/b': No such file or directory [root@zze testdir]# mkdir -p tmp/x/{y1,y2}/{a,b} [root@zze testdir]# tree tmp/ tmp/ └── x├── y1│ ├── a│ └── b└── y2├── a└── b7 directories, 0 files
result
练习2:如何一次性创建 'x_m' , 'y_m' , 'x_n' , 'y_n' ?
[root@zze testdir]# mkdir -p {x,y}_{m,n} [root@zze testdir]# ls x_m x_n y_m y_n
result
练习3:如何一次性创建 'tmp/bin' , 'tmp/sbin' , 'tmp/usr/bin' , 'tmp/usr/sbin' ?
[root@zze testdir]# mkdir -p tmp/{bin,sbin,usr/{bin,sbin}} [root@zze testdir]# tree tmp tmp ├── bin ├── sbin └── usr├── bin└── sbin5 directories, 0 files
result
命令的执行状态结果
bash 使用特殊变量 $? 保存最近一条命令的执行状态结果:
[root@zze testdir]# mkdir tmp [root@zze testdir]# echo $? 0 [root@zze testdir]# mkdir tmp mkdir: cannot create directory `tmp': File exists [root@zze testdir]# echo $? 1
0:成功,1-255:失败
转载于:https://www.cnblogs.com/zze46/p/10271641.html
Linux基础(2)-基础命令和bash的基础特性(1)相关推荐
- 网络基础:Ping命令的7种基础用法,掌握了秒变大神!
现在不管安防工程还是弱电工程,与网络的联系也越来越密切了,网络基础知识中的网络命令ping命令在项目中是使用频率最高的,一般我们用的都是它的基本功能,今天和大家来详细看下Ping命令的7个基础用法,掌 ...
- linux下如何bash文件,Linux系统的文件管理命令及bash的相关工作特性
一.Linux系统的文件管理命令: 1 \pwd:printing working directory 2 \cd:change directory cd [/PATH/TO/SOMEDIR ...
- linux找不到命令nginx,-bash: nginx: 未找到命令 (command not found) 解决方案
昨天在linux中安装了 nginx ,并按照网上教程 进行启动 如: ps -ef | grep nginx 可以查看到 我就想重新加载一次 如:提示我找不到 nginx 命令 -c参数指定了要加载 ...
- Linux上安装pstree命令(-bash: pstree: command not found)
一.pstree命令的安装 1.在 Mac OS上 brew install pstree 2.在 Fedora/Red Hat/CentOS yum -y install psmisc 3.在 Ub ...
- Linux下执行.sh命令出现-bash: ./bin/start.sh: /bin/bash^M: bad interpreter: No such file or directory...
原因是 文件的格式是dos,修改为unix 就OK了 查看文件格式 用vim 打开出错的文件 按 ESC键 再按shift+冒号 输入 set ff 回车 可以看见 该文件 ...
- Linux下执行.sh命令出现-bash: ./bin/start.sh: /bin/bash^M: bad interpreter: No such file or directory 问题解决
出现如下这个问题,一般都是文本格式是dos格式,需改为unix格式. 查看文件格式 用 vim 打开出错的文件 按 ESC键 再按shift+冒号 输入 set ff 回车 ...
- linux shell两条命令,在bash shell终端中执行命令时,若要将多条命令书写在一行中,每个命令之间应用( )符号进行分隔。...
[单选题]DE 杆的角速度的转向为: [判断题]中碳钢的含碳量在0.25%~0.6%之间 [多选题]在大众餐馆吃饭时,应该先( ),之后才能抽烟. [单选题]乳腺癌改良根治术后对患侧上肢肿胀的护理,错 ...
- 每个程序员都应该知道的Linux基础知识!全文4900字,没有耐心的不建议看 ——( Linux文件系统 + 系统管理类命令 + bash基础特性)
文章目录 - Linux的文件系统 - LSB - FHS - 根文件系统 - 对根文件系统下目录的概述: /boot /bin /sbin /lib /lib64 /etc /home /root ...
- linux超级基础系列——什么是shell? bash和shell有什么关系
什么是Shell? shell是你(用户)和Linux(或者更准确的说,是你和Linux内核)之间的接口程序.你在提示符下输入的每个命令都由shell先解释然后传给Linux内核. ...
最新文章
- Ubuntu中Atom安装与使用
- 真「祖传代码」!你的 GitHub 代码已打包运往北极,传给 1000 年后人类
- 【ShareCode】不错的技术文章 -- 如何使用异或(XOR)运算找到数组中缺失的数?...
- 深入理解SELinux SEAndroid
- Winform 导出成Excel打印代码
- python动态图片转字符画_GIF动态图,视频?都能用Python转换成字符画图像
- seata 如何开启tcc事物_微服务分布式事务4种解决方案实战
- mysql debug log_PHP代码调试与日志
- 知乎万赞回答!如何在一周内快速摸清一个行业?
- 技术圈儿007---Redis 生产架构选型解决方案
- 【第一篇】Volley的使用之json请求
- 基于隐式调用风格的kwic实现
- java ajax传值到后台_java ajax发送数据到后台,中文乱码
- matlab2016b版本安装
- android 校园 源码,android 校园交易APP
- xupt2017 I 威尔逊的无底胃
- 一个域名最多能对应几个IP地址?,一个IP地址可以绑定几个域名?
- linux 内核启动参数
- 网络编程懒人入门(七):深入浅出,全面理解HTTP协议
- serv u服务器显示图片,Serv-U、FTP服务器架设教程、蓝芯E管家图片储存教程