Shell
线上查询及帮助命令
- man [command name]
文件和目录操作命令
- pwd: show path to current workspace
- cd
- ls
- -h : 显示文件容量
- -a : all files
- -l: detail information
- cat "path to file": show content under folder
- -n : 显示行号
- -b: 显示行号 不包括空行
- more "path to file": 显示默认一页
- head/tail -n [数字x] "path to file": 显示头/末 x 行
- tr: 转换文本文件字符:
- cat a.txt|tr [a-z] [A-Z]: 小写变大写
- wc txtfile: 统计line word byte number
- -l : line
- -w : word
- -c : byte
- cut txtfile: 提取文本字符
- -f2: 指定只显示每一行的第二列
- -d :指定行的风格符号 如: 默认是tab
- cut -d: -f1 file.txt: 显示以:为行的第一列
- touch [选项] 文件: 创建空白文件 修改文件时间
- touch -t [201211142234] log.log
- mkdir
- mkdir -p a/b/c 多层目录
- mkdir -m 755 folder: 权限
- cp [source file] [objective file/folder]
- -r 递归复制
- -p 保留原始文件属性
- mv [source file] [objective file/folder]
- rm [source file]
- -f 忽略警告
- -r 删除文件夹
- -i 删除先前询问
有关磁盘文件系统的命令
- dd : convert and copy a file
- dd if=input_file of=output_file.
- bs :块的大小
- count : 块的个数
比如将光驱设备 拷贝成镜像 : dd if=/dev/cdrom of=xxx.iso count=1 bs=560M
sudo mount -t vboxsf ubuntushare /mnt/share : link device to a system folder
挂usb:
- fdish -l : 查找名称
- mount /dec/sda2 folder_linked
文件查询搜索
- grep objective_chacater text_file
- -b :将binary 当作text 执行
- -c :only show找到次数
- -i 忽略大小写
- -n 显示行号
- -v :反向选择 没有关键词的
- find 目标路径 寻找条件 操作
- find /home -name "name"
- find /home --size +50k/-50k
- find /home -name "name" -exec {} /home \; 把找到结果复制到/home目录. note it must be ended with "\;"
重启
- reboot : only with root right
基础网络操作命令
telnet ssh scp ping route ifup ifdown netsta
- wget
- wget -r http:xxx: 递归下载
- wget -p http:xxx: 下载所有资源
- ifconfig : 查看网卡配置
信息显示命令
- uname
- -a: show all system name
- -r :kernel name
uptime : 系统时间 运行时间 当前用户 平均负载
free
- free -m :以m为单位现实内存使用
系统用户登陆信息
- who : current user last : all users record
- df 显示挂载点和磁盘用量 (挂载后才显示呀)
- -a :显示所有文件系统
- -h : 容量格式k m g
- -i : inode 信息
du : 磁盘使用量
- du -h file_name : 容量格式k m g
- -s : 显示占用量总和
- -a : 显示每个文件用量 而不是总和
fdisk -l: 显示存储,和各种盘
others:
- echo [words]: output words in terminal
- $variable: show variables
- echo $HOSTNAME: 主机名
- echo $SHELL : current shell
- $variable: show variables
- date
- date -s "20051010 9:30:00" 设置系统时间
- date "+%Y-%m-%d" : show 2005-10-10
- date "+%j" :今天是一年中第几天
history : executed command, 事实是历史执行命令保存在 ~/.bash_history, 默认1000条
!$: 上一条命令参数 比如cd !$
alias lsal=ls -al/ unalias lsal
- type command :
Variables
export 声明全局变量
- -p: view all the env variables
- Set an Environment Variable . The following creates a new environment variable called “MYAPP” and assigns the value of 1. $export MYAPP=1
- Append a Value to an Environment Variable: export PATH=$PATH:/home/himanshu/practice/
- Exporting Variables Permanently:
- use export in " ~/.profile or ~/.bash_profile or /etc/profile"
env : show important classic variables
- HOME
- SHELL
- PATH
- EDITOR : default editor
系统安全相关命令
- sudo:
- sudo su : login in as root
- chmod: chmod -R 777 folder_name: -R means recursive
管道符 | : 前一命令stdout| 后一命令stdin
- grep a.txt "faf"| more
输入输出重定向 > 需要清空文件 and >> 把输出追加的文件末尾
stdin 文件描述符0 stdout 文件描述符1 stderr 文件描述符2
command > file/command >> file: stdout into a file
command 2> file/command 2> file: stderr into a file
command >> file 2&$1 : stdout and stderr into a file
- command < file: file as stdin
- command << 分界符 :从标准输入中读入 直到分界符
- command < file1 >file 2
通配符
- * : 任意n字符
- ? : 任意单字符
- [0-9]: 任意数字
- [a-z]: 任意字母
- echo "$PRICE": 变量依旧生效
- echo '$PRICE': 显示 $PRICE 。单引号内容转义所有内容
参数
http://www.runoob.com/linux/linux-shell-passing-arguments.html Shell 传递参数 我们可以在执行 Shell 脚本时,向脚本传递参数,脚本内获取参数的格式为:$n。n 代表一个数字,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推…… 实例 以下实例我们向脚本传递三个参数,并分别输出,其中 $0 为执行的文件名:
echo "Shell 传递参数实例!";
echo "执行的文件名:$0";
echo "第一个参数为:$1";
echo "第二个参数为:$2";
echo "第三个参数为:$3";
为脚本设置可执行权限,并执行脚本,输出结果如下所示:
$ chmod +x test.sh
$ ./test.sh 1 2 3
Shell 传递参数实例!
执行的文件名:./test.sh
第一个参数为:1
第二个参数为:2
第三个参数为:3