centos基础命令及shell编程基础

1.Tab 2下 补全命令

文件、目录操作
1.mkdir -p japan/canglas -p代表递归创建
  rm -rf japan  r代表目录,f代表强制    
2.cp -a anaconda /tmp/ana -a代表全部属性包括时间,/tmp/后跟ana代表复制后改名
3.ls -l == ll
4.mv janpan /tmp/japan1 剪切命令,不需要加-r
  mv japan japan1 剪切命令当成重命令使用
5.ln -s 
软链接文件:类似windows快捷方式,有自己的i节点号,链接文件记录源文件的i节点,源文件必须写绝对路径
ln -s /root/test /tmp/test.soft
硬链接文件:同一i节点号,可以不同名,不能跨分区,不能针对目录使用

说明:
/,/usr 下的bin为存储公用命令,sbin存储root命令

搜索
1.locate 文件名  在/var/lib/mlocate数据库中搜索,缺点数据库非实时更新,一天更新一次且仅能按照文件名搜索
  updatedb 立即更新数据库
  /etc/updatedb.conf内的配置文件决定搜索范围

2.whereis command   
  whatis command(可显示命令等级)==man -f command
  说明:仅用于命令查找
  which command(输出当前生效命令的位置 )
3.find / -name command
模糊搜索通配符
*  匹配任意内容
? 匹配任意一个字符
[] 匹配括号内指定任意一个字符
''代表文本名
""代表表示变量
``代表命令执行后的结果 aa=`ls` 等效与bb=$(ls)

#不区分大小写
find /root -iname install.log
#按照所有者搜索·
find /root -user root
find /root -nouser
#按照时间
find /var/log/ -mtime +10
-5 往前5天内
5往前第5天
+5往前6-10天
#按照大小(整数)
find . -size 25k  (k小写)
find /root -size 2M  (M大写)
#按照i节点
find . -inum 262422 
#逻辑组合 a(与) o(或)
find /root -size +25k -a -size -50k
#找到后处理命令
find /root -size +25k -a -size -50k -exec rm -rf {} \;

#查找文件内容,包含size的行
grep "size" filename
-i 忽略大小写
-v 排除字符


帮助命令
1.#帮助手册
man ls
#显示命令帮助等级,
man -f command == whatis command
#模糊搜索帮助命令查找
man -k command == apropos command
2.#shell命令帮助
help cd
3.详细帮助
Info command

压缩命令
1.常用压缩格式:.zip
#-r 压缩文件夹 
zip longzls.zip longzls
#解压缩
unzip jp.zip
2.gzip(不可压缩目录)
#压缩后源文件消失 
gzip file 
#解压
gzip -d file
gunzip file
3.bz2
bzip jp
bunzip jp.bz2
4..tar.gz
.tar.bz2
tar -zcvf jp.tar.gz jp
tar -zxvf jp.tar.gz
tar -jcvf /tmp/jp.tar.bz2 jp
tar -jxvf jp.tar.bz2 -C /tmp/
#查询
tar jtvf filename.tar.bz2


关机和重启
shutdown
-r 重启
-c 取消前一个关机
-h 关机
shutdown -r 05:30 &
shutdown -r now

reboot
logout

其他命令
挂载命令
#查看已经挂载目录
mount
#根据/etc/fstab配置自动挂载
mount -a
#挂载cd
mount /dev/sr0 /mnt/cdrom/
#卸载命令
umount /dev/sr0
umount /mnt/cdrom/
#u盘挂载
mount -t vfat /dev/sdb1 /mnt/usb/
#查看登录用户信息
who
w
last
lastlog


shell编程
#echo颜色输出
echo -e "\e[1;31m woailzls \e[0m"
#脚本擡头
#!/bin/bash
#直接执行脚本
bash command
#命令别名查询
alias    
alias vi=vim(临时生效)
unalias vi
vi /root/.bashrc(修改环境变量永久生效)
说明:.bashrc环境变量文件根据使用者不同位置于不同目录下


快捷键
ctrl c
ctrl l 清屏clear
ctrl u 光标删除到命令行首
ctrl a 光标移动到命令行首
ctrl e 光标移动到命令行尾
ctrl z 命令放到后台
历史命令
ctrl r 在历史命令中搜索(位于.bash_history)
说明:.bash_history 只包含了上次注销前写入的使用过的命令,因此直接history会比上次多,写入本次命令为history -w
     删除历史命令使用history -cfins
/etc/profile 可配置历史命令保存数量
!!重复执行上一条命令
!字符 重复执行最后一条以字符开头的命令

TAB
命令补全依赖于PATH
文件补全依赖于目录

输出重定向
ls > test.log(覆盖)
ls >> test.log(追加)

ls 2> test.log(标准错误输出重定向覆盖)
ls 2>> test.log(标准错误输出重定向追加)

ls >test2.log 2>&1 (正确错误均写入文件覆盖)
ls >> test2.log 2>&1 (正确错误均写入文件追加)***

ls &> test3.log(正确错误均写入文件覆盖)
ls &>> test3.log(正确错误均写入文件追加)***

ls &> /dev/null(该位置类似回收站,系统黑洞,人间蒸发)

cat xdy >> access.log  2>>error.log    (分开存放正确和错误的日志)***


输入重定向
wc < test.log

逻辑符
command1;command2 顺序执行 
command1&&command2 逻辑与1执行正确2才执行
command1||command2 逻辑或   1正确2就不执行,1错误2就执行
应用:ls && echo yes || echo no(监测命令是否正常执行)

管道符
ls /etc/ | more  (命令1的结果传给命令2)
netstat -an | grep ESTABLISHED

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章