http://crybit.com/find-command-usage-with-example-unixlinux/

find command is one of the best search tool under UNIX/LINUX. Here I’m discussing some common switches of find command with detailed example. Like the name find, the “find” command is using for search files under a directory hierarchy. One simle example is shown below,
find / name linux ;
here the second part that means “/” has an important role in the find command syntax. This is the path for searching the file having name linux. This command will return the file linux if it is exist under “/” .

Numeric arguments

+n >> for greater than n,
-n >> for less than n,
n >> for exactly n.

Switchs and usage:

1. -name pattern ==> Find the matched name pattern.

1.1 -iname pattern ==> Like -name, but the match is case insensitive.
Examples;

# find / -name test123.txt
/home/***/crybit/test123.txt # find / -iname TeSt123.txt
/home/***/crybit/test123.txt # find / -iname TeSt***.txt
/home/***/crybit/test123.txt # find / -name te**.txt
/home/***/crybit/test123.txt

2. -path pattern ==> It will list out the exact path if it is exist.

Examples,

# find / -path "/e**wd"
/etc/pam.d/chpasswd
/etc/pam.d/passwd
/etc/cron.daily/passwd
/etc/passwd
/etc/security/opasswd
............
# find / -path "/us**.conf"
/usr/share/onboard/onboard-defaults.conf
/usr/share/popularity-contest/default.conf
/usr/share/base-files/nsswitch.conf
/usr/share/samba/smb.conf
............
# find / -path "/us**.sh"
/usr/share/onboard/scripts/changekbd.sh
/usr/share/alsa-base/alsa-info.sh
/usr/share/libreoffice/shell-lib-extensions.sh
/usr/share/debconf/confmodule.sh
............

3. -perm mode ==> File’s permission bits are exactly mode (octal or symbolic).

Example;
# ll
total 8
drwxrwxr-x 2 root root 4096 Sep 5 20:37 ./
drwxr-xr-x 34 root root 4096 Sep 5 19:52 ../
-rwxrwxrwx 1 root eclinux 0 Sep 5 20:37 test1234.txt*
-rwx–x–x 1 root root 0 Sep 5 20:37 test1235.txt*
-rw-rw-r– 1 root root 0 Sep 5 20:38 test123.txt

# find ./ -perm 664
./test123.txt
{./ is the path for searching(current directory). This will find out the file having permission 664}

3.1 -readable >> Matches files which are readable.
3.2 -writable >> Matches files which are writable.
3.3 -executable >> Matches files which are executable.

Example;

# find ./ -executable
./
./test1235.txt
./test1234.txt

4. -gid & -uid

4.1 -gid n >> File's numeric group ID is n.
4.2 -group gname >> File belongs to group gname (numeric group ID allowed).
4.3 uid n >> File's numeric user ID is n.
4.4 -user name >> File belongs to user name (numeric user ID allowed).

Examples;

# ll
total 8
drwxrwxr-x 2 root root 4096 Sep 5 20:37 ./
drwxr-xr-x 34 root root 4096 Sep 5 19:52 ../
-rwxrwxrwx 1 root eclinux 0 Sep 5 20:37 test1234.txt*
-rwx--x--x 1 root root 0 Sep 5 20:37 test1235.txt*
-rw-rw-r-- 1 root root 0 Sep 5 20:38 test123.txt # find ./ -gid 1003
./test1234.txt
# find ./ -group eclinux
./test1234.txt

Similarly we can use -uid & -user.

5. -empty : this will find all files having empty content.

Example;

# find ./ -empty
./test1235.txt
./test1234.txt
./test123.txt

6. -size n[cwbkMG] ==> File uses n units of space. The following suffixes can be used:

'b' for 512-byte blocks (this is the default if no suffix is used)
'c' for bytes
'w' for two-byte words
'k' for Kilobytes (units of 1024 bytes)
'M' for Megabytes (units of 1048576 bytes)
'G' for Gigabytes (units of 1073741824 bytes)

7. -type ==> Specify the file type.

b block (buffered) special
c character (unbuffered) special
d directory
p named pipe (FIFO)
f regular file
l symbolic link
s socket
D door (Solaris)

Example;

# find ./ -type f
./test1235.txt
./test1234.txt
./test123.txt

8. Switches related to modification time

8.1 -amin n >> File was last accessed n minutes ago.
8.2 -atime n >> File was last accessed n*24 hours ago.
8.3 -cmin n >> File's status was last changed n minutes ago.
8.4 -ctime n >> File's status was last changed n*24 hours ago.
8.5 -mmin n >> File's data was last modified n minutes ago.
8.6 -mtime n >> File's data was last modified n*24 hours ago.

Example;

# find ./ -mmin +1
./test1235.txt
./test1234.txt

9. inode & links

9.1 -inum n >> File has inode number n.
9.2 -samefile name >> File refers to the same inode as name.
9.3 -links n >> File has n links.

Example;

ls -i to find out the inode number.
# ls -i test123.txt
1316256 test123.txt
# find ./ -inum 1316256
./test123.txt # ll
total 8
drwxrwxr-x 2 root root 4096 Sep 5 20:37 ./
drwxr-xr-x 34 root root 4096 Sep 5 19:52 ../
-rwxrwxrwx 1 root eclinux 0 Sep 5 20:37 test1234.txt*
-rwx--x--x 1 root root 0 Sep 5 20:37 test1235.txt*
-rw-rw-r-- 1 root root 0 Sep 5 20:38 test123.txt

# find ./ -links 1

./test1235.txt
./test1234.txt
./test123.txt

All three files having single links.

10. -delete & -exec operations

10.1 -delete : This switch is use to remove a particular that already specified in the find command. Use this switch with extra care.

Example;

# find ./ -inum 1316256
./test123.txt
# find ./ -inum 1316256 -delete
# find ./ -inum 1316256

In this case, -delete switch remove the file test123.txt . Similarly we can remove anything that found by find command.

10.2 -exec : This will execute commands on the find syntax.

Example;

# ll
total 8
drwxrwxr-x 2 root root 4096 Sep 5 20:37 ./
drwxr-xr-x 34 root root 4096 Sep 5 19:52 ../
-rwxrwxrwx 1 root eclinux 0 Sep 5 20:37 test1234.txt*
-rwx--x--x 1 root root 0 Sep 5 20:37 test1235.txt*
-rw-rw-r-- 1 root root 0 Sep 5 20:38 test123.txt

Run the command to change the permission.

# find ./ -type f -exec chmod 777 {} \;
# ll
total 8
drwxrwxr-x 2 root root 4096 Sep 5 20:37 ./
drwxr-xr-x 34 root root 4096 Sep 5 19:52 ../
-rwxrwxrwx 1 root eclinux 0 Sep 5 20:37 test1234.txt*
-rwxrwxrwx 1 root root 0 Sep 5 20:37 test1235.txt*
-rwxrwxrwx 1 root root 0 Sep 5 20:38 test123.txt*

the chmod command after -exec in find command change the file permission to 777.

# find ./ -type f -exec rm -rf {} \;

This will remove all files in the current working directory.

I think this article gave some ideas about the usages of find command under UNIX/LINUX to you.
Thank you for your time.

More:
groupdelgroupmemsgroupmoduseraddusermodchgrpchownlsheadtailtoppsfind,crontab

10+ commonly using find command switches with example Unix/Linux的更多相关文章

  1. 18 Command Line Tools to Monitor Linux Performance

    By Ravi Saive Under: Linux Commands, Monitoring Tools On: December 26, 2013 http://www.tecmint.com/c ...

  2. scrapy常用命令(持续) | Commonly used Scrapy command list (con't)

    以下命令都是在CMD中运行,首先把路径定位到项目文件夹 ------------------------------------------------------------------------ ...

  3. 轻松看懂机器学习十大常用算法 (Machine Learning Top 10 Commonly Used Algorithms)

    原文出处: 不会停的蜗牛    通过本篇文章可以对ML的常用算法有个常识性的认识,没有代码,没有复杂的理论推导,就是图解一下,知道这些算法是什么,它们是怎么应用的,例子主要是分类问题. 每个算法都看了 ...

  4. python常用命令(持续) | Commonly used Python command list (con't)

    ---------------------------------------------------------------------------------------------------- ...

  5. 15+ tar command usages with examples – Unix/Linux--reference

    reference :http://crybit.com/tar-command-usages-with-examples/ The ‘tar’ saves many files together i ...

  6. Deploy Oracle 10.2.0.5 DataGuard on Red Hat Enterprise Linux 6.4

    系统:Red Hat Enterprise Linux 6.4 数据库:Oracle 10.2.0.5.0 Patch Set 4 主机:10dg1 192.168.1.91 10dg2192.168 ...

  7. 10 Useeful Tips for Writing Effective Bash Scripts in Linux

    1.Always Use Comments in Scripts2.Make a Scripts exit When Fails    Sometimes bash may continue to e ...

  8. [Practical Git] Navigate git command pager output with Unix less commands

    When using a git command that can have a large amount of output (like git log, git diff, or git blam ...

  9. Unix/Linux环境C编程入门教程(10) SUSE Linux EnterpriseCCPP开发环境搭建

    安装SUSE企业版以及搭建C/C++开发环境 1.      SUSELinux Enterprise是一款服务器操作系统,异常稳定. 2.设置虚拟机类型. 3.选择稍后安装操作系统. 4.选择SUS ...

随机推荐

  1. 在ASM中移动数据文件

    实验目的:在ASM存储环境下,要删除一个磁盘组,从而将磁盘组中的数据文件移动到另外一个磁盘组中. 查看数据文件存放的位置: SQL> select file#,name from v$dataf ...

  2. ORA-15005: name "orcl" is already used by an existing alias

    在进行ASM操作的时候,如果目录不存在的话,那么可能会报如下的错误: <pre name="code" class="plain">RMAN> ...

  3. Jquery类级别与对象级别插件开发

    jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...

  4. Spark RDD概念学习系列之RDD的重要内部属性(十五)

    RDD的重要内部属性 通过 RDD 的内部属性,用户可以获取相应的元数据信息.通过这些信息可以支持更复杂的算法或优化. 1)分区列表:通过分区列表可以找到一个 RDD 中包含的所有分区及其所在地址. ...

  5. iOS图形处理和性能

    转自陶丰平的博客   原文的题目是Designing for iOS: Graphics & Performance,晚上花了两个不到小时大致翻译了下. ---Begin--- 在之前的文章里 ...

  6. ocp 1Z0-047 61-130题解析

    61. Evaluate the following SQL statements that are issued in the given order:CREATE TABLE emp(emp_no ...

  7. MSGPACK(一)

    MSGPACK跨平台的数据序列规范,为多种语言所支持.用它序列还是还原数据都异常方便. 而且它支持序列的数据格式非常之多,因为它支持的数据格式多,所以MSGPACK的第二功用:缓存. DELPHI的M ...

  8. VC的文件操作

    各种关于文件的操作在程序设计中是十分常见,如果能对其各种操作都了如指掌,就可以根据实际情况找到最佳的解决方案,从而在较短的时间内编写出高效的代码,因而熟练的掌握文件操作是十分重要的.本文将对Visua ...

  9. Check Box 用法

    void CMyDlg::OnInitDialog() //Check1 初始化为选中状态 void CMyDlg::OnInitDialog() { CDialog::OnInitDialog(); ...

  10. C# 调用第三方DLL完整实例

    C# 调用第三方DLL完整实例 分类: C/C++ 以下代码为本人在实际项目中编写的调用第三方DLL接口程序的完整代码. public class ExecuteDLL : Form { ...//忽 ...