Introduction
The GNU find command searches files within a directory and its subdirectories according to several criteria such as name, size and time of last read/write. By default find prints the name of the located files but it can also perform commands on these files.
The GNU find command is part of the GNU findutils and is installed on every Ubuntu system. findutils is actually made up of 4 utilities:
find - search for files in a directory hierarchy
locate - list files in databases that match a pattern
updatedb - update a file name database
xargs - build and execute command lines from standard input
This wiki page will be only be dealing with find while also briefly mentioning xargs. Hopefully locate and updatedb will be covered on their own page in the near future.
The Basics
The syntax for using find is:
find [-H] [-L] [-P] [path...] [expression]
The 3 options [-H] [-L] [-P] are not commonly seen but should at least be noted if only to realise that the -P option will be the default unless another option is specified:
-H : Do not follow symbolic links, except while processing the command line arguments.
-L : Follow symbolic links.
-P : Never follow symbolic links: the default option.
The option [path...] refers to the particular location that you wish to search, whether it be your $HOME directory, a particular directory such as /usr, your present working directory which can simply be expressed as '.' or your entire computer which can be expressed as '/'.
The option [expression] refers to one or a series of options which effect the overall option of the find command. These options can involve a search by name, by size, by access time or can also involve actions taken upon these files.
Locating Files by Name
The most common use of find is in the search for a specific file by use of its name. The following command searches the home directory and all of its subdirectories looking for the file mysong.ogg:
find $HOME -name 'mysong.ogg'
It is important to get into the habit of quoting patterns in your search as seen above or your search results can be a little unpredictable. Such a search can be much more sophisticated though. For example if you wished to search for all of the ogg files in your home directory, some of which you think might be named 'OGG' rather than 'ogg', you would run:
find $HOME -iname '*.ogg'
Here the option '-iname' performs a case-insensitive search while the wildcard character '*' matches any character, or number of characters, or zero characters. To perform the same search on your entire drive you would run:
sudo find / -iname '*.ogg'
This could be a slow search depending on the number of directories, sub-directories and files on your system. This highlights an important difference in the way that find operates in that it examines the system directly each time unlike programs like locate or slocate which actually examine a regularly updated database of filnames and locations.
Locating Files by Size
Another possible search is to search for files by size. To demonstrate this we can again search the home directory for Ogg Vorbis files but this time looking for those that are 100 megabytes or larger:
find $HOME -iname '*.ogg' -size +100M
There are several options with -size, I have used 'M' for 'megabytes' here but 'k' for 'kilobytes' can be used or 'G' for 'Gigabytes'. This search can then be altered to look for files only that are less than 100 megabytes:
find $HOME -iname '*.ogg' -type f -size -100M
Are you starting to see the power of find, and the thought involved in constructing a focused search? If you are interested there is more discussion of these combined searches in the Advanced Usage section below.
Locating Files by Access Time
It is also possible to locate files based on their access time or the time that they were last used, or viewed by the system. For example to show all files that have not been accessed in the $HOME directory for 30 days or more:
find $HOME -atime +30
This type of search is normally more useful when combined with other find searches. For example one could search for all ogg files in the $HOME directory that have an access time of greater than 30 days:
find $HOME -iname '*.ogg' -atime +30
The syntax works from left to right and by default find joins the 2 expressions with an implied "and". This is dealt with in more depth in the section below entitled "Combining Searches".
Advanced Usage
The sections above detail the most common usage of find and this would be enough for most searches. However there are many more possibilities in the usage of find for quite advanced searches and this sections discusses a few of these possibilities.
Combining Searches
It is possible to combine searches when using find with the use of what is known in the find man pages as operators. The classic example is the use of a logical AND syntax:
find $HOME -iname '*.ogg' -size +20M
This find search performs initially a case insensitive search for all the ogg files in your $HOME directory and for every true results it then searches for those with a size of 20 megabytes and over. This contains and implied operator which could be written joined with an -a. This search can be altered slightly by use of an exclamation point to signify negation of the result:
find $HOME -iname '*.ogg' ! -size +20M
This performs the same search as before but will look for ogg files that are not greater than 20 megabytes. It is possible also to use a logical OR in your find search:
find $HOME -iname '*.ogg' -o -iname '*.mp3'
This will perform a case insensitive search in the $HOME directories and find all files that are either ogg OR mp3 files. There is great scope here for creating very complex and finely honed searches and I would encourage a through reading of the find man pages searching for the topic OPERATORS.
Acting On The files
One advanced but highly useful aspect of find usage is the ability to perform a user-specified action on the result of a find search. For example the following search looks for all ogg vorbis files in the $HOME directory and then uses -exec to pass the result to the du program to give the size of each file:
find $HOME -name '*.ogg' -type f -exec du -h '{}' \;
This syntax is often used to delete files by using -exec rm -rf but this must be used with great caution, if at all, as recovery of any deleted files can be quite difficult.
Using xargs
When using a really complex search it is often a good idea to use another member of the findutils package: xargs. Without its use the message Argument list too long could be seen signalling that the kernel limit on the combined length of a commandline and its environment variables has been exceeded. xargs works by feeding the results of the search to the subsequent command in batches calculated on the system capabilities (based on ARG_MAX). An example:
find /tmp -iname '*.mp3' -print0 | xargs -0 rm
This example searches the /tmp folder for all mp3 files and then deletes them. You will note the use of both -print0 and xargs -0 which is a deliberate strategy to avoid problems with spaces and/or newlines in the filenames. Modern kernels do not have the ARG_MAX limitation but to keep your searches portable it is an excellent idea to use xargs in complex searches with subsequent commands.
相关推荐
此外,了解如何使用“grep”搜索文件内容,以及“find”查找文件,都是十分重要的技能。 接下来,我们来讨论文件系统管理。在Linux中,一切皆为文件,包括硬件设备。理解文件系统的层级结构(如/home、/usr、/etc、...
第一步,打开Ubuntu Software下载VSCode。(so easy) 官网地址:https://code.visualstudio.com/docs/?dv=linux64_deb 然后使用 sudo dpkg -i xxx.deb解压即可 第二步,如果你的电脑中没有gcc,g++,gdb的话,可以...
通过调整`configure`参数,比如`--disable-decoder=xxx`或`--disable-encoder=xxx`,可以去除不使用的模块。 9. **注意事项**:在某些Android版本中,可能需要开启硬件加速,如使用MediaCodec API,这需要在`...
# The primary network interface - use DHCP to find our address auto eth0 iface eth0 inet dhcp ``` 使设置生效: ```bash sudo service networking restart ``` **方法2:自定义静态IP地址** 同样编辑`/etc/...
光驱–iso9660,支持中文名:mount -o iocharset=x /dev/hdax/mnt/目录名,挂载光驱:mount -t auto /dev/cdrom /mnt/cdrom,挂载 ISO 文件:mount -t iso9660 -o loop xxx.iso /path * 解除挂载:umount /mnt/目录...
- 使用 `find src -type f -exec iconv -f GBK -t UTF-8 -o {}.utf8 {} \;` 命令批量转换文件编码。 **10.4 转换文件内容由GBK到UTF8** - 使用 `iconv -f GBK -t UTF-8 <file> > newfile` 命令转换文件编码。 **...
for f in `find . | grep '\.lua$'`; do f2=`echo $f | sed 's/lua$/luac/g'`; ln -sf `basename $f` $f2; done ``` #### 安装VLC 完成上述步骤后,VLC应该已经被成功编译。最后一步是将其安装到系统中: ```...
find . -type f | xargs grep "raw.githubusercontent" 修改下列文件的 URL 内容: ./rosdistro/__init__.py、./rosdep2/gbpdistro_support.py、./rosdep2/sources_list.py、./rosdep2/rep3.py 将 raw....
- `apt`/`apt-get` (Debian/Ubuntu):安装、更新、卸载软件包。 - `yum` (RHEL/CentOS):同上功能。 - `dnf` (Fedora):同上功能。 7. 脚本编写: - `bash`:Bourne Again Shell,Linux默认的shell,用于编写...
`find . -type f -name "*pattern*"`可以查找包含特定字符串的文件名。 **11.6 快速查找某个文件** `locate <filename>`命令可以快速查找文件的位置。 **11.7 创建两个空文件** 使用`touch file1 file2`可以创建两...
这只是Ubuntu常用命令的一部分,还有许多其他命令如`cp`(复制文件或目录),`mv`(移动或重命名文件/目录),`rm`(删除文件或目录),`mkdir`(创建目录),`rmdir`(删除空目录),`find`(搜索文件),`less`...
批量转换文件内容编码,可以使用`find`结合`iconv`命令。 ##### 10.4 转换文件内容由GBK到UTF8 转换单个文件的内容编码,可以直接使用`iconv`命令。 ##### 10.5 转换MP3标签编码 转换MP3标签编码,可以使用`mid3...
Ubuntu常用命令大全 本文档总结了 Ubuntu 中常用的命令,涵盖了文件和文件夹管理、系统管理、打包和解压、make 编译、apt 命令等多个方面。 一、 文件 / 文件夹管理 ...* dpkg -L xxx 命令:查找软件 xxx 安装内容
使用`dpkg -L xxx`可以列出已安装软件`xxx`的所有文件和它们在系统中的位置。 2. **查看显卡使用情况**: `nvidia-smi`是NVIDIA提供的工具,用于检查GPU的状态,包括显存使用、温度、风扇速度以及正在运行的GPU...
img_tags = soup.find_all('img', attrs={'src': True}) for img in img_tags: img_url = img['src'] print(img_url) ``` 4. **下载图片**: 将找到的图片链接保存到本地。创建一个函数,接收图片URL和保存...
1、Ubuntu 7.0.4发行版 2、u-boot-1.3.1 3、FS2410平台 4、交叉编译器 arm-softfloat-linux-gnu-gcc-3.4.5 【实验步骤】 一、建立自己的平台类型 (1)解压文件 #tar jxvf u-boot-1.3.1.tar.bz2 (2)进入 U...
3. **CMake命令**:CMake有许多内置命令,如`project`用于定义项目,`add_executable`和`add_library`用于创建可执行文件或库,`include_directories`添加头文件路径,`find_package`寻找依赖的库,`target_link_...
包含如下操作系统版本 FreeBSD Linux Solaris Windows 分别对应如下目录 MegaCLI for DOS MegaCLI for Linux MegaCLI for Solaris MegaCLI for FreeBSD MegaCLI for Windows ********************************...
它还有丰富的模块(如FindXXX模块)来帮助找到系统中的库,简化依赖项的处理。 在Linux环境中,CMake通常与`make`一起使用,但也可以与其他构建工具(如Ninja)结合,以提高构建速度。CMake的灵活性和跨平台性使其...