tree命令
先安装tree工具
yum install -y tree
tree /etc/yum/
控制查看深度
tree -L 2 /home
创建新目录–mkdir
mkdir lab
-p 选项级联创建目录
mkdir -p dir1/dir2/dir3/dir4
tree dir1
dir1
└── dir2
└── dir3
└── dir4
3 directories, 0 files
cp
复制单个文件到目标位置
cp /etc/hosts .
复制且重命名
cp /etc/hosts ./hosts-1
当前目录下有同名称文件,不会提示直接覆盖
复制多个文件,目标只能是目录
cp /etc/passwd /etc/host.conf ./hosts-all
-r选项复制目录
cp -r /etc/yum .
前目录下有同名称目录,则将源目录放到相同目录下面,而不是覆盖当前目录
cp -r /etc/yum ./yum
ls yum
fssnap.d pluginconf.d protected.d vars version-groups.conf yum
复制多个目录,目标必须是已经存在的目录
cp -r /etc /home ./mydir
cp: 目标"./mydir" 不是目录
mv命令
移动单个文件
mv hosts-1 ../lab1
移动多个文件,目标位置只能是目录
mv passwd hosts /home/laoma/lab1/
重命名文件
mv host.conf host.conf-new
移动单个目录
v etc ../lab1
移动多个目录
mv home/ yum/ yum-1/ ../lab1
rm和rmdir命令
删除文件
rm hosts
强制删除具有写保护的文件
rm cacerts
rm:是否删除有写保护的普通文件 "cacerts"?yes
使用-f选项,直接删除
rm -f cacerts
删除空目录
rmdir dir01
#非空非空目录不能删除
rmdir etc
rmdir: 删除 "etc" 失败:
-r选项递归删除目录
rm -fr etc
删除多个目录
rm -fr
-r选项递归删除目录
rm -r yum
递归强制删除目录 -fr选项
rm -fr etc
删除多个目录
rm -fr
mkdir命令
创建目录
mkdir lab
-p 选项级联创建目录
mkdir -p dir1/dir2/dir3/dir4
tree dir1
dir1
└── dir2
└── dir3
└── dir4
软连接
类似于windows中的快捷方式
创建软连接
ln -s /var/tmp/ mytmp
ls -l mytmp
硬连接
通过多个文件名访问同一个数据块
ln hosts-1 hosts-2
两个文件的inode是相同的
更新hosts-1时间,hosts-2也跟着一起变动
更新hosts-1文件内容,hosts-2也跟着一起变动
文件操作练习
-
在用户的主目录中,创建三个子目录:Music、Pictures 和 Videos。
mkdir Music Pictures Videos
-
在用户的主目录中:
- 创建六个文件,并以 songX.mp3 形式取名。
- 创建六个文件,并以 snapX.jpg 形式取名。
- 创建六个文件,并以 filmX.avi 形式取名。
在每一组文件中,将 X 替换为数字 1 到 6 。
touch song1.mp3 song2.mp3 song3.mp3 song4.mp3 song5.mp3 song6.mp3 touch snap1.jpg snap2.jpg snap3.jpg snap4.jpg snap5.jpg snap6.jpg touch film1.avi film2.avi film3.avi film4.avi film5.avi film6.avi
-
将 songX.mp3 所有文件移动到 Music 目录。
mv song1.mp3 song2.mp3 song3.mp3 song4.mp3 song5.mp3 song6.mp3 Music/
-
将 snapX.jpg 所有文件移动到 Pictures 目录。
mv snap1.jpg snap2.jpg snap3.jpg snap4.jpg snap5.jpg snap6.jpg Pictures/
-
将 filmX.avi 所有文件移动到 Videos 目录。
mv film1.avi film2.avi film3.avi film4.avi film5.avi film6.avi Videos/
-
在用户的主目录中,创建三个子目录:friends、family 和 work。
mkdir friends family work
-
复制文件到指定位置。
- 将所有序号是1和2的文件复制到 friends 目录。
- 将所有序号是3和4的文件复制到 family 目录。
- 将所有序号是5和6的文件复制到 work 目录。
cp Music/song1.mp3 Music/song2.mp3 Pictures/snap1.jpg Pictures/snap2.jpg Videos/film1.avi Videos/film2.avi friends/ cp Music/song3.mp3 Music/song4.mp3 Pictures/snap3.jpg Pictures/snap4.jpg Videos/film3.avi Videos/film4.avi family/ cp Music/song5.mp3 Music/song6.mp3 Pictures/snap5.jpg Pictures/snap6.jpg Videos/film5.avi Videos/film6.avi work/
-
复制 family 和 friends 目录到/tmp中。
cp -r family/ friends/ /tmp
-
删除 family 和 friends 目录。
rm -r /tmp/friends/ /tmp/family/ ls -d /tmp/friends/ /tmp/family/ ls: 无法访问/tmp/friends/: 没有那个文件或目录 ls: 无法访问/tmp/family/: 没有那个文件或目录
-
删除 work 目录中所有文件,然后再删除 work 目录。
cd work/ rm film5.avi film6.avi snap5.jpg snap6.jpg song5.mp3 song6.mp3 cd .. rmdir work/
-
在家目录下创建mytmp软连接指向/var/tmp,并验证。
ln -s /var/tmp/ mytmp ll mytmp lrwxrwxrwx. 1 laoma laoma 9 7月 21 11:41 mytmp -> /var/tmp/
-
删除以上步骤中创建的所有文件和目录。
rm -r family/ friends/ Music/ Pictures/ Videos/ rm mytmp
批量修改用户密码
[root@centos7 ~]# cat user.txt
yyy:456
[root@centos7 ~]# useradd yyy
[root@centos7 ~]# cat user.txt | chpasswd
shell扩展匹配文件
* 示例
ls /etc/ho*
/etc/host.conf /etc/hostname /etc/hosts /etc/hosts.allow /etc/hosts.deny
? 示例
ls file-?
file-1 file-2 file-a file-b
[] 示例
ls file-[12]
file-1 file-2
s file-[^12]
file-a file-b
~ 示例
当前用户的主目录
echo ~root
/root
匹配所有大写字母
ls file-[[:upper:]]
file-A file-Z
匹配所有小写字母
ls file-[[:lower:]]
file-a file-b file-z
匹配所有字母
ls file-[[:alpha:]]
file-a file-A file-b file-z file-Z
ls file-[a-Z]
file-a file-A file-b file-z file-Z
匹配所有字母和数字
ls file-[[:alnum:]]
file-1 file-2 file-9 file-a file-A file-b file-z file-Z
{} 示例
ls file-{a,b,c}
#相当于
ls file-a file-b file-c
{1…5}表达一个范围
man手册
1–用户命令
5–文件格式(用于配置文件和结构)
8–系统管理和特权命令(维护任务)
-
/string 在man page中向前(向下)搜索string
-
n 在man page中重复之前的向前(向下)搜索
-
n 在man page中重复之前的向后(向上)搜索
-
g 转到man page的第一行
-
G 转到man page的最后一行
-
q 退出man,并返回shell命令提示符
使用root用户更新man page库
mandb
使用-k选项搜索关键字
man -k passwd
帮助方法总结:
- 通过软件包安装的第三方,如,man…
- shell自带的程序,如:help history
- 通用–help选项
which
查看文件所在位置
hich vi
alias vi='vim'
/usr/bin/vim
whereis
查看关键字对于的程序位置,帮助文档,源码
whereis man
man: /usr/bin/man /usr/share/man /usr/share/man/man1/man.1.gz /usr/share/man/man1p/man.1p.gz /usr/share/man/man7/man.7.gz