Difference between revisions of "Find"

From DevOps Notebook
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
=== Find largest files in given location ===
 
=== Find largest files in given location ===
 
<pre>
 
<pre>
$ find /home/devops/Downloads/ -type f -exec du -Sh {} + | sort -rh | head -n 5
+
# find /home/devops/Downloads/ -type f -exec du -Sh {} + | sort -rh | head -n 5
 +
</pre>
 +
=== List and sort files or folders with find ===
 +
<pre>
 +
# find . -type d -ls | sort -n -r
 +
# find . -type f -ls | sort -n -r
 
</pre>
 
</pre>
 
=== Delete files older then X hours ===
 
=== Delete files older then X hours ===
 
<pre>
 
<pre>
$ find /backup -name *.log -type f -mmin +360 -delete
+
# find /backup -name *.log -type f -mmin +360 -delete
 
</pre>
 
</pre>
 
=== Delete files older then 30 days ===
 
=== Delete files older then 30 days ===
 
<pre>
 
<pre>
$ find /backup -type f -mtime +30 -exec rm -f {} \;
+
# find /backup -type f -mtime +30 -exec rm -f {} \;
 +
</pre>
 +
 
 +
=== Use find to notify via email on recently changed files ===
 +
<pre>
 +
# find ./*.log -type f -mmin -360 | xargs -n 1 -I % sh -c 'mail -s "MY LOG FILE - %" [email protected]  < %'
 
</pre>
 
</pre>

Latest revision as of 18:26, 8 October 2020

Find largest files in given location

# find /home/devops/Downloads/ -type f -exec du -Sh {} + | sort -rh | head -n 5

List and sort files or folders with find

# find . -type d -ls | sort -n -r
# find . -type f -ls | sort -n -r

Delete files older then X hours

# find /backup -name *.log -type f -mmin +360 -delete

Delete files older then 30 days

# find /backup -type f -mtime +30 -exec rm -f {} \;

Use find to notify via email on recently changed files

# find ./*.log -type f -mmin -360 | xargs -n 1 -I % sh -c 'mail -s "MY LOG FILE - %" [email protected]  < %'