Linux基础01篇

本文主要介绍文件和目录、文件搜素相关的命令。

1. 文件和目录

  • cd命令
1
2
3
4
5
6
7
8
9
cd /进入系统根目录
# cd /
cd /home进入根下的home目录
# cd /home

cd ..进入当前目录的上一级目录
# cd ..
cd -进入上次所在的目录
# cd -
  • ls和pwd命令
1
2
3
4
5
ls注意ls的参数
# ls -a -l -d -h

pwd显示当前工作路径
# pwd
  • mkdir命令
1
2
3
4
mkdir  /test在根目录下创建test目录
# mkdir /test
mkdir -p /a/b/c/d创建多级目录,-p参数是不报错的
# mkdir -p /a/b/c/d
  • cp命令
1
2
3
4
cp file1 file2复制一个文件
# cp a.txt b.txt
cp -ar dir1 dir2 复制一个目录
# cp -ar /abc /test
  • mv命令
1
2
3
4
mv /opt/a /root/移动文件
# mv /opt/a /root/
mv /opt/a /root/移动文件
# mv /opt/a /root/b
  • touch命令
1
2
3
4
5
6
7
touch 1.txt创建文件
# touch 1.txt
# touch {a..z}
# touch {a..z..2}
# touch a{1..5}
# touch a{1..5}.txt
# touch {1..5}{a..f}
  • file命令
1
2
查看文件的类型
# file /dev/sda
  • tree命令
1
2
3
显示文件和目录由根目录开始的树形结构
# yum install tree
# tree –L 1

2. 文件搜素

  • whereis和which命令
1
2
3
查找二进制文件
# which cat
# whereis whereis
  • find命令

按名称查找

1
2
3
4
# find /etc/ -name grub.conf
# find /etc/ -name "*.conf"
# find /etc/ -name ".*"
# find /test/ -iname Abc

按类型查找

1
2
3
4
5
6
7
# find / -type f
# find / -type b
# find / -type s
# find / -type c
# find / -type p
# find / -type d
# find / -type l

按大小查找

1
2
3
4
5
6
# find / -size +500M
# find / -size +1G
# find / -size +50k
# find / -size -1M
# find / -size +3c
# find / -size +80M -size -100M

按时间查找

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# stat 1 |tail -3
Access: 2014-07-23 11:56:42.297572398 +0800 --atime 阅读过,用cat,tail,head,more,less命令等或者vi访问过,但没有修改;执行过也会改变
Modify: 2014-07-23 11:56:44.836572907 +0800--mtime 修改过内容,用vi修改过或者echo一个值重定向
Change: 2014-07-23 11:56:44.856885177 +0800 —ctime 改变过内容,属主,属组,权限,创建软链接,硬链接等
找出 3 天”以前”被改动过的文件(> 72 小时)
find /var/log/ -mtime +3 -type f
找出 3 天内被改动过的文件(0 ~ 72 小时内)
find /var/log/ -mtime -3 -type f
找出前第 3 天被改动过的文件(72 ~ 96 小时)
find /var/log/ -mtime 3 -type f
find /var/log/ -mtime +2 -mtime -4 -type f
在20-50天内修改过的文件
find ./ -mtime +20 -a -mtime -50 -type f
在2018-04-11当天修改的文件
find / -type f -newermt '2018-04-11 00:00' -a -not -newermt '2018-04-11 23:56'

  • locate命令

速度快,通过系统带的一个数据库去查找,数据是非实时的。
速度快,通过系统带的一个数据库去查找,数据是非实时的。
系统每天自动更新一次,是通过时间任务执行的:/etc/cron.daily/mlocate.cron
/usr/bin/updatedb主要用来更新数据库,通过crontab自动完成的
/usr/bin/locate 查询文件位置
/etc/updatedb.confupdatedb的配置文件
/var/lib/mlocate/mlocate.db 存放文件信息的文件

1
# locate passwd

---------------- The End ----------------