2017-11-26 2281
命令帮助信息获取
sh 执行脚本
-x选项来查看这个脚本执行过程的
1
2
3
|
[root@localhost ~] # sh 1.sh
[root@localhost ~] # sh -x 1.sh
[root@localhost ~] # ./1.sh
|
-n 只读取shell脚本,但不实际执行
-x 进入跟踪方式,显示所执行的每一条命令
echo
将输入的字符串送往标准输出。
用echo命令输出加引号的字符串时,将字符串原样输出;用echo命令输出不加引号的字符串时,将字符串中的各个单词作为字符串输出,各字符串之间用一个空格分割。
参 数:
-n 不要在最后自动换行
-e 若字符串中出现以下字符,则特别加以处理,而不会将它当成一般文字输出:
\a 发出警告声;
\b 删除前一个字符;
\c 最后不加上换行符号;
\f 换行但光标仍旧停留在原来的位置;
\n 换行且光标移至行首;
1
2
3
4
|
[root@Zabbix ~] # echo -e 'aaa\nbbb'
aaa bbb [root@Zabbix ~] #
|
echo显示带颜色,需要使用参数-e
格式如下:
echo -e "\033[字背景颜色;文字颜色m字符串\033[0m"
例如:
echo -e "\033[41;37m TonyZhang \033[0m"
其中41的位置代表底色, 37的位置是代表字的颜色
注:
1、字背景颜色和文字颜色之间是英文的“""”
2、文字颜色后面有个m
3、字符串前后可以没有空格,如果有的话,输出也是同样有空格
下面看几个例子:
echo -e "\033[30m 黑色字 \033[0m"
echo -e "\033[31m 红色字 \033[0m"
echo -e "\033[32m 绿色字 \033[0m"
echo -e "\033[33m 黄色字 \033[0m"
echo -e "\033[34m 蓝色字 \033[0m"
echo -e "\033[35m 紫色字 \033[0m"
echo -e "\033[36m 天蓝字 \033[0m"
echo -e "\033[37m 白色字 \033[0m"
echo -e "\033[40;37m 黑底白字 \033[0m"
echo -e "\033[41;37m 红底白字 \033[0m"
echo -e "\033[42;37m 绿底白字 \033[0m"
echo -e "\033[43;37m 黄底白字 \033[0m"
echo -e "\033[44;37m 蓝底白字 \033[0m"
echo -e "\033[45;37m 紫底白字 \033[0m"
echo -e "\033[46;37m 天蓝底白字 \033[0m"
echo -e "\033[47;30m 白底黑字 \033[0m"
控制选项说明 :
\33[0m 关闭所有属性
\33[1m 设置高亮度
\33[4m 下划线
\33[5m 闪烁
\33[7m 反显
\33[8m 消隐
\33[30m -- \33[37m 设置前景色
\33[40m -- \33[47m 设置背景色
\33[nA 光标上移n行
\33[nB 光标下移n行
\33[nC 光标右移n行
\33[nD 光标左移n行
\33[y;xH设置光标位置
\33[2J 清屏
\33[K 清除从光标到行尾的内容
\33[s 保存光标位置
\33[u 恢复光标位置
\33[?25l 隐藏光标
\33[?25h 显示光标
\r 光标移至行首,但不换行;
\t 插入tab;
1
2
3
4
5
6
7
8
9
|
[root@Zabbix ~] # echo aaa\tbbb
aaatbbb [root@Zabbix ~] # echo 'aaa\tbbb'
aaa\tbbb [root@Zabbix ~] # echo -e aaa\tbbb
aaatbbb [root@Zabbix ~] # echo -e 'aaa\tbbb'
aaa bbb [root@Zabbix ~] #
|
\v 与\f相同;
\\ 插入\字符;
\nnn 插入nnn(八进制)所代表的ASCII字符;
``反引号的作用就是将反引号内的Linux命令先执行,然后将执行结果赋予变量。
如果在命令行上把Linux命令放在反引号中,这个命令会首先被执行,其结果会成为命令行的一个参数。类似的还有$( ),这里是括号,不是花括号,${ }就是取变量了,比如:echo ${PATH},{}是为了避免后面的影响到了,比如参数${a}a这样连着使用。如果不用{},系统就会识别成$aa了
1
2
3
4
|
[root@localhost ~] # echo `df -Th` > cat.txt
[root@localhost ~] # cat cat.txt
Filesystem Type Size Used Avail Use% Mounted on /dev/sda3 ext4 18G 3.0G 14G 18% / tmpfs tmpfs 504M 76K 504M 1% /dev/shm /dev/sda1 ext4 194M 28M 157M 15% /boot /dev/sr0 iso9660 64M 64M 0 100% /media/VMware Tools
[root@localhost ~] #
|
单引号和反引号之间的区别。单引号把Linux命令视为字符集合。反引号会强迫执行Linux命令。
cd--change directory---切换工作目录
1
2
3
4
5
6
7
|
[root@localhost ~] # cd /home
[root@localhost home] # pwd
/home [root@localhost home] # cd /usr/local/src/
[root@localhost src] # pwd
/usr/local/src [root@localhost src] #
|
说明:
1.目录名称省略,变换至使用者的 home directory (也就是刚 login 时所在的目录)
2."~" 表示为 home directory 的意思
3."." 则是表示目前所在的目录
4.".." 则表示目前目录位置的上一层目录
5.“-” 返回进入当前目录前所在目录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[root@localhost src] # cd
[root@localhost ~] # pwd
/root [root@localhost ~] # cd -
/usr/local/src [root@localhost src] # pwd
/usr/local/src [root@localhost src] # cd ..
[root@localhost local ] # pwd
/usr/local [root@localhost local ] # cd .
[root@localhost local ] # pwd
/usr/local [root@localhost local ] # cd ~
[root@localhost ~] # pwd
/root [root@localhost ~] #
|
6."!$" 把上个命令最后的参数作为输入
1
2
3
4
5
|
[root@localhost src] # mkdir /usr/local/nagios
[root@localhost src] # tar zxvf nagios-3.5.0.tar.gz -C /usr/local/nagios
[root@localhost src] # cd !$
cd /usr/local/nagios
[root@localhost nagios] #
|
ifconfig----查看本机IP地址
1
|
[root@Git-Client ~] # ifconfig
|
curl ifconfig.me查看本机公网IP
1
|
[root@Git-Client ~] # curl ifconfig.me
|
cp---复制文件或者目录
1
2
3
4
5
6
7
|
[root@localhost ~] # cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0bak
[root@localhost test ] # ls
test .txt
[root@localhost test ] # cp test.txt{,bak}
[root@localhost test ] # ls
test .txt test .txtbak
[root@localhost test ] # cp /etc/sysconfig/iptables{,`date +%Y%m%d`bak}
|
-a 尽可能将档案状态、权限等资料都照原状予以复制,其作用等于dpR选项的组合 ,
-r 若 source 中含有目录名,则将目录下档案也依序拷贝至目的地,复制目录时必须使用此选项
1
2
3
4
5
6
7
|
[root@localhost local ] # cp nagios/ /usr/local/src/
cp : 略过目录 "nagios/"
[root@localhost local ] # cd nagios/
[root@localhost nagios] # ls
nagios [root@localhost nagios] # cp -r nagios/ /usr/local/src/
[root@localhost nagios] # cp -a nagios/ /usr/local/src/nagios1
|
-f 若目的地已经有相同档名的档案存在,则在复制前先予以删除再行复制。
-i :若目的檔(destination)已经存在时,在覆盖时会先询问是继续
有时候使用-f参数也会提示是否覆盖,linux系统中有时候给cp建了一个alias:
1
2
3
|
[root@localhost ~] # alias|grep cp
alias cp = 'cp -i'
[root@localhost ~] #
|
当你执行cp时,其实执行的是cp -i,如果不想每次询问可以直接注释掉。重新打开回话窗口不再提示
1
2
3
|
[root@localhost ~] # cat ~/.bashrc |grep cp
#alias cp='cp -i' [root@localhost ~] #
|
如果临时取消掉,可以通过以下
1
2
3
|
[root@localhost static] # unalias cp
[root@localhost static] # cp /usr/local/src/static/* . -a
[root@localhost static] #
|
-d:拷贝软连接时需要加上该参数,否则把软连接的目标文件拷贝过去,而加上后,其实只是拷贝了一个连接文件。
-p :连同档案的属性一起复制过去,而非使用预设属性
拷贝目录:
1
2
3
4
5
6
|
[root@localhost dir ] # mkdir dir dir1
[root@localhost dir ] # touch dir/file{1..9}
[root@localhost dir ] # cp -r dir dir1/
[root@localhost dir1] # ls
dir [root@localhost dir1] #
|
Tips:可以多个目录一起拷贝 最后一个为目标目录
1
2
3
4
5
|
[root@justin ~] # cp /etc/rc.sysinit /etc/inittab /mnt/lv1/
[root@justin ~] # ls !$
ls /mnt/lv1/
inittab lost+found rc.sysinit [root@justin ~] #
|
如果要排除/home/data目录下面的a、b、c、三个目录,同时拷贝其它所有目录,执行以下命令
1
2
|
yum install rsync #安装rsync
rsync -av --exclude data /a --exclude data /b --exclude data /c data /bak
|
注意:--exclude后面的路径不能为绝对路径,必须为相对路径才可以,否则出错。且备份的目录data不能写成data/
-v, --verbose 详细模式输出
-c, --checksum 打开校验开关,强制对文件传输进行校验
-a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
-z, --compress 对备份的文件在传输时进行压缩处理
-r, --recursive 对子目录以递归模式处理
-R, --relative 使用相对路径信息
-b, --backup 创建备份,也就是对于目的已经存在有同样的文件名时,将老的文件重新命名为~filename。可以使用--suffix选项来指定不同的备份文件前缀。
--exclude=PATTERN 指定排除不需要传输的文件模式
--exclude=PATTERN 指定排除不需要传输的文件模式
--include=PATTERN 指定不排除而需要传输的文件模式
--exclude-from=FILE 排除FILE中指定模式的文件
--include-from=FILE 不排除FILE指定模式匹配的文件
scp
1
|
[root@localhost app] # scp -r ProxyServer root@192.168.1.104:/app
|
-p 可以保持文件属性不变
1
|
[root@localhost ~] # scp -rp * root@10.15.44.239:/var/log/remote/
|
登陆自动指定目录
在用户的.bash_profile里添加cd 目录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[root@localhost ~] # pwd
/root [root@localhost ~] # vim /root/.bash_profile
# .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi cd /sh
# User specific environment and startup programs PATH=$PATH:$HOME /bin
export PATH
|
mv---移动或重命名文件
1
2
3
4
5
6
|
[root@Zabbix_server fonts] # ls
DejaVuSans.ttf STXINGKA.TTF [root@Zabbix_server fonts] # mv DejaVuSans.ttf{,bak}
[root@Zabbix_server fonts] # ls
DejaVuSans.ttfbak STXINGKA.TTF [root@Zabbix_server fonts] #
|
-b 如果已存在相同文件名,则覆盖前进行备份
-f 如果已存在相同文件名,而用户不具有写的权限,则强制覆盖
-i 如果已存在相同文件名,覆盖前提示用户进行确认
-u 比较原文件与目标文件修改时间,如果目标文件较新则不覆盖
-v 列出所有被移动或重命名的文件
-i 若目的地已有同名档案,则先询问是否覆盖旧档
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
[root@localhost ~] # cd /home/
[root@localhost home] # touch justinmv
[root@localhost home] # ls
justin justinmv lost+found t [root@localhost home] # mv justinmv justin1
[root@localhost home] # ls
justin justin1 lost+found t [root@justin home] # ls
justin lost+found mv
[root@justin home] # mv mv{,bak}
[root@justin home] # ls
justin lost+found mvbak [root@localhost home] # mv justin1 /usr/local/src/
[root@localhost home] # ls
justin lost+found t [root@localhost home] # cd /usr/local/src/ && ls
justin1 nagios nagios1 nagios2 nagios-3.5.0. tar .gz
[root@localhost home] # touch justin1
[root@localhost home] # mv -i justin1 /usr/local/src/
mv :是否覆盖 "/usr/local/src/justin1" ? y
[root@justin touch ] # ls
file1 file2 file3 file4 file5 [root@justin touch ] # mv file1 file2 ../mv/
[root@justin touch ] # mv file{3..4} ../mv/
[root@justin touch ] # ls ../mv/
file1 file2 file3 file4 [root@justin touch ] #
|
last---显示当前和过去用户登陆的所有相关信息
last命令可以显示指定账户或终端登录用户的相关信息。不带参数的last命令显示/var/log/wtmp文件中记录的登录用户的清单。
1
2
3
4
|
[root@Zabbix_server home] # last
justin pts /1 10.15.71.162 Thu Aug 28 13:44 still logged in justin pts /1 10.15.71.162 Thu Aug 28 13:31 - 13:44 (00:12)
root pts /0 10.15.71.162 Tue Aug 26 09:58 still logged in
|
-n<显示行数>或-<显示行数> 设置列出名单的显示行数。
-f file 指定记录文件,默认情况下last命令读取/var/log/wtmp文件。
-t YYYYMMDDHHMMSS 显示指定时间为止的所有登录信息,日期格式为年月日时分秒
1
2
3
|
[root@Zabbix_server home] # last -t 20140828134000
justin pts /1 10.15.71.162 Thu Aug 28 13:31 gone - no logout root pts /0 10.15.71.162 Tue Aug 26 09:58 still logged in
|
-R 不显示登录系统的主机名或IP地址。
-a 在最后一列显示登录系统主机的主机名或IP地址。
-o 读取老格式的wtmp文件。
-x 显示登录登出的记录
1
2
3
4
5
6
7
8
9
|
[root@Zabbix_server home] # last -5xa
justin pts /1 Thu Aug 28 13:44 still logged in 10.15.71.162
justin pts /1 Thu Aug 28 13:31 - 13:44 (00:12) 10.15.71.162
root pts /0 Tue Aug 26 09:58 still logged in 10.15.71.162
root pts /0 Tue Aug 26 09:57 - 09:58 (00:00) :0.0
root tty1 Tue Aug 26 09:57 still logged in :0
wtmp begins Wed Jul 16 01:45:27 2014 [root@Zabbix_server home] #
|
注:显示特定的终端,可以用数字表示,如:last 0 ===> last tty0
1
2
3
4
5
6
7
|
[root@Zabbix_server home] # last 1 -3
root tty1 :0 Tue Aug 26 09:57 still logged in root tty1 :0 Mon Aug 25 22:55 - down (00:01) root tty1 :0 Thu Aug 21 10:42 - down (03:45) wtmp begins Wed Jul 16 01:45:27 2014 [root@Zabbix_server home] #
|
lastb---显示登陆失败的信息
选项和last一样
1
2
3
4
5
6
7
|
[root@Zabbix_server home] # lastb -3a
justin ssh :notty Thu Aug 28 13:44 - 13:44 (00:00) 10.15.71.162
justin ssh :notty Thu Aug 28 13:44 - 13:44 (00:00) 10.15.71.162
justin ssh :notty Thu Aug 28 13:44 - 13:44 (00:00) 10.15.71.162
btmp begins Tue Aug 5 13:51:27 2014 [root@Zabbix_server home] #
|
lastlog---显示用户最后登陆的相关信息
如果此用户从来没有登录,则显示:**Never logged in**
1
2
3
|
[root@Zabbix_server home] # lastlog
zabbix **Never logged in **
justin pts /1 10.15.71.162 Thu Aug 28 13:44:24 +0800 2014
|
-u 查询指定uid的用户登陆信息
1
2
3
4
|
[root@Zabbix_server home] # lastlog -u 500
Username Port From Latest zabbix **Never logged in **
[root@Zabbix_server home] #
|
whoami---显示登陆名
1
2
3
|
[root@localhost ~] # whoami
root [root@localhost ~] #
|
users---只能列出当前登录的用户名
1
2
3
|
[root@justin ~] # users
root [root@justin ~] #
|
who---可以列出用户名、终端、登录时间、来源地点等信息
1
2
3
|
[root@justin ~] # who
root pts /0 2013-10-22 17:34 (10.15.72.73)
[root@justin ~] #
|
who -b 查看最后一次系统启动的时间。
1
2
3
|
[root@localhost ~] # who -b
system boot 2016-03-17 10:11
[root@localhost ~] #
|
who -r 查看当前系统运行时间
1
2
3
|
[root@localhost ~] # who -r
run-level 3 2016-05-24 08:41 last=S
[root@localhost ~] #
|
last reboot 可以看到Linux系统历史启动的时间
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@localhost ~] # last reboot
reboot system boot 2.6.32-431.el6.x Thu Mar 17 10:11 - 10:22 (00:11) reboot system boot 2.6.32-431.el6.x Wed Mar 16 12:48 - 10:22 (21:34) reboot system boot 2.6.32-431.el6.x Tue Mar 15 17:31 - 20:39 (03:07) reboot system boot 2.6.32-431.el6.x Thu Mar 3 13:50 - 15:19 (9+01:29) reboot system boot 2.6.32-431.el6.x Mon Feb 1 14:30 - 14:55 (00:24) reboot system boot 2.6.32-431.el6.x Fri Jan 29 10:24 - 14:55 (3+04:30) reboot system boot 2.6.32-431.el6.x Mon Dec 28 06:48 - 14:37 (2+07:49) reboot system boot 2.6.32-431.el6.x Sat Oct 10 23:08 - 15:56 (-7:-12) wtmp begins Sat Oct 10 23:08:49 2015 [root@localhost ~] # last reboot|head -1
reboot system boot 2.6.32-431.el6.x Thu Mar 17 10:11 - 10:25 (00:14) [root@localhost ~] #
|
uptime---列出用户名、终端、来源地点、登录时间、执行的命令等参数
1
2
3
|
[root@localhost ~] # uptime
09:23:34 up 56 min, 2 users , load average: 0.00, 0.00, 0.00
[root@localhost ~] #
|
w---列出用户名、终端、来源地点、登录时间、执行的命令等参数
1
2
3
4
5
|
[root@justin ~] # w
19:51:03 up 2:19, 1 user, load average: 0.08, 0.02, 0.01
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts /0 10.15.72.73 17:34 0.00s 0.22s 0.01s w
[root@justin ~] #
|
!!---执行最近一次的命令
1
2
3
4
|
[root@localhost ~] # !!
whoami root [root@localhost ~] #
|
du---统计目录(或文件)所占磁盘空间的大小
-c或--total 除了显示目录或文件的大小外,同时也显示所有目录或文件的总和。
-a:全部文件与目录大小都列出来。如果不加任何选项和参数只列出目录(包含子目录)大小。
-c:最后加总
-b:列出的值以bytes为单位输出,默认是以Kbytes
-k:以KB为单位输出
-m:以MB为单位输出
-h或--human-readable 以K,M,G为单位,提高信息的可读性。系统自动调节单位,例如文件太小可能就几K,那么就以K为单位显示,如果大到几G,则就以G为单位显示
-k或--kilobytes 以1024 bytes为单位。
-m或--megabytes 以1MB为单位
-s或--summarize 仅显示总计,即当前目录的大小。
1
2
3
4
5
6
7
8
9
10
11
|
[root@justin ~] # du -c /mnt/lv1
16 /mnt/lv1/lost +found
44 /mnt/lv1
44 total [root@justin ~] # du -sh /mnt/lv1
44K /mnt/lv1
[root@justin ~] # du -shm /mnt/lv1
1 /mnt/lv1
[root@justin ~] # du -shk /mnt/lv1
44 /mnt/lv1
[root@justin ~] #
|
df---检查linux服务器的文件系统的磁盘空间占用情况
-h 方便阅读方式显示
-i 显示inode信息
-T 文件系统类型
1
2
3
4
5
6
7
8
9
10
11
|
[root@justin ~] # df -Th
Filesystem Type Size Used Avail Use% Mounted on /dev/sda2 ext4 9.9G 2.4G 7.0G 26% /
tmpfs tmpfs 504M 0 504M 0% /dev/shm
/dev/sda1 ext4 194M 26M 158M 15% /boot
/dev/sda5 ext4 7.7G 146M 7.2G 2% /home
/dev/mapper/sdcvg-sdclv ext4 4.0G 137M 3.7G 4% /mnt/lv1
/dev/mapper/sdcvg-sdclv1 ext4 4.0G 72M 3.7G 2% /mnt/lv2
[root@justin ~] #
|
id---显示用户的ID,以及所属群组的ID
1
2
|
[root@localhost ~] # id justin
uid=500(justin) gid=500(justin) 组=500(justin) |
说明:id会显示用户以及所属群组的实际与有效ID。若两个ID相同,则仅显示实际ID。若仅指定用户名称,则显示目前用户的ID。执行id时不添加参数是查看当前用户的身份标识信息;gid”:表示的是用户的基本组;“组”:表示的是用户所属的所有组,其中“组”中的第1个组账号为该用户的基本组,其他组账号为该用户的附属组
file---查看文件类型
1
2
3
4
5
|
[root@justin 2.6.32-279.el6.i686] # file extra/
extra/: directory [root@justin 2.6.32-279.el6.i686] # file modules.dep
modules.dep: ASCII text, with very long lines [root@justin 2.6.32-279.el6.i686] #
|
Linux中文件类型不是按后缀名来定义,而是根据文件内容来判断,即使后缀类似windows的.exe也可能只是文本文件
ln---为文件或目录建立链接
-s---进行软链结(symbolic link)
1
2
3
4
|
#为网卡建立软连接wk [root@localhost ~] # ln -s /etc/sysconfig/network-scripts/ifcfg-eth0 /root/wk
[root@localhost ~] # ls -lh /root/wk
lrwxrwxrwx. 1 root root 41 9月 23 15:55 /root/wk -> /etc/sysconfig/network-scripts/ifcfg-eth0
|
说明:软连接相当于window快捷方式,可以在wk里修改文件,删除链接时,直接用rm命令:rm -f /root/wk
Linux/Unix 档案系统中,有所谓的连结(link),我们可以将其视为档案的别名,链接文件分为硬链接、软链接两种类型,主要区别是:不能对目录创建硬链接,也不能跨越不同分区创建硬链接文件,而软链接则没有这些限制,所以平时使用的大都是软链接。硬连结的意思是一个档案可以有多个名称,而软连结的方式则是产生一个特殊的档案,该档案的内容是指向另一个档案的位置。硬连结是存在同一个档案系统中,而软连结却可以跨越不同的档案系统。不论是硬连结或软链结都不会将原本的档案复制一份,只会占用非常少量的磁碟空间。
alias---设置命令别名
说明:命令别名通常是命令的缩写,对于经常使用的命令,通过设置别名可以简化操作,提高工作效率。
单独执行alias命令可以列出当前系统中已经存在的别名命令。在执行这个命令时需要注意,“=”的两边不能有空格,在标准命令的两端要使用单引号。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
[root@localhost ~] # alias wk='/etc/sysconfig/network-scripts/ifcfg-eth0'
[root@localhost ~] # alias
alias cp = 'cp -i'
alias l.= 'ls -d .* --color=auto'
alias ll= 'ls -l --color=auto'
alias ls = 'ls --color=auto'
alias mv = 'mv -i'
alias rm = 'rm -i'
alias which = 'alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
alias wk= '/etc/sysconfig/network-scripts/ifcfg-eth0'
[root@localhost ~] # unalias wk
[root@localhost ~] # alias
alias cp = 'cp -i'
alias l.= 'ls -d .* --color=auto'
alias ll= 'ls -l --color=auto'
alias ls = 'ls --color=auto'
alias mv = 'mv -i'
alias rm = 'rm -i'
alias which = 'alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@localhost ~] #
|
设置别名也可以在文件/boot/.bashrc中设置
1
|
[root@localhost ~] # vim /root/.bashrc
|
用source命令在当前bash环境下读取并执行bashrc中的命令,这样我们自定义的命令就生效了
1
|
[root@localhost ~] # source .bashrc
|
date
打印当前系统的时间
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
[root@localhost ~] # date
Thu Nov 13 07:36:28 CST 2014 [root@localhost ~] # date "+%y%m%d"
141113 [root@localhost ~] # date "+%Y%m%d"
20141113 [root@localhost ~] # date "+%Y%m%d %H:%M:%S"
20141113 07:37:04 [root@localhost ~] # date -d "+1 day" "+%Y%m%d %H:%M:%S"
20141114 07:37:31 [root@localhost ~] # date -d "-1 month" "+%Y%m%d %H:%M:%S"
20141013 07:37:45 [root@localhost ~] # date -d "-1 year" "+%Y%m%d %H:%M:%S %w"
20131113 07:38:22 3 [root@localhost ~] # date "+%Y年%m月%d日 %H:%M:%S 星期%w"
2014年11月20日 13:39:17 星期4 [root@localhost ~] # date -d "-5 month" "+%Y年%m月%d日 %H:%M:%S 星期%w"
2014年06月20日 13:41:12 星期5 [root@Monitor ~] # date -d "yesterday" +"%Y%m%d"
20150917 [root@Monitor ~] # rm -rf `date -d "today" +"%Y%m%d"`
[root@Monitor ~] # mkdir `date -d "today" +"%Y%m%d"`
[root@localhost home] # touch `date +%Y%m%d`
[root@localhost home] # touch file`date +%Y%m%d`
|
%Y表示年(注意%y和%Y的区别),%m表示月,%d表示日期,%H表示小时,%M表示分钟,%S表示秒,%w表示星期几,-d 选项可以打印n天前或者n天后的日期,当然也可以打印n个月/年前或者后的日期。
给定的格式FORMAT 控制着输出
%a 当前locale 的星期名缩写(例如: 日,代表星期日)
%A 当前locale 的星期名全称 (如:星期日)
%b 当前locale 的月名缩写 (如:一,代表一月)
%B 当前locale 的月名全称 (如:一月)
%c 当前locale 的日期和时间 (如:2005年3月3日 星期四 23:05:25)
%C 世纪;比如 %Y,通常为省略当前年份的后两位数字(例如:20)
%d 按月计的日期(例如:01)
%D 按月计的日期;等于%m/%d/%y
%e 按月计的日期,添加空格,等于%_d
%F 完整日期格式,等价于 %Y-%m-%d
%h 等于%b
%H 小时(00-23)
%I 小时(00-12)
%c 按年计的日期(001-366)
%k 时(0-23)
%l 时(1-12)
%m 月份(01-12)
%M 分(00-59)
%n 换行
%p 当前locale 下的"上午"或者"下午",未知时输出为空
%P 与%p 类似,但是输出小写字母
%r 当前locale 下的 12 小时时钟时间 (如:11:11:04 下午)
%R 24 小时时间的时和分,等价于 %H:%M
%s 自UTC 时间 1970-01-01 00:00:00 以来所经过的秒数
%S 秒(00-60)
%t 输出制表符 Tab
%T 时间,等于%H:%M:%S
%u 星期,1 代表星期一
%U 一年中的第几周,以周日为每星期第一天(00-53)
%V ISO-8601 格式规范下的一年中第几周,以周一为每星期第一天(01-53)
%w 一星期中的第几日(0-6),0 代表周一
%W 一年中的第几周,以周一为每星期第一天(00-53)
%x 当前locale 下的日期描述 (如:12/31/99)
%X 当前locale 下的时间描述 (如:23:13:48)
%y 年份最后两位数位 (00-99)
%Y 年份
设置系统所在的时区
1
2
3
4
5
6
7
8
9
|
[root@localhost mpc-0.8.1] # date -R #查看时区
Sat, 22 Jul 2017 09:39:08 +0800 [root@localhost mpc-0.8.1] # more /etc/sysconfig/clock |grep -v "#" #查看clock系统配置文件
ZONE= "Asia/Shanghai"
UTC= true
ARC= false
[root@localhost mpc-0.8.1] # tzselect
#复制相应的时区文件,替换系统时区文件;或者创建链接文件 [root@localhost mpc-0.8.1] # cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
|
tzselect命令只告诉你选择的时区的写法,并不会生效。所以现在它还不是东8区北京时间。你可以在.profile、.bash_profile或者/etc/profile中设置正确的TZ环境变量并导出。 例如在.bash_profile里面设置 TZ='Asia/Shanghai'; export TZ并使其生效。
1
2
3
4
|
[root@localhost ~] # vim /etc/profile
TZ= 'Asia/Shanghai'
export TZ
[root@localhost ~] # source /etc/profile
|
临时修改系统时间
1
2
3
4
5
6
7
8
9
10
11
|
[root@localhost remote] # date
Mon Jan 2 12:34:11 CST 2017 [root@localhost remote] # date -s 20161020
Thu Oct 20 00:00:00 CST 2016 [root@localhost remote] # date -s 10:23:14
Mon Jan 2 10:23:14 CST 2017 [root@localhost remote] # date -s "20161020 10:23:14"
Thu Oct 20 10:23:14 CST 2016 [root@localhost remote] # date -s "10/21/2016 10:23:14"
Fri Oct 21 10:23:14 CST 2016 [root@localhost remote] #
|
在系统启动时,Linux操作系统将时间从CMOS中读到系统时间变量中,以后修改时间通过修改系统时间实现。为了保持系统时间与CMOS时间的一致性,Linux每隔一段时间会将系统时间写入CMOS。由于该同步是每隔一段时间(大约是11分钟)进行的,在我们执行date -s后,如果马上重起机器,修改时间就有可能没有被写入CMOS,这就是问题的原因。
硬件时间的设置,可以用hwclock或者clock命令。其中,clock和hwclock用法相近,只用一个就行,只不过clock命令除了支持x86硬件体系外,还支持Alpha硬件体系。//查看硬件时间可以是用hwclock,hwclock --show 或者hwclock -r
1
2
3
|
[root@localhost remote] # hwclock --show
Mon 02 Jan 2017 12:47:28 PM CST -0.232754 seconds [root@localhost remote] #
|
系统时间和硬件时间的同步
以系统时间为基准,修改硬件时间
1
|
[root@localhost remote] # clock -w
|
以硬件时间为基准,修改系统时间
1
|
[root@localhost remote] # hwclock -s
|
hwclock参数可以通过--help获取
1
2
3
4
5
6
7
8
|
[root@localhost remote] # hwclock --help
-r | --show read hardware clock and print result
-- set set the rtc to the time given with -- date
-s | --hctosys set the system time from the hardware clock
-w | --systohc set the hardware clock to the current system time
--systz set the system time based on the current timezone
--adjust adjust the rtc to account for systematic drift since
the clock was last set or adjusted
|
CST:中国标准时间(China Standard Time),这个解释可能是针对RedHat Linux。
UTC:协调世界时,又称世界标准时间,简称UTC,从英文国际时间/法文协调时间“Universal Time/Temps Cordonné”而来。
1
2
3
4
5
6
7
8
9
10
|
[root@localhost ~] # cal
July 2017
Su Mo Tu We Th Fr Sa 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 [root@localhost ~] #
|
su、sudo命令
su命令是一个切换用户的工具,将普通用户切换到超级用户下,也可以从超级用户切换到普通用户;
sudo被称为受限制的su,也被称为授权认证的su。他是允许系统管理员分配给普通用户一些合理的“权利”,并且不需要普通用户知道超级用户密码,就能让他们执行一些只有超级用户或其他特许用户才能完成的任务,sudo执行命令的流程是:将当前用户切换到超级用户下,或切换到指定的用户下,然后以超级用户或其指定切换到的用户身份执行命令,执行完成后,直接退回到当前用户,而这一切的完成要通过sudo的配置文件/etc/sudoers来进行授权。sudo都提供了一个编辑该文件的命令:visudo来对该文件进行修改。强烈推荐使用该命令修改sudoers。因为它会帮你校验文件配置是否正确,如果不正确,在保存退出时就会提示你哪段配置出错的。
例如:普通用户justin对/etc/shadow没有访问权限
1
2
3
4
5
|
[justin@localhost ~]$ cat /etc/shadow
cat : /etc/shadow : 权限不够
[justin@localhost ~]$ who am i
justin pts /1 2014-11-18 04:41 (10.15.24.142)
[justin@localhost ~]$ |
执行visudo之后,可以看见缺省只有一条root的配置,我们在起下面加一条jutsin对/etc/shadow有访问的权限:
1
2
3
4
5
6
7
|
[root@localhost root] # visudo
97 ## Allow root to run any commands anywhere
98 root ALL=(ALL) ALL
99 justin ALL= /bin/cat /etc/shadow [root@localhost root] # su - justin
[justin@localhost ~]$ sudo cat /etc/shadow
[ sudo ] password for justin:
|
执行这个命令后,需要输入justin用户的密码,然后就可访问文件内容了。在这里sudo使用时间戳文件来完成类似“检票”的系统,当用户输入密码后就获得了一张默认存活期为5分钟的“入场券”(默认值可以在编译的时候改变)。超时以后,用户必须重新输入密码才能查看文件内容。
如果每次都需要输入密码,那么某些自动调用超级权限的程序就会出现问题,此时可以通过下面的设置,让普通用户无需输入密码即可执行具有超级权限的程序。
1
|
justin ALL= NOPASSWD: /bin/cat /etc/shadow
|
如果要让一个普通用户justin具有超级用户的所有权限,而又不想输入超级用户的密码:
1
2
|
#justin ALL= NOPASSWD: /bin/cat /etc/shadow justin ALL=(ALL) NOPASSWD: ALL |
history---查看历史记录
在Linux下可通过history命令查看用户所有的历史操作记录,同时shell命令操作记录默认保存在用户目录下的.bash_history文件中,通过这个文件可以查询shell命令的执行历史,
1
2
3
4
5
|
#列出最近执行过的3条历史命令 [root@localhost ~] # history 3
15 wc /etc/resolv .conf
16 wc -l /etc/passwd
17 history 3
|
在每一个执行过的shell命令行前均有一个编号,代表其在历史列表中的序号。如果想重新执行其中某一条命令,可以采用“!序号”的格式。如“!16”就表示把第16条历史命令重新执行一遍。!! (连续两个”!”),表示执行上一条指令;!字符串(字符串大于等于1),例如!ta,表示执行命令历史中最近一次以ta为开头的指令。
1
2
3
|
[root@localhost ~] # !16
wc -l /etc/passwd
35 /etc/passwd
|
默认的history命令只能查看用户历史操作记录,并不能区分每个用户操作命令的时间,通过在/etc/bashrc文末加入以下四行内容让history命令自动记录所有shell命令的执行时间:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
[root@localhost ~] # history|tail -5
89 history | tail 90 history | tail -5
91 exit
92 date
93 history | tail -5
[root@localhost ~] # vim /etc/bashrc
87 #HISTFILESIZE定义了在.bash_history文件中保存命令的记录总数,默认值是1000,这里设置为4000
88 HISTFILESIZE=4000
89 #HISTSIZE定义了history命令输出的记录总数
90 HISTSIZE=4000
91 #HISTTIMEFORMAT定义时间显示格式,这里的格式与date命令后的“+"%F %T"”是一致的的,HISTTIMEFORMA 作为history的时间变量将值传递给history命令
92 HISTTIMEFORMAT= '%F %T'
93 export HISTTIMEFORMAT
: set nu
[root@localhost ~] # source /etc/bashrc
[root@localhost ~] # history|tail -5
94 2014-11-18 04:16:11vim /etc/bashrc 95 2014-11-18 04:21:48history| tail -5
96 2014-11-18 04:22:03source /etc/bash
97 2014-11-18 04:22:06source /etc/bashrc 98 2014-11-18 04:22:10history| tail -5
[root@localhost ~] # date
2014年 11月 18日 星期二 04:22:17 CST [root@localhost ~] #
|
为了确保服务器的安全,可以实现详细记录登录过系统的用户、IP地址、shell命令以及详细操作时间等,并将这些信息以文件的形式保存在一个安全的地方
将下面这段代码添加到/etc/profile文件末尾中,即可实现上述功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
[root@localhost ~] # vim /etc/profile
79 #history
80 USER_IP=` who -u am i 2> /dev/null | awk '{print $NF}' | sed -e 's/[()]//g' `
81 HISTDIR= /usr/share/ . history
82 if [ -z $USER_IP ]
83 then
84 USER_IP=` hostname `
85 fi
86 if [ ! -d $HISTDIR ]
87 then
88 mkdir -p $HISTDIR
89 chmod 777 $HISTDIR
90 fi
91 if [ ! -d $HISTDIR/${LOGNAME} ]
92 then
93 mkdir -p $HISTDIR/${LOGNAME}
94 chmod 300 $HISTDIR/${LOGNAME}
95 fi
96 export HISTSIZE=4000
97 DT=` date +%Y%m%d_%H%M%S`
98 export HISTFILE= "$HISTDIR/${LOGNAME}/${USER_IP}.history.$DT"
99 export HISTTIMEFORMAT= "[%Y.%m.%d %H:%M:%S]"
100 chmod 600 $HISTDIR/${LOGNAME}/*. history * 2> /dev/null
:wq [root@localhost ~] # source /etc/profile
[root@localhost ~] #
|
这段代码将每个用户的shell命令执行历史以文件的形式保存在/usr/share/.history目录中,每个用户一个文件夹,并且文件夹下的每个文件以IP地址加shell命令操作时间的格式命名。下面是user01用户执行shell命令的历史记录文件,保存历史命令的文件夹目录要尽量隐蔽,避免被黑客发现后删除。基本效果如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
[root@localhost ~] # cd /usr/share/.history/
[root@localhost . history ] # ll
总用量 8 d-wx------ 2 justin justin 4096 11月 18 04:36 justin d-wx------ 2 root root 4096 11月 18 04:36 root [root@localhost . history ] # cd justin/
[root@localhost justin] # ls -la
总用量 12 d-wx------ 2 justin justin 4096 11月 18 04:36 . drwxrwxrwx 4 root root 4096 11月 18 04:33 .. -rw------- 1 justin justin 79 11月 18 04:36 10.15.24.142. history .20141118_043321
[root@localhost justin] # cat 10.15.24.142.history.20141118_043321
#1416256413 echo $LANG
#1416256445 cat /proc/cpuinfo | grep ml| grep flags| wc -l
[root@localhost justin] # cd ../root/
[root@localhost root] # ls -al
总用量 12 d-wx------ 2 root root 4096 11月 18 04:36 . drwxrwxrwx 4 root root 4096 11月 18 04:33 .. -rw------- 1 root root 577 11月 18 04:36 10.15.24.142. history .20141118_043037
[root@localhost root] # cat 10.15.24.142.history.20141118_043037
#1416255336 date #1416255351 history | tail -5
#1416255371 vim /etc/bashrc #1416255708 history | tail -5
#1416255723 source /etc/bash
|
shutdown---关闭或重启系统
-r : 关机后重新开机
-h : 关机后停机
-t seconds : 设定在几秒钟之后进行关机程序
-c : 取消目前已经进行中的关机动作
-f : 关机时,不做 fcsk 动作(检查 Linux 档系统)
-F : 关机时,强迫进行 fsck 动作
1
2
3
4
5
6
|
#使用shutdown命令马上重启系统 [root@localhost ~] # shutdown –r now
#使用shutdown命令马上关闭系统 [root@localhost ~] # shutdown –h now
#使用shutdown命令设置在15分钟以后自动重启系统 [root@localhost ~] # shutdown –r +15
|
说明:
halt:马上关闭系统
reboot:马上重启系统。
“init 0”:关闭系统
“init 6”:重启系统
重定向
说明:Linux系统中标准的输入设备为键盘,标准输出设备为屏幕,但在某些情况下,我们希望能从键盘以外的其他输入设备读取数据,或者将数据送到屏幕外的其他输出设备,这种情况称为重定向。Shell中输入输出重定向主要依靠重定向符号来实现,重定向的目标通常是一个文件。
输入重定向:输入重定向就是将命令中接收输入的途径由默认的键盘重定向为指定的文件,需要使用“<”重定向操作符。如命令“wc < f1”就表示将f1文件的信息作为wc命令的输入。
输出重定向:输出重定向是将命令的输出结果重定向到一个文件中,而不是显示在屏幕上。输出重定向使用“>”或“>>”操作符,分别用于覆盖、追加文件。“>”重定向符后面指定的文件如果不存在,在命令执行中将建立该文件,并保存命令结果到文件中。“>”重定向符后面指定的文件如果存在,命令执行时将清空文件的内容并保存命令结果到文件中。
1
2
|
#查看/etc/passwd文件的内容,并将输出结果保存到pass.txt文件中。 [root@localhost ~] # cat /etc/passwd > pass.txt
|
执行该命令后,会在当前目录下生成一个名为pass.txt的文件,文件中的内容就是“cat /etc/passwd”命令执行的结果。
“>>”重定向操作符可以将命令执行的结果重定向并追加到指定文件的末尾保存,而不覆盖文件中原有的内容。
1
2
|
#查看/etc/shadow文件的后3行内容,并将输出结果追加保存到pass.txt文件中。 [root@localhost ~] # tail -3 /etc/shadow >> pass.txt
|
使用重定向就不会输出到屏幕,如果想同时输出到屏幕和重定向到指定文件可以使用tee命令,
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@localhost test ] # tail -5 /etc/passwd|tee tee.txt
rpcuser:x:29:29:RPC Service User: /var/lib/nfs : /sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User: /var/lib/nfs : /sbin/nologin
gdm:x:42:42:: /var/lib/gdm : /sbin/nologin
sshd:x:74:74:Privilege-separated SSH: /var/empty/sshd : /sbin/nologin
tcpdump:x:72:72::/: /sbin/nologin [root@localhost test ] # cat tee.txt
rpcuser:x:29:29:RPC Service User: /var/lib/nfs : /sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User: /var/lib/nfs : /sbin/nologin
gdm:x:42:42:: /var/lib/gdm : /sbin/nologin
sshd:x:74:74:Privilege-separated SSH: /var/empty/sshd : /sbin/nologin
tcpdump:x:72:72::/: /sbin/nologin
[root@localhost test ] #
|
tee--重定向到文件并打印到屏幕
1
2
3
4
5
|
[root@localhost dir1] # cat /proc/cpuinfo |grep flags|grep lm|wc -l|tee -a cpuinfo.txt
2 [root@localhost dir1] # cat cpuinfo.txt
2 [root@localhost dir1] #
|
默认tee为叠加,参数-a为追加
tr :
替换字符,常用来处理文档中出现的特殊符号,如DOS文档中出现的^M符号。常用的选项有两个:
-d :删除某个字符,-d 后面跟要删除的字符
-s :把重复的字符去掉
1
2
3
4
5
|
[root@localhost test ] # cat /etc/passwd|grep ^root
root:x:0:0:root: /root : /bin/bash
[root@localhost test ] # cat /etc/passwd|grep ^root|tr '[a-z]' '[A-Z]'
ROOT:X:0:0:ROOT: /ROOT : /BIN/BASH
[root@localhost test ] #
|
wget
-O –output-document=FILE下载文件保存为别的文件名
1
|
[root@localhost ~] # wget -O rinetd1.tar.gz http://www.boutell.com/rinetd/http/rinetd.tar.gz
|
-c 断点续传
1
|
[root@localhost ~] # wget -cO rinetd1.tar.gz
|
–limit -rate限速下载
1
|
[root@localhost ~] # wget –limit-rate=300k http://cn.wordpress.org/wordpress-3.1-zh_CN.zip
|
-b后台下载
–spider测试下载链接
如果下载链接正确,将会显示
1
2
3
4
5
6
|
wget –spider URL Spider mode enabled. Check if remote file exists.
HTTP request sent, awaiting response… 200 OK Length: unspecified [text /html ]
Remote file exists and could contain further links,
but recursion is disabled — not retrieving. |
但当你给错了一个链接,将会显示如下错误
1
2
3
4
|
wget –spider url Spider mode enabled. Check if remote file exists.
HTTP request sent, awaiting response… 404 Not Found Remote file does not exist — broken link!!!
|
使用spider参数可以
定时下载之前进行检查
间隔检测网站是否可用
检查网站页面的死链接
–tries增加重试次数
wget –tries=40 URL
-i下载多个文件
1
2
3
4
5
6
7
|
[root@localhost ~] # cat > filelist.txt
url1 url2 url3 url4 [root@localhost ~] # wget -i filelist.txt
[root@localhost ~] #
|
–mirror镜像网站
wget –mirror -p –convert-links -P ./LOCAL URL
–miror:开户镜像下载
-p:下载所有为了html页面显示正常的文件
–convert-links:下载后,转换成本地的链接
-P ./LOCAL:保存所有文件和目录到本地指定目录
–reject过滤指定格式下载
wget –reject=gif url
-o把下载信息存入日志文件
wget -o download.log URL
-Q限制总下载文件大小
wget -Q5m -i filelist.txt 下载的文件超过5M而退出下载
这个参数对单个文件下载不起作用,只能递归下载时才有效
-r -A下载指定格式文件
wget -r -A.pdf url 载一个网站的所有PDF文件
FTP下载
wget –ftp-user=USERNAME –ftp-password=PASSWORD url
启动参数:
-V, –version 显示wget的版本后退出
-h, –help 打印语法帮助
-b, –background 启动后转入后台执行
-e, –execute=COMMAND 执行`.wgetrc’格式的命令,wgetrc格式参见/etc/wgetrc或~/.wgetrc
记录和输入文件参数:
-o, –output-file=FILE 把记录写到FILE文件中
-a, –append-output=FILE 把记录追加到FILE文件中
-d, –debug 打印调试输出
-q, –quiet 安静模式(没有输出)
-v, –verbose 冗长模式(这是缺省设置)
-nv, –non-verbose 关掉冗长模式,但不是安静模式
-i, –input-file=FILE 下载在FILE文件中出现的URLs
-F, –force-html 把输入文件当作HTML格式文件对待
-B, –base=URL 将URL作为在-F -i参数指定的文件中出现的相对链接的前缀
–sslcertfile=FILE 可选客户端证书
–sslcertkey=KEYFILE 可选客户端证书的KEYFILE
–egd-file=FILE 指定EGD socket的文件名
下载参数:
–bind-address=ADDRESS 指定本地使用地址(主机名或IP,当本地有多个IP或名字时使用)
-t, –tries=NUMBER 设定最大尝试链接次数(0 表示无限制).
-O –output-document=FILE 把文档写到FILE文件中
-nc, –no-clobber 不要覆盖存在的文件或使用.#前缀
-c, –continue 接着下载没下载完的文件
–progress=TYPE 设定进程条标记
-N, –timestamping 不要重新下载文件除非比本地文件新
-S, –server-response 打印服务器的回应
–spider 不下载任何东西
-T, –timeout=SECONDS 设定响应超时的秒数
-w, –wait=SECONDS 两次尝试之间间隔SECONDS秒
–waitretry=SECONDS 在重新链接之间等待1…SECONDS秒
–random-wait 在下载之间等待0…2*WAIT秒
-Y, –proxy=on/off 打开或关闭代理
-Q, –quota=NUMBER 设置下载的容量限制
–limit-rate=RATE 限定下载输率
目录参数:
-nd –no-directories 不创建目录
-x, –force-directories 强制创建目录
-nH, –no-host-directories 不创建主机目录
-P, –directory-prefix=PREFIX 将文件保存到目录 PREFIX/…
–cut-dirs=NUMBER 忽略 NUMBER层远程目录
HTTP 选项参数:
–http-user=USER 设定HTTP用户名为 USER.
–http-passwd=PASS 设定http密码为 PASS
-C, –cache=on/off 允许/不允许服务器端的数据缓存 (一般情况下允许)
-E, –html-extension 将所有text/html文档以.html扩展名保存
–ignore-length 忽略 `Content-Length’头域
–header=STRING 在headers中插入字符串 STRING
–proxy-user=USER 设定代理的用户名为 USER
–proxy-passwd=PASS 设定代理的密码为 PASS
–referer=URL 在HTTP请求中包含 `Referer: URL’头
-s, –save-headers 保存HTTP头到文件
-U, –user-agent=AGENT 设定代理的名称为 AGENT而不是 Wget/VERSION
–no-http-keep-alive 关闭 HTTP活动链接 (永远链接)
–cookies=off 不使用 cookies
–load-cookies=FILE 在开始会话前从文件 FILE中加载cookie
–save-cookies=FILE 在会话结束后将 cookies保存到 FILE文件中
FTP 选项参数:
-nr, –dont-remove-listing 不移走 `.listing’文件
-g, –glob=on/off 打开或关闭文件名的 globbing机制
–passive-ftp 使用被动传输模式 (缺省值).
–active-ftp 使用主动传输模式
–retr-symlinks 在递归的时候,将链接指向文件(而不是目录)
递归下载参数:
-r, –recursive 递归下载--慎用!
-l, –level=NUMBER 最大递归深度 (inf 或 0 代表无穷)
–delete-after 在现在完毕后局部删除文件
-k, –convert-links 转换非相对链接为相对链接
-K, –backup-converted 在转换文件X之前,将之备份为 X.orig
-m, –mirror 等价于 -r -N -l inf -nr
-p, –page-requisites 下载显示HTML文件的所有图片
递归下载中的包含和不包含(accept/reject):
-A, –accept=LIST 分号分隔的被接受扩展名的列表
-R, –reject=LIST 分号分隔的不被接受的扩展名的列表
-D, –domains=LIST 分号分隔的被接受域的列表
–exclude-domains=LIST 分号分隔的不被接受的域的列表
–follow-ftp 跟踪HTML文档中的FTP链接
–follow-tags=LIST 分号分隔的被跟踪的HTML标签的列表
-G, –ignore-tags=LIST 分号分隔的被忽略的HTML标签的列表
-H, –span-hosts 当递归时转到外部主机
-L, –relative 仅仅跟踪相对链接
-I, –include-directories=LIST 允许目录的列表
-X, –exclude-directories=LIST 不被包含目录的列表
-np, –no-parent 不要追溯到父目录
wget -S –spider url 不下载只显示过程
| 管道
说明:管道符“|”用于连接左右两个命令,将“|”左边命令的执行结果作为“|”右边命令的输入,这样“|”就像一根管道一样连接着左右两条命令,并在管道中实现数据从左至右的传输。默认从管道传来的值是放在最后的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#分页显示/etc目录下所有文件和子目录的详细信息。 [root@localhost ~] # ls -lh /etc | more
#显示/etc目录下包含有“net”关键字的所有文件和子目录的详细信息。 [root@localhost ~] # ls -lh /etc | grep net
-rwxr-xr-x. 1 root root 1.3K 4月 10 2012 auto.net
-rw-r--r--. 1 root root 74 5月 31 2012 issue.net
-rw-r--r--. 1 root root 767 11月 30 2009 netconfig
-rw-r--r--. 1 root root 58 5月 23 2012 networks
drwxr-xr-x. 2 root root 4.0K 1月 8 19:14 xinetd.d
#统计一下/etc目录下所有以“.conf”结尾的文件的个数。 [root@localhost ~] # ls -l /etc/*.conf | wc –l
44
#查看/etc/httpd/conf/httpd.conf文件中除了以“#”开头的行和空行以外的内容。 [root@localhost ~] # grep -v “^#” /etc/httpd/conf/httpd.conf | gerp –v “^$”
|
&&
&&前面的命令成功执行后才能执行后面的命令,前面命令失败则停止
1
2
3
4
5
6
|
[root@localhost ~] # date && printf "The date command was succussful"
2013年 09月 23日 星期一 16:53:54 CST The date command was succussful
[root@localhost ~] # data && printf "The date command was succussful"
- bash : data: command not found
[root@localhost ~] #
|
||
有命令成功执行则停止执行后续命令,即使后续命令正确
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[root@localhost ~] # date || ls -l
2013年 09月 23日 星期一 16:59:44 CST [root@localhost ~] # date && ls -l
2013年 09月 23日 星期一 16:59:50 CST 总用量 96 -rw-------. 1 root root 1357 9月 18 15:58 anaconda-ks.cfg -rw-r--r--. 1 root root 43294 9月 18 15:58 install .log
-rw-r--r--. 1 root root 10151 9月 18 15:55 install .log.syslog
lrwxrwxrwx. 1 root root 41 9月 23 15:55 wk -> /etc/sysconfig/network-scripts/ifcfg-eth0
drwxr-xr-x. 2 root root 4096 9月 22 14:22 公共的 drwxr-xr-x. 2 root root 4096 9月 22 14:22 模板 drwxr-xr-x. 2 root root 4096 9月 22 14:22 视频 drwxr-xr-x. 2 root root 4096 9月 22 14:22 图片 drwxr-xr-x. 2 root root 4096 9月 22 14:22 文档 drwxr-xr-x. 2 root root 4096 9月 22 14:22 下载 drwxr-xr-x. 2 root root 4096 9月 22 14:22 音乐 drwxr-xr-x. 2 root root 4096 9月 22 14:22 桌面 [root@localhost ~] #
|
; 分号
平时我们都是在一行中敲一个命令,然后回车就运行了,那么想在一行中运行两个或两个以上的命令如何呢?则需要在命令之间加一个”;”了。
1
2
3
4
|
[root@localhost test ] # echo 3444 > 1.txt ;cat 1.txt;ll 1.txt
3444 -rw-r--r-- 1 root root 5 Nov 6 09:17 1.txt [root@localhost test ] #
|
& 命令挂起;
把一条命令放到后台执行的话,则需要加上这个符号。通常用于命令运行时间非常长的情况。使用jobs可以查看当前shell中后台执行的任务。用fg可以调到前台执行。这里的sleep命令就是休眠的意思,后面跟数字,单位为秒,想要把任务调到前台执行的话,fg后面跟任务号,任务号可以使用jobs命令得到。
1
2
3
4
5
6
|
[root@localhost test ] # sleep 100 &
[1] 15556 [root@localhost test ] # jobs
[1]+ Running sleep 100 &
[root@localhost test ] # fg 1
sleep 100
|
后台作业管理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
[root@localhost rsync_server] # jobs #查看后台作业
[1]- Running nohup /bin/sh /app/inotify/rsync .sh > /app/inotify/nohup .out 2>&1 &
[2]+ Running nohup /bin/sh /app/inotify/rsync .sh &
[root@localhost rsync_server] # fg 2 #将后台作业2跳到前台
nohup /bin/sh /app/inotify/rsync .sh
^Z #ctrl+z 将当前作业放到后台“暂停”
[2]+ Stopped nohup /bin/sh /app/inotify/rsync .sh
[root@localhost rsync_server] # jobs
[1]- Running nohup /bin/sh /app/inotify/rsync .sh > /app/inotify/nohup .out 2>&1 &
[2]+ Stopped nohup /bin/sh /app/inotify/rsync .sh
[root@localhost rsync_server] # jobs -l #列出作业PID
[1]- 17938 Running nohup /bin/sh /app/inotify/rsync .sh > /app/inotify/nohup .out 2>&1 &
[2]+ 18231 Stopped nohup /bin/sh /app/inotify/rsync .sh
[root@localhost rsync_server] # jobs -r #列出仅在后台运行(run)的作业
[1]- Running nohup /bin/sh /app/inotify/rsync .sh > /app/inotify/nohup .out 2>&1 &
[root@localhost rsync_server] # jobs -s #列出仅在后台暂停的作业
[2]+ Stopped nohup /bin/sh /app/inotify/rsync .sh
[root@localhost rsync_server] # bg 2 #ctrl+z让当前作业到后台去暂停,bg 作业号就可以在后台run
[2]+ nohup /bin/sh /app/inotify/rsync .sh &
[root@localhost rsync_server] # jobs -l
[1]- 17938 Running nohup /bin/sh /app/inotify/rsync .sh > /app/inotify/nohup .out 2>&1 &
[2]+ 18231 Running nohup /bin/sh /app/inotify/rsync .sh &
[root@localhost rsync_server] # kill -9 %2
[root@localhost rsync_server] # jobs -l
[1]- 17938 Running nohup /bin/sh /app/inotify/rsync .sh > /app/inotify/nohup .out 2>&1 &
[2]+ 18231 Killed nohup /bin/sh /app/inotify/rsync .sh
[root@localhost rsync_server] #
|
sleep: 将目前动作延迟一段时间
sleep [--help] [--version] number[smhd]
其中 s 为秒,m 为 分钟,h 为小时,d 为日数,默认是秒
显示目前时间后延迟 1 分钟,之后再次显示时间 :
1
2
3
4
|
[root@localhost ~] # date;sleep 10;date
Wed Nov 12 01:43:47 CST 2014 Wed Nov 12 01:43:57 CST 2014 [root@localhost ~] #
|
nohup 不挂断地运行命令
Unix/Linux下多程序并不象mysqld一样做成守护进程,可能我们的程序只是普通程序而已,一般这种程序使用& 结尾,但是如果终端关闭,那么程序也会被关闭。但是为了能够后台运行,那么我们就可以使用nohup和&命令,
语法:
nohup Command [ Arg ... ] [ & ]
eg、nohup ./filetransserver64 1>/dev/null 2>&1 &