Sunday, October 7, 2012

Use of Find Command in Unix

To find the largest 10 files (linux/bash):

# find . -type f -print0 | xargs -0 du -s | sort -n | tail -10 | cut -f2 | xargs -I{} du -sh {}


To find the largest 10 directories:

# find . -type d -print0 | xargs -0 du -s | sort -n | tail -10 | cut -f2 | xargs -I{} du -sh {}

Only difference is -type {d:f}. No type for combined results.

For a quick view:

# du | sort -n

Find directory size more than 500MB

# find . -type f -size +500000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'

Find OUT large hists form IP

# awk '{print $1};' /var/log/httpd/access_log | sort | uniq -c | sort -rn | head

Find out directory files count

#  ls -l | awk '!/^d/{print }' | wc -l

#  ls -la |wc -l

#  find . |wc –l

Find file which older than 30 days or modify

# find . -mtime +30

Delete old files older than x days with subdirectories
 
# find . -mtime +10 -exec rm {} \; 

This will show you how to find the ten biggest files / folders on your linux system

# du -a / | sort -n -r | head -n 10

You can use the command to find the biggest files in a specific location like this

# du -a /var/www | sort -n -r | head -n 10

And you can return more than ten results like this

# du -a /var/www | sort -n -r | head -n 25 

Large Files

Find files larger than 10MB in the current directory downwards…
find . -size +10000000c -ls
Find files larger than 100MB…
find . -size +100000000c -ls

Old Files

Find files last modified over 30days ago…
find . -type f -mtime 30 -ls
Find files last modified over 365days ago…
find . -type f -mtime 365 -ls
Find files last accessed over 30days ago…
find . -type f -atime 30 -ls
Find files last accessed over 365days ago…
find . -type f -atime 365 -ls

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
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

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

Large Directories

List, in order, the largest sub-directories (units are in Kb)…
du -sk * | sort -n
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 {} \;

0 comments:

Post a Comment