linux学习命令总结③

#cd ~ 回到当前用户的家目录

[[email protected]_168_102_centos etc]# pwd
/etc //当前所在目录
[[email protected]_168_102_centos etc]# cd ~ //回到当前用户家木里
[[email protected]_168_102_centos ~]# pwd
/root //当前用户家目录
[[email protected]_168_102_centos ~]# su wanghan
[[email protected]_168_102_centos rott]$ cd ~ //回到当前用户家木里
[[email protected]_168_102_centos ~]$ pwd
/home/wanghan  //当前用户家目录
[[email protected]_168_102_centos ~]$ 

#cd - 回到上一次所在目录

[[email protected]_168_102_centos ~]$ cd /etc //切换到/etc目录
[[email protected]_168_102_centos etc]$ cd /tmp //从/etc目录切换到/tmp目录
[[email protected]_168_102_centos tmp]$ cd -  //从当前/tmp回到上一次所在目录
/etc //回到了/etc
[[email protected]_168_102_centos etc]$ cd - //再从/etc回到上一次所在目录
/tmp //回到了/tmp
[[email protected]_168_102_centos tmp]$ 

#pwd命令:查看当前工作目录的完整路径

[[email protected]_168_102_centos /]$ pwd
/
[[email protected]_168_102_centos /]$ cd /etc
[[email protected]_168_102_centos etc]$ pwd
/etc
[[email protected]_168_102_centos etc]$ 

#mkdir命令:用来创建指定的名称的目录,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录。

mkdir [选项] 目录…

[[email protected]_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
[[email protected]_168_102_centos tmp]$ mkdir /tmp/test
[[email protected]_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 目录…

[[email protected]_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
[[email protected]_168_102_centos tmp]$ mkdir /tmp/wanghan/test
mkdir: cannot create directory `/tmp/wanghan/test‘: No such file or directory
[[email protected]_168_102_centos tmp]$ mkdir -p /tmp/wanghan/test
[[email protected]_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
[[email protected]_168_102_centos tmp]$ ls wanghan
test
[[email protected]_168_102_centos tmp]$ 

若创建的路径中有尚不再存的目录即可按照路径全部创建

mkdir –v 目录…

[[email protected]_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命令:删除已空的目录

[[email protected]_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
[[email protected]_168_102_centos tmp]$ ls test
test_1
[[email protected]_168_102_centos tmp]$ rmdir /tmp/test
rmdir: failed to remove `/tmp/test‘: Directory not empty  //该目录非空
[[email protected]_168_102_centos tmp]$ rmdir /tmp/test/test_1
[[email protected]_168_102_centos tmp]$ ls test
[[email protected]_168_102_centos tmp]$ rmdir /tmp/test
[[email protected]_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中的引用

‘ ’:强引用,变量替换不会进行

[[email protected]_168_102_centos tmp]$ num=123
[[email protected]_168_102_centos tmp]$ echo $num
123
[[email protected]_168_102_centos tmp]$ echo ‘$num‘
$num

“ ”:若引用,能够执行变量替换

[[email protected]_168_102_centos tmp]$ num=456
[[email protected]_168_102_centos tmp]$ echo num
num
[[email protected]_168_102_centos tmp]$ echo "num"
num
[[email protected]_168_102_centos tmp]$ 

· ·:命令替换,引用命令的执行结果;命令替换的另外一符号:$(命令);

[[email protected]_168_102_centos tmp]$ echo ls /tmp
ls /tmp
[[email protected]_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

结合使用:

[[email protected]_168_102_centos tmp]$ echo $test
tmp
[[email protected]_168_102_centos tmp]$ echo "This is $test"
This is tmp
[[email protected]_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
[[email protected]_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

#创建一个一当前日期时间创建一个目录

[[email protected]_168_102_centos tmp]$ date=`date +%Y-%m-%d-%H%M%S`
[[email protected]_168_102_centos tmp]$ mkdir -pv /tmp/$date
mkdir: created directory `/tmp/2014-08-04-212053‘
[[email protected]_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
[[email protected]_168_102_centos tmp]$ 

#history命令:查看在当前shell进程的保存在缓冲区中的执行过的命令列表,当shell退出时缓冲区的命令会自动化保存至当前用户的家目录中的.bash_history文件中。

[[email protected]_168_102_centos ~]# su wanghan
[[email protected]_168_102_centos rott]$ history
    1  exit
    2  history
[[email protected]_168_102_centos rott]$ who
root     pts/0        Aug  4 19:11 (222.36.253.150)
[[email protected]_168_102_centos rott]$ pwd
/root
[[email protected]_168_102_centos rott]$ history
    1  exit
    2  history
    3  who
    4  pwd
    5  history
[[email protected]_168_102_centos rott]$ cat /home/wanghan/.bash_history
exit
[[email protected]_168_102_centos rott]$ exit
exit
[[email protected]_168_102_centos ~]# su wanghan
[[email protected]_168_102_centos rott]$ cat /home/wanghan/.bash_history
exit
history
who
pwd
history
cat /home/wanghan/.bash_history
exit
[[email protected]_168_102_centos rott]$ 

!#:通过命令历史列表中的命令编号引用并执行第#条命令

[[email protected]_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
[[email protected]_168_102_centos rott]$ !3
who
root     pts/0        Aug  4 19:11 (222.36.253.150)
[[email protected]_168_102_centos rott]$ 

!-#:通过命令历史列表中的命令编号引用并执行倒数第#条命令

[[email protected]_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
[[email protected]_168_102_centos rott]$ !-3
who
root     pts/0        Aug  4 19:11 (222.36.253.150)
[[email protected]_168_102_centos rott]$ 

!!:执行上一次历史命令

[[email protected]_168_102_centos rott]$ pwd
/root
[[email protected]_168_102_centos rott]$ !!
pwd
/root
[[email protected]_168_102_centos rott]$ !!
pwd
/root

! string:执行命令历史列表中最近一次以string开头的命令

[[email protected]_168_102_centos rott]$ whatis
usage: whatis keyword ...
[[email protected]_168_102_centos rott]$ who
root     pts/0        Aug  4 19:11 (222.36.253.150)
[[email protected]_168_102_centos rott]$ !w
who
root     pts/0        Aug  4 19:11 (222.36.253.150)
[[email protected]_168_102_centos rott]$ 

! $:引用上一个命令的最后一个参数,快捷键:Esc .

[[email protected]_168_102_centos etc]$ cd /tmp
[[email protected]_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:清空当前缓存中的历史记录

[[email protected]_168_102_centos tmp]$ history
    1  who
    2  pwd
    3  history
[[email protected]_168_102_centos tmp]$ history -c
[[email protected]_168_102_centos tmp]$ history
    1  history

history –d #:删除与命令历史列表中命令编号相对应的命令记录

[[email protected]_168_102_centos tmp]$ history
    1  who
    2  pwd
    3  history
[[email protected]_168_102_centos tmp]$ history -d 1
[[email protected]_168_102_centos tmp]$ history
    1  pwd
    2  history
    3  history -d 1
    4  history
[[email protected]_168_102_centos tmp]$ 

history -a:追加当前历史记录保存至命令历史文件中

[[email protected]_168_102_centos rott]$ cat /home/wanghan/.bash_history
exit
[[email protected]_168_102_centos rott]$ history
    1  exit
    2  cat /home/wanghan/.bash_history
    3  history
[[email protected]_168_102_centos rott]$ who
root     pts/0        Aug  4 19:11 (222.36.253.150)
[[email protected]_168_102_centos rott]$ pwd
/root
[[email protected]_168_102_centos rott]$ histroy -a
bash: histroy: command not found
[[email protected]_168_102_centos rott]$ history -a
[[email protected]_168_102_centos rott]$ cat /home/wanghan/.bash_history
exit
cat /home/wanghan/.bash_history
history
who
pwd
histroy -a
history -a
[[email protected]_168_102_centos rott]$ 

命令历史相关的环境变量:

HISTSIZE:命令历史中可以保存的命令个数

[[email protected]_168_102_centos ~]# echo $HISTSIZE
1000

HISTFILESIZE:命令文件中可以保存的命令个数

[[email protected]_168_102_centos ~]# echo $HISTFILESIZE
1000

HISTFILE:命令历史文件

[[email protected]_168_102_centos ~]# echo $HISTFILE
/root/.bash_history

HISTCONTROL:控制命令历史的生成

[[email protected]_168_102_centos ~]# echo $HISTCONTROL
ignoredups

ignoredups:忽略重复记录的命令,连续相同的命令才算重复

[[email protected]_168_102_centos ~]# history
    1  history
[[email protected]_168_102_centos ~]# who
root     pts/0        Aug  4 19:11 (222.36.253.150)
[[email protected]_168_102_centos ~]# who
root     pts/0        Aug  4 19:11 (222.36.253.150)
[[email protected]_168_102_centos ~]# history
    1  history
    2  who
    3  history
[[email protected]_168_102_centos ~]# who
root     pts/0        Aug  4 19:11 (222.36.253.150)
[[email protected]_168_102_centos ~]# history
    1  history
    2  who
    3  history
    4  who
    5  history
[[email protected]_168_102_centos ~]# 

ignorespace:不记录以空白字符开头的命令

[[email protected]_168_102_centos ~]# echo $HISTCONTROL
ignoredups
[[email protected]_168_102_centos ~]# HISTCONTROL=ignorespace
[[email protected]_168_102_centos ~]# echo $HISTCONTROL
ignorespace
[[email protected]_168_102_centos ~]# history
    1  history
    2  echo $HISTCONTROL
    3  HISTCONTROL=ignorespace
    4  echo $HISTCONTROL
    5  history
[[email protected]_168_102_centos ~]#  who
root     pts/0        Aug  4 19:11 (222.36.253.150)
[[email protected]_168_102_centos ~]#  pwd
/root
[[email protected]_168_102_centos ~]# history
    1  history
    2  echo $HISTCONTROL
    3  HISTCONTROL=ignorespace
    4  echo $HISTCONTROL
    5  history
    6  history
[[email protected]_168_102_centos ~]# 

ignoreboth:同时具有ignoredups和ignorespace的特性

shell中的变量赋值:

变量名=值

[[email protected]_168_102_centos ~]# num=123
[[email protected]_168_102_centos ~]# echo $num
123

注意:变量在赋值时不能使用$

变量名只能包含字母、数字和下划线,而且不能以数字开头

变量名区别大小写

[[email protected]_168_102_centos ~]# num=123
[[email protected]_168_102_centos ~]# echo $NUM

[[email protected]_168_102_centos ~]# echo $num
123

linux学习命令总结③

时间: 2024-08-16 16:43:29

linux学习命令总结③的相关文章

linux学习命令总结②

#shutdown命令:系统关机.重启等 shutdown [options]- Time Time : now(马上) +#(多少分钟后) hh:mm(几点几分) 系统五分钟后关机: [[email protected]_168_102_centos ~]# shutdown 5//系统5分钟后重启 Broadcast message from [email protected]_168_102_centos (/dev/pts/0) at 16:19 ... The system is go

linux学习命令总结⑧

#chown命令:通过chown改变文件的属主和属组.在更改文件的属主或所属组时,可以使用用户名称和用户识别码设置.普通用户不能将自己的文件改变成其他的属主.其操作权限一般为管理员. 修改文件属主和属组: [[email protected]_168_102_centos ~]# ls -l total 8 drwxr-xr-x 2 wanghan hx 4096 Aug 12 10:32 abe drwxr-xr-x 2 root wanghan 4096 Aug 12 10:32 xab [

linux学习命令总结⑨

#重定向 输出重定向: 1>覆盖输出(1可省略) [[email protected]_168_102_centos tmp]# ls functions >shuchu [[email protected]_168_102_centos tmp]# cat shuchu functions [[email protected]_168_102_centos tmp]# ls fstab >shuchu [[email protected]_168_102_centos tmp]# ca

linux学习命令总结⑤

#stat命令:查看文件信息 [[email protected]_168_102_centos ~]# stat test.log File: `test.log' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: ca01h/51713d Inode: 483339 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 500/ wanghan) Access:

linux学习命令总结④

#ls命令:通过ls 命令不仅可以查看linux文件夹包含的文件,而且可以查看文件权限(包括目录.文件夹.文件权限),查看目录信息等等 [[email protected]_168_102_centos /]# ls bin data etc lib lost+found mnt proc sbin srv tmp var boot dev home lib64 media opt root selinux sys usr ls –a:显示所有文件,包含.开头的隐藏文件 [[email prot

linux学习命令总结⑦

#useradd命令:建立用户帐号和创建用户的起始目录,使用权限是超级用户 [[email protected]_168_102_centos ~]# useradd test [[email protected]_168_102_centos ~]# id test uid=502(test) gid=502(test) groups=502(test) [[email protected]_168_102_centos ~]# tail -n 1 /etc/passwd test:x:502

linux学习命令总结⑩②

#fdisk命令:磁盘分区工具 fdisk –l:查看机器所挂硬盘个数及分区情况 [[email protected]_168_102_centos ~]# fdisk -l Disk /dev/xvda: 8589 MB, 8589934592 bytes 255 heads, 63 sectors/track, 1044 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physic

linux学习命令总结⑥

#文件名通配 通配符: *:匹配任意长度的任意字符(0到多个) [[email protected]_168_102_centos ~]# ls * 0812080808 2014-05-16: test.log ceshi_1: test: [[email protected]_168_102_centos ~]# ls -ld t* drwxr-xr-x 2 root wanghan 4096 Aug 11 15:46 test ?:匹配任意单个字符 [[email protected]_1

Linux 学习命令之修改日期时间

Linux 学习命令之修改日期时间 一.日期时间修改 1. 查看时间和日期 [[email protected] ~]# date 2017年 11月 03日 星期五 11:39:49 CST 或 [[email protected] ~]# clock 2017年11月03日 星期五 11时42分52秒  -1.563496 seconds 显示日历 [[email protected] ~]# cal 十一月 2017 日 一 二 三 四 五 六 1  2  3  4 5  6  7  8