shell

Подписаться на эту метку по RSS

Here is how you can find all files newer than a certain date, excluding directories from the search ("doc" and "misc", in my case) and printing the modified time along with the file name.

find -L . -newermt "Dec 5, 2015 22:00" -not -path './doc/*' -not -path './misc/*' -printf "%TY-%Tm-%Td %TH:%TM:%TS %Tz %p\n"  | less

This oneliner will also follow symlinks (thanks to -L option).

It would seem there is no problem to find all symbolic links within the directory recursively like find . -type l . This is exactly how I hoped to make it, but got not the result I expected.

Actually, this way we don't get those symbolic links that are located within the directories which, in its turn, are pointed by the symbolic links that are found in the directory of initial search. It is complicated enough, but I think you see the issue :)

So this is how to solve it:

1. You can use ls recursively and so that this command will follow all the symbolic links found and search new links there.

ls -lR . | grep ^l

2. Another way is to take advantage of find command, but note, firstly, there must be -l option which will allow us to follow symbolic links already found, and secondly, pay attention that I use -xtype instead of -type. This is due to the fact that:

l    symbolic link; this is never true if the -L option or the
     -follow option is in effect, unless the symbolic link  is
     broken.  If you want to search for symbolic links when -L
     is in effect, use -xtype.

(from man find )

Thus, the second solution:

find -L . -xtype l

This is my solution on how to control disk space usage on a server. The solution is a simple one-line shell script that is executed by cron and sends an e-mail alert each time your disk space gets low.

First of all, let's create a shell file.

vi /home/username/bin/check-disk-space.sh

#!/bin/sh
df -Pl|grep "^/dev"|awk '{sub("%","",$5);if($5 > 95) print $5"%%\t"$6}'|xargs -0 sh -c 'test "$1" && printf "`hostname -f` is running low on disk space:\nUse%%\tMounted on\n$1"' $0

As you can see it's simple enough. You can adjust the parameter in percent (in our case it is 95% ) on after overbrim of which you'll get an email alert like:

<hostname> is running low on disk space:
Use%	Mounted on
95%	/

At last, you must set a crontab task:

crontab -e u username

MAILTO=your-email@example.com
15 */2 * * * /home/username/bin/check-disk-space.sh

This cron task will check disk space every 2 hours.

Hope this helps you :)