目录
4.10 linux的快捷键和basename及dirname
一.linux概述
自个百度随便了解点即可~
二.安装VMware
操作简单,直接一直默认下一步,如果之前有过vm,删除的时候没删好可能要下个cccleaner去清理下注册表,安装成功后检查网络适配器是否多出了vmnet1和vmnet8
三.安装LINUX
配置虚拟机硬件信息,这方面按照自己的电脑量力而行,电脑好的也可以选择向下兼容,一般使用就2G2核50G内存即可,然后再安装一个centos的镜像文件,最小版和可视化的都行
四.linux常用命令
4.1 命令格式的说明
进入命令行界面的提示符解析
[root@localhost ~]#
root位置: 登录用户名
@: 连接符号
localhost位置: 本机的主机名
~位置: 当前的所在位置
#位置: 表示是超级管理员还是普通用户
超级管理员则使用#
普通用户则使用$
4.2 三个常用的命令
1. pwd : print current work directory三个单词的简写
作用就是以绝对路径的形式显示当前的位置所在
eg:
[root@localhost network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@localhost ~]# pwd
/root
2. ls : list directory contents的简写
作用,就是列出指定目录下的内容(文件,子目录等)
eg: ls 默认列出当前工作空间里的内容
常用的选项:
-l : 列出每个子文件的属性详情
-a : 显示所有的内容,包括隐藏的
-S : 以大小进行降序排序显示, 尽量与-h,-l一起使用
-h: 以方便人类可读的显示效果显示大小的单位,比如k,MB,G
eg: ls -l 显示属性详情, 可以简化写: ll
eg: ls -a 显示所有包括隐藏的内容
eg: ls -l -a 显示所有包括隐藏的内容的属性详情 ,简化写:ll -a 或者ls -la
eg: ls -lhS 显示当前目录下的所有内容,并降序排序
eg: ls -lhS /etc 显示指定目录/etc下的所有内容,并降序排序
3. cd : change directory的简写, 切换工作空间。
注意:是一个特殊指令,特殊在是一个shell内置指令。
cd [target directory]
eg: cd 回家
eg: cd ~ 回家
eg: cd - 回到上一次的位置
eg: cd /etc 切换到/etc下
eg: cd .. 回到上一级目录(父目录)
cd . 表示不动
4.3 帮助命令
man指令:
作用:查看指定命令的帮助文档
语法: man 指令
eg: man ls
man pwd
man cd
help指令:
作用:查看指定命令的主题信息
语法: help 指令
注意: 不是所有的指令都有主题信息
info指令:
作用:用来查看指令命令的详细信息
语法: info 指令
4.4 文件处理指令
4.4.1 touch
作用:用于创建一个空文件
语法: touch filename.....
eg:
[root@localhost etc]# touch file1 file2 file3
[root@localhost etc]# pwd
/etc
[root@localhost etc]# touch ~/file4
[root@localhost etc]# touch ~/{file5,file6}
[root@localhost etc]# ls ~
anaconda-ks.cfg file1 file2 file3 file4 file5 file6
4.4.2 mkdir
作用:用于创一个目录
语法:mkdir [-p] dirname.....
[root@localhost ~]# mkdir dir1
[root@localhost ~]# mkdir ./dir2 ./dir3
[root@localhost ~]# cd /etc
[root@localhost etc]# mkdir ~/{dir4,dir5}
[root@localhost etc]# mkdir ~/dir6/dir66 #会报错,因为dir6不存在,所以不能创建dir66
[root@localhost etc]# mkdir -p ~/dir6/dir66 #表示多层级创建目录
[root@localhost etc]# ls -l ~
总用量 4
-rw-------. 1 root root 1259 11月 29 19:03 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 11月 29 22:32 dir1
drwxr-xr-x. 2 root root 6 11月 29 22:33 dir2
drwxr-xr-x. 2 root root 6 11月 29 22:33 dir3
drwxr-xr-x. 2 root root 6 11月 29 22:33 dir4
drwxr-xr-x. 2 root root 6 11月 29 22:33 dir5
drwxr-xr-x. 3 root root 19 11月 29 22:34 dir6
-rw-r--r--. 1 root root 0 11月 29 22:28 file1
-rw-r--r--. 1 root root 0 11月 29 22:28 file2
-rw-r--r--. 1 root root 0 11月 29 22:28 file3
-rw-r--r--. 1 root root 0 11月 29 22:29 file4
-rw-r--r--. 1 root root 0 11月 29 22:30 file5
-rw-r--r--. 1 root root 0 11月 29 22:30 file6
[root@localhost etc]# ls ~/dir6
dir66
4.4.3 rm
作用:删除文件或者是目录
语法:rm [-rf] filename .....
eg:
[root@localhost ~]# ls
anaconda-ks.cfg dir1 dir2 dir3 dir4 dir5 dir6 file1 file2 file3 file4 file5 file6
[root@localhost ~]# rm file1
rm:是否删除普通空文件 "file1"?y
[root@localhost ~]# ls
anaconda-ks.cfg dir1 dir2 dir3 dir4 dir5 dir6 file2 file3 file4 file5 file6
[root@localhost ~]# rm dir1
rm: 无法删除"dir1": 是一个目录
[root@localhost ~]# rm -f file2
[root@localhost ~]# ls
anaconda-ks.cfg dir1 dir2 dir3 dir4 dir5 dir6 file3 file4 file5 file6
[root@localhost ~]# rm -r dir1
rm:是否删除目录 "dir1"?y
[root@localhost ~]# ls
anaconda-ks.cfg dir2 dir3 dir4 dir5 dir6 file3 file4 file5 file6
[root@localhost ~]#
总结: 默认情况下,是有询问的删除文件,输入y表示删除,输入n表示不删除
如果想要强制删除文件,添加-f, 但是要慎用。
如果想要删除目录,必须添加-r,表示递归删除。
4.4.4 mv
作用:移动文件或者目录,有更名作用
语法: mv [OPTION]... SOURCE... DIRECTORY
eg:
[root@localhost ~]# ls
anaconda-ks.cfg dir2 dir3 dir4 dir5 dir6 file3 file4 file5 file6
[root@localhost ~]# mv file3 file4 dir2 dir3 # 将file3 file4 dir2 移动到dir3里
[root@localhost ~]# ls
anaconda-ks.cfg dir3 dir4 dir5 dir6 file5 file6
[root@localhost ~]# ls dir3
dir2 file3 file4
[root@localhost ~]# mv file6 dir3/file7 #将file6移动到dir3里并更名为file7
[root@localhost ~]# ls dir3
dir2 file3 file4 file7
[root@localhost ~]#
4.4.5 cp
作用:拷贝文件或者是目录
语法:cp [-r] source....directory
eg:
[root@localhost ~]# rm -rf ./* 删除当前目录下的所有非隐藏文件
[root@localhost ~]# touch file1 file2 file3
[root@localhost ~]# mkdir dir1 dir2 dir3
[root@localhost ~]# ll
总用量 0
drwxr-xr-x. 2 root root 6 11月 29 23:04 dir1
drwxr-xr-x. 2 root root 6 11月 29 23:04 dir2
drwxr-xr-x. 2 root root 6 11月 29 23:04 dir3
-rw-r--r--. 1 root root 0 11月 29 23:04 file1
-rw-r--r--. 1 root root 0 11月 29 23:04 file2
-rw-r--r--. 1 root root 0 11月 29 23:04 file3
[root@localhost ~]# cp file1 dir1 # 拷贝file1到dir1里
[root@localhost ~]# ll
总用量 0
drwxr-xr-x. 2 root root 19 11月 29 23:04 dir1
drwxr-xr-x. 2 root root 6 11月 29 23:04 dir2
drwxr-xr-x. 2 root root 6 11月 29 23:04 dir3
-rw-r--r--. 1 root root 0 11月 29 23:04 file1 #源文件依然存在
-rw-r--r--. 1 root root 0 11月 29 23:04 file2
-rw-r--r--. 1 root root 0 11月 29 23:04 file3
[root@localhost ~]# ls dir1 # 查看dir1里的内容
file1
[root@localhost ~]# echo "helloworld" > file2 #写一些字符串到file2文件里
[root@localhost ~]# ll
总用量 4
drwxr-xr-x. 2 root root 19 11月 29 23:04 dir1
drwxr-xr-x. 2 root root 6 11月 29 23:04 dir2
drwxr-xr-x. 2 root root 6 11月 29 23:04 dir3
-rw-r--r--. 1 root root 0 11月 29 23:04 file1
-rw-r--r--. 1 root root 11 11月 29 23:05 file2
-rw-r--r--. 1 root root 0 11月 29 23:04 file3
[root@localhost ~]# cp file2 dir1/file22 # 复制file2到dir1里同时更名为file22
[root@localhost ~]# ll dir1
总用量 4
-rw-r--r--. 1 root root 0 11月 29 23:04 file1
-rw-r--r--. 1 root root 11 11月 29 23:05 file22
[root@localhost ~]# cp file3 file4 #拷贝并更名
[root@localhost ~]# ll
总用量 4
drwxr-xr-x. 2 root root 33 11月 29 23:05 dir1
drwxr-xr-x. 2 root root 6 11月 29 23:04 dir2
drwxr-xr-x. 2 root root 6 11月 29 23:04 dir3
-rw-r--r--. 1 root root 0 11月 29 23:04 file1
-rw-r--r--. 1 root root 11 11月 29 23:05 file2
-rw-r--r--. 1 root root 0 11月 29 23:04 file3
-rw-r--r--. 1 root root 0 11月 29 23:07 file4
[root@localhost ~]# cp dir2 dir3 # 拷贝目录时, 不带参数-r,会忽略目录
cp: 略过目录"dir2"
[root@localhost ~]# ll dir3
总用量 0
[root@localhost ~]# cp dir1 dir3
cp: 略过目录"dir1"
[root@localhost ~]# ll dir3
总用量 0
[root@localhost ~]# cp -r dir1 dir3 # 如果想要拷贝目录,应该带上-r参数,表示递归拷贝
[root@localhost ~]# ll dir3
总用量 0
drwxr-xr-x. 2 root root 33 11月 29 23:08 dir1
4.4.6 ln
作用:用于创建链接文件
语法: ln [-s] filename newfilename
解析:
linux的链接文件分为两类,
一类是软链接文件: 软链接文件相当于windows的快捷方式
文件和目录都可以有软链接
创建语法: ln -s filename newfilename
一类是硬链接文件: 文件可以有硬链接,目录不能有硬链接
创建语法: ln filename newfilename
总结: 软链接,新开辟了一个文件块和inode
硬链接,就是源文件名的别名
4.4.7 echo
作用: 用于展示一行文件信息
语法: echo 字符串|环境变量名
[root@localhost ~]# echo you are best # 打印一串字符,到控制台
you are best
[root@localhost ~]# echo "you are best"
you are best
[root@localhost ~]# echo $HOME #打印HOME变量的值
/root
[root@localhost ~]# echo $PATH #打印PATH变量的值
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost ~]# echo $HOSTNAME #打印HOSTNAME变量的值
localhost.localdomain
> : 重定向到某一个位置,会覆盖原有的内容
>>: 重定向到某一个文职,追加到原有内容的后面
[root@localhost ~]# echo "hello world"
hello world
# 将一串字符输入到content.txt文件里, 注意,如果该文件不存在,会直接创建。
[root@localhost ~]# echo "hello world" > content.txt
[root@localhost ~]# ll
总用量 16
-rw-r--r--. 1 root root 12 11月 29 23:43 content.txt
[root@localhost ~]# echo "hello" > content.txt #覆盖
[root@localhost ~]# ll
总用量 16
-rw-r--r--. 1 root root 6 11月 29 23:43 content.txt #变成6个字节
[root@localhost ~]# echo "world" >> content.txt #追加
[root@localhost ~]# ll
总用量 16
-rw-r--r--. 1 root root 12 11月 29 23:44 content.txt #变成12个字节
4.5 文件查看指令
4.5.1 cat
作用: 查看整个文件的内容
语法: cat [-An] filename
解析: -A 显示隐藏的字符
-n 显示行号
4.5.2 more/less
作用: 用于分页查看文件内容
语法: more filename
解析: 默认查看第一页内容
空格键/f键 查看下一页
enter键: 一行一行的滚动
b :往回翻页
q|Q: 退出
less和more用法一样
4.5.3 head
作用: 查看文件的头部信息,默认查看10行
语法: head [-number] filename
解析: 如果想要查看指定行数,添加-数字
4.5.4 tail
作用:查看文件的末尾信息,默认查看10行
语法: tail [-number] filename
解析: 如果想要查看指定行数,添加-数字
4.6 文件查找指令
4.6.1 find
作用:是可以根据指定类型参数,来查找文件系统中的文件或者是目录的
语法: find 搜索位置 条件
eg:
[root@localhost ~]# find /etc -name 'init*' 按照名字查找init开头的文件或目录
/etc/inittab
/etc/sysconfig/init
/etc/sysconfig/network-scripts/init.ipv6-global
/etc/init.d
/etc/rc.d/init.d
/etc/selinux/targeted/active/modules/100/init
/etc/selinux/targeted/contexts/initrc_context
[root@localhost ~]# find /etc -name 'init' 按照名字为init的文件或目录
/etc/sysconfig/init
/etc/selinux/targeted/active/modules/100/init
[root@localhost ~]# find /etc -name 'in??' 按照名字查找in开头并且长度为4的文件或目录
/etc/sysconfig/init
/etc/selinux/targeted/active/modules/100/init
[root@localhost ~]# find /etc -name '?i*' 按照名字查找第二个字符是i的文件或目录
find /etc -type d 查看/etc下的所有目录,包括子目录
d:目录 l:软链接, f:普通文件
find /etc -size -1024
find /etc -size +2K -size -3k 单位: k,M,G等
注意: 默认单位为一个block 一个block相当于512byte
如果想要查询小于100MB的文件, 100*1024KB 100*1024*2block
所以:find /etc -size -204800
find /etc -size +1024 查看大于512KB的文件
4.6.2 grep
作用:用于过滤查询文件内容
语法:grep [-cinv] '搜寻字符串' filename
-c :输出匹配行的次数(是以行为单位,不是以出现次数为单位)
-i :忽略大小写,所以大小写视为相同
-n :显示匹配行及行号
-v :反向选择,显示不包含匹配文本的所有行。
eg:
[root@localhost ~]# grep -i HOST ./profile 忽略大小写的查找host所在的行信息
[root@localhost ~]# grep -ci HOST ./profile 忽略大小写的查找host所在的行的数量
[root@localhost ~]# grep -in HOST ./profile 忽略大小写的查找host所在的行信息以及行号
[root@localhost ~]# grep -v HOST ./profile 查找除了HOST所在的行的其他行的信息
管道: |
作用: 将前一个命令的结果通过管道交给后一个命令,继续操作
[root@localhost ~]# grep -i HOST ./profile
HOSTNAME=`/usr/bin/hostname 2>/dev/null`
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
# 前一个查询的结果交给下一个grep继续过滤
[root@localhost ~]# grep -i HOST ./profile | grep USER
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
4.6.3 which
作用:用来在path变量的值中查找命令的位置
语法: which 命令
4.6.4 whereis
作用:用于查看命令的位置和帮助文件的位置
语法: whereis 命令
4.7 文件(解)压缩指令
4.7.1 gzip/gunzip
作用:压缩指令,将每一个文件进行压缩。一个文件对应一个压缩文件
语法: gzip filename.....
特点:
- 只能压缩文件,
- 并且源文件会消失
- 压缩文件名的后缀.gz
gzip -d 表示解压缩,相当于gunzip的作用
4.7.2 bzip2/bunzip2
作用:压缩指令,将每一个文件进行压缩。一个文件对应一个压缩文件
语法: bzip2 filename.....
特点:
- 只能压缩文件,
- 默认情况下,源文件会消失, 除非带上-k参数,会保留源文件
- 压缩文件名的后缀.bz2
bzip2 -d 表示解压缩,相当于bunzip2的作用
4.7.3 zip
作用:压缩指令,可以将多个文件或者目录压缩到一个压缩文件中。
语法: zip -r compressfilename.zip file1 file2.... dir1 dir2....
特点:
- 保留源文件
- 需要自定义压缩文件名
- 如果有目录要压缩,必须添加-r参数
小贴士:zip是为了兼容windows的压缩工具,而提供的
zip和unzip都需要安装
4.7.4 tar
作用:打包指令,用于将多个文件打成一个包,也就是一个包文件
语法: tar -[cxvf] tarfilename.tar file............
解析: -c :表示打包,即create
-f :用于指定新文件名,必须和新文件名挨着
-x :拆包, 不能和-c共存
-v :显示压缩过程
eg: tar -cvf michael.tar file1 file2 dir1 dir2
tar -xvf michael.tar 拆包
小贴士:
- 打包后的文件大小,是打包前的总和。
- 所以,如果想要压缩包文件,tar指令一般会和压缩(解压缩)指令一起使用。
常用组合: zcvf 中的z 表示gzip
jcvf 中的j 表示bzip2
打包并压缩:tar -[zcvf] arfilename.tar file............
tar -[jcvf] arfilename.tar file............
解压缩并拆包:tar -[zxvf] arfilename.tar
tar -[jxvf] arfilename.tar
4.8 时间指令date
作用:查看或者设置时间
reg:
date 查看系统当前时间
以自定义的方式显示系统时间: date +'%Y-%m-%d %H:%M:%S'
注意:+与字符串之间不能有空格,与date之间要有空格
设置时间
eg: date -s "2015-5-8 19:48:00"
同步到bios,重启之后才能继续生效
eg: hwclock -w
4.9 系统关机指令
1. 重启指令
reboot
init 6
2. 关机指令:
shutdown -h now 立即关机
shutdown -h 20:30 定时关机
poweroff
half
init 0
4.10 linux的快捷键和basename及dirname
ctrl+c 终止前台程序
ctrl+z 将前台程序挂起, fg指令是用于将挂起程序调度到前台运行
ctrl+l 清屏,相当于clear
ctrl + a 回到命令行的最前端
ctrl + e 回到命令行的最后面
ctrl + w 删除光标前的一个单词
ctrl + k 删除光标后的所有单词
basename /root/profile 用于显示整个路径中的最后一个名字 结果:profile
dirname /etc/dir1/profile 显示最后一个名字之前的整个路径 结果:/etc/dir1
(>-<,第一天的大数据学习,东西还是有点多的,大把大把的命令,不过都比较好理解,就是需要一段时间去记忆,运用方面后续还要配合各类特殊符号进行灵活操作,对之后的学习还是充满期待的~冲冲冲!!!)