centOS
CentOS(Communi y Enterprise Operating System,社区企业操作系统)是一个基于Red Hat Linux 提供的可自由使用源代码的企业级Linux发行版本,以高效、稳定著称。我使用的是CentOS 7 虚拟机是VM。
Linux下的命令
ls 查看文件
- ls -a 查看隐藏目录或文件
- ls -h 增加可读性
- ls -d 查看文件目录
ls -l 查看文件或目录详细信息
可多个选项嵌套,并且不需要加空格列如:
[root@love454 ~]# ls -ldh /etc/passwd
-rw-r--r--. 1 root root 2.3K 7月 19 20:02 /etc/passwd
pwd 查看当前路径
- 它没有选项
[root@love454 ~]# pwd
/root
cd 切换目录
- 它没有选项
相关用法:
切换目录 cd [文件夹路径]
[root@love454 /]# cd /etc/yum
[root@love454 yum]# pwd
/etc/yum
直接输入 cd 回到用户的目录下
[root@love454 yum]# cd
[root@love454 ~]# pwd
/root
cd - 回到上次目录下
[root@love454 ~]# cd -
/etc/yum
[root@love454 yum]#
cd / 切换到根目录下
[root@love454 yum]# cd /
[root@love454 /]# pwd
/
hwclock 查看系统和BIOS硬件时间
[root@love454 /]# hwclock
2018年07月23日 星期一 22时46分22秒 -0.539127 秒
命令查看帮助
任何命令后面跟上 -h 或者–help 就可以查看该命令的帮助,例如:
[root@love454 /]# ls --help
用法:ls [选项]... [文件]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
Mandatory arguments to long options are mandatory for short options too.
-a, --all 不隐藏任何以. 开始的项目
-A, --almost-all 列出除. 及.. 以外的任何项目
--author 与-l 同时使用时列出每个文件的作者
-b, --escape 以八进制溢出序列表示不可打印的字符
--block-size=SIZE scale sizes by SIZE before printing them; e.g.,
'--block-size=M' prints sizes in units of
1,048,576 bytes; see SIZE format below
.......
第二种 通过man命令查看帮助,例如:
[root@love454 /]# man ls
那么它会进入该命令的帮助文档
关机命令
shutdown 关机命令
shutdown -k 并非真正关机,只向所有人显示警告信息。
shutdown -r 重启,它也有time参数和 -h 同理,相当于reboot。
shutdown -h 关机,如果后面还有time参数那么就是定时关机一般情况下默认值是now。
- shutdown -c 取消之前的shutdown命令,也就是停止之前设置的定时关机。
例如:
[root@love454 /]#shutdown -h +10 #十分钟后关机
[root@love454 /]#shutdown -r 22:22 #在22:22重启
init 切换系统运行级别
- init 参数:0-6
SIGRTMIN+0 # 关机
Enters default mode, starts the default.target unit. This is mostly
equivalent to systemctl start default.target.
SIGRTMIN+1 # 单用户模式
Enters rescue mode, starts the rescue.target unit. This is mostly
equivalent to systemctl isolate rescue.target.
SIGRTMIN+2 # 多用户模式
Enters emergency mode, starts the emergency.service unit. This is mostly
equivalent to systemctl isolate emergency.service.
SIGRTMIN+3 # 完整的多用户模式支持网络
Halts the machine, starts the halt.target unit. This is mostly equivalent
to systemctl start halt.target.
SIGRTMIN+4 # 指定某个服务是在该模式下开起还是关闭
Powers off the machine, starts the poweroff.target unit. This is mostly
equivalent to systemctl start poweroff.target.
SIGRTMIN+5 # 图形界面
Reboots the machine, starts the reboot.target unit. This is mostly
equivalent to systemctl start reboot.target.
SIGRTMIN+6 # 重启
Reboots the machine via kexec, starts the kexec.target unit. This is
mostly equivalent to systemctl start kexec.target.
runlevel查看当前系统运行级别
[root@love454 /]# runlevel
N 5
这个5就代表我当前在5这个运行级别下。
目录文件的操作
touch 创建空文件
[root@love454 ~]# touch a.txt
[root@love454 ~]# ls a.txt
a.txt
mkdir 创建目录
[root@love454 ~]# mkdir show
love454 ~]# ls !$
ls show
- mkdir -p 连同父目录一起创建
[root@love454 ~]# mkdir -p a/b/c/d
[root@love454 ~]# ls !$
ls a/b/c/d
cat 查看文件内容
[root@love454 ~]# 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
sync:x:5:0:sync:/sbin:/bin/sync
......
more 查看文件内容
它的区别是不会一次性全部显示。空格下翻显示一屏,回车下翻显示一行,q退出。
[root@love454 ~]# more /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
sync:x:5:0:sync:/sbin:/bin/sync
......
--More--(53%)
less 查看文件内容
它也是不会一次性全部显示。空格下翻显示一屏,回车下翻显示一行,q退出,不过它支持回滚pageup可以上翻pagedown可以下翻,读到最后不会直接退出需要按q。
[root@love454 ~]# less /etc/passwd
head 查看文件内容
从第一行开始查看文件默认显示10行,显示完就退出了。
“`shell
[root@love454 ~]# head /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
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
- head -n 选择显示多少行
```shell
[root@love454 ~]# head -n 5 /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
tail 查看文件内容
从最后一行开始查看文件默认显示10行,显示完就退出了。
“`shell
[root@love454 ~]# tail /etc/passwd
qemu:x:107:107:qemu user:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:989:983::/run/gnome-initial-setup/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
love_454:x:1000:1000:love_454:/home/love_454:/bin/bash
mysql:x:988:1001::/home/mysql:/bin/bash
- tail -n 选择显示多少行
```shell
[root@love454 ~]# tail -n 5 /etc/passwd
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
love_454:x:1000:1000:love_454:/home/love_454:/bin/bash
mysql:x:988:1001::/home/mysql:/bin/bash
- tail -f 动态的显示文件,如果对文件进行修改那么它会动态的显示添加的内容。
cp 复制文件
语法 cp 源文件 目标
“`shell
[root@love454 ~]# cp a.txt a/
[root@love454 ~]# ls a
a.txt b
- cp -r 包含子目录和文件,它可以拷贝目录
```shell
[root@love454 ~]# cp -r /etc/passwd a
[root@love454 ~]# ls a
a.txt b passwd
<div class="se-preview-section-delimiter"></div>
rm 删除文件或目录
- rm -rf 常用这个命令删除 -r递归删除 -f强制删除
[root@love454 ~]# rm -rf a
[root@love454 ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg show
a.txt mysql-community-release-el7-5.noarch.rpm
<div class="se-preview-section-delimiter"></div>
mv 移动文件或目录
语法 mv 源文件 目标。
[root@love454 ~]# mv show xiaomin
[root@love454 ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg xiaomin