Raspberry Pi

How to Find a File in Raspberry Pi

File management is an integral part of managing any system. Even after perfectly organizing files sometimes it becomes difficult to find a required file. A lot of time can be wasted while manually searching for a particular file. Therefore, in the Raspberry Pi system, a special command can be used to find a file or directory to save a lot of your time. In this write-up, we will present how to find a file in Raspberry Pi.

How to Find a File in Raspberry Pi

To find a file in the Raspberry Pi system a find command is used the syntax of which is shared below:

Syntax

$ sudo find [file-path]

There are several ways to use the find command to locate a file. Using this find command along with other options you can find the files:

  • In a Specific Directory
  • With Specific Time
  • With Specific Character in the Name
  • With Specific Size
  • With Specific Permission

All these ways are shared below along with some examples also:

To Find a File in a Specific Directory

To find out the files present in a particular folder or directory below-mentioned command syntax can be used:

Syntax

$ sudo find <Directory-Path>

Example

In the example below, we have found the files which are present in the home directory:

$ sudo find /home/pi

Output will be displayed on the screen, which will display all the files that are present in the home directory.

And once you have found the specific file in the directory then you can easily access the file.

To Find the File with a Specific Time

Sometimes you might feel the need to find the file that has been created or modified in a particular time frame like within the past two or three days, so to find such files below-mentioned command syntax can be used:

i: Files Modified

In the below-mentioned command syntax the “m” flag along with time is used to represent the modified files:

Syntax

$ sudo find <Directory Path> -mtime +<Days>

Example

To find the files in the home directory that have been modified in the past two days, the below-mentioned command is used:

$ sudo find /home/pi -mtime +2

ii: Files Created

To find the files that are created on a specific number of days the below-mentioned syntax is used, the “c” flag along with time is used to represent created files:

Syntax

$ sudo find <Directory-Path> -ctime +<Days>

Example

To find the files that have been created in the past three days in the home directory:

$ sudo find /home/pi -ctime +3

The output will display all the files that are created in past three days in the home directory:

To Find a File with a Specific Character in Name

To find a file with specific characters that are present in the file’s name below-mentioned syntax can be used:

Syntax

In this syntax, any character can be used, it is not necessary to use single character only:

$ sudo find <Directory-Path> -name <character>*

Example 1

To find the files that start with a character “a” in their name:

$ sudo find /home/pi -name a*

The output is showing the Files which are present in the home directory and their name starts with a character “a”.

Example 2

To find the files that have the letters “am” in their names the “*” sign at the start and end of names shows that other character or characters can be present before and after “am” in a file’s name.

$ sudo find /home/pi -name *am*

To Find the File with a Specific Size

The below-mentioned syntax can be used to find the file with a specific size:

Syntax

$ find <Directory name> -size +<size in number>

Example 1

$ find /home/pi -size +20M

The output of the above command will display the files present in the home directory with size equal to 20 Mega Bytes:

Example 2

To find the file with specific size and specific time below-mentioned command can be used:

$ find /home/pi -size +20M -mtime +30

The output of the above command will display the files present in the home directory that are of size 20M and are modified in the past 30 days:

To Find File with Specific Permissions

To find the files with specific read, write and execute permissions assigned to user (u), group (g) or others (o) below-mentioned syntax is used:

Syntax

$ find . -perm /<permission character>

Permission characters are:

u = the user or owner, g = groups in which owner is present, o = others.

w = write permission, r  = read permission, x = execute permission.

Example

The command to find the files that have permission/access to writing by both owner and groups is mentioned below:

$ find . -perm /u=w, g=w

The output of the above command will display all files that have writing permission for both user and group:

After finding out file permissions if you want to change the file permissions follow the article. 

That’s it for using the find command to find a file.

Bonus Tip

If you get stuck somewhere and you feel the need to access the manual of your system, then by using the below-mentioned find command you can access the manual:

$ man find

The manual of the system will display on the screen as an output for the above command:

Conclusion

To find a file in the Raspberry Pi system the find command is used along with the name of the directory. There are several ways to use the find command to find a file of a particular size, character, or time. Each of these ways is discussed in detail in the article. Lastly, the command to find the manual is also discussed as a bonus tip.

About the author

Zahra Zamir

An Electronics graduate who loves to learn and share the knowledge, my passion for my field has helped me grasp complex electronics concepts and now I am here to share them with others.