Useful find Commands: find

hrdedicated
Site Admin
Posts: 50
Joined: Thu May 30, 2013 10:28 pm

Useful find Commands: find

Postby hrdedicated » Sat Jun 01, 2013 11:52 am

Useful find Commands:

Introduction:

The Linux find command is very powerful. It can search the entire file system for one or more files that you specify. It's an extremely helpful Linux command to understand -- especially when you need to find a file or directory.
Besides using the Linux find command to locate files, you can also perform some type of action after you've located the files you're interested in. With the find command, you can locate files using powerful search criteria, and then run any Unix command you want on the files you locate, as I'll show in the find command examples below.

1. Changing permission for file only

find . -type f -exec chmod 644 {} \;

2. for directory

find . -type d -exec chmod 775 {} \;

3. Search and list all files from current directory and down for the string ABC:

find ./ -name "*" -exec grep -H ABC {} \;

find ./ -type f -print | xargs grep -H "ABC" /dev/null

egrep -r ABC *

4.Find all files of a given type from current directory on down:

find ./ -name "*.conf" -print

5. Find all user files larger than 5Mb:

find /home -size +5000000c –print

6. Find all files owned by a user (defined by user id number. see /etc/passwd) on the system: (could take a very long time)

find / -user 501 -print

7. Find all files created or updated in the last five minutes: (Great for finding effects of make install)

find / -cmin -5

8. Find all users in group 20 and change them to group 102: (execute as root)

find / -group 20 -exec chown :102 {} \;

9. Find all suid and setgid executables:

find / \( -perm -4000 -o -perm -2000 \) -type f -exec ls -ldb {} \;
find / -type f -perm +6000 -ls

Note: suid executable binaries are programs which switch to root privileges to perform their tasks. These are created by applying a "sticky" bit: chmod +s. These programs should be watched as they are often the first point of entry for hackers. Thus it is prudent to run this command and remove the "sticky" bits from executables which either won't be used or are not required by users. chmod -s filename

10. Find all world writable directories:

find / -perm -0002 -type d -print

11. Find all world writable files:

find / -perm -0002 -type f -print
find / -perm -2 ! -type l -ls

12. Find files with no user:

find / -nouser -o -nogroup -print

13. Find files modified in the last two days:

find / -mtime 2 -o -ctime 2

14. Compare two drives to see if all files are identical:

find / -path /proc -prune -o -path /new-disk -prune -o -xtype f -exec cmp {} /new-disk{} \;

Return to “Hostripples dedicated Servers (Linux/Windows)”

Who is online

Users browsing this forum: No registered users and 3 guests