Linux基础操作及命令管理
一、基础操作
(1)连接到讲师界面
Application --> Internet --> TigerVNC Viewer–>在对话框输入(172.25.254.250:8)–>点击(connect)
(2)截屏(截屏并查看图片)
按[PrtScr] --> 点击Places–>选择Pictures
(3)语言调整
Applications–>system tools–>settings–>keyboard–>typing或者Rejion&language–>input sources–>chinese (intelligent pinyin)–>add
(4)设置快捷键
Applications–>system tools–>settings–>keyboard–>shortcuts–>custorm shortcuts–> "+"号–>编辑–>Add
(5)截屏热键修改
Applications–>system tools–>settings–>keyboard–>shortcuts–>screenshots–>save a screenshot of an area to picture -->设置快捷键
二、虚拟机控制命令
- 常规操作
[[email protected] ~]$ rht-vmctl status desktop ###查看虚拟机状态
desktop RUNNING ###虚拟机开启
[[email protected] ~]$ rht-vmctl stop desktop ###关闭虚拟机
Stopping desktop... ###虚拟机关闭
[[email protected] ~]$ rht-vmctl reset desktop ###重置虚拟机(当虚拟机出现任何解决不了的问题时用此命令来还原)
Are you sure you want to reset desktop? (y/n) y ###yes or no
Powering off desktop.. ###强制关闭虚拟机
Resetting desktop. ###重置虚拟机
Creating virtual machine disk overlay for rh124-desktop-vda.qcow2
Creating virtual machine disk overlay for rh124-desktop-vdb.qcow2
Starting desktop. ####开启虚拟机
[[email protected] ~]$ rht-vmctl start desktop ###开启虚拟机desktop
Starting desktop.
[[email protected] ~]$ rht-vmctl view desktop ###打开虚拟机视图desktop
[[email protected] ~]$ rht-vmctl poweroff desktop ###关闭虚拟机desktop
Powering off desktop..
[[email protected] ~]$ rht-vmctl start desktop ###开启虚拟机desktop
Starting desktop.
[[email protected] ~]$ rht-vmctl view desktop ###打开虚拟机视图desktop
- 可能出现的报错
[[email protected] ~]$ rht-vmctl start desktop ###打开虚拟机
Error: desktop not started (is already running) ###报错,因为虚拟机desktop已开启,不能打开
[[email protected] ~]$ rht-vmctl view desktop ###打开虚拟机视图
Error: unable to view desktop - not currently running. ###虚拟机未打开,不能显示(打开虚拟机的方式解决)
二、系统登陆
-
图形界面登陆
(1) Desktop:
用户 密码 student(普通用户) student root(超级用户) redhat (2) Server:
用户 密码 student(普通用户) student root(超级用户) redhat (3) root:
root登录方式: not list? --> root -->redhat -
文本界面登陆
red hat enterprise linux server 7.0(maiop) ##系统版本
kernel 3.10.0-123/el7.x86_64 on an x86_64 ## 内核版本desktop16 login: ## 输入用户名称
password: ##输入密码(输入密码时没有回显,为了安全)注意: 如果用户或者密码有任何问题请重置虚拟机
-
当图形界面出现问题
(1) 进入虚拟机控制台在linux中一共有5个虚拟控制,按 ctrl+alt+F2进入到虚拟机控制台ctrl+alt+(F2-F6)
(2) 当切换到虚拟控制台后想回到图形控制台按ctrl+alt+F1|F7,前提是图形开启
(3) 超级用户登陆
主机名称 login: root ##再次输入用户名称 passwd: westos ##再次输入用户密码,注意输入的密码没有任何回显
(4) 当登陆成功后得到一个可以操作的用户界面
[[email protected] ~]# init 3 ##在此输入init 3 图形关闭(5) 再次完成登陆操作
[[email protected] ~]# init 5 ##在此输入init 5 开启图形
三、什么是shell
-
shell的定义:
shell是保护内核的软件并提供用户和Linux操作系统之间的接口。shell的类型中bash是GNU计划的一部分,用来替代Bourne shell。它用于基于GNU的系统如Linux,大多数的Linux(Red Hat,Slackware,Caldera),都以bash作为缺省的shell,并且运行sh时,其实调用的是bash。Shell基本上是一个命令解释器,类似于DOS下的command。它接收用户命令(如ls等),然后调用相应的应用程序。 -
当前系统默认使用使用类型:bash ##GNU Bourne again Shell
-
shell的分类:shell分为两大类,图形界面shell和命令行式shell
四、如何开启shell
-
鼠标右键------>open in terminal
-
在shell已经开启一个的情况下:
ctrl+shift+n ##开启一个新的shell
ctrl+shift+t ##在同一个界面中开启一个新的tab
gnome-terminal ##shell运行命令 -
关闭shell
ctrl +D ##关闭shell的快捷键
exit ##此命令可以关闭shell -
设定shell开启的快捷键
gnome-control-center ##打开系统设置中心
[keyboard] -->[shortcuts] --> [custom shortcuts] --> 选择+ --> 填入快捷键名称到name选项–>填入命令[gnome-terminal]到command中点击disable输入快捷键
五、在Linux中如何运行命令
命令 | 参数 | 对象 |
---|---|---|
ls | -s | test |
ls | –size | test |
注意:
1. 参数用来指定命令的某些功能,可以加也可以不加
2. 命令和参数和目标之间要用空格分开
3. 参数中“-” 表示单词的缩写,“–”表示单词的全拼
4. 在执行命令时通常遵循这些规则,特殊情况除外
5. 命令必须在行提示符之后输入,没有行提示符的输入时无效的。如何释放行提示符(CTRL+C)
六、命令行提示符中字符的含义
[[email protected] Desktop]$
[1] [2] [3] [4] [5]
[1] kiosk: 运行shell的用户是谁
[2] @: 分割符号
[3] foundation0: 主机短名称
[4] Desktop: 所在当前主机的位置
[5] 身份提示符号($表示普通用户,#表示超级用户)
七、Linux系统结构及其二级目录
-
Linux系统结构
linux是一个倒树型结构,最大的目录名称叫“/” (根目录) -
Linux系统中的二级目录
/bin | 二进制可执行文件,系统常规命令 |
---|---|
/boot | 启动目录,存放系统自动启动文件,内核,初始化程序 |
/dev | 系统设备管理文件 |
/etc | 大多数系统配置文件存放路径 |
/home/子目录 | 普通用户家目录 |
/lib | 函数库 |
/lib64/ | 64位函数库 |
/media | 临时挂载点 |
/mnt | 临时挂载点 |
/run | 自动临时设备挂载点 |
/opt | 第三方软件安装路径 |
/proc | 系统硬件信息和系统进程信息 |
/root | 超级用户家目录 |
/sbin | 系统管理命令,通常只有root可以执行 |
/srv | 系统数据目录 |
/var | 系统数据目录 |
/sys | 内核相关数据 |
/tmp | 临时文件产生目录 |
/usr | 用户相关信息数据 |
八、文件管理命令
-
文件建立:
touch 文件名
注意: touch不但可以建立文件也可以修改文件的时间戳时间戳分为:
atime 文件内容被访问的时间标识,即访问时间 mtime 文件内容被修改的时间标识,即修改时间 ctime 文件属性或文件内容被修改的时间标识,即状态时间 时间戳拓展:
访问时间 对文件进行一次读操作,它的访问时间就会改变。例如像:cat、more等操作,但是像之前的state还有ls命令对atime是不会有影响的 修改时间 当文件的状态被改变的时候,状态时间就会随之改变,例如当使用chmod、chown等改变文件属性的操作是会改变文件的ctime的 状态时间 文件属性或文件内容被修改的时间标识,即状态时间 -
目录建立:
mkdir 目录名
mkdir -p 一级目录名/二级目录名/三级目录名注意:-p建立递归目录时,当上层目录不存在自动建立
-
文件删除:
rm 文件名
rm -f 文件名注意: -f=force 表示强制删除不提示
-
目录删除:
rm -r 目录名 ## -r标示递归删除,也就是删除目录本身和里面的所有内容
rm -r -f 目录名 ## 删除目录不提示
rm -rf 目录名 ##在命令参数使用过程中,-a -b -c =-abc =-bac ,顺序是不需要注意的。
rm -fr 目录名
rm -fr * ##清除desktop桌面上所有文件 -
编辑文件:
(1) gedit:
gedit file ##必须有图形
(2) vim:
vim file -->按[i]进入insert模式–>书写内容–>按[esc]退出insert模式–>:wq退出保存
九、vim异常退出
当vim异常退出时会生成.file.swp文件,当再次打开此文件时会出现以下情况:
file name: ~kiosk/Desktop/hello
modified: YES
user name: kiosk host name: foundation0.ilt.example.com
process ID: 31820
If this already, delete the swap file ".hello.swp" <<<此文件中存放大是未保存的数据
Swap file ".hello.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:
只读打开 继续编辑 恢复数据 删除swap文件 退出 退出
注意:
(1) 无论按[O] (E) ® (Q) (A) 任何一个都不会删除.swp文件
(2) 那么再次打开hello文件是还会出现以上界面直到按(D),.swp文件被删除, vim恢复正常工作
十、文件的复制
cp 源文件 目的地文件
cp -r 源文目录 目的地目录
cp 源文件1 源文件2 目的地目录 ##目的地目录必须存在
cp -r 源目录1 源目录2 目的地目录 ##目的地目录必须存在
十一、文件移动
mv 源文件 目的地文件 ##重命名
mv 源目录 目的地目录
mv dir/* . ##把dir中的所有文件移动到当前目录中,"."代表当前目录
注意:
(1) 相同磁盘的文件移动只是一个重明名过程
(2) 不同磁盘的文件移动是复制删除过程
十二、文件查看
cat 文件名 ##查看文件的全部内容
cat -b 文件名 ##-b显示行号不包含空行
cat -n 文件名 ##-n显示行号包含空行
less 文件名 ##分页浏览
用法:
上 /下 | 逐行移动 |
---|---|
pageup / pagedown | 逐页移动 |
/关键字 | 高亮显示关键字,n向下匹配,N向上匹配 |
v | 进入vim模式 在vim模式中按:wq 退回到less模式 |
q | 退出 |
head file ##查看文件前10行
head -n 3 file ##查看文件前3行
tail file ##查看文件后10行
tail -n 3 file ##查看文件后3行
十三、文件的寻址
文件的地址分为:相对路径和绝对路径
-
相对路径:
(1) 相对与当前系统所在目录的一个文件名称的简写
(2) 此名称省略了系统当前所在目录的名称
(3) 此名称不以“/”开头
(4) 此名称在命令执行时会自动在操作对象前加入"pwd"所显示的值 -
绝对路径:
(1) 绝对路径是文件在系统的真实位置
(2) 此命令是以“/”开头的
(3) 此命令在命执行时系统不会考虑现在所在位置的信息
例:/root//desktop/file
/desktop/file注意:
当操作对象是 对象1 空格 对象2 时,这两个对象之间没有任何关系
eg1:
豆角 茄子 ##这是2个对象
豆角茄子 ##这是一个对象
eg2:
亲 ##动作是被系统执行的,不能作为名称出现
“亲” ##用引号的作用是把动作变成名称字符,这种方法叫引用
十四、自动补齐
Tab:系统中的Tab键可以实现命令的自动补齐
(1) 可以补齐系统中存在的命令,文件名称,和部分命令的参数
(2) 当一次补齐不了时代表以此关键字开头的内容不唯一,可以用X2来列出所有以此关键字开头的内容
十五、关于路径的命令
-
显示路径:
pwd -
切换路径:
cd 目录名称 ##进入到指定目录中
cd - ##当前目录和当前目录值前所在目录之间的切换
cd ~ ##进入当前用户家目录
cd ~user ##进入user家目录
cd … ##进入当前目录的上级目录
~ | 当前用户家目录 |
---|---|
~username | 指定用户的家目录. |
~+ | 当前目录 |
~- | 进入当前目录之前所在目录(olddir)cd - 中的"-" |
3. 查看:
ls
ls -l 文件名 ##文件属性
ls 目录名 ##目录中保函的内容
ls -d 目录名 ##目录本身
ls -a 目录名 ##所有文件包含隐藏的
ls -S 目录名 ##查看并且按照大小排序
ls -s 文件名|目录名 ##查看文件大小
ls -A ##不显示当前目录以及当前目录的上一级目录
du -sh ##查看文件大小
4. 文件查看:
cat ##输出文件全部内容
less ##分页浏览
head file ##查看文件前10行
head -n 3 file ##查看文件前3行
tail file ##查看文件后10行
tail -n 3 file ##查看文件后3行
tail -f file ##监控文件后10行内容的变化
5. 文件大小统计
wc
wc -l ##行数
wc -w ##单词数
wc -m ##字符数
wc -c ##字节数
注意:(1) 1k=1024byte
(2) 一个汉字等于两个字节
十六、系统中历史的调用
history ##查看历史
history -c ##清空当前环境中的历史
echo $HISTSIZE ## 显示容量
!数字 ##调用此行历史比如"!21"时执行历史中地21条命令
!w ##执行以w关键字开头的最近一条历史
ctrl +r +关键字 ##调用历史中最近一条含有此关键字的历史
十七、在系统中获得帮助
- 查看命令大用途:
whatis 命令 ##查看命令的用途 - 查看命令的用法:
命令 --help | 查看命令的基本用法,适用于多数命令 |
---|---|
[ ] | 标示选项可以加也可以不加 |
… | 标示选项加入的个数任意 |
<> | 标示选项是必须要加入的 |
Userage: | 标示命令用法 |
Opetions: | 对参数的作用做说明 |
3. man查看命令或文件的说说明手册
man 命令
man 5 文件
man的级别:
1 | 命令 |
---|---|
2 | 系统调用接口 |
3 | 函数库调用接口 |
4 | 特殊设备文件 |
5 | 系统配置文件 |
6 | 游戏规则说明 |
7 | 包说明 |
8 | 系统管理命令 |
9 | 内核调用规则 |
man的基本用法:
上/下 | 逐行查看 |
---|---|
pgup/pgdn | 分页查看 |
/关键字 | 高亮显示关键字,n向下匹配,N上向匹配 |
:q | 退出 |
man -k 字符串 | 下看当前字符串在系统中拥有的man的级别 |
/usr/share/doc | 系统中所有安装过的软件的帮助文档 |
date:
DATE(1) User Commands DATE(1)
NAME
date - print or set the system date and time
SYNOPSIS
date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
DESCRIPTION
Display the current time in the given FORMAT, or set the system date.
Mandatory arguments to long options are mandatory for short options
too.
-d, --date=STRING
display time described by STRING, not 'now'
-f, --file=DATEFILE
like --date once for each line of DATEFILE
-I[TIMESPEC], --iso-8601[=TIMESPEC]
output date/time in ISO 8601 format. TIMESPEC='date' for date
only (the default), 'hours', 'minutes', 'seconds', or 'ns' for
date and time to the indicated precision.
-r, --reference=FILE
display the last modification time of FILE
-R, --rfc-2822
output date and time in RFC 2822 format. Example: Mon, 07 Aug
--rfc-3339=TIMESPEC
output date and time in RFC 3339 format. TIMESPEC='date', 'sec-
onds', or 'ns' for date and time to the indicated precision.
Date and time components are separated by a single space:
2006-08-07 12:34:56-06:00
-s, --set=STRING
set time described by STRING
-u, --utc, --universal
print or set Coordinated Universal Time (UTC)
--help display this help and exit
--version
output version information and exit
FORMAT controls the output. Interpreted sequences are:
%% a literal %
%a locale's abbreviated weekday name (e.g., Sun)
%A locale's full weekday name (e.g., Sunday)
%b locale's abbreviated month name (e.g., Jan)
%B locale's full month name (e.g., January)
%c locale's date and time (e.g., Thu Mar 3 23:05:25 2005)
%C century; like %Y, except omit last two digits (e.g., 20)
%d day of month (e.g., 01)
%D date; same as %m/%d/%y
%e day of month, space padded; same as %_d
%F full date; same as %Y-%m-%d
%g last two digits of year of ISO week number (see %G)
%G year of ISO week number (see %V); normally useful only with %V
%h same as %b
%H hour (00..23)
%I hour (01..12)
%j day of year (001..366)
%k hour, space padded ( 0..23); same as %_H
%l hour, space padded ( 1..12); same as %_I
%m month (01..12)
%M minute (00..59)
%n a newline
%N nanoseconds (000000000..999999999)
%p locale's equivalent of either AM or PM; blank if not known
%P like %p, but lower case
%r locale's 12-hour clock time (e.g., 11:11:04 PM)
%R 24-hour hour and minute; same as %H:%M
%s seconds since 1970-01-01 00:00:00 UTC
%S second (00..60)
%t a tab
%T time; same as %H:%M:%S
%u day of week (1..7); 1 is Monday
%U week number of year, with Sunday as first day of week (00..53)
%V ISO week number, with Monday as first day of week (01..53)
%w day of week (0..6); 0 is Sunday
%W week number of year, with Monday as first day of week (00..53)
%x locale's date representation (e.g., 12/31/99)
%X locale's time representation (e.g., 23:13:48)
%y last two digits of year (00..99)
%Y year
%z +hhmm numeric time zone (e.g., -0400)
%:z +hh:mm numeric time zone (e.g., -04:00)
%::z +hh:mm:ss numeric time zone (e.g., -04:00:00)
%:::z numeric time zone with : to necessary precision (e.g., -04,
+05:30)
%Z alphabetic time zone abbreviation (e.g., EDT)
By default, date pads numeric fields with zeroes. The following
optional flags may follow '%':
- (hyphen) do not pad the field
_ (underscore) pad with spaces
0 (zero) pad with zeros
^ use upper case if possible
# use opposite case if possible
After any flags comes an optional field width, as a decimal number;
then an optional modifier, which is either E to use the locale's alter-
nate representations if available, or O to use the locale's alternate
numeric symbols if available.
十八、系统中的通用配置符号
* | 匹配0到任意字符 |
---|---|
? | 匹配单个字符 |
[[:alpha:]] | 单个字母 |
[[:lower:]] | 单个小写字母 |
[[:upper:]] | 单个大写字母 |
[[:digit:]] | 单个数字 |
[[:punct:]] | 单个符号 |
[[:space:]] | 单个空格 |
[[:alnum:]] | 单个数字或字母 |
[[:punct:][:upper:]] | 单个符号或但个大写字母 |
[1-9] | 1位,这位是1-9的任意数字 |
[!3-5] | 1位,不是3-5的数字 |
[^3-5] | 1位,不是3-5的数字 |
[a-z] | 1位,a-z的任意字母 |
{1…3} | 3个,1 2 3 |
{1,4,7} | 3个1,4,7这三个数 |