Linux常见命令

本文摘自《linux就该这么学》,由博主丁一整理
linux基础
学习方法和建议:
1、高效而愉快的学习
2、先建立一个整体框架、然后细节
3、不需要掌握所有的linux指令,要学会用百度和查询手册
4、适当的囫囵吞枣
5、linux不是编程,重点是实际操作,各种常用指令要玩的遛。
1、VM提供虚拟空间安装centos

1、vm软件安装
2、通过VM软件来创建一个虚拟机空间
3、使用vm软件来创建好的虚拟空间上,安装centos操作系统
4、使用centos

VM软件安装步骤
1、去BIOS里修改设置开启虚拟化设备支持(F2)
2、安装虚拟软件

3、桥连接:unnux可以和其他系统通信,但是可能造成IP冲突
4、NAT:网络地址装换方式,linux可以访问外网,不会造成IP冲突
5、主机模式:你的linux是不能访问外网

网络配置:
点击上面右侧的两个计算机图片,选择echo即可成功连接到网络。

linux的目录结构
1、linux的目录中有且只有一个根目录
2、linux的各个目录存放的内容是规划好,不用乱放文件
3、linux是以文件的形式管理我们的设备的
4、linux的各个目录存放什么文件要很清楚
5、学习后,你的脑海中应该有一科linux目录树

远程登录linux

分区
lsblk -f 查看磁盘分区情况
sda 分区情况
/boot 挂载点
ext4 分区类型

如何增加一块硬盘
1、虚拟机添加硬盘
2、分区
3、格式化
4、挂载

常用RPM软件包命令
安装软件的命令格式 rpm -ivh filename.rpm
升级软件的命令格式 rpm -Uvh filename.rpm
卸载软件的命令格式 rpm -e filename.rpm
查询软件描述信息的命令格式 rpm -qpi filename.rpm
列出软件文件信息的命令格式 rpm -qpl filename.rpm
查询文件属于哪个 RPM 的命令格式 rpm -qf filename

Yum命令
yum repolist all 列出所有仓库
yum list all 列出仓库中所有软件包
yum info 软件包名称 查看软件包信息
yum install 软件包名称 安装软件包
yum reinstall软件包名称 重新安装软件包
yum update 软件包名称 升级软件包
yum remove 软件包名称 移除软件包
yum clean all 清除所有仓库缓存
yum check-update 检查可更新的软件包
yum grouplist 查看系统中已经安装的软件包组
yum groupinstall 软件包组 安装指定的软件包组
yum groupremove 软件包组 移除指定的软件包组
yum groupinfo 软件包组 查询指定的软件包组信息

新手必会命令
执行查看帮助命令
长格式 man --help
短格式 man -h

man 命令中常用按键以及用途
空格键 向下翻一页
PaGe down 向下翻一页
PaGe up 向上翻一页
home 直接前往首页
end 直接前往尾页
/ 从上至下搜索某个关键词,如“/linux”
? 从下至上搜索某个关键词,如“?linux”
n 定位到下一个搜索到的关键词
N 定位到上一个搜索到的关键词
q 退出帮助文档

man 命令帮助信息的结构以及意义
NAME 命令的名称
SYNOPSIS 参数的大致使用方法
DESCRIPTION 介绍说明
EXAMPLES 演示(附带简单说明)
OVERVIEW 概述
DEFAULTS 默认的功能
OPTIONS 具体的可用选项(带介绍)
ENVIRONMENT 环境变量
FILES 用到的文件
SEE ALSO 相关的资料
HISTORY 维护历史与联系方式

echo命令
例如,把指定字符串“Linuxprobe.com”输出到终端屏幕的命令为:
[root@linuxprobe ~]# echo Linuxprobe.Com

data命令
%t 跳格[Tab 键]
%H 小时(00~23)
%I 小时(00~12)
%M 分钟(00~59)
%S 秒(00~59)
%j 今年中的第几天

按照默认格式查看当前系统时间的 date 命令如下所示:
[root@linuxprobe ~]# date
Mon Aug 24 16:11:23 CST 2017

按照“年-月-日 小时:分钟:秒”的格式查看当前系统时间的 date 命令如下所示:
[root@linuxprobe ~]# date “+%Y-%m-%d %H:%M:%S”
2017-08-24 16:29:12

将系统的当前时间设置为 2017 年 9 月 1 日 8 点 30 分的 date 命令如下所示:
[root@linuxprobe ~]# date -s “20170901 8:30:00”
Fri Sep 1 08:30:00 CST 2017

再次使用 date 命令并按照默认的格式查看当前的系统时间,如下所示:
[root@linuxprobe ~]# date
Fri Sep 1 08:30:01 CST 2017

date 命令中的参数%j 可用来查看今天是当年中的第几天。
[root@linuxprobe ~]# date “+%j”
244

reboot命令
reboot 命令用于重启系统,其格式为 reboot。
[root@linuxprobe ~]# reboot

poweroff命令
[root@linuxprobe ~]# poweroff

weget命令
wget 命令用于在终端中下载网络文件,格式为“wget [参数] 下载地址”。

wget 命令的参数以及作用
-b 后台下载模式
-P 下载到指定目录
-t 最大尝试次数
-c 断点续传
-p 下载页面内所有资源,包括图片、视频等
-r 递归下载

尝试使用 wget 命令从本书的配套站点中下载本书的最新 pdf 格式电子文档,这个文
件的完整路径为 http://www.linuxprobe.com/docs/LinuxProbe.pdf,执行该命令后的下载效果
如下:

[root@linuxprobe ~]# wget http://www.linuxprobe.com/docs/LinuxProbe.pdf
–2017-08-24 19:30:12 – http://www.linuxprobe.com/docs/LinuxProbe.pdf
Resolving www.linuxprobe.com (www.linuxprobe.com)… 220.181.105.185
Connecting to www.linuxprobe.com (www.linuxprobe.com)|220.181.105.185|:80…
connected.
HTTP request sent, awaiting response… 200 OK
Length: 45948568 (44M) [application/pdf]

ps命令:
ps 命令用于查看系统中的进程状态,格式为“ps [参数]”。
ps 命令的参数以及作用
-a 显示所有进程(包括其他用户的进程)
-u 用户以及其他详细信息
-x 显示没有控制终端的进程

pidof命令:
pidof 命令用于查询某个指定服务进程的 PID 值,格式为“pidof [参数] [服务名称]”。

[root@linuxprobe ~]# pidof sshd
2156

kill命令:
kill 命令用于终止某个指定 PID 的服务进程,格式为“kill [参数] [进程 PID]”。
[root@linuxprobe ~]# kill 2156

killall命令:
killall 命令用于终止某个指定名称的服务所对应的全部进程,格式为:“killall [参数] [进
程名称]”。
[root@linuxprobe ~]# pidof httpd
13581 13580 13579 13578 13577 13576
[root@linuxprobe ~]# killall httpd
[root@linuxprobe ~]# pidof httpd
[root@linuxprobe ~]#

系统状态检测命令
1.ifconfig命令
ifconfig 命令用于获取网卡配置与网络状态等信息,格式为“ifconfig [网络设备] [参数]”。
[root@linuxprobe ~]# ifconfig
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.10 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::20c:29ff:fec4:a409 prefixlen 64 scopeid 0x20
ether 00:0c:29:c4:a4:09 txqueuelen 1000 (Ethernet)
RX packets 36 bytes 3176 (3.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 38 bytes 4757 (4.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 0 (Local Loopback)
RX packets 386 bytes 32780 (32.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 386 bytes 32780 (32.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

2.uname命令
uname 命令用于查看系统内核与系统版本等信息,格式为“uname [-a]”。
在使用 uname 命令时,一般会固定搭配上-a 参数来完整地查看当前系统的内核名称、主
机名、内核发行版本、节点名、系统时间、硬件名称、硬件平台、处理器类型以及操作系统
名称等信息。
[root@linuxprobe ~]# uname -a
Linux linuxprobe.com 3.10.0-123.el7.x86_64 #1 SMP Mon May 5 11:16:57 EDT 2017
x86_64 x86_64 x86_64 GNU/Linux

顺带一提,如果要查看当前系统版本的详细信息,则需要查看 redhat-release 文件,其命
令以及相应的结果如下:
[root@linuxprobe ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.0 (Maipo)

uptime命令
uptime 用于查看系统的负载信息,格式为 uptime。
[root@linuxprobe ~]# uptime
22:49:55 up 10 min, 2 users, load average: 0.01, 0.19, 0.18

free命令:
free 用于显示当前系统中内存的使用量信息,格式为“free [-h]”。
[root@linuxprobe ~]# free -h

who命令:
who 用于查看当前登入主机的用户终端信息,格式为“who [参数]”。
[root@linuxprobe ~]# who

last命令:
last 命令用于查看所有系统的登录记录,格式为“last [参数]”。
[root@linuxprobe ~]# last
root pts/0 :0 Mon Aug 24 17:52 still logged in
root :0 :0 Mon Aug 24 17:52 still logged in
(unknown :0 :0 Mon Aug 24 17:50 - 17:52 (00:02)
reboot system boot 3.10.0-123.el7.x Tue Aug 25 01:49 - 18:17 (-7:-32)
root pts/0 :0 Mon Aug 24 15:40 - 08:54 (7+17:14)
root pts/0 :0 Fri Jul 10 10:49 - 15:37 (45+04:47)

history命令:
history 命令用于显示历史执行过的命令,
格式为“history [-c]”。

[root@linuxprobe ~]# history
1 tar xzvf VMwareTools-9.9.0-2304977.tar.gz
2 cd vmware-tools-distrib/
3 ls
4 ./vmware-install.pl -d 5 reboot
6 df -h 7 cd /run/media/
8 ls
9 cd root/
10 ls
11 cd VMware\ Tools/
12 ls
13 cp VMwareTools-9.9.0-2304977.tar.gz /home
14 cd /home
15 ls
16 tar xzvf VMwareTools-9.9.0-2304977.tar.gz
17 cd vmware-tools-distrib/
18 ls
19 ./vmware-install.pl -d
20 reboot
21 history
[root@linuxprobe ~]# !15
anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates
Desktop Downloads Music Public Videos

历史命令会被保存到用户家目录中的.bash_history 文件中。Linux 系统中以点(.)开
头的文件均代表隐藏文件,这些文件大多数为系统服务文件
[root@linuxprobe ~]# cat ~/.bash_history

要清空当前用户在本机上执行的 Linux 命令历史记录信息,可执行如下命令:
[root@linuxprobe ~]# history -c

sosreport命令:
sosreport 命令用于收集系统配置及架构信息并输出诊断文档,格式为 sosreport。
[root@linuxprobe ~]# sosreport
sosreport (version 3.0)
This command will collect diagnostic and configuration information from
this Red Hat Enterprise Linux system and installed applications.
An archive containing the collected information will be generated in
/var/tmp and may be provided to a Red Hat support representative.
Any information provided to Red Hat will be treated in accordance with
the published support policies at:
https://access.redhat.com/support/
The generated archive may contain data considered sensitive and its
content should be reviewed by the originating organization before being
passed to any third party.
No changes will be made to system configuration.
Press ENTER to continue, or CTRL-C to quit. 此处敲击回车来确认收集信息
Please enter your first initial and last name [linuxprobe.com]: 此处敲击回车来确认主机编号
Please enter the case number that you are generating this report for:此处敲击回车来确认主机编号
Running plugins. Please wait …
Running 70/70: yum…
Creating compressed archive…
Your sosreport has been generated and saved in:
/var/tmp/sosreport-linuxprobe.com-20170905230631.tar.xz
The checksum is: 79436cdf791327040efde48c452c6322
Please send this file to your support representative.

工作目录切换命令
1.pwd命令:
pwd 命令用于显示用户当前所处的工作目录,格式为“pwd [选项]”。
[root@linuxprobe etc]# pwd
/etc

2.cd命令:
cd 命令用于切换工作路径,格式为“cd [目录名称]”。
[root@linuxprobe ~]# cd /etc

同样的道理,可使用下述命令切换到/bin 目录中:
[root@linuxprobe etc]# cd /bin

此时,要返回到上一次的目录(即/etc 目录),可执行如下命令:
[root@linuxprobe bin]# cd -
/etc
[root@linuxprobe etc]#

还可以通过下面的命令快速切换到用户的家目录:
[root@linuxprobe etc]# cd ~
[root@linuxprobe ~]#

ls命令:
ls 命令用于显示目录中的文件信息,格式为“ls [选项] [文件] ”。
[root@linuxprobe ~]# ls -al

total 60
dr-xr-x—. 14 root root 4096 May 4 07:56 .
drwxr-xr-x. 17 root root 4096 May 4 15:55 …
-rw-------. 1 root root 1213 May 4 15:44 anaconda-ks.cfg
-rw-------. 1 root root 957 May 4 07:54 .bash_history
-rw-r–r--. 1 root root 18 Dec 28 2013 .bash_logout
-rw-r–r--. 1 root root 176 Dec 28 2013 .bash_profile
-rw-r–r--. 1 root root 176 Dec 28 2013 .bashrc
drwx------. 10 root root 4096 May 4 07:56 .cache
drwx------. 15 root root 4096 May 4 07:49 .config
-rw-r–r--. 1 root root 100 Dec 28 2013 .cshrc
drwx------. 3 root root 24 May 4 07:46 .dbus
drwxr-xr-x. 2 root root 6 May 4 07:49 Desktop
drwxr-xr-x. 2 root root 6 May 4 07:49 Documents
drwxr-xr-x. 2 root root 6 May 4 07:49 Downloads
-rw-------. 1 root root 16 May 4 07:49 .esd_auth
-rw-------. 1 root root 628 May 4 07:56 .ICEauthority
-rw-r–r--. 1 root root 1264 May 4 07:48 initial-setup-ks.cfg
drwx------. 3 root root 18 May 4 07:49 .local
drwxr-xr-x. 2 root root 6 May 4 07:49 Music
drwxr-xr-x. 2 root root 6 May 4 07:49 Pictures
drwxr-xr-x. 2 root root 6 May 4 07:49 Public
-rw-r–r--. 1 root root 129 Dec 28 2013 .tcshrc
drwxr-xr-x. 2 root root 6 May 4 07:49 Templates
drwxr-xr-x. 2 root root 6 May 4 07:49 Videos
-rw-------. 1 root root 1962 May 4 07:54 .viminfo

如果想要查看目录属性信息,则需要额外添加一个-d 参数。
[root@linuxprobe ~]# cat -n initial-setup-ks.cfg
1 #version=RHEL7
2 # X Window System configuration information
3 xconfig --startxonboot
45 # License agreement
6 eula --agreed
7 # System authorization information
8 auth --enableshadow --passalgo=sha512
9 # Use CDROM installation media
10 cdrom
11 # Run the Setup Agent on first boot
12 firstboot --enable
13 # Keyboard layouts
14 keyboard --vckeymap=us --xlayouts=‘us’
15 # System language
16 lang en_US.UTF-8

more命令:
more 命令用于查看纯文本文件(内容较多的),格式为“more [选项]文件”。
[root@linuxprobe ~]# more initial-setup-ks.cfg
#version=RHEL7

X Window System configuration information

xconfig --startxonboot

License agreement

eula --agreed

System authorization information

auth --enableshadow --passalgo=sha512

Use CDROM installation media

cdrom # Run the Setup Agent on first boot
firstboot --enable

Keyboard layouts

keyboard --vckeymap=us --xlayouts=‘us’

System language

lang en_US.UTF-8
ignoredisk --only-use=sda

Network information

network --bootproto=dhcp --device=eno16777728 --onboot=off --ipv6=auto
network --bootproto=dhcp --hostname=linuxprobe.com
–More–(43%)

head命令:
head 命令用于查看纯文本文档的前 N 行,格式为“head [选项] [文件]”。
[root@linuxprobe ~]# head -n 20 initial-setup-ks.cfg
#version=RHEL7

X Window System configuration information

xconfig --startxonboot

License agreement

eula --agreed

System authorization information

auth --enableshadow --passalgo=sha512

Use CDROM installation media

cdrom

Run the Setup Agent on first boot

firstboot --enable

Keyboard layouts

keyboard --vckeymap=us --xlayouts=‘us’

System language

lang en_US.UTF-8
ignoredisk --only-use=sda

Network information

network --bootproto=dhcp --device=eno16777728 --onboot=off --ipv6=auto
[root@linuxprobe ~]#

tail命令:
tail 命令用于查看纯文本文档的后 N 行或持续刷新内容,格式为“tail [选项] [文件]”。
[root@linuxprobe ~]# tail -f /var/log/messages
May 4 07:56:38 localhost gnome-session: Window manager warning: Log level 16:
STACK_OP_ADD: window 0x1e00001 already in stack
May 4 07:56:38 localhost gnome-session: Window manager warning: Log level 16:
STACK_OP_ADD: window 0x1e00001 already in stack
May 4 07:56:38 localhost vmusr[12982]: [ warning] [Gtk] gtk_disable_setlocale()
must be called before gtk_init()
May 4 07:56:50 localhost systemd-logind: Removed session c1.
Aug 1 01:05:31 localhost systemd: Time has been changed
Aug 1 01:05:31 localhost systemd: Started LSB: Bring up/down networking.
Aug 1 01:08:56 localhost dbus-daemon: dbus[1124]: [system] Activating service
name=‘com.redhat.SubscriptionManager’ (using servicehelper)
Aug 1 01:08:56 localhost dbus[1124]: [system] Activating service name=‘com.
redhat.SubscriptionManager’ (using servicehelper)
Aug 1 01:08:57 localhost dbus-daemon: dbus[1124]: [system] Successfully activated
service ‘com.redhat.SubscriptionManager’
Aug 1 01:08:57 localhost dbus[1124]: [system] Successfully activated service ’
com.redhat.SubscriptionManager’

tr命令:
tr 命令用于替换文本文件中的字符,格式为“tr [原始字符] [目标字符]”。
[root@linuxprobe ~]# cat anaconda-ks.cfg | tr [a-z] [A-Z]
#VERSION=RHEL7

SYSTEM AUTHORIZATION INFORMATION

AUTH --ENABLESHADOW --PASSALGO=SHA512

USE CDROM INSTALLATION MEDIA

CDROM

RUN THE SETUP AGENT ON FIRST BOOT

FIRSTBOOT --ENABLE
IGNOREDISK --ONLY-USE=SDA

KEYBOARD LAYOUTS

KEYBOARD --VCKEYMAP=US --XLAYOUTS=‘US’

SYSTEM LANGUAGE

LANG EN_US.UTF-8 # NETWORK INFORMATION
NETWORK --BOOTPROTO=DHCP --DEVICE=ENO16777728 --ONBOOT=OFF --IPV6=AUTO
NETWORK --HOSTNAME=LOCALHOST.LOCALDOMAIN

ROOT PASSWORD

ROOTPW --ISCRYPTED 6 6 6PDJJF42G8C6PL069$II.PX/YFAQPO0ENW2PA7MOMKJLYOAE2ZJMZ2UZJ7
BH3UO4OWTR1.WK/HXZ3XIGMZGJPCS/MGPYSSOI8HPCT8B/

SYSTEM TIMEZONE

TIMEZONE AMERICA/NEW_YORK --ISUTC
USER --NAME=LINUXPROBE --PASSWORD= 6 6 6A9V3INSTNBWEIR7D$JEGFYWBCDOOOKJ9SODECCDO.
ZLF4OSH2AZ2SS2R05B6LZ2A0V2K.RJWSBALL2FEKQVGF640OA/TOK6J.7GUTO/ --ISCRYPTED –
GECOS=“LINUXPROBE”

X WINDOW SYSTEM CONFIGURATION INFORMATION

XCONFIG --STARTXONBOOT

SYSTEM BOOTLOADER CONFIGURATION

BOOTLOADER --LOCATION=MBR --BOOT-DRIVE=SDA
AUTOPART --TYPE=LVM

PARTITION CLEARING INFORMATION

CLEARPART --NONE --INITLABEL
%PACKAGES
@BASE
@CORE
@DESKTOP-DEBUGGING
@DIAL-UP
@FONTS
@GNOME-DESKTOP
@GUEST-AGENTS
@GUEST-DESKTOP-AGENTS
@INPUT-METHODS
@INTERNET-BROWSER
@MULTIMEDIA
@PRINT-CLIENT
@X11
%END

wc命令:
wc 命令用于统计指定文本的行数、字数、字节数,格式为“wc [参数] 文本”。
wc 的参数以及作用:
-l 只显示行数
-w 只显示单词数
-c 只显示字节数

[root@linuxprobe ~]# wc -l /etc/passwd
38 /etc/passwd

stat命令:
stat 命令用于查看文件的具体存储信息和时间等信息,格式为“stat 文件名称”
[root@linuxprobe ~]# stat anaconda-ks.cfg
File: ‘anaconda-ks.cfg’
Size: 1213 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 68912908 Links: 1
Access: (0600/-rw-------) Uid: ( 0/ root) Gid: ( 0/ root)
Context: system_u:object_r:admin_home_t:s0
Access: 2017-07-14 01:46:18.721255659 -0400
Modify: 2017-05-04 15:44:36.916027026 -0400
Change: 2017-05-04 15:44:36.916027026 -0400
Birth: -

cut命令:
cut 命令用于按“列”提取文本字符,格式为“cut [参数] 文本”。
[root@linuxprobe ~]# head -n 2 /etc/passwd
root❌0:0:root:/root:/bin/bash
bin❌1:1:bin:/bin:/sbin/nologin
[root@linuxprobe ~]# cut -d: -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
dbus
polkitd
unbound
colord
usbmuxd
avahi
avahi-autoipd
libstoragemgmt
saslauth
qemu
rpc
rpcuser
nfsnobody
rtkit
radvd
ntp
chrony
abrt
pulse
gdm
gnome-initial-setup
postfix
sshd
tcpdump
linuxprobe

diff命令:
diff 命令用于比较多个文本文件的差异,格式为“diff [参数] 文件”。
[root@linuxprobe ~]# cat diff_A.txt
Welcome to linuxprobe.com
Red Hat certified
Free Linux Lessons
Professional guidance
Linux Course
[root@linuxprobe ~]# cat diff_B.txt
Welcome tooo linuxprobe.com
Red Hat certified
Free Linux LeSSonS

Professional guidance
Linux Course

接下来使用 diff --brief 命令显示比较后的结果,判断文件是否相同:
[root@linuxprobe ~]# diff --brief diff_A.txt diff_B.txt
Files diff_A.txt and diff_B.txt differ

最后使用带有-c 参数的 diff 命令来描述文件内容具体的不同:
[root@linuxprobe ~]# diff -c diff_A.txt diff_B.txt
*** diff_A.txt 2017-08-30 18:07:45.230864626 +0800
— diff_B.txt 2017-08-30 18:08:52.203860389 +0800


*** 1,5 ****
! Welcome to linuxprobe.com
Red Hat certified
! Free Linux Lessons
Professional guidance
Linux Course
— 1,7 ----
! Welcome tooo linuxprobe.com
!
Red Hat certified
! Free Linux LeSSonS
! …
Professional guidance
Linux Course

2.7 文件目录管理命令
touch命令:
touch 命令用于创建空白文件或设置文件的时间,格式为“touch [选项] [文件]”。
touch 命令的参数及其作用
-a 仅修改“读取时间”(atime)
-m 仅修改“修改时间”(mtime)
-d 同时修改 atime 与 mtime

接下来,我们先使用 ls 命令查看一个文件的修改时间,然后修改这个文件,最后再通过 touch
命令把修改后的文件时间设置成修改之前的时间(很多黑客就是这样做的呢):
[root@linuxprobe ~]# ls -l anaconda-ks.cfg
-rw-------. 1 root root 1213 May 4 15:44 anaconda-ks.cfg
[root@linuxprobe ~]# echo “Visit the LinuxProbe.com to learn linux skills” >>
anaconda-ks.cfg
[root@linuxprobe ~]# ls -l anaconda-ks.cfg
-rw-------. 1 root root 1260 Aug 2 01:26 anaconda-ks.cfg
[root@linuxprobe ~]# touch -d “2017-05-04 15:44” anaconda-ks.cfg
[root@linuxprobe ~]# ls -l anaconda-ks.cfg
-rw-------. 1 root root 1260 May 4 15:44 anaconda-ks.cfg

mkidrml:
mkdir 命令用于创建空白的目录,格式为“mkdir [选项] 目录”。
[root@linuxprobe ~]# mkdir linuxprobe
[root@linuxprobe ~]# cd linuxprobe
[root@linuxprobe linuxprobe]# mkdir -p a/b/c/d/e
[root@linuxprobe linuxprobe]# cd a
[root@linuxprobe a]# cd b
[root@linuxprobe b]#

cp命令:
cp 命令用于复制文件或目录,格式为“cp [选项] 源文件 目标文件”。
cp 命令的参数及其作用:
-p 保留原始文件的属性
-d 若对象为“链接文件”,则保留该“链接文件”的属性
-r 递归持续复制(用于目录)
-i 若目标文件存在则询问是否覆盖
-a 相当于-pdr(p、d、r 为上述参数)

使用 touch 创建一个名为 install.log 的普通空白文件,然后将其复制为一份名为
x.log 的备份文件,最后再使用 ls 命令查看目录中的文件:
[root@linuxprobe ~]# touch install.log
[root@linuxprobe ~]# cp install.log x.log
[root@linuxprobe ~]# ls
install.log x.log

mv命令:
mv 命令用于剪切文件或将文件重命名,格式为“mv [选项] 源文件 [目标路径|目标文件名]”。
[root@linuxprobe ~]# mv x.log linux.log
[root@linuxprobe ~]# ls
install.log linux.log

rm命令:
rm 命令用于删除文件或目录,格式为“rm [选项] 文件”。
[root@linuxprobe ~]# rm install.log
rm: remove regular empty file ‘install.log’? y
[root@linuxprobe ~]# rm -f linux.log
[root@linuxprobe ~]# ls
[root@linuxprobe ~]#

dd命令:
dd 命令用于按照指定大小和个数的数据块来复制文件或转换文件,格式为“dd [参数]”。
dd命令的参数及其作用:
if 输入的文件名称
of 输出的文件名称
bs 设置每个“块”的大小
count 设置要复制“块”的个数

例如我们可以用 dd 命令从/dev/zero 设备文件中取出一个大小为 560MB 的数据块,然后
保存成名为 560_file 的文件。
[root@linuxprobe ~]# dd if=/dev/zero of=560_file count=1 bs=560M
1+0 records in
1+0 records out
587202560 bytes (587 MB) copied, 27.1755 s, 21.6 MB/s

但在 Linux 系统中可以直接
使用 dd 命令来压制出光盘镜像文件,将它变成一个可立即使用的 iso 镜像:
[root@linuxprobe ~]# dd if=/dev/cdrom of=RHEL-server-7.0-x86_64-LinuxProbe.Com.iso
7311360+0 records in
7311360+0 records out
3743416320 bytes (3.7 GB) copied, 370.758 s, 10.1 MB/s

file命令:
file 命令用于查看文件的类型,格式为“file 文件名”。
[root@linuxprobe ~]# file anaconda-ks.cfg
anaconda-ks.cfg: ASCII text
[root@linuxprobe ~]# file /dev/sda
/dev/sda: block special

打包压缩与搜索命令
tar命令:
tar 命令用于对文件进行打包压缩或解压,格式为“tar [选项] [文件]”。
-c 创建压缩文件
-x 解开压缩文件
-t 查看压缩包内有哪些文件
-z 用 Gzip 压缩或解压
-j 用 bzip2 压缩或解压
-v 显示压缩或解压的过程
-f 目标文件名
-p 保留原始的权限与属性
-P 使用绝对路径来压缩
-C 指定解压到的目录

演示下打包压缩与解压的操作。先使用 tar 命令把/etc 目录
通过 gzip 格式进行打包压缩,并把文件命名为 etc.tar.gz:
[root@linuxprobe ~]# tar -czvf etc.tar.gz /etc
tar: Removing leading ‘/’ from member names
/etc/
/etc/fstab
/etc/crypttab
/etc/mtab
/etc/fonts/
/etc/fonts/conf.d/
/etc/fonts/conf.d/65-0-madan.conf
/etc/fonts/conf.d/59-liberation-sans.conf
/etc/fonts/conf.d/90-ttf-arphic-uming-embolden.conf
/etc/fonts/conf.d/59-liberation-mono.conf
/etc/fonts/conf.d/66-sil-nuosu.conf
接下来将打包后的压缩包文件指定解压到/root/etc 目录中(先使用 mkdir 命令来创建
/root/etc 目录):
[root@linuxprobe ~]# mkdir /root/etc
[root@linuxprobe ~]# tar xzvf etc.tar.gz -C /root/etc
etc/
etc/fstab
etc/crypttab
etc/mtab
etc/fonts/
etc/fonts/conf.d/
etc/fonts/conf.d/65-0-madan.conf
etc/fonts/conf.d/59-liberation-sans.conf
etc/fonts/conf.d/90-ttf-arphic-uming-embolden.conf
etc/fonts/conf.d/59-liberation-mono.conf
etc/fonts/conf.d/66-sil-nuosu.conf
etc/fonts/conf.d/65-1-vlgothic-gothic.conf
etc/fonts/conf.d/65-0-lohit-bengali.conf
etc/fonts/conf.d/20-unhint-small-dejavu-sans.conf

grep命令:
grep 命令用于在文本中执行关键词搜索,并显示匹配的结果,格式为“grep [选项] [文件]”。
grep命令的参数及其作用
-b 将可执行文件(binary)当作文本文件(text)来搜索
-c 仅显示找到的行数
-i 忽略大小写
-n 显示行号
-v 反向选择—仅列出没有“关键词”的行

使用 grep 命令来查找出当前系统中不允许登录系统的所有用户信息:
[root@linuxprobe ~]# grep /sbin/nologin /etc/passwd
bin❌1:1:bin:/bin:/sbin/nologin
daemon❌2:2:daemon:/sbin:/sbin/nologin
adm❌3:4:adm:/var/adm:/sbin/nologin
lp❌4:7:lp:/var/spool/lpd:/sbin/nologin
mail❌8:12:mail:/var/spool/mail:/sbin/nologin
operator❌11:0:operator:/root:/sbin/nologin

find命令:
find 命令用于按照指定条件来查找文件,格式为“find [查找路径] 寻找条件 操作”。
find命令的参数以及作用:
-name 匹配名称
-perm 匹配权限(mode 为完全匹配,-mode 为包含即可)
-user 匹配所有者
-group 匹配所有组
-mtime -n +n 匹配修改内容的时间(-n 指 n 天以内,+n 指 n 天以前)
-atime -n +n 匹配访问文件的时间(-n 指 n 天以内,+n 指 n 天以前)
-ctime -n +n 匹配修改文件权限的时间(-n 指 n 天以内,+n 指 n 天以前)
-nouser 匹配无所有者的文件
-nogroup 匹配无所有组的文件
-newer f1 !f2 匹配比文件 f1 新但比 f2 旧的文件
–type b/d/c/p/l/f
匹配文件类型(后面的字母参数依次表示块设备、目录、字符设备、管道、链接文件、文本文件)
-size 匹配文件的大小(+50KB 为查找超过 50KB 的文件,而-50KB 为查找小于50KB 的文件)
-prune 忽略某个目录
-exec …… {}; 后面可跟用于进一步处理搜索结果的命令

Linux 系统中的配置文件会保存到/etc 目录中
[root@linuxprobe ~]# find /etc -name “host*” -print
/etc/avahi/hosts
/etc/host.conf
/etc/hosts
/etc/hosts.allow
/etc/hosts.deny
/etc/selinux/targeted/modules/active/modules/hostname.pp
/etc/hostname

如果要在整个系统中搜索权限中包括 SUID 权限的所有文件
[root@linuxprobe ~]# find / -perm -4000 -print
/usr/bin/fusermount
/usr/bin/su
/usr/bin/umount
/usr/bin/passwd
/usr/sbin/userhelper
/usr/sbin/usernetctl

新手博主,请前辈批评指正———丁一

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林中有神君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值