说明:命令及测试经来自:centos6.5版本测试及命令结果。
运维思想——简单、易用、高效**
基础命令:100多个
mkdir 创建目录的命令 make directory,-p 递归创建目录。
[root@jackroo test]# mkdir -p dir/dir1/dir2
[root@jackroo test]# ll
total 4
drwxr-xr-x 3 root root 4096 Oct 21 18:31 dir
ls -l(long)d(directory)显示目录或文件;-h(可读);-i(inode文件的索引号),F(给文件结尾加不同的符号);-p(给目录添加/);-t按修改时间,-r翻转排序。
[root@jackroo file]# touch test.txt
[root@jackroo file]# ls
test.txt
[root@jackroo file]# ls -lh
total 0
-rw-r--r-- 1 root root 0 Oct 21 18:33 test.txt
[root@jackroo file]# ls -lhi
total 0
131089 -rw-r--r-- 1 root root 0 Oct 21 18:33 test.txt
[root@jackroo file]# ls -p
test/ test.txt
[root@jackroo file]# ll -t
total 4
drwxr-xr-x 2 root root 4096 Oct 21 18:34 test
-rw-r--r-- 1 root root 0 Oct 21 18:33 test.txt
[root@jackroo file]# ll -tr
total 4
-rw-r--r-- 1 root root 0 Oct 21 18:33 test.txt
drwxr-xr-x 2 root root 4096 Oct 21 18:34 test
echo 打印输出内容(print复杂的输出),n 不换行,e 可以指定特殊的字符(\n \t等)。
[root@jackroo file]# echo 'test echo '
test echo
cd 切换目录层次 change directory。
touch 就是”摸“一下文件,如果文件不存在,就建立新文件,如果存在,就改变文件的访问时间atime等时间戳信息。
vi/vim a(i)编辑模式,esc命令模式,wq(保存并退出)。:set nu 显示行号,dd删除当前行,yy拷贝当前行,p复制当前行。行号gg定位该行,G定位到文件尾部,gg文件头部。
cp copy 拷贝,默认不能拷贝目录
cp [-aidfpr] [源文件] [目标文件]
-a:相当于 -pdr
-p:连同档案属性一起复制过去,而非使用默认属性;
-d:若源文件为链接文件,则复制链接文件属性而非档案本身
-r:递归,用于复制目录;
-f:强制,若目标档案已经存在且无法开启,则移除后再尝试;
-i:若目标文件已经存在时,在覆盖前会先询问;
cat -n 显示行号。
[root@jackroo file]# cat -n test.txt
1 asfdjdasfas
2 sdfds
3 sdfds
4 test
5 水电费
6 水电费
7 水电费
8 双方都
tac 反转输出。
rm -f 强制删除文件 -r 递归删除文件或目录。
head 头部 显示文件头部-行数。
tail 尾部 显示文件尾部的函数 -行数,默认结尾10行。
grep 核心命令之一 -v “排除的内容” 直接过滤想要的“东西”,-E可以过滤多个,-E “a|b” ,相当于egrep.
[root@jackroo file]# cat grep.txt
i am study linux
i like study
this is a test
my name is zhangsan , qq is 9999234;
[root@jackroo file]# grep -v zhangsan
^C
[root@jackroo file]# grep -v zhangsan grep.txt
i am study linux
i like study
this is a test
[root@jackroo file]# grep -E 'zhangsan|qq' grep.txt
my name is zhangsan , qq is 9999234;
sed 取各种内容; -n 取消默认输出,sed /^正则/pd,擅长行。
[root@jackroo file]# cat grep.txt
i am study linux
i like study
this is a test
my name is zhangsan , qq is 9999234;
[root@jackroo file]# sed -n '/my name/p' grep.txt
my name is zhangsan , qq is 9999234;
awk 擅长列。
[root@jackroo file]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@jackroo file]# awk -F ":" '{print $1}' /etc/passwd
root
bin
daemon
adm
lp
tree yum install tree -y 安装tree命令。tree -L 1/ 显示一层目录;-d列目录。
[root@jackroo etc]# tree -L 1
.
├── abrt
├── acpi
├── adjtime
├── aliases
├── aliases.db
├── alsa
├── alternatives
├── anacrontab
├── asound.conf
├── at.deny
├── audisp
├── audit
├── bash_completion.d
├── bashrc
├── blkid
├── cas.conf
├── centos-release
├── chkconfig.d
├── compat-openmpi-psm-x86_64
├── compat-openmpi-x86_64
├── ConsoleKit
├── cron.d
├── cron.daily
├── cron.deny
├── cron.hourly
├── cron.monthly
├── crontab
├── cron.weekly
├── crypttab
├── csh.cshrc
├── csh.login
├── dbus-1
├── default
├── depmod.d
├── dhcp
├── DIR_COLORS
├── DIR_COLORS.256color
├── DIR_COLORS.lightbgcolor
├── dracut.conf
├── dracut.conf.d
├── environment
├── ethers
├── event.d
├── exports
├── favicon.png -> /usr/share/icons/hicolor/16x16/apps/system-logo-icon.png
├── filesystems
├── fonts
├── fprintd.conf
├── fstab
├── gai.conf
├── gcrypt
├── gnupg
├── group
├── group-
├── grub.conf -> ../boot/grub/grub.conf
├── gshadow
├── gshadow-
├── gtk-2.0
├── hal
├── host.conf
├── hosts
├── hosts.allow
├── hosts.deny
├── init
├── init.d -> rc.d/init.d
├── inittab
├── inputrc
├── iproute2
├── issue
├── issue.net
├── kdump-adv-conf
├── kdump.conf
├── krb5.conf
├── latrace.conf
├── latrace.d
├── ld.so.cache
├── ld.so.conf
├── ld.so.conf.d
├── libaudit.conf
├── libibverbs.d
├── libreport
├── libuser.conf
├── localtime
├── login.defs
├── logrotate.conf
├── logrotate.d
├── ltrace.conf
├── lvm
├── magic
├── mailcap
├── mail.rc
├── makedev.d
├── man.config
├── mcelog
├── mime.types
├── minicom.users
├── mke2fs.conf
├── modprobe.d
├── modulefiles
├── motd
├── mtab
├── my.cnf
├── nanorc
├── NetworkManager
├── networks
├── nsswitch.conf
├── ntp
├── ntp.conf
├── openldap
├── opt
├── pam.d
├── pango
├── passwd
├── passwd-
├── pcmcia
├── pinforc
├── pkcs11
├── pki
├── plymouth
├── pm
├── pm-utils-hd-apm-restore.conf
├── polkit-1
├── popt.d
├── postfix
├── ppp
├── prelink.cache
├── prelink.conf
├── prelink.conf.d
├── printcap
├── profile
├── profile.d
├── protocols
├── quotagrpadmins
├── quotatab
├── rc -> rc.d/rc
├── rc0.d -> rc.d/rc0.d
├── rc1.d -> rc.d/rc1.d
├── rc2.d -> rc.d/rc2.d
├── rc3.d -> rc.d/rc3.d
├── rc4.d -> rc.d/rc4.d
├── rc5.d -> rc.d/rc5.d
├── rc6.d -> rc.d/rc6.d
├── rc.d
├── rc.local -> rc.d/rc.local
├── rc.sysinit -> rc.d/rc.sysinit
├── readahead.conf
├── redhat-release -> centos-release
├── resolv.conf
├── rpc
├── rpm
├── rsyslog.conf
├── rsyslog.d
├── rwtab
├── rwtab.d
├── sasl2
├── scl
├── securetty
├── security
├── selinux
├── services
├── sestatus.conf
├── setuptool.d
├── shadow
├── shadow-
├── shells
├── skel
├── smartd.conf
├── sos.conf
├── ssh
├── ssl
├── statetab
├── statetab.d
├── subversion
├── sudo.conf
├── sudoers
├── sudoers.d
├── sudo-ldap.conf
├── sysconfig
├── sysctl.conf
├── system-release -> centos-release
├── system-release-cpe
├── termcap
├── terminfo
├── udev
├── updatedb.conf
├── vimrc
├── virc
├── warnquota.conf
├── wgetrc
├── wvdial.conf
├── X11
├── xdg
├── xinetd.d
├── yum
├── yum.conf
└── yum.repos.d
89 directories, 107 files
seq -s 指定分隔符,打印数字序列。
[root@jackroo etc]# seq 5
1
2
3
4
5
[root@jackroo etc]# seq -s '=' 10
1=2=3=4=5=6=7=8=9=10
man 查看命令帮助的手段。
alias 显示设置别名。
[root@jackroo etc]# 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'
unalias 取消别名。
which 查看命令的路径whereis ,locate,find
which cat <<==>> find / -type f -name "cat"
find 查找命令 -type(f d) -name “ddd” 按名字查找 -mtime 按照修改时间查找;-maxdepth levels查找目录的深度。
[root@jackroo file]# find / -type f -name "test.txt"
/tmp/test.txt
/opt/data/test/file/test.txt
/opt/data/test/test.txt
pwd 显示当前的路径。
[root@jackroo file]# pwd
/opt/data/test/file
history c查看历史记录,-c 清空,-d 指定行号删除。
[root@jackroo file]# history
85 history
diff 比较文件内容的差别。
useradd 添加用户。
passwd --stdin 修改用户密码,如果不指定用户,则修改当前用户的密码,普通用户只能修改自己的密码。
whoami 查看当前命令行的用户。
su 切换用户角色,注意要带上-。
sudo 让普通用户可以拥有指定用户root的权限的命令。
yum linux软件安装工具。
rmp -ivh 软件安装命令
chkconfig 查看设置开机自启动软件的命令。 -add / --list 程序名
[root@jackroo file]# chkconfig
abrt-ccpp 0:off 1:off 2:off 3:off 4:off 5:off 6:off
abrtd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
acpid 0:off 1:off 2:off 3:off 4:off 5:off 6:off
atd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
auditd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
blk-availability 0:off 1:on 2:off 3:off 4:off 5:off 6:off
cpuspeed 0:off 1:on 2:off 3:off 4:off 5:off 6:off
crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netstat 查看网络状态 lntup或an。
[root@jackroo file]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:2222 0.0.0.0:* LISTEN 794/sshd
tcp 0 0 :::2222 :::* LISTEN 794/sshd
udp 0 0 0.0.0.0:68 0.0.0.0:* 741/dhclient
lsof -i :port 查看端口。
[root@jackroo file]# lsof -i:2222
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 794 root 3u IPv4 8384 0t0 TCP *:EtherNet/IP-1 (LISTEN)
sshd 794 root 4u IPv6 8386 0t0 TCP *:EtherNet/IP-1 (LISTEN)
sshd 833 root 3r IPv4 8526 0t0 TCP 10.0.2.15:EtherNet/IP-1->10.0.2.2:61008 (ESTABLISHED)
sshd 835 studyboy 3u IPv4 8526 0t0 TCP 10.0.2.15:EtherNet/IP-1->10.0.2.2:61008 (ESTABLISHED)
chattr change file attributes (+i,-i)。
lsattr list file attributes。
w 显示当前登陆用户和在做什么。
[root@jackroo file]# w
19:06:01 up 7:35, 1 user, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
studyboy pts/0 10.0.2.2 02:34 0.00s 0.23s 0.01s sshd: studyboy [priv]
halt 关机(init 0)。
reboot 重启。
shutdown -r now 重启 -h now (关机)。
last show listing of last logged in users。
lastlog reports the most recent login of all users or of a given user。
file 查看文件类型。
[root@jackroo file]# file test.txt
test.txt: UTF-8 Unicode text
ln make links between files.创建链接,-s创建软链接。
more 查看文件但不能向上回滚。
less 查看文件可以向上回滚。
tar 打包命令。
gzip打包组合zcvf,解包组合zxvf
bzip打包组合jcvf,解包组合jxvf
z(gizp),c(create),v(verbose),file,x(extract),j(bzip)
--exclude -X(--exclude-from),-C指定目录解压。
cut 按列切割。-d指定分割符,-f数字选列,-c按字符切割。
**date命令 -s 修改时间。
chmod 修改文件访问权限,分为数字和字符表示法,-R递归。
chown 改变文件拥有者和用户组,-R递归。
chgrp 改变用户组。
符号
; 多个命令的分隔符;
/ 根或者路径的分隔符;
>或1> 输出重定向(数据流朝着箭头的方向流动),覆盖原来的文件。
>>或1>> 追加重定向(数据流朝着箭头的方向流动),在原来的文件【结尾】,【追加】内容。
<或<0 输入重定向:用于改变命令的输入,指定输入的内容,后跟文件名。
<<或<<0 输入重定向:后跟字符串,用来表示”输入结束",可以用ctrl+d来结束输入。
2> 错误重定向:把错误的信息输入到后边的文件中,会删除原文件原有内容。
2>> 错误追加重定向:不会删除原文件原有内容。
.. 上一级目录
. 当前目录(.任意一个字符,隐藏文件的标识,可以使得一个配置文件生效)。
~ 用户的家目录
| 管道,把前一个命令的输出交给后一个命令继续处理。
^ 的意思是以...开头。
$ 的意思是以...结尾。例如boy$表示以boy字符串结尾。
快捷键
tab 自动补全命令;
ctrl+c 终止当前任务命令或程序;
ctrl+d 退出当前任务命令或程序;
示例:
1、 创建一个目录/data?
mkdir /data
cd /data;mkdir data
2、在/data下面建立一个文件test.txt。
touch /data/test.txt
cd /data/;touch test.txt
3、在test.txt增加内容为“I am studying linux"。
vi test.txt // a或i进入编辑s命令,添加内容即可。cat test.txt;即可查看内容
echo "I am studying linux" >>test.txt //>>追加重定向
cat>>test.txt<<EOF
1
2
3
EOF // 多行内容的输入
4、把test.txt 拷贝到/tmp下。
cp test.txt /tmp
5、把/data目录移动到/tmp下。
mv /data/ /tmp
6、进入/tmp/下删除/data/目录。
rm -rf /tmp/data/
# 在脚本中删除文件或目录生产使用方法,一般为
(1) find . -type f -mtime +15 |xargs rm -rf //删除文件15天文件
find . -type d -mtime +30 |xargs rm -rf //删除目录30天以前,慎用
(2) find /log3 -type f -mtime +5 -exec rm {} \; //在/log3目录中查找更改时间在5日一前的文件并删除他们。
7、已知文件test.txt内容为:
test
liyao
oldboy
请给出打印test.txt内容时,不包含oldboy字符串的命令。
grep -v oldboy test.txt
head -2 test.txt
sed /^oldboy/d test.txt
awk /^oldboy/ test.txt
8、请用一条命令创建/data/a/b/c目录。
mkdir -p /data/a/b/c
9、如何将/mnt/test.txt 拷贝到/tmp/目录下,/tmp/路径下已经存在test.txt,让其复制不提示覆盖。
\cp /mnt/test.txt /tmp/
unalias cp //取消cp别名,cp -i = cp 别名(不建议更改系统默认别名)
别名的作用:通过给危险命令加些保护参数,防止人为误操作。把复杂的操作变成简易命令。
alias cp="cp -i" //恢复cp别名
cat ~/.bashrc //系统别名的位置
10、只查看test.txt文件(100行)内第20行到第30行的内容。
seq 100 >test.txt #模拟100行数据
head -30 test.txt |tail -11
sed -n 20,30p test.txt # 高效,打印20到30行
awk '{if(NR>19&&NR<31) print $0}' test.txt
awk '{if(NR>19&&NR<31) print $0}' test.txt
20
21
22
23
24
25
26
27
28
29
30
11、把linux中所有的test.txt 目录中保护保护linux的字符串替换为windows。
sed -i s#oldboylinux#oldboywindowds#g test.txt
find / -type f -name "test.txt" |xargs sed -i s#oldboylinux#oldboywinds#g # i 写入,s编辑 g,全局
12.date输出结果为什么。
cat >oldboy.txt<<EOF
`date`
EOF
[root@lvtong data]# cat oldboy.txt
Tue Oct 2 00:36:44 CST 2018