Sep 23rd 2010 × Bash scripting: find recently modified files on a linux/*nix server
This has turned out to be a somewhat tricky task – to find all updated files since nn-number of days within the current path / directory tree.
The first bit was, use the find command like so:
find . -mtime -3 -printf "%Tx\t%p\n"
This gives you the modified date and name / relative path of all changed files within the last 3 days However, setting this as a bash alias means you cannot pass an argument for the number of days to go back. That’s when using bash functions can come to the rescue (stick this in your ~/.bashrc):
function nf() { find . -mtime -$1 -printf "%Tx\t\t\t%p\n" ;}
to use, simply do:
# nf 4 09/23/2010 ./migrate-newproduct.sh 09/22/2010 ./tips.sql # nf 20 | grep sql ...
You may need to do . ~/.bashrc first to make the function stick. Simple, really. For more formatting on find, just google the printf API.
Another really useful bash feature I have recently added to my shell is forward and backward lookup history via .inputrc:
"\C-f": history-search-backward "\C-g": history-search-forward
This works by typing the first few letters of a recent command you may have entered on the console and pressing CTRL-F for a forward lookup or CTRL-G to go back to the previous entry – in a way, tab completion. Fully supported by MySQL as well, which is very handy indeed!




