命令【参数选项】【文件或者路径】
新建、查看目录
[root@safly /]# mkdir /data
[root@safly /]# mkdir safly
[root@safly /]# ls
bin data etc lib media opt root safly srv tmp var
boot dev home lib64 mnt proc run sbin sys usr
[root@safly /]# ls -l
total 32
lrwxrwxrwx. 1 root root 7 Apr 25 10:59 bin -> usr/bin
dr-xr-xr-x. 4 root root 4096 Apr 25 11:18 boot
drwxr-xr-x. 2 root root 6 Apr 25 21:05 data
drwxr-xr-x. 20 root root 3180 Apr 25 11:17 dev
drwxr-xr-x. 89 root root 8192 Apr 25 13:11 etc
相对、绝对路径
从根开始的路径就是绝对路径
从当前的路径就是相对路径
[root@safly /]# cd safly
[root@safly safly]# pwd
/safly
[root@safly safly]# cd /data
[root@safly data]# ^C
创建文件
[root@safly data]# touch a.txt
[root@safly data]# touch /data/b.txt
[root@safly data]# ls
a.txt b.txt
[root@safly data]# ls -l
total 0
-rw-r--r--. 1 root root 0 Apr 25 21:13 a.txt
-rw-r--r--. 1 root root 0 Apr 25 21:13 b.txt
[root@safly data]#
编辑
>清除原文件内容,把新内容追加到末尾
>>追加重定向
[root@safly data]# vi /data/a.txt
[root@safly data]# cat a.txt
hello linux
root@safly data]# echo "use python" >a.txt
[root@safly data]# cat a.txt
use python
[root@safly data]# echo "add context" >> a.txt
[root@safly data]# cat a.txt
use python
add context
[root@safly data]#
:wq保存并退出
:x保存并退出
:q!强制退出
复制
[root@safly data]# ls
a.txt b.txt
[root@safly data]# cd /safly
[root@safly safly]# ls
[root@safly safly]# cd /data/
[root@safly data]# cp a.txt /safly
[root@safly data]# ls -l /safly
total 4
-rw-r--r--. 1 root root 23 Apr 25 21:23 a.txt
[root@safly data]# ls -l /data
total 4
-rw-r--r--. 1 root root 23 Apr 25 21:20 a.txt
-rw-r--r--. 1 root root 0 Apr 25 21:13 b.txt
[root@safly data]#
当前目录备份
[root@safly data]# ls
a.txt b.txt
[root@safly data]# cp a.txt.bak
cp: missing destination file operand after ‘a.txt.bak’
Try 'cp --help' for more information.
[root@safly data]# cp a.txt a.txt.bak
[root@safly data]# ls
a.txt a.txt.bak b.txt
剪切
[root@safly data]# ls
a.txt a.txt.bak b.txt
[root@safly data]# mv a.txt.bak /safly
[root@safly data]# ls
a.txt b.txt
[root@safly data]# cd /safly/
[root@safly safly]# ls -l /safly
total 8
-rw-r--r--. 1 root root 23 Apr 25 21:23 a.txt
-rw-r--r--. 1 root root 23 Apr 25 21:28 a.txt.bak
[root@safly safly]#
vi/vim 快捷键
复制 yy
粘贴 p
删除、剪切 dd
撤销 u
把光标所在行到文件最后一行删除 dG
移动光标
把光标移动到文件的最后一行 G
把光标移动到文件的第一行 gg
批量删除
第1个里程碑-按ctrl + v 进入批量编辑模式
第2个里程碑-选择
第3个里程碑-删除 d
删除
[root@safly safly]# ls
a.txt a.txt.bak
[root@safly safly]# rm /safly a.txt.bak
rm: cannot remove ‘/safly’: Is a directory
rm: remove regular file ‘a.txt.bak’? y
[root@safly safly]# ls
a.txt
[root@safly safly]#
[root@safly safly]# ls
a.txt
[root@safly safly]# rm -f a.txt
[root@safly safly]# ls
[root@safly safly]#
删除目录
[root@safly safly]# ls
a.txt
[root@safly safly]# mkdir zimulu
[root@safly safly]# ls
a.txt zimulu
[root@safly safly]# rm -rf zimulu
[root@safly safly]# ls
a.txt
[root@safly safly]#
grep
输出除之外的所有行 -v 选项
[root@safly safly]# cat a.txt
wjefjw ljf
whelfh lweh lfwhl
hwlefl hwe l
[root@safly safly]# grep -v "whel" a.txt
wjefjw ljf
hwlefl hwe l
head
-n 打印每个文件的前n行,而不是打印默认的前10行
[root@safly safly]# head -2 a.txt
wjefjw ljf
whelfh lweh lfwhl
[root@safly safly]# head -n 2 a.txt
wjefjw ljf
whelfh lweh lfwhl
[root@safly safly]#
tail
读取文件后几行,默认10行
[root@safly safly]# tac a.txt | tail -2
whelfh lweh lfwhl
wjefjw ljf
[root@safly safly]# cat a.txt
wjefjw ljf
whelfh lweh lfwhl
hwlefl hwe l
[root@safly safly]#
mkdir -p
一条命令创建目录、文件
[root@safly safly]# ls
a.txt
[root@safly safly]# mkdir -p /wyf/a.txt
[root@safly safly]# ls
a.txt
[root@safly safly]# cd /wyf/
[root@safly wyf]# ls
a.txt
[root@safly wyf]#
sed替换
1#永久 修改配置文件 重启服务器之后生效
cp /etc/selinux/config /etc/selinux/config.bak
#快捷键:esc + .(点) 使用上一个命令的最后一个东西(参数)
sed 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
[root@oldboyedu-s8 ~]# sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
[root@oldboyedu-s8 ~]# cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
2#临时 重启服务器之后失效
[root@oldboyedu-s8 ~]# getenforce
Enforcing
[root@oldboyedu-s8 ~]# #显示当前selinux
[root@oldboyedu-s8 ~]# #显示当前selinux的运行状态
[root@oldboyedu-s8 ~]# setenforce
usage: setenforce [ Enforcing | Permissive | 1 | 0 ]
[root@oldboyedu-s8 ~]# setenforce 0
[root@oldboyedu-s8 ~]# getenforce
Permissive
3
[root@safly data]# grep =disabled /etc/selinux/config.bak
SELINUX=disabled
[root@safly data]# getenforce
Permissive
[root@safly data]#
生成序列
[root@safly safly]# seq 5
1
2
3
4
5
[root@safly safly]#
awk
NR表示行号,&&表示两边同时成立
[root@safly safly]# cat a.txt
wjefjw ljf
whelfh lweh lfwhl
hwlefl hwe lere
hwlefl hwe lrewr
hwlefl hwe lrer
hwlefl hwtre l
hwlefl hwe rtl
hwlefl htwe l
hwlefretl hwe l
hwle4fl hwe l
hwrelefl hwe l
hewlefl hwe l
dhwlefl hwe l
hwlefl hwe l
[root@safly safly]# awk 'NR>3 && NR<7' a.txt
hwlefl hwe lrewr
hwlefl hwe lrer
hwlefl hwtre l
[root@safly safly]#
awk 结合|
awk ‘NR>3’ a.txt | awk ‘NR<7’
[root@safly safly]# cat a.txt
wjefjw ljf
whelfh lweh lfwhl
hwlefl hwe lere
hwlefl hwe lrewr
hwlefl hwe lrer
hwlefl hwtre l
hwlefl hwe rtl
hwlefl htwe l
hwlefretl hwe l
hwle4fl hwe l
hwrelefl hwe l
hewlefl hwe l
dhwlefl hwe l
hwlefl hwe l
[root@safly safly]# awk 'NR>3' a.txt | awk 'NR<7'
hwlefl hwe lrewr
hwlefl hwe lrer
hwlefl hwtre l
hwlefl hwe rtl
hwlefl htwe l
hwlefretl hwe l
[root@safly safly]#
awk ‘NR==1,NR==3’ a.txt
[root@safly safly]# cat a.txt
wjefjw ljf
whelfh lweh lfwhl
hwlefl hwe lere
hwlefl hwe lrewr
hwlefl hwe lrer
hwlefl hwtre l
hwlefl hwe rtl
hwlefl htwe l
hwlefretl hwe l
hwle4fl hwe l
hwrelefl hwe l
hewlefl hwe l
dhwlefl hwe l
hwlefl hwe l
[root@safly safly]# awk 'NR==1,NR==3' a.txt
wjefjw ljf
whelfh lweh lfwhl
hwlefl hwe lere
[root@safly safly]#
awk ‘NR<5’ a.txt |tail -2
[root@safly safly]# cat a.txt
wjefjw ljf
whelfh lweh lfwhl
hwlefl hwe lere
hwlefl hwe lrewr
hwlefl hwe lrer
hwlefl hwtre l
hwlefl hwe rtl
hwlefl htwe l
hwlefretl hwe l
hwle4fl hwe l
hwrelefl hwe l
hewlefl hwe l
dhwlefl hwe l
hwlefl hwe l
[root@safly safly]# awk 'NR<5' a.txt |tail -2
hwlefl hwe lere
hwlefl hwe lrewr
[root@safly safly]#
下载
CentOS
1、备份
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
2、下载新的CentOS-Base.repo 到/etc/yum.repos.d/
CentOS 5
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo
或者
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo
CentOS 6
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
或者
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
CentOS 7
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
或者
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum install -y tree bash-completion wget vim
取消NetworkManager自动运行
[root@oldboyedu-s8 ~]# #systemctl system ctrl
[root@oldboyedu-s8 ~]# systemctl stop NetworkManager
[root@oldboyedu-s8 ~]# #stop 关闭正在运行的
[root@oldboyedu-s8 ~]#
[root@oldboyedu-s8 ~]# systemctl disable NetworkManager
Removed symlink /etc/systemd/system/multi-user.target.wants/NetworkManager.service.
Removed symlink /etc/systemd/system/dbus-org.freedesktop.NetworkManager.service.
Removed symlink /etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service.
[root@oldboyedu-s8 ~]# #不让NetworkManager 开机自动运行
利用光盘安装
[root@oldboyedu-s8 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 is write-protected, mounting read-only
[root@oldboyedu-s8 ~]# #mount 给/dev/cdrom 创建一个入口 /mnt
[root@oldboyedu-s8 ~]# cd /mnt/
[root@oldboyedu-s8 mnt]# ls -l
total 664
利用rpm指令安装
[root@safly mnt]# ll
total 1399
-rw-r--r--. 1 500 502 14 Jul 5 2014 CentOS_BuildTag
drwxr-xr-x. 3 500 502 2048 Jul 4 2014 EFI
-rw-r--r--. 1 500 502 611 Jul 5 2014 EULA
、、、
[root@safly mnt]# ls
CentOS_BuildTag EULA images LiveOS repodata RPM-GPG-KEY-CentOS-Testing-7
EFI GPL isolinux Packages RPM-GPG-KEY-CentOS-7 TRANS.TBL
[root@safly mnt]# ls -l /mnt/Packages/bash-completion-2.1-6.el7.noarch.rpm
-rw-rw-r--. 1 500 502 87272 Jul 4 2014 /mnt/Packages/bash-completion-2.1-6.el7.noarch.rpm
[root@safly mnt]# rpm -ivh /mnt/Packages/bash-completion-2.1-6.el7.noarch.rpm
Preparing... ################################# [100%]
package bash-completion-1:2.1-6.el7.noarch is already installed
[root@safly mnt]#
检查软件是否安装
[root@safly data]# rpm -qa vim
[root@safly data]# rpm -qa |grep vim
vim-common-7.4.160-2.el7.x86_64
vim-filesystem-7.4.160-2.el7.x86_64
vim-enhanced-7.4.160-2.el7.x86_64
vim-minimal-7.4.160-1.el7.x86_64
[root@safly data]# rpm -qa vim-enhanced tree wget
vim-enhanced-7.4.160-2.el7.x86_64
wget-1.14-15.el7_4.1.x86_64
tree-1.6.0-10.el7.x86_64
[root@safly data]# ^C
[root@safly data]#
时间
[root@oldboyedu-s8 ~]# date
Thu Apr 26 12:13:53 CST 2018
[root@oldboyedu-s8 ~]# date -s "20180101 01:01:01"
Mon Jan 1 01:01:01 CST 2018
[root@oldboyedu-s8 ~]# date
Mon Jan 1 01:01:05 CST 2018
让系统自动同步时间
yum install ntpdate -y
[root@oldboyedu-s8 ~]# ntpdate ntp1.aliyun.com
26 Apr 12:19:53 ntpdate[18819]: step time server 182.92.12.11
offset 9976502.795919 sec
[root@oldboyedu-s8 ~]# date
Thu Apr 26 12:20:24 CST 2018
防火墙
#当前正在运行的防火墙 --- 临时
systemctl stop firewalld.service
#让防火墙不会开机自启动 --- 永久
systemctl disable firewalld.service
#systemctl is-active firewalld.service
#is-active 是否正在运行 是否健在
#systemctl is-enabled firewalld.service
#is-enabled 是否开机自启动
[root@oldboyedu-s8 ~]# systemctl is-active firewalld.service
unknown
[root@oldboyedu-s8 ~]# systemctl is-active crond.service
active
[root@oldboyedu-s8 ~]# systemctl is-enabled firewalld.service
disabled
[root@oldboyedu-s8 ~]# systemctl start firewalld.service
[root@oldboyedu-s8 ~]# systemctl is-active firewalld.service
active
[root@oldboyedu-s8 ~]#
[root@oldboyedu-s8 ~]# systemctl enable firewalld.service
Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service.
[root@oldboyedu-s8 ~]# systemctl is-enabled firewalld.service
enabled
[root@safly data]# systemctl status firewalld.service
firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled)
Active: inactive (dead)
小结:
systemctl 管理服务
如何关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
systemctl status firewalld
systemctl is-active firewalld
systemctl is-enabled firewalld
定时任务:
每天的早上8:30到学校上车(go to school)
30 08 * * * go to school
每天的晚上12点整回家自己开车(go to bed)
00 00 ***go to bed
显示命令的绝对路径
[root@safly data]# which ntpdate
/usr/sbin/ntpdate
[root@safly data]# find / -type f -name "ntpdate"
/etc/sysconfig/ntpdate
/usr/sbin/ntpdate
[root@safly data]#