Finding files

In a filesystem, there is huge number of files available. Sometimes, there are external devices that are attached as well, which may also contain huge number of files. Imagine that there are millions and billions of files in a system and in which we have to search for a specific file or pattern of a file. Manual searching of a file is possible if the number of files is from 10 to 100, but it is almost impossible to search in millions of files. To solve this problem, UNIX and Linux provide the find command. It is a very useful command for searching files in a computer.

The syntax of using the find command is as follows:

find search_path [option]

Here, in search_path, specify the path in which find should search for file_search_pattern.

A few important options are mentioned in the following table:

Option

Description

-P

Don't follow symbolic link. This is default behavior

-L

Follow symbolic link while searching

-exec cmd ;

Execute command cmd passed as parameter to -exec

-mount

Don't search in other file system

-executable

Matches executable files

-group gname

File belongs to group gname

-user uname

Files owned by user uname

-name pattern

Search file for given pattern

-iname pattern

Case insensitive search of file for given pattern

-inum N

Search file with inode number N

-samefile name

File with same inode number as name

-regex pattern

Match files with given regular expression pattern. Matches for whole path.

-iregex pattern

Case insensitive match of files with given regular expression pattern. Matches for whole path.

Searching files according to use case

The following shell script shows some use cases of how to use the find command:

#!/bin/bash
# Filename: finding_files.sh
# Description: Searching different types of file in system

echo -n "Number of C/C++ header files in system: "
find / -name "*.h" 2>/dev/null |wc -l
echo -n "Number of shell script files in system: "
find / -name "*.sh" 2>/dev/null |wc -l
echo "Files owned by user who is running the script ..."
echo -n "Number of files owned by user $USER :"
find / -user $USER 2>/dev/null |wc -l
echo -n "Number of executable files in system: "
find / -executable 2>/dev/null | wc -l

The following is the sample output after executing the preceding finding_files.sh script:

Number of C/C++ header files in system: 73950
Number of shell script files in system: 2023
Files owned by user who is running the script ...
Number of files owned by user foo :341726
Number of executable files in system: 127602

Finding and deleting a file based on inode number

The find command can be used to find a file based on its inode number.

$ find ~/ -inum 8142358
/home/foo/Documents

The -inum option is good to use with exec to delete files that cannot be deleted by a filename. For example, a file named -test.txt can't be deleted using the rm command:

$  ls -i ~ |grep  test  # Viewing file with its inode number
8159146 -test.txt

To delete the -test.txt file, execute the following command:

$ find ~/ -inum 8159146 -exec rm -i {} ;  # Interactive deletion
rm: remove regular file '/home/skumari/-test.txt?' y
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.242.9