How to find files and folders using SSH commands

Searching for files and folders on your Linux server is a common task that every server administrator faces daily. In particular, the ability to quickly locate specific files, directories, or text strings within files can save you hours of manual searching. Furthermore, mastering SSH search commands like find and grep will dramatically improve your efficiency when managing your hosting environment, troubleshooting issues, or performing system maintenance.

Understanding SSH file search commands

Linux provides powerful built-in utilities for searching your file system through SSH. The two most important commands for locating files and folders are find and grep. Moreover, the find command excels at locating files and directories based on names, types, sizes, and modification dates, while grep specializes in searching for text patterns within files. Together, these tools form an essential part of every server administrator’s toolkit.

Whether you’re hunting for a specific configuration file, tracking down large files consuming disk space, or searching for code snippets across multiple files, these SSH commands provide the precision and flexibility you need.

Searching for files by name

The most common search operation is finding a file when you know its exact name. To accomplish this, use the find command with the -name option:

find . -name "index.php"

This command searches the current directory (represented by the dot) and all subdirectories for a file named “index.php”.

Breaking down the syntax:

  • find – the command that initiates the search
  • . (dot) – the starting point for the search (current directory)
  • -name – specifies you’re searching by filename
  • “index.php” – the exact filename to find

Practical example: Finding your WordPress configuration file:

find ~/public_html -name "wp-config.php"

As a result, this searches your entire website directory for the WordPress configuration file.

Searching for files without knowing the extension

Sometimes you remember the filename but not the extension. Fortunately, you can search for files using partial names with wildcards:

find . -name "config*"

This finds all files starting with “config” regardless of their extension (config.php, config.json, config.yml, etc.).

Example: Finding all image files with a specific name:

find ~/public_html/images -name "logo*"

Consequently, this returns logo.png, logo.jpg, logo.svg, and any other files beginning with “logo”.

For case-insensitive searches, use the -iname option instead:

find . -iname "README*"

This matches README.md, readme.txt, ReadMe.MD, and all case variations.

Searching for directories only

To locate directories specifically and exclude files from your search results, use the -type d option:

find / -type d -name "uploads"

This searches the entire filesystem (starting from /) for directories named “uploads”.

Understanding the -type option:

  • -type d – searches for directories only
  • -type f – searches for regular files only
  • -type l – searches for symbolic links only

Practical example: Finding all cache directories in your website:

find ~/public_html -type d -name "*cache*"

This helps you identify cache folders that might need clearing during troubleshooting. If you need to organize these directories better, you can create new directory structures or move them to different locations.

Finding files by modification time

When troubleshooting issues or tracking changes, finding recently modified files becomes crucial. The -mtime option filters files based on their modification date:

find . -mtime -4

This locates all files modified in the last 4 days.

Understanding time-based searches:

  • -mtime -4 – files modified less than 4 days ago
  • -mtime +4 – files modified more than 4 days ago
  • -mtime 4 – files modified exactly 4 days ago

Additional time options:

  • -mmin – search by minutes instead of days
  • -atime – search by access time
  • -ctime – search by status change time

Example: Finding files modified in the last hour:

find ~/public_html -mmin -60

This is particularly useful for identifying files that changed during a recent update or potential security incident.

Finding files by size

To locate large files that might be consuming your disk space, use the -size option:

find ~/public_html -type f -size +100M

This finds all files larger than 100 megabytes.

Size units available:

  • k – kilobytes
  • M – megabytes
  • G – gigabytes
  • c – bytes

Example: Finding all files larger than 50MB and listing them with details:

find ~/public_html -type f -size +50M -exec ls -lh {} \;

Similarly, you can find small files or files within a size range:

find . -size +10M -size -100M

This finds files between 10MB and 100MB.

Searching for text within files using grep

While find locates files by their properties, grep searches for specific text content within files. To search for a word or phrase inside a file, use:

grep "database_password" wp-config.php

This searches for the text “database_password” in the wp-config.php file and displays matching lines.

Practical example: Finding your database credentials:

grep "DB_PASSWORD" ~/public_html/wp-config.php

As a result, this displays the line containing your database password.

Recursive text search across multiple files

When you don’t know which file contains the text you’re looking for, use grep with recursive search options:

grep -r -H "function_name" *

This searches all files in the current directory and subdirectories for “function_name”.

Understanding grep options:

  • -r (recursive) – searches through all subdirectories
  • -H – displays the filename along with matching lines
  • -i – case-insensitive search
  • -n – shows line numbers
  • -l – lists only filenames (not the content)
  • -v – inverts the match (shows non-matching lines)

Example: Finding all PHP files containing a specific function:

grep -r -H -n "wp_enqueue_script" ~/public_html/*.php

This shows the filename, line number, and content for every match.

Advanced search combinations

Moreover, you can combine find and grep to create powerful search queries:

Finding PHP files containing specific text:

find . -name "*.php" -exec grep -H "mysql_connect" {} \;

This locates all PHP files and searches them for the deprecated mysql_connect function.

Finding recently modified configuration files:

find ~/public_html -name "*.conf" -mtime -7

Locating empty directories:

find ~/public_html -type d -empty

Finding files with specific permissions:

find ~/public_html -type f -perm 777

This identifies files with potentially insecure permissions.

Common use cases for web hosting

In practice, here are typical scenarios where these search commands prove invaluable for linux hosting and general server management:

1. Finding all log files:

find ~/logs -name "*.log" -mtime -7

2. Locating malware or suspicious files:

find ~/public_html -name "*.php" -mtime -1

3. Locating all JavaScript files in your theme:

find ~/public_html/wp-content/themes -name "*.js"

4. Searching for hardcoded email addresses:

grep -r -i "example@domain.com" ~/public_html/

5. Finding backup files:

find ~/public_html -name "*.bak" -o -name "*.backup"

6. Locating large database dumps:

find ~ -name "*.sql" -size +100M

7. Identifying files owned by specific user:

find ~/public_html -user username

Best practices and tips

Therefore, to maximize your search efficiency and avoid common pitfalls, follow these best practices:

  • Start with specific search paths rather than searching from root to reduce search time
  • Use quotes around search terms that contain spaces or special characters
  • Combine multiple search criteria with -and, -or, and -not operators for precision
  • Redirect large search results to files for easier analysis: find . -name "*.log" > logfiles.txt
  • Leverage the -exec option to perform actions on found files automatically
  • Use case-insensitive searches (-iname, -i) when you’re unsure of capitalization
  • Test your search commands in a small directory first before running on large file systems
  • Save frequently used searches as shell aliases for quick access

Pro tip: Create an alias for common searches in your ~/.bashrc file:

alias findphp='find . -name "*.php" -type f'
alias findlarge='find . -type f -size +50M -exec ls -lh {} \;'

New hosting plan with 80% OFF

Ready to put your SSH search skills to work? Upgrade to a hosting plan that gives you full SSH access and complete control over your server files. Additionally, enjoy a huge 80% discount with reliable performance, free SSH access, and full Linux terminal support. Furthermore, you’ll be able to search, locate, and manage files with professional-grade command-line tools. Perfect for developers, power users, or anyone who prefers the control of a Linux environment.

Hosting from JetHost

Need more help?

Mastering SSH file search commands is an essential skill that dramatically improves your server management efficiency. Whether you’re hunting for specific files by name, locating directories, finding recently modified content, or searching for text within files, the find and grep commands provide the power and flexibility you need. By following the examples and best practices outlined in this tutorial, you’ll be able to locate any file or folder on your Linux server quickly and confidently.

Remember that effective searching is about combining the right tools with the right options. Nevertheless, with practice and experimentation, you’ll develop search workflows that perfectly match your specific needs, making you a more efficient and capable server administrator.

Discover more SSH tutorials and Linux command guides in our comprehensive knowledgebase. Master Linux file search commands on a shared hosting account with 80% OFF the regular price now!