Linux查找文件内容的常用命令方法。
从文件内容查找匹配指定字符串的行:
$ grep "被查找的字符串" 文件名
例子:在当前目录里第一级文件夹中寻找包含指定字符串的.in文件
grep "thermcontact" */*.in
从文件内容查找与正则表达式匹配的行:
$ grep –e “正则表达式” 文件名
查找时不区分大小写:
$ grep –i "被查找的字符串" 文件名
查找匹配的行数:
$ grep -c "被查找的字符串" 文件名
从文件内容查找不匹配指定字符串的行:
$ grep –v "被查找的字符串" 文件名
学习更多linux知识《Linux就该这么学》,从根目录开始查找所有扩展名为.log的文本文件,并找出包含”ERROR”的行
find / -type f -name "*.log" | xargs grep "ERROR"
例子:从当前目录开始查找所有扩展名为.in的文本文件,并找出包含”thermcontact”的行
find . -name "*.in" | xargs grep "thermcontact"
2. 在linux中如何用命令查找文件在哪
需要准备的材料分别是:电脑、linux连接工具。
1、首先连接上linux主机,进入等待输入指令的linux命令行状态。
3. Linux中如何查找文件
查找文件使用find命令,命令格式:find 路径 -name 文件名
例如要在整个根目录下查找test.txt文件,find / -name test.txt,需要注意的是必须对要查找的那个目录有可读权限
以上就是关于Linux中查找文件的内容,学习软件测试也需要学习Linux,如果你想学,可以看黑马程序员视频库的视频哦!
4. 如何在linux中查看占用空间大文件和大文件夹
1.如何查找大文件
通过下面的命令我们可以查看当前目录下那些文件超过我们设定的阀值。
find -type f -size +100M -print0 | xargs -0 -h,其中100M就是我们设定的阀值,我们可以根据我们的需求来调整这个阀值。
如果我们想对结果做一个排序输出,我们可以运行下面的命令:find -type f -size +100M
-print0 | xargs -0 -h | sort -nr
2.如何查找大文件夹
通过下面的命令,我们可以查看当前目录下文件夹的大小
-h --max-depth=1,其中--max-depth是指文件夹的层级,例如1就是指当前目录下的文件夹,如果我们想对输出结果排序,可以使用下面的命令: -h --max-depth=1 | sort -nr,如果我们想对上面的结果只输出前n个结果,可以使用下面的命令: -h --max-depth=1
| sort -nr | head -n
5. Linux 查找文件用什么命令好
find:查找文件或目录所在路径
格式:find
[路径]
[表达式]
表达式:
-name
:查找名为filename的文件
-perm
:按执行权限来查找
-empty
:查找空文件或空目录
-user
:按文件属主来查找
-group
:按组来查找
-nogroup
:查无有效属组的文件,即文件的属组在/etc/groups中不存在
-nouser
:查无有效属主的文件,即文件的属主在/etc/passwd中不存
-mtime
:按文件更改时间来查找文件
-atime
:按文件访问时间来查找文件
-ctime
:按文件创建时间来查找文件
-newer
:查更改时间更新的文件或目录
-type
:查是块设备b、目录d、字符设备c、管道p、符号链接l、普通文件f
-size
n[c]
:查找大小为n块(512字节)[或n字节]的文件
-inum
:根据i节点查找
-depth
:使查找在进入子目录前先行查找完本目录
-fstype
:查位于某一类型文件系统中的文件,这些文件系统类型通常可
在/etc/fstab中找到
-mount
:查文件时不跨越文件系统mount点
-cpio
:对匹配的文件使用cpio命令,将他们备份到磁带设备中
-prune
:忽略某个目录
-maxdepth
:查询的目录深度
-exec
:查找文件并执行后面的命令
find
...
-exec
CMD
{}
\;
-ok
:询问是否要执行后面的命令
find
...
-ok
CMD
{}
\;
-perm
mode表示严格匹配
-perm
-mode
表示mode中转换成二进制的1必须全部匹配(不管0位)
-perm
+mode
表示mode中转换成二进制的1必须部分匹配(不管0位)
-ctime/atime/mtime/cmin/amin/mmin:按时间查找
以天为单位的:ctime、atime、mtime
以分钟为单位的:cmin、amin、mmin
c--change表示文件的属性被修改过
a--access
m--modify表示文件的内容被修改过
+n表示n天以前
-n表示n天以内
[root@rhel6
~]#
find
/etc/
-name
"host*"
"查询/etc/目录(包括子目录)中以host开头的文件或目录"
[root@rhel6
~]#
find
-type
l
"查询当前目录下文件类型为链接的文件"
[root@rhel6
~]#
find
-size
+10000000c
"查询当前目录中>10M的文件"
[root@rhel6
~]#
find
-size
-1K
"查询当前目录中<1K的文件"
[root@rhel6
~]#
find
/etc
-name
inittab
-o
-size
+17M
"查询/etc/目录中文件名为inittab或文件>17M的文件"
[root@rhel6
~]#
find
/etc
-name
"*.conf"
[-a]
-size
+20k
"查询/etc/目录中文件名为*.conf且文件<20k的文件"
[root@rhel6
~]#
find
/etc/*
-name
"*.conf"
-not
-name
"*http*"
"查询/etc目录中文件名为*.conf但不包含http的文件"
[root@rhel6
~]#
find
/etc/
-empty
"查询/etc/目录中的空文件或空目录"
[root@rhel6
~]#
find
/var
-user
Oracle
"查询/var/目录中属于用户oracle的文件或目录"
[root@rhel6
~]#
find
/home
-group
xfcy
[root@rhel6
~]#
find
-inum
1024
"查询当前目录中
i
节点为1024的文件或目录"
[root@rhel6
~]#
find
-newer
new
"查询当前目录中比文件new还新的文件或目录"
[root@rhel6
~]#
find
/etc/
-nouser
-o
-nogroup
"查询/etc/目录中不属于本地用户的文件或目录(危险文件)"
[root@rhel6
~]#
find
/data/
-mmin
-10
"查询/data/目录中十分钟内文件内容被修改过的文件"
[root@rhel6
~]#
find
/proc/
-type
f
-maxdepth
1
"查询/data/目录中文件类型为普通文件的文件且不查询子目录"
[root@rhel6
~]#
find
/data/
-mtime
-10
-exec
rm
{}
\;
"查询/data/目录中十分钟内内容被修改过的文件并将其删除"
[root@rhel6
~]#
find
/data/
-mtime
-10
-ok
rm
{}
\;
"查询/data/目录中十分钟内内容被修改过的文件并询问是否将其删除(y/n)"
6. 怎么样查看linux下占用空间最大的文件
主要使用find和命令来实现。
1、使用find命令找到大于指定大小的文件:
a.下例中查找大于10G的文件
[root@localhost data]# find / -type f -size +10G
将输出:
/usr/local/apache2/logs/access_log
b.通过命令查看此文件的大小:
[root@localhost data]# -h /usr/local/apache2/logs/access_log
24G /usr/local/apache2/logs/access_log
c.重复执行find命令来查找大文件,例如:find / -type f -size +5G 找到大于5G的文件。 find / -type f -size +1G 找到大于1G的文件
2、可以递归使用 --max-depth=1
7. linux系统中find命令怎么查找大文件
用-size参数,直接跟大小就行;
例如你要在根目录下找大于10M的文件
find/-size+10M
或者你要在/home目录下找大于10k的文件
find/home-size+10K
8. Linux中查找文件夹的命令
Linux中查找文件夹的命令是find命令。
Linux-文件搜索命令find的操作使用方法如下:
1、全盘搜索,也可以指定目录搜索。find搜索目录 -name目标名字,find / -name file
9. linux查找文件内容命令
搜索、查找文件当中的内容,一般最常用的是grep命令,另外还有egrep, vi命令也能搜索文件里面内容
1:搜索某个文件里面是否包含字符串,使用grep "search content" filename1, 例如
$ grep ORA alert_gsp.log
$ grep "ORA" alert_gsp.log
例如我们需要搜索、查找utlspadv.sql文件中包含ORA的字符内容
[oracle@DB-Server admin]$ grep "ORA" utlspadv.sql
-- ORA-XXXXX: Monitoring already started. If for example you want
-- ORA-20111:
-- ORA-20112:
-- ORA-20113: 'no active monitoring job found'
-- ORA-20113: 'no active monitoring job found'
-- 0 |<PS> =>DBS2.REGRESS.RDBMS.DEV.US.ORACLE.COM 0 0 2 99.3% 0% 0.7% ""
-- |<PR> DBS1.REGRESS.RDBMS.DEV.US.ORACLE.COM=> 100% 0% 0% "" |<PR> ...
-- =>DBS2.REGRESS.RDBMS.DEV.US.ORACLE.COM 92 7 99.3% 0% 0.7% "" |<PR> ...
-- |<C> CAPTURE_USER1=>DBS2.REGRESS.RDBMS.DEV.US.ORACLE.COM 2 0 0 0.E+00
-- |<C> CAPTURE_USER1=>DBS2.REGRESS.RDBMS.DEV.US.ORACLE.COM
-- ORA-20111:
-- ORA-20112:
-- ORA-20100:
-- ORA-20113: 'no active monitoring job found'
-- ORA-20113: 'no active monitoring job found'
[oracle@DB-Server admin]$
如上所示,这个是一个模糊匹配,其实我是想要查看ORA这类错误,那么我要过滤掉哪一些没有用的,搜索的内容修改一下即可(当然也可以使用特殊参数,后面有讲述),如下所示。
[oracle@DB-Server admin]$ grep "ORA-" utlspadv.sql
-- ORA-XXXXX: Monitoring already started. If for example you want
-- ORA-20111:
-- ORA-20112:
-- ORA-20113: 'no active monitoring job found'
-- ORA-20113: 'no active monitoring job found'
-- ORA-20111:
-- ORA-20112:
-- ORA-20100:
-- ORA-20113: 'no active monitoring job found'
-- ORA-20113: 'no active monitoring job found'
[oracle@DB-Server admin]$
7:有些场景,我们并不知道文件类型、或那些文件包含有我们需要搜索的字符串,那么可以递归搜索某个目录以及子目录下的所有文件
[oracle@DB-Server ~]$ grep -r "v$temp_space_header" /u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspace.sql:create or replace view v_$temp_space_header as select * from v$temp_space_header;
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspace.sql:create or replace public synonym v$temp_space_header for v_$temp_space_header;
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspace.sql:create or replace view gv_$temp_space_header as select * from gv$temp_space_header;
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspace.sql:create or replace public synonym gv$temp_space_header
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspace.sql: FROM gv$temp_space_header
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspacd.sql:drop public synonym v$temp_space_header;
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspacd.sql:drop public synonym gv$temp_space_header;
[oracle@DB-Server ~]$
8:如果我们只想获取那些文件包含搜索的内容,那么可以使用下命令
[oracle@DB-Server ~]$ grep -H -r "v$temp_space_header" /u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/ | cut -d: -f1
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspace.sql
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspace.sql
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspace.sql
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspace.sql
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspace.sql
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspacd.sql
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspacd.sql
[oracle@DB-Server ~]$ grep -H -r "v$temp_space_header" /u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/ | cut -d: -f1 | uniq
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspace.sql
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspacd.sql
[oracle@DB-Server ~]$
9:如果只想获取和整个搜索字符匹配的内容,那么可以使用参数w
你可以对比一下两者的区别
[oracle@DB-Server admin]$ grep -w "ORA" utlspadv.sql
-- ORA-XXXXX: Monitoring already started. If for example you want
-- ORA-20111:
-- ORA-20112:
-- ORA-20113: 'no active monitoring job found'
-- ORA-20113: 'no active monitoring job found'
-- ORA-20111:
-- ORA-20112:
-- ORA-20100:
-- ORA-20113: 'no active monitoring job found'
-- ORA-20113: 'no active monitoring job found'
[oracle@DB-Server admin]$ grep "ORA" utlspadv.sql
-- ORA-XXXXX: Monitoring already started. If for example you want
-- ORA-20111:
-- ORA-20112:
-- ORA-20113: 'no active monitoring job found'
-- ORA-20113: 'no active monitoring job found'
-- 0 |<PS> =>DBS2.REGRESS.RDBMS.DEV.US.ORACLE.COM 0 0 2 99.3% 0% 0.7% ""
-- |<PR> DBS1.REGRESS.RDBMS.DEV.US.ORACLE.COM=> 100% 0% 0% "" |<PR> ...
-- =>DBS2.REGRESS.RDBMS.DEV.US.ORACLE.COM 92 7 99.3% 0% 0.7% "" |<PR> ...
-- |<C> CAPTURE_USER1=>DBS2.REGRESS.RDBMS.DEV.US.ORACLE.COM 2 0 0 0.E+00
-- |<C> CAPTURE_USER1=>DBS2.REGRESS.RDBMS.DEV.US.ORACLE.COM
-- ORA-20111:
-- ORA-20112:
-- ORA-20100:
-- ORA-20113: 'no active monitoring job found'
-- ORA-20113: 'no active monitoring job found'
[oracle@DB-Server admin]$
10: grep命令结合find命令搜索
[oracle@DB-Server admin]$ find . -name '*.sql' -exec grep -i 'v$temp_space_header' {} ; -print
create or replace view v_$temp_space_header as select * from v$temp_space_header;
create or replace public synonym v$temp_space_header for v_$temp_space_header;
create or replace view gv_$temp_space_header as select * from gv$temp_space_header;
create or replace public synonym gv$temp_space_header
FROM gv$temp_space_header
./catspace.sql
drop public synonym v$temp_space_header;
drop public synonym gv$temp_space_header;
./catspacd.sql
[oracle@DB-Server admin]$
11: egrep -w -R 'word1|word2' ~/klbtmp
12: vi命令其实也能搜索文件里面的内容,只不过没有grep命令功能那么方便、强大。