获得主机名[hostname]
#显示主机名
[root@localhost scripts]# hostname
localhost.localdomain
#使得环境变量获得
[root@localhost scripts]# echo $HOSTNAME
localhost.localdomain
获取路径基名(basename)
#获取路径基名
[root@localhost scripts]# basename /root/scripts/test1.sh
test1.sh
显示用户的登录情况[lastlog]
lastlog显示/var/log/lastlog文件中的信息,会显示登录用户名,什么方式登录,最后的登录时间,默认根据/etc/passwd的顺序显示所有用户的最后访问信息。
#显示当前系统上每一个用户最近一次成功登录信息
[root@localhost scripts]# lastlog
Username Port From Latest
root tty1 Mon Feb 1 16:40:09 +0800 2021
#显示指定用户最近一次登录信息
[root@localhost scripts]# lastlog -u root
Username Port From Latest
root tty1 Mon Feb 1 16:40:09 +0800 2021
显示用户登录的错误尝试信息(lastb)
显示 /var/log/btmp文件中的信息
#显示用户错误的登录尝试, /var/log/btmp文件
[root@localhost scripts]# lastb
root ssh:notty 192.168.22.131 Wed Jan 13 06:55 - 06:55 (00:00)
btmp begins Wed Jan 13 06:55:21 2021
显示用户的最后登录信息(last)
显示登录日志,still logged in 依然登录中
#last显示/var/log/wtmp文件中的内容
#last -n# 只显示最近#次的相关信息
[root@localhost ~]# last
root pts/1 192.168.22.140 Tue Feb 2 06:51 still logged in
root pts/0 192.168.22.140 Tue Feb 2 06:48 still logged in
reboot system boot 2.6.32-696.el6.x Tue Feb 2 06:47 - 07:00 (00:13)
[root@localhost ~]# last -3
root pts/1 192.168.22.140 Tue Feb 2 06:51 still logged in
root pts/0 192.168.22.140 Tue Feb 2 06:48 still logged in
reboot system boot 2.6.32-696.el6.x Tue Feb 2 06:47 - 07:01 (00:13)
wtmp begins Fri Jan 1 04:48:55 2021
[root@localhost ~]# last -n3
root pts/1 192.168.22.140 Tue Feb 2 06:51 still logged in
root pts/0 192.168.22.140 Tue Feb 2 06:48 still logged in
reboot system boot 2.6.32-696.el6.x Tue Feb 2 06:47 - 07:01 (00:13)
wtmp begins Fri Jan 1 04:48:55 2021
显示登录用户在做什么(w)
#显示用户信息和用户在做什么
[root@localhost scripts]# w
17:01:13 up 40 min, 3 users, load average: 0.05, 0.08, 0.03
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 - 16:40 21:05 0.03s 0.03s -bash
root pts/0 192.168.22.140 16:35 1.00s 0.27s 0.13s w
water pts/1 192.168.22.140 16:59 1:56 0.00s 0.00s -bash
显示当前登录用户(who)
#显示当前登录用户
[root@localhost ~]# whoami
root
[root@localhost ~]# who
root pts/0 2021-02-01 16:35 (192.168.22.140)
用户名称 哪个终端 登录时间和IP地址
#显示当前运行级别
[root@localhost ~]# who -r
run-level 3 2021-02-01 16:21
#显示头部
[root@localhost ~]# who -H
NAME LINE TIME COMMENT
root tty1 2021-02-01 16:40
root pts/0 2021-02-01 16:35 (192.168.22.140)
切换目录(cd[change directory])
cd [-L|-P] [相对路径|绝对路径]
#不加任何路径,返回用户家目录或者使用cd ~返回家目录
[root@localhost etc]# cd
[root@localhost ~]#
[root@localhost ~]# cd scripts
[root@localhost scripts]# pwd
/root/scripts
#【..】表示返回上一级目录
[root@localhost scripts]# cd ..
[root@localhost ~]#
#cd后面跟路径,直接进入指定的目录
[root@localhost ~]# cd /home/edison
[root@localhost edison]#
#【-】表示返回上一次进入的目录
[root@localhost ~]# cd /
[root@localhost /]# cd /home
[root@localhost home]# cd -
/
显示当前目录的路径(pwd[print working directory])
[root@localhost edison]# pwd
/home/edison
建立新目录(mkdir[make directory])
mkdir [-m|-p] 目录名称
-m:设置文件权限,直接设置,不使用默认权限(umask)
-p:帮助你直接将所需要的目录(包含上层目录)递归创建
#普通创建文件夹过程
[root@localhost /]# cd /tmp
[root@localhost tmp]# mkdir test
[root@localhost tmp]# cd test
[root@localhost test]#
#创建文件夹时,指定文件夹的权限
[root@localhost tmp]# mkdir -m777 test1
[root@localhost tmp]# ll
total 100
-rw-r--r--. 1 root root 89118 Jan 31 17:58 dmesginfo
drwxrwsr-x. 2 root project 4096 Jan 31 16:45 project
drwxr-xr-x. 2 root root 4096 Feb 3 18:26 test
drwxrwxrwx. 2 root root 4096 Feb 3 18:29 test1
#不使用-p创建级连文件夹会报以下错误
[root@localhost tmp]# mkdir test2/test21/test211
mkdir: cannot create directory `test2/test21/test211': No such file or directory
#使用-p选项创建级连文件夹
[root@localhost tmp]# mkdir -p test2/test21/test211
[root@localhost tmp]# tree test2
test2
└── test21
└── test211
2 directories, 0 files
环境变量
[root@localhost tmp]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
#PWD环境变量保存当前目录路径
[root@director etc]# echo $PWD
/etc
#OLDPWD显示上一次所在目录路径
[root@director etc]# echo $OLDPWD
/root
- 不同身份用户默认的PATH不同,默认随意执行的命令也不同
- PATH是可以修改的
- 使用绝对路径或相对路径直接指定某个命令的文件名来执行,会比查找PATH来的正确
- 命令应该放置到正确的目录下,执行会比较方便
- 本目录(.)最好不要放到PATH中
文件与目录的查看 ls[list]
默认显示目录下非隐藏文件的文件名,以文件名进行排序及文件名代表的颜色显示
使用方法:
ls [OPTION]… [FILE]…
【-a|–all】显示目录下所有的文件或目录名字,包含【.】【…】
【-A】显示目录下所有的文件或目录名字,不包含【.】【…】
【–author】显示文件的创建者与【-l】一起使用
【–color=when】是否显示颜色,–color=never不显示颜色,–color=always显示颜色,–color=auto让系统自行依据判断是否显示颜色
【-d】仅列出目录本身,而不是列出目录内的文件内容
【-h】将文件容量以人容易阅读的方式显示(如KB,GB)
【-i】列出inode号码
【-l】详细信息显示,包含文件的属性与权限等数据
【-n】同【-l】只是将属主和属组的名称替换为UID和GID
【-r】将排序结果反向输出
【-R】连同子目录内容一起列出来,等于该目录下的所有文件都会显示出来
【-S】以文件容量大小排序,而不是用文件名排序
【-t】依时间排序,而不是文件名排序
colorize the output. WHEN defaults to ‘always’ or can be ‘never’ or ‘auto’. More info below
#执行ls默认是ls --color=auto
[root@localhost ~]# alias ls
alias ls='ls --color=auto'
#【-a】
[root@localhost ~]# ls -a
. anaconda-ks.cfg .bashrc initramfs-2.6.32-696.el6.x86_64.tar Jan scripts test
.. .bash_history .cshrc inittab .lesshst scripts.bak testfile
2 .bash_logout CST install.log mydev .ssh test.zip
AM .bash_profile Fri install.log.syslog rc.sysinit .tcshrc .viminfo
#【-A】
[root@localhost ~]# ls -A
2 .bash_logout CST install.log mydev .ssh test.zip
AM .bash_profile Fri install.log.syslog rc.sysinit .tcshrc .viminfo
anaconda-ks.cfg .bashrc initramfs-2.6.32-696.el6.x86_64.tar Jan scripts test
.bash_history .cshrc inittab .lesshst scripts.bak testfile
#【--author】
[root@localhost ~]# ls --author -l
total 72840
-rw-r--r--. 1 root root root 25 Jan 23 16:30 2
-r--rwxr--. 1 root root root 0 Jan 1 06:27 AM
-rw-------. 1 root root root 1171 Jan 1 04:46 anaconda-ks.cfg
-rw-r--r--. 1 root root root 0 Jan 1 06:27 CST
-rw-r--r--. 1 root root root 0 Jan 1 06:27 Fri
#【--color】
[root@localhost ~]# ls --color=never
2 CST inittab Jan scripts testfile
AM Fri install.log mydev scripts.bak test.zip
anaconda-ks.cfg initramfs-2.6.32-696.el6.x86_64.tar install.log.syslog rc.sysinit test
[root@localhost ~]# ls --color=auto
2 CST inittab Jan scripts testfile
AM Fri install.log mydev scripts.bak test.zip
anaconda-ks.cfg initramfs-2.6.32-696.el6.x86_64.tar install.log.syslog rc.sysinit test
#【-d】
[root@localhost ~]# ls -d scripts
scripts
[root@localhost ~]# ls -ld scripts
drwxr-xr-x. 3 root root 4096 Feb 7 17:06 scripts
#【-h】一般与【-l】一起使用
[root@localhost ~]# ls -lh /etc
total 1.4M
-rw-r--r--. 1 root root 44 Feb 8 16:49 adjtime
-rw-r--r--. 1 root root 1.5K Jan 12 2010 aliases
-rw-r--r--. 1 root root 12K Jan 1 04:49 aliases.db
#【-i】列出inode号码
[root@localhost ~]# ls -i scripts
919947 abc 919998 formatdisk.txt 920069 mycar.sh 919907 sum.sh 919931 test7.sh
919975 case.sh 919973 getMax.sh 919922 readtest1.sh 919926 test 919932 test8.sh
#【-l】
[root@localhost ~]# ls -l scripts
total 276
drwxr-xr-x. 2 root root 4096 Jan 30 17:46 abc
-rw-r--r--. 1 root root 51200 Feb 7 06:27 case.sh
#【-n】
[root@localhost ~]# ls -n scripts
total 276
drwxr-xr-x. 2 0 0 4096 Jan 30 17:46 abc
-rw-r--r--. 1 0 0 51200 Feb 7 06:27 case.sh
#【-r】
[root@localhost ~]# ls -r
test.zip test scripts mydev install.log.syslog inittab Fri anaconda-ks.cfg 2
testfile scripts.bak rc.sysinit Jan install.log initramfs-2.6.32-696.el6.x86_64.tar CST AM
#【-R】
[root@localhost ~]# ls -R
.:
2 CST inittab Jan scripts testfile
AM Fri install.log mydev scripts.bak test.zip
anaconda-ks.cfg initramfs-2.6.32-696.el6.x86_64.tar install.log.syslog rc.sysinit test
./scripts:
abc deluser.sh isHasSpaceRow.sh mycar.sh readtest.sh test2.sh test7.sh testout
case.sh detimateExpire.sh isLogin.sh readtest1.sh service.sh test2.tar.gz test8.sh test.sh
changeHostName.sh EVENOLD.sh isSameUIDandGID.sh readtest2.sh showinfo.sh test3.sh testerror test.tar
changeupper.sh formatdisk.sh locationTest2.sh readtest3.sh showlogged.sh test3.tar.bz2 testfile test.tar.gz
countUsers.sh formatdisk.txt locationTest.sh readtest4.sh sum.sh test4.sh testfile1
createdelUser.sh getMax.sh managerUser2.sh readtest5.sh test test4.tar.xz testfile2
createNewUserNew.sh helloeveryone.sh manageUser3.sh readtest6.sh test1.sh test5.sh testFileType.sh
createNewUser.sh isHasBash.sh manageUser.sh readtest7.sh test1.tar test6.sh testing.bKnhNh
./scripts/abc:
./scripts.bak:
abc deluser.sh isHasBash.sh manageUser3.sh readtest6.sh test test7.sh testing.bKnhNh
case.sh detimateExpire.sh isHasSpaceRow.sh manageUser.sh readtest7.sh test1.sh test8.sh testout
changeHostName.sh EVENOLD.sh isLogin.sh readtest1.sh readtest.sh test2.sh testerror test.sh
countUsers.sh formatdisk.sh isSameUIDandGID.sh readtest2.sh scripts test3.sh testfile test.tar
createdelUser.sh formatdisk.txt locationTest2.sh readtest3.sh service.sh test4.sh testfile1
createNewUserNew.sh getMax.sh locationTest.sh readtest4.sh showlogged.sh test5.sh testfile2
createNewUser.sh helloeveryone.sh managerUser2.sh readtest5.sh sum.sh test6.sh testFileType.sh
./scripts.bak/abc:
./scripts.bak/scripts:
abc deluser.sh isHasBash.sh manageUser3.sh readtest6.sh test1.sh test8.sh testout
case.sh detimateExpire.sh isHasSpaceRow.sh manageUser.sh readtest7.sh test2.sh testerror test.sh
changeHostName.sh EVENOLD.sh isLogin.sh readtest1.sh readtest.sh test3.sh testfile test.tar
countUsers.sh formatdisk.sh isSameUIDandGID.sh readtest2.sh service.sh test4.sh testfile1
createdelUser.sh formatdisk.txt locationTest2.sh readtest3.sh showlogged.sh test5.sh testfile2
createNewUserNew.sh getMax.sh locationTest.sh readtest4.sh sum.sh test6.sh testFileType.sh
createNewUser.sh helloeveryone.sh managerUser2.sh readtest5.sh test test7.sh testing.bKnhNh
./scripts.bak/scripts/abc:
./test:
1xyz a123 a12d a b ab123 abc helloy123 k 67 m.z x12 xyz xyz123
./testfile:
test1.txt test2.txt test3.txt test4.txt test5.txt test6.txt test7.txt test.txt
#【-S】
[root@localhost ~]# ls -lhS
total 72M
-rw-------. 1 root root 72M Feb 7 06:40 initramfs-2.6.32-696.el6.x86_64.tar
-rwxr-xr-x. 1 root root 20K Jan 28 05:45 rc.sysinit
-rw-r--r--. 1 root root 16K Jan 1 04:46 install.log
-rw-r--r--. 1 root root 4.7K Jan 1 04:45 install.log.syslog
drwxr-xr-x. 3 root root 4.0K Feb 7 17:06 scripts
#【-t】
[root@localhost ~]# ls -lt
total 72840
drwxr-xr-x. 3 root root 4096 Feb 7 17:06 scripts
-rw-------. 1 root root 74508288 Feb 7 06:40 initramfs-2.6.32-696.el6.x86_64.tar
...