find

get list of subdirectories containing file

approach#1
$ find . -type f -name '*f*' | grep -o "\(.*\)/" | sort -u | head -5 ./dir1/dir103/ ./dir1/dir104/ ./dir1/dir105/ ./dir1/dir106/ ./dir1/dir107/

approach#2
$ find . -type f -name '*f*' -exec dirname {} \; | sort -u | head -5 ./dir1/dir103 ./dir1/dir104 ./dir1/dir105 ./dir1/dir106 ./dir1/dir107

---------
Find all txt and copy to newfolder
find . -name "*.txt" -exec cp {} newfolder \;
-------

$ find ./test -name "*.php"
./test/subdir/how.php
./test/cool.php
To ignore the case, just use the "iname" option instead of the "name" option.

$ find ./test -iname "*.Php"
./test/subdir/how.php
./test/cool.php
For example we don't want to go more than 2 or 3 levels down in the sub directories. This is done using the maxdepth option.

$ find ./test -maxdepth 2 -name "*.php"
./test/subdir/how.php
./test/cool.php


$ find ./test -not -name "*.php"
./test
./test/abc.txt
./test/subdir


$ find ./test -name 'abc*' ! -name '*.php'
./test/abc.txt
./test/abc
OR operator

$ find -name '*.php' -o -name '*.txt'
./abc.txt
./subdir/how.php
./abc.php
./cool.php


Only files

$ find ./test -type f -name "abc*"
./test/abc.txt

Only directories

$ find ./test -type d -name "abc*"
./test/abc

multiple directiries

$ find ./test ./dir2 -type f -name "abc*"
./test/abc.txt
./dir2/abcdefg.txt
find hidden files

$ find ~ -type f -name ".*"
permission

$ find . -type f -perm 0664
./abc.txt
./subdir/how.php
./abc.php
./cool.php

Find files with sgid/suid bits set

The "perm" option of find command accepts the same mode string like chmod. The following command finds all files with permission 644 and sgid bit set.

# find / -perm 2644


Find all Read Only files.

$ find /etc -maxdepth 1 -perm /u=r

$ find . -user bob
To find all the files which are modified 50 days back.

# find / -mtime 50
Find files modified within the last 1 hour.

$ find /home/bob -cmin -60
To find all the files which are modified in last 1 hour.

# find / -mmin -60
To find all the files which are greater than 50MB and less than 100MB.

$ find / -size +50M -size -100M
The following command will remove all text files in the tmp directory.

$ find /tmp -type f -name "*.txt" -exec rm -f {} \;


-------
[t@3be6a53b1e76 t]$ find . -name 'fil*'                                                                                
./file1                                                                                                                
./file2                                                                                                                
./filess    
-----------
find / -name *.tar - find all files with extension .tar
------------

find all directories
bash-3.00# find . -type d -print

find files owned by user1
bash-3.00# find . -type f -user user1 -print

all file txt to t2.txt file
find . -type f -name "*.txt" -exec cat {} \; > t2.tx2

copy all txt to OLD folder
bash-3.00# find . -type f -name "*.txt" -exec cp {} OLD \;

zip all txt in path
bash-3.00# find /tmp/test -type f -name "*.txt" -exec gzip {} \;

-----------------------------
find . -type d | grep -i _c | xargs chmod 777

найти все папки, имена которых заканчиваются на _c, начиная с текущей директории и во всех вложенных директориях, и выполнить для них команду chmod 777 (это полезно для работы Smarty, если шаблоны, которые компилируются, хранятся в разных местах)

find . | xargs chown user:group
изменить права владения всех вложенных файлов и папок начиная с текущей

find . -type f | grep -i .txt | xargs chmod 666
найти все файлы с расширением .txt, начиная с текущей директории и во всех вложенных директориях, и выполнить для них команду chmod 666
-----------------------------
prints files and files in folders
bash-3.00# find . -print

./2.txt
./1.txt
./test
./test/file1
./test/file2
./test/file3

[t@3be6a53b1e76 t]$ ps -ef                                                                                                
UID        PID  PPID  C STIME TTY          TIME CMD                                                                       
root         1     0  0 06:35 ?        00:00:02 --CODINGGROUND--                                                          
root         8     1  0 06:36 pts/0    00:00:00 sh                                                                        
root        17     8  0 06:36 pts/0    00:00:00 /bin/bash                                                                 
root        31    17  0 07:13 pts/0    00:00:00 su t                                                                      
t           32    31  0 07:13 pts/0    00:00:00 bash                                                                      
t           81    32  0 07:23 pts/0    00:00:00 ps -ef                                                                    
[t@3be6a53b1e76 t]$ ps -ef | awk '{print $2}'                                                                             
PID                                                                                                                       
1                                                                                                                         
8                                                                                                                         
17                                                                                                                        
31                                                                                                                        
32                                                                                                                        
82                                                                                                                        
83                                                                                                                        
[t@3be6a53b1e76 t]$  

Comments

Popular posts from this blog

HAproxy logging

tomcat catalina coyote jasper cluster

NFS mount add in fstab _netdev instead of default | firewall-cmd --list-all