Using bash to find a file modified in a specific date/time interval?

Did you ever need to find a file stored somewhere in a bunch of folders, which you didn’t remember the name, but you remember about when you last modified the file? At work, it sometimes happens that I need to find some log file, among 20 Gigs of log files, which was modified on a certain day. Here is a simple bash trick to do just that:

 touch -d "13 october 2006 15:00:00" ~/date_start
 touch -d "14 october 2006 21:00:00" ~/date_end
 find some/path -newer ~/date_start -and -not -newer ~/date_end

Start Cygwin from a folders context menu

My friend had a great little program, which essentially let you right click on any folder and open the command prompt there. The only program is that I never use the command line (CMD.EXE)… I use cygwin. A wonderful program that emulates Linux for windows, but also makes everything complicated!

After a short search on the web, I found a nice little discussion on a newsgroup that explained how to do it for cygwin. I just wanted to note the steps for windows XP, just so I don’t forget (If you really care… you can find steps for other versions on the discussion board).

1. Edit Registry

Start regedit.exe.

HKEY_CLASSES_ROOT\Directory\shell\BashHere="Bash Here"
HKEY_CLASSES_ROOT\Directory\shell\BashHere\command="C:\\WINDOWS\\System32\\CMD.EXE /E:4096 /c C:\\cygwin\\cygwin.bat %1"

Adjust the path to cygwin.bat if necessary.

Finish regedit.

You should now see the entry “Bash Here” in the context menu of a folder. However, when you use it, it will start the shell in your home directory ($HOME).

2. Edit cygwin.bat in c:/cygwin

Insert “set BASHHERE=%1″ before the line with “bash…”. This Environment variable now contains the complete path of the opened folder and is availabile in the bash shell (echo $BASHHERE).

3. Edit profile

Make $BASHHERE the current directory by adding the following line to
/etc/profile or ~/.profile:

if [ "$BASHHERE" != "" ]; then
    cd $( echo $BASHHERE | tr "\134" /)
fi

Vim Tip: Restore cursor’s last position

Here’s a useful vi trick that turned out to be a real time saver. It’s a set of commands you add to your vimrc file (on windows) that will recover the cursor’s last position when the file was last closed. The original tip was found on the vim wikia, which is full of those very useful vim tricks. Give it a try!

" Tell vim to remember certain things when we exit
"  '10 : marks will be remembered for up to 10 previously edited files
"  "100 : will save up to 100 lines for each register
"  :20 : up to 20 lines of command-line history will be remembered
"  % : saves and restores the buffer list
"  n... : where to save the viminfo files
set viminfo='10,\"100,:20,%,n~/.viminfo
 
" when we reload, tell vim to restore the cursor to the saved position
augroup JumpCursorOnEdit
 au!
 autocmd BufReadPost *
 \ if expand("<afile>:p:h") !=? $TEMP |
 \ if line("'\"") > 1 && line("'\"") <= line("$") |
 \ let JumpCursorOnEdit_foo = line("'\"") |
 \ let b:doopenfold = 1 |
 \ if (foldlevel(JumpCursorOnEdit_foo) > foldlevel(JumpCursorOnEdit_foo - 1)) |
 \ let JumpCursorOnEdit_foo = JumpCursorOnEdit_foo - 1 |
 \ let b:doopenfold = 2 |
 \ endif |
 \ exe JumpCursorOnEdit_foo |
 \ endif |
 \ endif
 " Need to postpone using "zv" until after reading the modelines.
 autocmd BufWinEnter *
 \ if exists("b:doopenfold") |
 \ exe "normal zv" |
 \ if(b:doopenfold > 1) |
 \ exe "+".1 |
 \ endif |
 \ unlet b:doopenfold |
 \ endif
augroup END

Fixing ASCII/Text to Binary mode in CVS

I’ve often found myself in the situation where I need to quickly fix the file mode from ASCII to Binary in CVS. Here are quick instructions:

cvs admin -kb BinaryFile.xls
cvs update -A BinaryFile.xls
cvs commit -m "make it binary" BinaryFile.xls

Useful keyboard shortcuts for Bash

I’ve been playing around with bash for the past two years now, and found some of these keyboard shortcuts to be very useful. I’ve tried to put the ones I use the most often on here. For other cool bash shortcuts and tricks, visit

  • CTRL + R: Reverse search through history for a previously used command;
  • CTRL + A: Moves your cursor to the beginning of the current line;
  • CTRL + E: Moves your cursor to the end of the current line;
  • ALT + F: Moves your cursor forward one word on the current line;
  • ALT + B: Moves your cursor backward one word on the current line;
  • CTRL + W: Deletes the word before the cursor;
  • ESC + D: Deletes the word after the cursor;
  • CTRL + U: Clears the line before the cursor position. If you are at the end of the line, clears the entire line;
  • CTRL + K: Clears the line after the cursor;
  • CTRL + C: Kills the current running process;
  • CTRL + Z: Suspends the current job and runs it as a background process;
  • TAB: Auto-complete files and folder names;

For the sake of completeness, I’ve added these other shortcuts that I saw on another website. However, I find that I do not use those on a regular basis. In reality, there are plenty more bash keyboard shortcuts around, but I just never bothered trying them out yet… For a much more extensive list of shortcuts (way too extensive for my tastes), visit web hosting uk‘s blog post on the subject.

  • CTRL + L: Clears the Screen (I use the “clear” command);
  • CTRL + H: Same as backspace;
  • CTRL + D: Exit the current shell (I type “exit” in Putty);
  • CTRL + T: Swaps the last two characters before the cursor;
  • ESC + T: Swaps the last two words before the cursor;
  • CTRL + XX: Moves between end-of-line and current cursor position;
  • CTRL + Y: Recovers previous deletion (Not file deletion!);
Page 1 of 212
© Copyright Bonuel Photography - Theme by Pexeto
Follow

Get every new post on this blog delivered to your Inbox.

Join other followers: