学习linux 3
直播课
- 优秀笔记学习
- 系统目录结构的学习
- 比较难懂的知识点 alias 别名的设置
PATH 环境变量的学习
umask的理解 - 容易混淆的知识点 cp(拷贝) mv(移动)
为了防止出错在使用cp 和mv命令时,如果右边是一个目录,一定要加/
比如 cp 123 abc/
学习笔记
2.(1-2)系统目录结构
2.3 ls命令
2.4 文件类型
2.5 alias命令
2.6 相对和绝对路径
2.7 CD命令
2.8 创建和删除目录mkdir rmdir
2.9 rm命令
2.10 环境变量PATH
2.11 cp命令
2.12 mv命令
2.13 文档查看cat_more_less_head_tail
2.14 文件或目录权限
2.15 更改所有者和所属组chown
2.16 umask
2.1-2.1 .系统目录结构
系统结构
bin=普通用户命令
sbin=root用户下命令
boot=系统启动相关文件 (grub)
dev=设备文件
etc=系统配置文件目录 例如:/etc/sysconfig/network-sceiptes/ifconfig-ens33
home=用户家目录
lib lib64= 系统库文件 例如:ldd /bin/ls
media=媒介目录 mnt= 挂载目录 opt 这三个都是空目录
proc=系统启动进程
run=进程临时文件目录
srv=服务产生文件 空目录
sys=系统内核相关文件
tmp=系统临时文件 任何用户都可操作
usr= 用户文件目录
var=是储存各种变化的文件,比如log等等
常用目录:ls /usr/bin /usr/sbin / bin/ /sbin / /etc/ /var/ /usr/local/
[[email protected] ~]# ls /
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr
tree命令安装
[[email protected] ~]# yum install -y tree
tree – help 显示常用选项 tree -L 2 / (最大指定为二层)
[[email protected] ~]# tree -L 2
.
└── anaconda-ks.cfg
0 directories, 1 file
2.3. ls 命令
- ls -l =列出文件详细信息 (显示文件权限 属主 属组 大小 日期 文件名)
[[email protected] ~]# ls -l
总用量 4
-rw-------. 1 root root 1257 3月 11 00:58 anaconda-ks.cfg
ls -i =显示文件和目录的inode编号
[[email protected] ~]# ls -i anaconda-ks.cfg
33582978 anaconda-ks.cfg
ls -la=查看所有隐藏文件
[[email protected] ~]# ls -la 总用量 28 dr-xr-x—. 3 root root 147 3月
10 18:58 . dr-xr-xr-x. 17 root root 224 3月 10 20:16 …
-rw-------. 1 root root 1257 3月 11 00:58 anaconda-ks.cfg
-rw-------. 1 root root 1209 3月 14 20:20 .bash_history
-rw-r–r--. 1 root root 18 12月 29 2013 .bash_logout
-rw-r–r--. 1 root root 176 12月 29 2013 .bash_profile
-rw-r–r--. 1 root root 176 12月 29 2013 .bashrc
-rw-r–r--. 1 root root 100 12月 29 2013 .cshrc drwx------. 2 root root 48 3月 11 16:05 .ssh
-rw-r–r--. 1 root root 129 12月 29 2013 .tcshrc
ls -ld 只列出目录本身
[[email protected] ~]# ls -ld /root/ dr-xr-x—. 3 root root 147 3月
10 18:58 /root/
ls -lta=时间顺序排序 时间越近排在最前面
[[email protected] ~]# ls -lta 总用量 28
-rw-------. 1 root root 1209 3月 14 20:20 .bash_history drwx------. 2 root root 48 3月 11 16:05 .ssh
-rw-------. 1 root root 1257 3月 11 00:58 anaconda-ks.cfg dr-xr-xr-x. 17 root root 224 3月 10 20:16 … dr-xr-x—. 3 root root 147 3月 10
18:58 .
-rw-r–r--. 1 root root 18 12月 29 2013 .bash_logout
-rw-r–r--. 1 root root 176 12月 29 2013 .bash_profile
-rw-r–r--. 1 root root 176 12月 29 2013 .bashrc
-rw-r–r--. 1 root root 100 12月 29 2013 .cshrc
-rw-r–r--. 1 root root 129 12月 29 2013 .tcshrc
其它ls命令用法 可用man命令查看
ls命令总结:
ls -l 列出文件详细信息
ls -i 显示文件和目录的inode编号
ls -la 查看隐藏文件
ls -lh 直观显示文件大小
ls -ld 只列出目录本身
ls -lta 按时间顺序排序
2.4 文件类型
在linux系统中用ls -la查看目录
[[email protected] ~]# ls -la
总用量 28
dr-xr-x---. 3 root root 147 3月 10 18:58 .
dr-xr-xr-x. 17 root root 224 3月 10 20:16 ..
-rw-------. 1 root root 1257 3月 11 00:58 anaconda-ks.cfg
-rw-------. 1 root root 1209 3月 14 20:20 .bash_history
-rw-r--r--. 1 root root 18 12月 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 12月 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 12月 29 2013 .bashrc
-rw-r--r--. 1 root root 100 12月 29 2013 .cshrc
drwx------. 2 root root 48 3月 11 16:05 .ssh
-rw-r--r--. 1 root root 129 12月 29 2013 .tcshrc
如上所示第一位表示文件类型:
- 是普通文件
d 是文件夹目录
c 是字符串设备 键盘 鼠标
l 是软连接文件
s 通信文件
b 块设备 光盘磁盘
2.5. alias命令
- alias命令;用来设置指令的别名(which查看文件所在路径),命令加选项组合形成新的命令。
[[email protected] ~]# which ls
alias ls=‘ls --color=auto’
/usr/bin/ls
2 .命令别名设置:定义命令别名,格式为“命令别名=‘实际命令’”。
alias 的基本使用方法为
例如:alias qkai=‘ls -lha’
要删除一个别名,可以使用 unalias 命令
[[email protected] ~]# unalias qkai
[[email protected] ~]# qkai
-bash: qkai: 未找到命令
which命令式通过PATH环境变量里的路径来查找文件
2.6. 相对和绝对路径
1 .绝对路径:从根目录开始,依次将各级子目录的名字组合起来,形成的路径就称为某个文件的绝对路径
例如:
#ls /etc/hostname
[[email protected] ~]# ls /etc/hostname
/etc/hostname
2 .相对路径:相对当前所在路径的位置不是以 / 开头
比如:authorized_keys 文件 当前目录是root
[[email protected] ~]# ls /root/.ssh/authorized_keys
/root/.ssh/authorized_keys
[[email protected] ~]# pwd
/root
[[email protected] ~]# ls .ssh/authorized_keys
.ssh/authorized_keys
pwd=查看当前所在路径目录
2.7 . cd命令
cd命令总结:
cd 命令用来切换工作目录
cd - 表示上次所在目录
cd 进入当前用户家目录下
cd~ 用户家目录
cd .. 返回上一级目录
下面是cd具体用法实例
[[email protected] ~]# cd /etc/sysconfig/
[[email protected] sysconfig]# cd -
/root
[[email protected] ~]# cd -
/etc/sysconfig
[[email protected] sysconfig]# pwd
/etc/sysconfig
[[email protected] sysconfig]# cd -
/root
[[email protected] ~]# pwd
/root
[[email protected] ~]# cd -
/etc/sysconfig
[[email protected] sysconfig]# pwd
/etc/sysconfig
[[email protected] sysconfig]# cd
[[email protected] ~]# cd /tmp/
[[email protected] tmp]# cd
[[email protected] ~]# cd ~
[[email protected] ~]# cd ..
[[email protected] /]# cd ..
[[email protected] /]# cd /etc/sysconfig/network-scripts/
[[email protected] network-scripts]# pwd
/etc/sysconfig/network-scripts
[[email protected] network-scripts]# cd ..
[[email protected] sysconfig]# pwd
/etc/sysconfig
[[email protected] sysconfig]# cd ..
[[email protected] etc]# pwd cd ..
/etc
[[email protected] etc]# cd ..
[[email protected] /]#
2.8 .创建和删除目录mkdir rmdir
mkdir :创建目录 指定要创建的目录列表,多个目录之间用空格隔开。
例如: 创建一个/tmp/qkailinux
[[email protected] ~]# mkdir /tmp/qkailinux
[[email protected] ~]# ls -ld /tmp/qkailinux/
drwxr-xr-x. 2 root root 6 3月 16 15:30 /tmp/qkailinux/
mkdir -p 若所要建立目录的上层目录目前尚未建立,则会一并建立上层目录 级联创建目录
例如: 创建一个 /tmp/qkailinux/1/2
[[email protected] ~]# mkdir /tmp/qklinux/1/2/
mkdir: 无法创建目录"/tmp/qklinux/1/2/": 没有那个文件或目录
[[email protected] ~]# ls -l /tmp/qkailinux/
总用量 0
[[email protected] ~]# mkdir -p /tmp/qkailinux/1/2/
[[email protected] ~]# ls -l /tmp/qkailinux/
总用量 0
drwxr-xr-x. 3 root root 15 3月 16 15:32 1
mkdir -pv能看到创建目录的过程
例如 :创建/tmp/qkailinux/2/3/4
[[email protected] ~]# mkdir -pv /tmp/qkailinux/2/3/4
mkdir: 已创建目录 "/tmp/qkailinux/2"
mkdir: 已创建目录 "/tmp/qkailinux/2/3"
mkdir: 已创建目录 "/tmp/qkailinux/2/3/4"
rmdir命令用来删除空目录
删除刚才创建的/tmp/qkailinux/2/3 ,可以看到3目录下还有一个4目录所以不能删除
[[email protected] ~]# rmdir /tmp/qkailinux/2/3
rmdir: 删除 "/tmp/qkailinux/2/3" 失败: 目录非空
[[email protected] ~]# rmdir /tmp/qkailinux/2/3/4
touch命令是用来创建一个空白文件
[[email protected] ~]# touch /tmp/qkailinux/2/3/1.txt
[[email protected] ~]# ls /tmp/qkailinux/2/3
1.txt
rmdir -p删除指定目录后,若该目录的上层目录已变成空目录,则将其一并删除
[[email protected] ~]# rmdir -p /tmp/qkailinux/1/2
rmdir: 删除目录 "/tmp/qkailinux" 失败: 目录非空
当前工作目录必须在被删除目录之上,不能是被删除目录本身,也不能是被删除目录的子目录。
2.9 . rm命令
rm 命令可以删除一个目录中的一个或多个文件或目录,也可以将某个目录及其下属的所有文件及其子目录均删除掉
-f:强制删除文件或目录;
连续按两次Tab可以列出目录下所有文件!tree :表示使用命令历史里上次最近tree开头命令
rm -r或-R:递归处理,将指定目录下的所有文件与子目录一并处理
rm -rf :加 f 没有提示直接删除 (-f 即使原档案属性设为唯读,亦直接删除,无需逐一确认)
2.10 .环境变量PATH
which可以查看一个命令的路径 ,包括它有没有别名
[[email protected] ~]# which rm
alias rm=‘rm -i’
/usr/bin/rm
which要查找的命令是在下面的目录查找,如果命令在这几个目录里面就可以不用绝对路径
[[email protected] ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
把ls放入、tmp/ls2中 /tmp/ls2也可以直接使用 也可以把/tmp/目录加入环境变量目录下,ls2就可以使用
[[email protected] ~]# cp /usr/bin/ls /tmp/ls2
[[email protected] ~]# /tmp/ls2
anaconda-ks.cfg
[[email protected] ~]# ls2
-bash: ls2: 未找到命令
[[email protected] ~]# ls
anaconda-ks.cfg
[[email protected] ~]# PATH=$PATH:/tmp/
[[email protected] ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/tmp/
[[email protected] ~]# ls2
anaconda-ks.cfg
[[email protected] ~]# which ls2
/tmp/ls2
如果需要永久生效需要编辑配置文件 vi /etc/profile 添加配置 PATH=$PATH:/tmp/
2.11 .cp命令
cp命令用来将一个或多个源文件或者目录复制到指定的目的文件或目录。
比如拷贝一个文件: [[email protected] ~]# cp /etc/passwd /tmp/1.txt
cp -r拷贝目录
[[email protected] ~]# cp -r /tmp/qkailinux/ /tmp/qkai1/
[[email protected] ~]# tree !$ (!$指的是上一条命令的最后一个参数/tmp/qkai1/)
tree /tmp/qkai1/
/tmp/qkai1/
└── 2
1 directory, 0 files
当目标目录已经存在的时候 ,会把源目录放到目标目录下;如果不存在会拷贝过来改名字。
[[email protected] ~]# ls /tmp/qkai1/
2
[[email protected] ~]# cp -r /tmp/qkailinux/ /tmp/qkai1/
[[email protected] ~]# ls /tmp/qkai1/
2 qkailinux
格式:cp /源文件/源目录/ /目标文件/目标目录/
2.12 .mv命令
mv 命令是move的缩写可以用来移动文件或者将文件改名
在同一个目录下移动文件就是 改文件名字
[[email protected] ~]# ls
anaconda-ks.cfg
[[email protected] ~]# mv anaconda-ks.cfg anaconda-ks.cfg.1
[[email protected] ~]# ls
anaconda-ks.cfg.1
使用mv命令在同一个目录下改名字,如果目标的文件已经存在会提示是否覆盖,如果目标是一个目录且目录不存在会改目录名字,如果目录存在会把文件放在目标目录里面。
[[email protected] tmp]# mv qkai2/ qkailinux/
[[email protected] tmp]# ls qkailinux/
2 qkai2
[[email protected] tmp]# mkdir qkai2
[[email protected] tmp]# touch qkai2/1.txt
[[email protected] tmp]# tree qkai2
qkai2
└── 1.txt
0 directories, 1 file
[[email protected] tmp]# tree qkailinux/
qkailinux/
├── 2
└── qkai2
├── 2
├── ls2
└── qkai1
├── 2
└── qkailinux
└── 2
7 directories, 1 file
[[email protected] tmp]# mv qkai2/ qkailinux/
mv:是否覆盖"qkailinux/qkai2"? y
mv: 无法将"qkai2/" 移动至"qkailinux/qkai2": 文件已存在
2.13. 文档查看cat_more_less_head_tail
cat:查看一个文件的内容并显示在屏幕上,后面直接跟文件名
cat -A :显示所有东西出来,包括特殊字符
cat -n:查看文件时,把行号也显示在屏幕上
more:用来查看一个文件的内容,当一屏幕不足以完全显示完文件内容时 空格键翻页看下一屏幕, “Ctrl+B”上翻,
less作用跟more一样,但是支持方向键
空格键和Ctrl+f往后查看; ctrl+b,往前查看. 查看完毕以后,需要按q退出
搜索功能: 输入/可以向后搜素 ;输入 ?从后向前搜索 输入n往下看 N往前看
shift+g 直接定位到最后,g定位到首行. G定位到末行
head:查看显示文件的前十行
head -n 查看文件前N行
tail :查看显示文件后十行
tail -f 动态显示文件内容 多用来查看日志
2.14 . 文件或目录权限chmod
输入ls -l查看
-rw-r–r--.
第一位 -表示文件类型
rw-r–r--表示文件权限
rw-表示文件所有者权限
中间位r–文件所属组权限
后三位r–其他用户权限,其中.受制于selinux 关闭selinux后会消失
r:读取属性 w: 写入属性 X:执行属性
权限值分别是: r=4 w=2 x=1
所以rw-r–r-- 可表示为644
修改权限的命令是:chmod
如下修改2.txt文件权限:文件所有者权限变为 rwx
也可以写成chmod u(所有者)=rwx, g(所属组)=r, o(其它用户)=r 文件或目录
或者chmod a+x 文件或目录 同时加上权限或者减权限
chmod -R 可以把同时更改目录及目录下所有的权限
2.15 更改所有者权限和所属组chown
chown 可以更改所有者,也可以更改所属组
cat /etc/passwd 查看系统用户有哪些
chown 所有者:所属组 文件名或者目录
修改tmp目录下的yum.log文件所有者
[[email protected] ~]# ls -l /tmp/yum.log
-rw-------. 1 root root 0 3月 11 00:52 /tmp/yum.log
[[email protected] ~]# chown qkai /tmp/yum.log
[[email protected] ~]# !ls
ls -l /tmp/yum.log
-rw-------. 1 qkai root 0 3月 11 00:52 /tmp/yum.log
chgrp命令用来修改所属组
[[email protected] ~]# chgrp user1 /tmp/yum.log
[[email protected] ~]# !ls
ls -l /tmp/yum.log
-rw-------. 1 qkai user1 0 3月 11 00:52 /tmp/yum.log
chown也可以同时修改所有者和所属组或者单独修改
[[email protected] ~]# chown user1:qkai /tmp/yum.log
[[email protected] ~]# !ls
ls -l /tmp/yum.log
-rw-------. 1 user1 qkai 0 3月 11 00:52 /tmp/yum.log
[[email protected] ~]# chown :root /tmp/yum.log
[[email protected] ~]# !ls
ls -l /tmp/yum.log
-rw-------. 1 user1 root 0 3月 11 00:52 /tmp/yum.log
chown -R 可以同时更改目录及子目录 下的文件所有者和所属组
[[email protected] ~]# chown -R user1:qkai /tmp/qkai2
[[email protected] ~]# ls -l /tmp/qkai2/
总用量 0
-rwxrwx---. 1 user1 qkai 0 3月 16 18:10 1.txt
[[email protected] ~]# ls -ld /tmp/qkai2
drw-r--r--. 2 user1 qkai 19 3月 16 18:10 /tmp/qkai
chown -R username:group filenme
chown -R 后面只跟一个字符串之更改所有者,如果加冒号字符串更改所属组
2.16 .umask
命令umask用于改变文件的默认权限,其格式为 umask 数字
输入命令查看root用户umask值为 0022
[[email protected] ~]# umask
0022
创建文件11.txt和目录123 权限值分别是644和755
[[email protected] ~]# touch 11.txt
[[email protected] ~]# ls -l 11.txt
-rw-r--r--. 1 root root 0 3月 17 14:48 11.txt
[[email protected] ~]# mkdir 123
[[email protected] ~]# ls -ld 123
drwxr-xr-x. 2 root root 6 3月 17 14:49 123
修改umask值为002 在创建文件3.txt和目录234
[[email protected] ~]# umask 002
[[email protected] ~]# umask
0002
[[email protected] ~]# mkdir 234
[[email protected] ~]# ls -ld 234
drwxrwxr-x. 2 root root 6 3月 17 14:50 234
[[email protected] ~]# touch 3.txt
[[email protected] ~]# ls -l 3.txt
-rw-rw-r--. 1 root root 0 3月 17 14:51 3.txt
当umask值为002 文件权限值默认664 目录权限值默认775
所以目录必须有X权限,因为查看一个目录必须先进去目录。
所以默认最大是7. 文件不需要必须有X权限,所以默认是6
当umask值为003时,如果使用单纯的减法,文件的默认权限则为666-003=663,但实际权限应该为664
[[email protected] ~]# umask 003
[[email protected] ~]# touch 1.txt
[[email protected] ~]# ls -l 1.txt
-rw-rw-r--. 1 root root 0 3月 17 15:06 1.txt
[[email protected] ~]# mkdir 111
[[email protected] ~]# ls -ld 111
drwxrwxr--. 2 root root 6 3月 17 15:11 111
算法为:
文件:666-003=rw-rw-rw- - -------wx=rw-rw-r-- 664
目录:777-003=rwxrwxrwx - -------wx=rwxrwxr-- 774