Find Command

From rbachwiki
Jump to navigation Jump to search

Set File And Directory Permissions using Find Command

This wll set all the directores to 755 and all the files to 644

find  -type d -print | xargs chmod 755
find  -type f -print | xargs chmod 644

printf format specifiers

%s - files size in bytes
%p Files name
%n - number of hard links to file
%P files name with the name of the starting point under which it was found removed
%t files last modification time in the format returned by the ctime function
%f file name
% M files permission

Find Command

Part I – Basic Find Commands for Finding Files with Names

1. Find Files Using Name in Current Directory Find all the files whose name is tecmint.txt in a current working directory.

# find . -name tecmint.txt

./tecmint.txt 2. Find Files Under Home Directory

Find all the files under /home directory with name tecmint.txt.

# find /home -name tecmint.txt

/home/tecmint.txt

3. Find Files Using Name and Ignoring Case

Find all the files whose name is tecmint.txt and contains both capital and small letters in /homedirectory.

# find /home -iname tecmint.txt

./tecmint.txt ./Tecmint.txt

4. Find Directories Using Name

Find all directories whose name is Tecmint in / directory.

# find / -type d -name Tecmint

/Tecmint 5. Find PHP Files Using Name Find all php files whose name is tecmint.php in a current working directory.

# find . -type f -name tecmint.php

./tecmint.php

6. Find all PHP Files in Directory

Find all php files in a directory.

# find . -type f -name "*.php"

./tecmint.php ./login.php ./index.php Part II – Find Files Based on their Permissions 7. Find Files With 777 Permissions Find all the files whose permissions are 777.

# find . -type f -perm 0777 -print

8. Find Files Without 777 Permissions

Find all the files without permission 777.

# find / -type f ! -perm 777

9. Find SGID Files with 644 Permissions

Find all the SGID bit files whose permissions set to 644.

# find / -perm 2644

10. Find Sticky Bit Files with 551 Permissions

Find all the Sticky Bit set files whose permission are 551.

# find / -perm 1551

11. Find SUID Files

Find all SUID set files.

# find / -perm /u=s

12. Find SGID Files Find all SGID set files.

# find / -perm /g+s

13. Find Read Only Files Find all Read Only files.

# find / -perm /u=r

14. Find Executable Files Find all Executable files.

# find / -perm /a=x

15. Find Files with 777 Permissions and Chmod to 644 Find all 777 permission files and use chmod command to set permissions to 644.

# find / -type f -perm 0777 -print -exec chmod 644 {} \;

16. Find Directories with 777 Permissions and Chmod to 755 Find all 777 permission directories and use chmod command to set permissions to 755.

# find / -type d -perm 777 -print -exec chmod 755 {} \;

17. Find and remove single File To find a single file called tecmint.txt and remove it.

# find . -type f -name "tecmint.txt" -exec rm -f {} \;

18. Find and remove Multiple File To find and remove multiple files such as .mp3 or .txt, then use.

# find . -type f -name "*.txt" -exec rm -f {} \;

OR

# find . -type f -name "*.mp3" -exec rm -f {} \;

19. Find all Empty Files To file all empty files under certain path.

# find /tmp -type f -empty

20. Find all Empty Directories To file all empty directories under certain path.

# find /tmp -type d -empty

21. File all Hidden Files To find all hidden files, use below command.

# find /tmp -type f -name ".*"

Part III – Search Files Based On Owners and Groups 22. Find Single File Based on User To find all or single file called tecmint.txt under /root directory of owner root.

# find / -user root -name tecmint.txt

23. Find all Files Based on User To find all files that belongs to user Tecmint under /home directory.

# find /home -user tecmint

24. Find all Files Based on Group To find all files that belongs to group Developer under /home directory.

# find /home -group developer

25. Find Particular Files of User To find all .txt files of user Tecmint under /home directory.

# find /home -user tecmint -iname "*.txt"

Part IV – Find Files and Directories Based on Date and Time 26. Find Last 50 Days Modified Files To find all the files which are modified 50 days back.

  1. find / -mtime 50

27. Find Last 50 Days Accessed Files To find all the files which are accessed 50 days back.

# find / -atime 50

28. Find Last 50-100 Days Modified Files To find all the files which are modified more than 50 days back and less than 100 days.

# find / -mtime +50 –mtime -100

29. Find Changed Files in Last 1 Hour To find all the files which are changed in last 1 hour.

# find / -cmin -60

30. Find Modified Files in Last 1 Hour To find all the files which are modified in last 1 hour.

# find / -mmin -60

31. Find Accessed Files in Last 1 Hour To find all the files which are accessed in last 1 hour.

# find / -amin -60

Part V – Find Files and Directories Based on Size 32. Find 50MB Files To find all 50MB files, use.

# find / -size 50M

33. Find Size between 50MB – 100MB To find all the files which are greater than 50MB and less than 100MB.

# find / -size +50M -size -100M

34. Find and Delete 100MB Files To find all 100MB files and delete them using one single command.

# find / -size +100M -exec rm -rf {} \;

35. Find Specific Files and Delete Find all .mp3 files with more than 10MB and delete them using one single command.

# find / -type f -name *.mp3 -size +10M -exec ls -l {} \;

That’s it, We are ending this post here, In our next article we will discuss more about other Linux commands in depth with practical examples. Let us know your opinions on this article using our comment section.

Searching for Files Using Locate and Find

Searching for Files

Ubuntu keeps a database of all the files in the file system (with a few exceptions defined in /etc/updatedb.conf) using features of the mlocate package. The locate command allows you to search that database. (On Ubuntu, the locate command is a symbolic link to the secure version of the command, slocate.) The results come back instantly, since the database is searched and not the actual file system. Before locate was available, most Linux users ran the find command to find files in the file system. Both locate and find are covered here.


Finding Files with Locate

Because the database contains the name of every node in the file system, and not just commands, you can use locate to find commands, devices, man pages, data files, or anything else identified by a name in the file system. Here is an example:

$ locate e1000
/lib/modules/2.6.20-16-generic/kernel/drivers/net/e1000
/lib/modules/2.6.20-16-generic/kernel/drivers/net/e1000/e1000.ko
/lib/modules/2.6.20-15-generic/kernel/drivers/net/e1000
/lib/modules/2.6.20-15-generic/kernel/drivers/net/e1000/e1000.ko
/usr/src/linux-headers-2.6.20-16-generic/include/config/e1000
/usr/src/linux-headers-2.6.20-16-generic/include/config/e1000/napi.h
/usr/src/linux-headers-2.6.20-16-generic/include/config/e1000.h
/usr/src/linux-headers-2.6.20-15-generic/include/config/e1000
/usr/src/linux-headers-2.6.20-15-generic/include/config/e1000/napi.h
/usr/src/linux-headers-2.6.20-15-generic/include/config/e1000.h
/usr/src/linux-headers-2.6.20-15/include/config/e1000.h
/usr/src/linux-headers-2.6.20-15/drivers/net/e1000
/usr/src/linux-headers-2.6.20-15/drivers/net/e1000/Makefile
/usr/src/linux-headers-2.6.20-16/include/config/e1000.h
/usr/src/linux-headers-2.6.20-16/drivers/net/e1000
/usr/src/linux-headers-2.6.20-16/drivers/net/e1000/Makefile

The above example found two versions of the e1000.ko and e1000.ko kernel modules. locate is case sensitive unless you use the –i option. Here's an example:

$ locate -i itco_wdt
/lib/modules/2.6.20-16-generic/kernel/drivers/char/watchdog/iTCO_wdt.ko
/lib/modules/2.6.20-15-generic/kernel/drivers/char/watchdog/iTCO_wdt.ko

The slocate package (or mlocate on some Linux distributions) includes a cron job that runs the updatedb command once per day to update the locate database of files.

To update the locate database immediately, you can run the updatedb command manually:

$ sudo updatedb

Locating Files with Find

Before the days of locate, the way to find files was with the find command. Although locate will come up with a file faster, find has many other powerful options for finding files based on attributes other than the name.
    Note     

Searching the entire file system can take a long time to complete. Before searching the whole file system, consider searching a subset of the file system or excluding certain directories or remotely mounted file systems.

This example searches the root file system (/) recursively for files named e100:

$ find / -name "e100*" -print
find: /usr/lib/audit: Permission denied
find: /usr/libexec/utempter: Permission denied
/sys/module/e100
/sys/bus/pci/drivers/e100
...

Running find as a normal user can result in long lists of Permission denied as find tries to enter a directory you do not have permissions to. You can filter out the inaccessible directories:

$ find / -name e100 -print 2>&1 | grep -v "Permission denied"

Or send all errors to the /dev/null bit bucket:

$ find / -name e100 -print 2> /dev/null

Because searches with find are case sensitive and must match the name exactly (e100 won't match e100.ko), you can use regular expressions to make your searches more inclusive. Here's an example:

$ find / -name 'e100*' -print
/lib/modules/2.6.20-16-generic/kernel/drivers/net/e1000
/lib/modules/2.6.20-16-generic/kernel/drivers/net/e1000/e1000.ko
/lib/modules/2.6.20-16-generic/kernel/drivers/net/e100.ko
/lib/modules/2.6.20-15-generic/kernel/drivers/net/e1000
/lib/modules/2.6.20-15-generic/kernel/drivers/net/e1000/e1000.ko
/lib/modules/2.6.20-15-generic/kernel/drivers/net/e100.ko
/usr/src/linux-headers-2.6.20-16-generic/include/config/e100.h
/usr/src/linux-headers-2.6.20-16-generic/include/config/e1000
/usr/src/linux-headers-2.6.20-16-generic/include/config/e1000.h
/usr/src/linux-headers-2.6.20-15-generic/include/config/e100.h
/usr/src/linux-headers-2.6.20-15-generic/include/config/e1000
/usr/src/linux-headers-2.6.20-15-generic/include/config/e1000.h
/usr/src/linux-headers-2.6.20-15/include/config/e100.h
/usr/src/linux-headers-2.6.20-15/include/config/e1000.h
/usr/src/linux-headers-2.6.20-15/drivers/net/e1000
/usr/src/linux-headers-2.6.20-16/include/config/e100.h
/usr/src/linux-headers-2.6.20-16/include/config/e1000.h
/usr/src/linux-headers-2.6.20-16/drivers/net/e1000

You can also find files based on timestamps. This command line finds files in /usr/bin/ that have been accessed in the past two minutes:

$ find /usr/bin/ -amin -2 -print
/usr/bin/
/usr/bin/find

This command line finds files that have not been accessed in /home/chris for more than 60 days:

$ find /home/chris/ -atime +60

Use the -type d option to find directories. The following command line finds all directories under /etc and redirects stderr to the bit bucket (/dev/null):

$ find /etc -type d -print 2> /dev/null

This command line finds files in /sbin with permissions that match 750:

$ find /sbin/ -perm 750 -print

(which match none in a default Ubuntu installation.)

The exec option to find is very powerful, because it lets you act on the files found with the find command. The following command finds all the files in /var owned by the user francois (must be a valid user) and executes the ls -l command on each one:

$ find /var -user francois -exec ls -l {} \;

An alternative to find's exec option is xargs:

$ find /var -user francois -print | xargs ls -l

There are big differences on how the two commands just shown operate, leading to very different performance. The find -exec spawns the command ls for each result it finds. The xargs command works more efficiently by passing many results as input to a single ls command.

To negate a search criteria, place an exclamation point (!) before that criteria. The next example finds all the files that are not owned by the group root and are regular files, and then does an ls -l on each:

$ find / ! -group root -type f -print 2> /dev/null | xargs ls -l

The next example finds the files in /sbin that are regular files and are not writable by others, then feeds them to an ls -l command:

$ find /sbin/ -type f ! -perm /o+w -print | xargs ls -l
-rwxr-xr-x 1 root root     3056 2007-03-07 15:44 /sbin/acpi_available
-rwxr-xr-x 1 root root    43204 2007-02-18 20:18 /sbin/alsactl

Find Files by Size

  • b - 512-byte blocks (this is the default if no suffix is used)
  • c - bytes
  • w - two-byte words
  • k - Kilobytes
  • M - Megabytes
  • G - Gigabytes

Finding files by size is a great way to determine what is filling up your hard disks. The following command line finds all files that are greater than 10 MB (+10M), lists those files from largest to smallest (ls -lS) and directs that list to a file (/tmp/bigfiles.txt):

find / -xdev -size +10M -print | xargs ls -lS > /tmp/bigfiles.txt

In this example, the -xdev option prevents any mounted file systems, besides the root file system, from being searched. This is a good way to keep the find command from searching the /proc directory and any remotely mounted file systems, as well as other locally mounted file systems.

In this example we will use find command to search for files greater than 10MB but smaller than 20MB:

find . -size +10M -size -20M

In this example we use the find command to search for files in /etc directory which are greater than 5MB and we also print its relevant file size:

$ find /etc -size +5M -exec ls -sh {} +
6.1M /etc/udev/hwdb.bin

Find first 3 smallest files located in a in a current directory recursively:

$ find /etc/ -type f -exec ls -s {} + | sort -n | head -3

In the last example we will use find command to search for empty files:

$ find . -type f -size 0b
OR 
$ find . -type f -empty

Using Other Commands to Find Files

Other commands for finding files include the whereis and which commands. Here are some examples of those commands:

whereis man
man: /usr/bin/man /usr/X11R6/bin/man /usr/bin/X11/man /usr/local/man
/usr/share/man /usr/share/man/man1/man.1.gz /usr/share/man/man7/man.7.gz
 which ls
/bin/ls

The whereis command is useful because it not only finds commands, it also finds man pages and configuration files associated with a command. From the example of whereis for the word man, you can see the man executable, its configuration file, and the location of man pages for the man command. The which example shows where the ls executable is (/bin/ls). The which command is useful when you're looking for the actual location of an executable file in your PATH, as in this example:




Back To Top- Home - Category