Skip to content
IT related

Find command

Updated 2 min read Linked from 1 page

Using the find command

Some examples

Find a file called a specific thing

sudo find . -type f -name *.rar

You can make the find command run commands on the items it finds with the execdir flag, like below. This will find all the files over 11gb size and convert them with ffmpeg.

sudo find . -type f -size +11G -execdir bash -c 'date && ffmpeg -i "$0"  -map 0 -map -0:t -map -0:s -c:a aac -c:v libx265 -s 1920:1080 -preset slow -crf 18 "${0%.*}new.mp4" && rm "$0" && mv "${0%.*}new.mp4" "${0%.*}.mp4" && chown ed:ed "${0%.*}.mp4" && date || echo "This task has failed"' "{}" \;

This command will find all the compressed zip files (mulipart rar included) and extract them.

sudo find . -type f -name *.rar -execdir bash -c 'unrar e "$0"' "{}" \;

This will call the bash command to run whatever you put in the single quotes. The "{}" section at the end is where the find command enters what it found on this loop.

To remove all the rar and multi part files after, run this.

find . -type f \( -name "*.rar" -o -name "*.r*" -o -name "*.c" \)

The -o specifies or, this allows the use of multiple names. The brackets are there for character escaping.

Find and count results

find . -type f -size +11G -printf '.' | wc -c
find . -type f -size +11G -printf '.' | wc -c

Sure! Here's the complete find command cheat sheet in a neatly formatted Markdown document:

# Find Command Cheat Sheet

`find` is a powerful command in Unix and Linux used for searching files in a directory hierarchy.

## Basic Usage

### Find Files by Name
```bash
find /path/to/search -name 'filename'

Find Files with Case-Insensitive Names

find /path/to/search -iname 'filename'

Find Directories by Name

find /path/to/search -type d -name 'dirname'

Find Files with Specific Extensions

find /path/to/search -type f -name '*.ext'

Find Files by Permissions

find /path/to/search -type f -perm 0664

Find Files by User

find /path/to/search -user username

Find Files Modified N Days Ago

find /path/to/search -mtime N

Find Files Accessed N Days Ago

find /path/to/search -atime N

Find Files Larger than a Specific Size

find /path/to/search -size +500k

Advanced Usage

Execute Commands on Found Files

find /path/to/search -type f -name '*.ext' -exec command {} \;

Find Files and Output with Detailed Info (like ls -l)

find /path/to/search -type f -exec ls -l {} \;

Find and Delete Files

find /path/to/search -type f -name '*.ext' -delete

Find Files by Modification Time and Size

find /path/to/search -mtime +30 -size +500k

Find Files and Exclude a Directory

find /path/to/search -path /path/to/search/exclude -prune -o -name '*.ext' -print

Find and Copy Found Files to Another Directory

find /path/to/search -type f -name '*.ext' -exec cp {} /path/to/destination \;

Practical Examples

Find All .jpg Files and Move Them to a Different Directory

find /path/to/search -type f -name '*.jpg' -exec mv {} /path/to/destination \;

Find Files Modified in the Last 7 Days

find /path/to/search -mtime -7

Find and Tar.gz Found Files

find /path/to/search -type f -name '*.ext' | tar -czvf archive.tar.gz -T -

Find and DELETE all Empty Directories.

find /path/to/SRC -type d -empty -delete

Find All Empty Files

find /path/to/search -type f -empty

Find and Count All .html Files

find /path/to/search -type f -name '*.html' | wc -l

This Markdown document provides a structured overview of the `find` command with examples under each category. You can use it as a quick reference guide or modify it to suit your documentation needs.

## Attachments

- main.yml
- ansible.cfg
- readme.rd
- patch.log
IT related RSS feed Published with Oblish

Keyboard shortcuts

/
Search
[
Previous page
]
Next page
?
Show this list
Esc
Close