#cd ~ 回到当前用户的家目录
[root@VM_168_102_centos etc]# pwd /etc //当前所在目录 [root@VM_168_102_centos etc]# cd ~ //回到当前用户家木里 [root@VM_168_102_centos ~]# pwd /root //当前用户家目录 [root@VM_168_102_centos ~]# su wanghan [wanghan@VM_168_102_centos rott]$ cd ~ //回到当前用户家木里 [wanghan@VM_168_102_centos ~]$ pwd /home/wanghan //当前用户家目录 [wanghan@VM_168_102_centos ~]$
#cd - 回到上一次所在目录
[wanghan@VM_168_102_centos ~]$ cd /etc //切换到/etc目录 [wanghan@VM_168_102_centos etc]$ cd /tmp //从/etc目录切换到/tmp目录 [wanghan@VM_168_102_centos tmp]$ cd - //从当前/tmp回到上一次所在目录 /etc //回到了/etc [wanghan@VM_168_102_centos etc]$ cd - //再从/etc回到上一次所在目录 /tmp //回到了/tmp [wanghan@VM_168_102_centos tmp]$
#pwd命令:查看当前工作目录的完整路径
[wanghan@VM_168_102_centos /]$ pwd / [wanghan@VM_168_102_centos /]$ cd /etc [wanghan@VM_168_102_centos etc]$ pwd /etc [wanghan@VM_168_102_centos etc]$
#mkdir命令:用来创建指定的名称的目录,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录。
mkdir [选项] 目录…
[wanghan@VM_168_102_centos tmp]$ ls agent_cmd.sock ap_1002.pid ap_1004.pid ap_1005.pid ap_1007.pid ap_1008.pid ap_1014.pid [wanghan@VM_168_102_centos tmp]$ mkdir /tmp/test [wanghan@VM_168_102_centos tmp]$ ls agent_cmd.sock ap_1002.pid ap_1004.pid ap_1005.pid ap_1007.pid ap_1008.pid ap_1014.pid test
mkdir –p 目录…
[wanghan@VM_168_102_centos tmp]$ ls agent_cmd.sock ap_1002.pid ap_1004.pid ap_1005.pid ap_1007.pid ap_1008.pid ap_1014.pid test [wanghan@VM_168_102_centos tmp]$ mkdir /tmp/wanghan/test mkdir: cannot create directory `/tmp/wanghan/test‘: No such file or directory [wanghan@VM_168_102_centos tmp]$ mkdir -p /tmp/wanghan/test [wanghan@VM_168_102_centos tmp]$ ls agent_cmd.sock ap_1002.pid ap_1004.pid ap_1005.pid ap_1007.pid ap_1008.pid ap_1014.pid test wanghan [wanghan@VM_168_102_centos tmp]$ ls wanghan test [wanghan@VM_168_102_centos tmp]$
若创建的路径中有尚不再存的目录即可按照路径全部创建
mkdir –v 目录…
[wanghan@VM_168_102_centos tmp]$ mkdir -pv /tmp/a/b/c/d/e/f/abcd mkdir: created directory `/tmp/a‘ mkdir: created directory `/tmp/a/b‘ mkdir: created directory `/tmp/a/b/c‘ mkdir: created directory `/tmp/a/b/c/d‘ mkdir: created directory `/tmp/a/b/c/d/e‘ mkdir: created directory `/tmp/a/b/c/d/e/f‘ mkdir: created directory `/tmp/a/b/c/d/e/f/abcd‘
显示创建目录过程的详细信息
#rmdir命令:删除已空的目录
[wanghan@VM_168_102_centos tmp]$ ls a agent_cmd.sock ap_1002.pid ap_1004.pid ap_1005.pid ap_1007.pid ap_1008.pid ap_1014.pid ceshi test wanghan [wanghan@VM_168_102_centos tmp]$ ls test test_1 [wanghan@VM_168_102_centos tmp]$ rmdir /tmp/test rmdir: failed to remove `/tmp/test‘: Directory not empty //该目录非空 [wanghan@VM_168_102_centos tmp]$ rmdir /tmp/test/test_1 [wanghan@VM_168_102_centos tmp]$ ls test [wanghan@VM_168_102_centos tmp]$ rmdir /tmp/test [wanghan@VM_168_102_centos tmp]$ ls a agent_cmd.sock ap_1002.pid ap_1004.pid ap_1005.pid ap_1007.pid ap_1008.pid ap_1014.pid ceshi wanghan
#shell中的引用
‘ ’:强引用,变量替换不会进行
[wanghan@VM_168_102_centos tmp]$ num=123 [wanghan@VM_168_102_centos tmp]$ echo $num 123 [wanghan@VM_168_102_centos tmp]$ echo ‘$num‘ $num
“ ”:若引用,能够执行变量替换
[wanghan@VM_168_102_centos tmp]$ num=456 [wanghan@VM_168_102_centos tmp]$ echo num num [wanghan@VM_168_102_centos tmp]$ echo "num" num [wanghan@VM_168_102_centos tmp]$
· ·:命令替换,引用命令的执行结果;命令替换的另外一符号:$(命令);
[wanghan@VM_168_102_centos tmp]$ echo ls /tmp ls /tmp [wanghan@VM_168_102_centos tmp]$ echo `ls /tmp` a agent_cmd.sock ap_1002.pid ap_1004.pid ap_1005.pid ap_1007.pid ap_1008.pid ap_1014.pid ceshi wanghan
结合使用:
[wanghan@VM_168_102_centos tmp]$ echo $test tmp [wanghan@VM_168_102_centos tmp]$ echo "This is $test" This is tmp [wanghan@VM_168_102_centos tmp]$ echo `ls /$test` a agent_cmd.sock ap_1002.pid ap_1004.pid ap_1005.pid ap_1007.pid ap_1008.pid ap_1014.pid ceshi wanghan [wanghan@VM_168_102_centos tmp]$ echo "/tmp:`ls /$test`" /tmp:a agent_cmd.sock ap_1002.pid ap_1004.pid ap_1005.pid ap_1007.pid ap_1008.pid ap_1014.pid ceshi wanghan
#创建一个一当前日期时间创建一个目录
[wanghan@VM_168_102_centos tmp]$ date=`date +%Y-%m-%d-%H%M%S` [wanghan@VM_168_102_centos tmp]$ mkdir -pv /tmp/$date mkdir: created directory `/tmp/2014-08-04-212053‘ [wanghan@VM_168_102_centos tmp]$ ls 2014-08-04-212053 a agent_cmd.sock ap_1002.pid ap_1004.pid ap_1005.pid ap_1007.pid ap_1008.pid ap_1014.pid ceshi wanghan [wanghan@VM_168_102_centos tmp]$
#history命令:查看在当前shell进程的保存在缓冲区中的执行过的命令列表,当shell退出时缓冲区的命令会自动化保存至当前用户的家目录中的.bash_history文件中。
[root@VM_168_102_centos ~]# su wanghan
[wanghan@VM_168_102_centos rott]$ history
    1  exit
    2  history
[wanghan@VM_168_102_centos rott]$ who
root     pts/0        Aug  4 19:11 (222.36.253.150)
[wanghan@VM_168_102_centos rott]$ pwd
/root
[wanghan@VM_168_102_centos rott]$ history
    1  exit
    2  history
    3  who
    4  pwd
    5  history
[wanghan@VM_168_102_centos rott]$ cat /home/wanghan/.bash_history
exit
[wanghan@VM_168_102_centos rott]$ exit
exit
[root@VM_168_102_centos ~]# su wanghan
[wanghan@VM_168_102_centos rott]$ cat /home/wanghan/.bash_history
exit
history
who
pwd
history
cat /home/wanghan/.bash_history
exit
[wanghan@VM_168_102_centos rott]$ 
!#:通过命令历史列表中的命令编号引用并执行第#条命令
[wanghan@VM_168_102_centos rott]$ history
    1  exit
    2  history
    3  who
    4  pwd
    5  history
    6  cat /home/wanghan/.bash_history
    7  exit
    8  cat /home/wanghan/.bash_history
    9  history
   10  history who
   11  history pwd
   12  history
   13  history who
   14  exit
   15  history
   16  who
   17  history
[wanghan@VM_168_102_centos rott]$ !3
who
root     pts/0        Aug  4 19:11 (222.36.253.150)
[wanghan@VM_168_102_centos rott]$ 
!-#:通过命令历史列表中的命令编号引用并执行倒数第#条命令 
[wanghan@VM_168_102_centos rott]$ history
    1  exit
    2  history
    3  who
    4  pwd
    5  history
    6  cat /home/wanghan/.bash_history
    7  exit
    8  cat /home/wanghan/.bash_history
    9  history
   10  history who
   11  history pwd
   12  history
   13  history who
   14  exit
   15  history
   16  who
   17  history
   18  who
   19* 
   20  history
[wanghan@VM_168_102_centos rott]$ !-3
who
root     pts/0        Aug  4 19:11 (222.36.253.150)
[wanghan@VM_168_102_centos rott]$ 
!!:执行上一次历史命令
[wanghan@VM_168_102_centos rott]$ pwd /root [wanghan@VM_168_102_centos rott]$ !! pwd /root [wanghan@VM_168_102_centos rott]$ !! pwd /root
! string:执行命令历史列表中最近一次以string开头的命令
[wanghan@VM_168_102_centos rott]$ whatis usage: whatis keyword ... [wanghan@VM_168_102_centos rott]$ who root pts/0 Aug 4 19:11 (222.36.253.150) [wanghan@VM_168_102_centos rott]$ !w who root pts/0 Aug 4 19:11 (222.36.253.150) [wanghan@VM_168_102_centos rott]$
! $:引用上一个命令的最后一个参数,快捷键:Esc .
[wanghan@VM_168_102_centos etc]$ cd /tmp [wanghan@VM_168_102_centos tmp]$ ls /!$ ls //tmp 2014-08-04-212053 a agent_cmd.sock ap_1002.pid ap_1004.pid ap_1005.pid ap_1007.pid ap_1008.pid ap_1014.pid ceshi wanghan
history –c:清空当前缓存中的历史记录
[wanghan@VM_168_102_centos tmp]$ history
    1  who
    2  pwd
    3  history
[wanghan@VM_168_102_centos tmp]$ history -c
[wanghan@VM_168_102_centos tmp]$ history 
    1  history
history –d #:删除与命令历史列表中命令编号相对应的命令记录
[wanghan@VM_168_102_centos tmp]$ history
    1  who
    2  pwd
    3  history
[wanghan@VM_168_102_centos tmp]$ history -d 1
[wanghan@VM_168_102_centos tmp]$ history
    1  pwd
    2  history
    3  history -d 1
    4  history
[wanghan@VM_168_102_centos tmp]$ 
history -a:追加当前历史记录保存至命令历史文件中
[wanghan@VM_168_102_centos rott]$ cat /home/wanghan/.bash_history
exit
[wanghan@VM_168_102_centos rott]$ history
    1  exit
    2  cat /home/wanghan/.bash_history
    3  history
[wanghan@VM_168_102_centos rott]$ who
root     pts/0        Aug  4 19:11 (222.36.253.150)
[wanghan@VM_168_102_centos rott]$ pwd
/root
[wanghan@VM_168_102_centos rott]$ histroy -a
bash: histroy: command not found
[wanghan@VM_168_102_centos rott]$ history -a
[wanghan@VM_168_102_centos rott]$ cat /home/wanghan/.bash_history
exit
cat /home/wanghan/.bash_history
history
who
pwd
histroy -a
history -a
[wanghan@VM_168_102_centos rott]$ 
命令历史相关的环境变量:
HISTSIZE:命令历史中可以保存的命令个数
[root@VM_168_102_centos ~]# echo $HISTSIZE 1000
HISTFILESIZE:命令文件中可以保存的命令个数
[root@VM_168_102_centos ~]# echo $HISTFILESIZE 1000
HISTFILE:命令历史文件
[root@VM_168_102_centos ~]# echo $HISTFILE /root/.bash_history
HISTCONTROL:控制命令历史的生成
[root@VM_168_102_centos ~]# echo $HISTCONTROL ignoredups
ignoredups:忽略重复记录的命令,连续相同的命令才算重复
[root@VM_168_102_centos ~]# history
    1  history
[root@VM_168_102_centos ~]# who
root     pts/0        Aug  4 19:11 (222.36.253.150)
[root@VM_168_102_centos ~]# who
root     pts/0        Aug  4 19:11 (222.36.253.150)
[root@VM_168_102_centos ~]# history
    1  history
    2  who
    3  history
[root@VM_168_102_centos ~]# who
root     pts/0        Aug  4 19:11 (222.36.253.150)
[root@VM_168_102_centos ~]# history
    1  history
    2  who
    3  history
    4  who
    5  history
[root@VM_168_102_centos ~]# 
ignorespace:不记录以空白字符开头的命令
[root@VM_168_102_centos ~]# echo $HISTCONTROL
ignoredups
[root@VM_168_102_centos ~]# HISTCONTROL=ignorespace
[root@VM_168_102_centos ~]# echo $HISTCONTROL
ignorespace
[root@VM_168_102_centos ~]# history
    1  history
    2  echo $HISTCONTROL
    3  HISTCONTROL=ignorespace
    4  echo $HISTCONTROL
    5  history
[root@VM_168_102_centos ~]#  who 
root     pts/0        Aug  4 19:11 (222.36.253.150)
[root@VM_168_102_centos ~]#  pwd
/root
[root@VM_168_102_centos ~]# history
    1  history
    2  echo $HISTCONTROL
    3  HISTCONTROL=ignorespace
    4  echo $HISTCONTROL
    5  history
    6  history
[root@VM_168_102_centos ~]# 
ignoreboth:同时具有ignoredups和ignorespace的特性
shell中的变量赋值:
变量名=值
[root@VM_168_102_centos ~]# num=123 [root@VM_168_102_centos ~]# echo $num 123
注意:变量在赋值时不能使用$
变量名只能包含字母、数字和下划线,而且不能以数字开头
变量名区别大小写
[root@VM_168_102_centos ~]# num=123 [root@VM_168_102_centos ~]# echo $NUM [root@VM_168_102_centos ~]# echo $num 123
原文:http://putongren.blog.51cto.com/9086263/1535692