find

Find files and folders and optionally do something with them

Find how many files with specific user/group ownership and permissions are in current directory and subfolders:

find . -user USERNAME -group GROUPNAME -perm 644 |wc -l

Find FILES with specific permissions in current directory and subfolders:

find . -type f -perm 640

Change file owner and group for files/directories which group membership is oldgroup

find . -group oldgroup -exec chown newuser:newgroup '{}' \;

Find and delete files/directories which are over 62 days old

find . -mtime +62 -exec rm '{}' \; > /dev/null 2>&1 

Find and delete files not having .gz extension

find /foo/ -type f ! -name "*.gz" -exec rm -f '{}' \;

Find files with SUID bit

find . -perm -4000
or
find . -perm -4000 -type f

find files and print only file name instead of full path  – use formating with printf

find . -type f -printf '%f\n'

there are many formating options, just run man find and search -printf in there.

Note – always use single quotes around parenthesis when using -exec like so: -exec rm ‘{}’

find files and print out file size, permissions, owner ID and last modification time in seconds from unix epoch along with the files md5 hash

find . -mount -type f -printf "%k %U %m %T@ " -exec md5sum {} \;

-mount will not descend directories on other filesystems. Its useful if you have mounted other filesystems, especially network shares to the directory tree.

Find files with suid bit which are owned by root, executable for group or all, from all local filesystems. Find runs against one local filesystem at the time not decending to other filesystems.


# for fs in `findmnt -it sysfs,cgroup,proc,devtmpfs,devpts,pstore,debugfs,hugetlbfs,mqueue,configfs,autofs,tmpfs,securityfs,sunrpc,fusectl,binfmt_misc,nfs,cifs -o SOURCE,TARGET -n -l |grep "^/"|awk '{print $2}'`; do find ${fs} -type f -perm /4000 \( -perm -g=x -o -perm -o=x \) -mount -uid 0 2>/dev/null ;done

Find files with SUID or SGID bits from all listed filesystems, print out file permissions, user id group id and file name. This is another way how to find files from all local filesystems. Just include them in the list, currently onlyt xfs, ext2/3/4 are there. This example I aklso used mount to list filesystems instead of findmnt.

# for mount in `for t in xfs ext2 ext3 ext4; do mount -l -t ${t};done |awk '{print $3}'`; do find ${mount} -xdev -type f \( -perm -4000 -o -perm -2000 \) -exec stat -c "%a %u %g %n" '{}' \; 2>/dev/null ;done

If you found this useful, say thanks, click on some banners or donate, I can always use some beer money.