Some Useful Unix File Finding Commands

Following are some bunch of commands that might be useful if you want to find files in unix/linux.

Large Files

Find files larger than 10MB in the current directory downwards…
find . -size +10000000c -ls
Code language: Bash (bash)
Find files larger than 100MB…
find . -size +100000000c -ls
Code language: Bash (bash)

Old Files

Find files last modified over 30days ago…
find . -type f -mtime 30 -ls
Code language: Bash (bash)
Find files last modified over 365days ago…
find . -type f -mtime 365 -ls
Code language: Bash (bash)
Find files last accessed over 30days ago…
find . -type f -atime 30 -ls
Code language: Bash (bash)
Find files last accessed over 365days ago…
find . -type f -atime 365 -ls
Code language: Bash (bash)

Find Recently Updated Files

There have been instances where a runaway process is seemingly using up any and all space left on a partition. Finding the culprit file is always useful. If the file is being updated at the current time then we can use find to find files modified in the last day…
find . -type f -mtime -1 -ls
Code language: Bash (bash)
Better still, if we know a file is being written to now, we can touch a file and ask the find command to list any files updated after the timestamp of that file, which will logically then list the rogue file in question.
touch testfile find . -type f -newer testfile -ls
Code language: Bash (bash)

Finding tar Files

A clean up of redundant tar (backup) files, after completing a piece of work say, is sometimes forgotten. Conversely, if tar files are needed, they can be identified and duly compressed (using compress or gzip) if not already done so, to help save space. Either way, the following lists all tar files for review.
find . -type f -name "*.tar" -ls find . -type f -name "*.tar.Z" -ls
Code language: Bash (bash)

Large Directories

List, in order, the largest sub-directories (units are in Kb)…
du -sk * | sort -n
Code language: Bash (bash)
Sometimes it is useful to then cd into that suspect directory and re-run the du command until the large files are found.

Removing Files using Find

The above find commands can be edited to remove the files found rather than list them. The “-ls” switch can be changed for “-exec rm {}\;”=. e.g.
find . -type f -mtime 365 -exec rm {} \;
Code language: Bash (bash)
Running the command with the “-ls” switch first, is always prudent to see what will be removed. The “-ls” switch prints out summary information about the file (like owner and permissions). If just the filename is required then swap “-ls” switch for “-print”. Are you using different commands to find a file? Please share it using below comment form. :)

View Comments

  • to find text inside files (in current directory and also subdirectories), grep can be used recursively:

    grep -r "findthistext" *

    if text is to be searched in only a type of files e.g. text files or shell scripts:


    grep -r "findthistex" *.txt
    grep -r "findthistex" *.sh

Recent Posts

  • Java

Java URL Encoder/Decoder Example

Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…

4 years ago
  • General

How to Show Multiple Examples in OpenAPI Spec

Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…

4 years ago
  • General

How to Run Local WordPress using Docker

Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…

4 years ago
  • Java

Create and Validate JWT Token in Java using JJWT

1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…

4 years ago
  • Spring Boot

Spring Boot GraphQL Subscription Realtime API

GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…

4 years ago
  • Spring Boot

Spring Boot DynamoDB Integration Test using Testcontainers

1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…

4 years ago