Archive for the 'Scripting' Category

Linux:Script to find files

Wednesday, August 17th, 2011

I regularly have to find where a file is located and usually use find:

find . | grep 

Where is replaced with the filename or partial name to match. So I finally decided to write a script to just run this and allow me to print the matching names and even potentially pass the -i flag to grep.

#!/bin/sh


if [ $# -lt 1 ]; then
  echo 1>&2 Usage: $0 ""
  exit 127
fi
icase=

while [ $# -ge 1 ]; do
   case $1 in
     -i)  icase=$1;;
      *)  search=$1 ;;
   esac
   shift
done

find .  | grep $icase $search

This is just a simple script which I have named ffind that searches starting from the current directory. This lets me search with the following commands:

ffind bak$

This will search for all files that end with “bak” .

ffind -i edr

This will search for all files with “edr” regardless of case

Tags: , , , ,

Using find and grep to search files

Tuesday, August 16th, 2011

I have been searching through source code and files for years. And I used to use find with a few arguments to get what I was looking for.

find . -exec grep  {} ; -print

I used the -print after the exec arguments to show the file only when the grep succeeded.  This worked but was a bit to type each time I used it. So I came up with a little script that would help me to search for the an argument and even allow a case insensitive search as well.
 

#!/bin/sh

if [ $# -lt 1 ]; then
  echo 1>&2 Usage: $0 ""
  exit 127
fi
icase=
search=

while [ $# -ge 1 ]; do
  case $1 in 
     -i) icase=$1;;
      *) search=$1 ;;
  esac
  shift
done

find . -type f -print0 | xargs -0 grep $icase $search

The script takes at least one argument and excepts a ‘-i’ argument to make grep use a case insensitive search. This will print the file name and the line that matches the search term. It can be easily adapted to show the count of matched patterns (‘-c’) or the line number (‘-n’).

Tags: , , ,

How to add library paths to perl

Saturday, August 13th, 2011

I need to write custom Perl libraries sometimes and an easy method to make sure that they are found on the path is to use the PERL5LIB environment variable.

So either set it in your current environment or in a shell you can just specify the contents before you run your script.

PERL5LIB=/PATH/WHERE/LIB/IS perl scripttorun.pl 

Tags: ,

Perl: CPAN installing modules

Friday, August 5th, 2011

Sometimes it is necessary to install other modules into the Perl environment. Luckily there is a great repository of modules known as CPAN (The Comprehensive Perl Archive Network). This lets you install modules to help with encryption, XML,YAML, and many other things. There are a few ways to install a package and here are the ones that I use the most.

Using the CPAN shell:

perl -MCPAN -e shell

This will open a command prompt that will let you search for CPAN modules with:

i /PACKAGENAME/

or install a new module:

install /DateTime/

The nice thing about installing with CPAN is that it will look for the dependencies and suggest installing them before the module you requested.

NOTE:This can take some time and your dependencies could have dependencies.

The other method I use is to skip the CPAN prompt and just directly execute the install of the module:

perl -MCPAN -e 'install DateTime'

This is just the most basic of usage, and there is probably a lot more you can do, but I have not had to use much more than this.

Tags: , ,

Using tar over ssh

Thursday, July 21st, 2011

Every once in awhile, I need to backup a large directory from another computer. This can be done by creating a zip or tar of the files you are looking to save and then copying those zips to the other computer. But what it you have a very large directory and not enough room to store the extra file. Here is a way to use tar over ssh:

ssh tar zcvf – /wwwdata | ssh root@backupserver.mydomain.com “cat > /backup/wwwdata.tar.gz”

Setting timestamp with Touch

Wednesday, July 20th, 2011

Did you know? You can Set file timestamp using touch command. One way to do this is:
touch -c -t YYMMDDhhmm filename.txt

Retrieve drive letter on Windows

Thursday, June 16th, 2011

The other day I had to write a script to change to a working directory and back to the original directory. The problem was they were on different drives on Windows. So I had to find a way to capture the drive letter
and switch back to that drive. When using the following command,

cd F:somedir

you are not moved to the “F:” drive without an explicit “F:” command.

The cd Command on windows will show the current directory location. Using some string manipulation on that and capturing it to a variable allows us to go back to that drive.

REM Capture the current drive letter
set ORIGDRIVE=%cd:~0,2%

F:
REM some other scripting things here

REM Return to the original drive letter
ORIGDRIVE

The first line captures the current drive letter by specifying the first character with 0 and the ,2 states to pick up the next 2 characters which would hold the “C:” or whatever drive you started on.

Tags: , , ,

Utility: Find files files older than a certain amount of days

Monday, June 13th, 2011

I had to write a backup script the other day and used the find command to find the files that were older than a given amount of days.

DAYS_TO_KEEP=7
find /path/to/files* -type f -name *.bak -mtime $DAYS_TO_KEEP -exec rm {} ;

The -name argument uses the escaped asterisk so command completion does not replace it with all the file names.

The -mtime argument takes a number that is the number of days to look at.

The -exec argument takes a command and the {} is replaced with the current file name and the ; is the end of the command.

This is a small part of the power of the find command.

Linux: watch command

Monday, March 14th, 2011

So every once in a while I want to see the progress of an upload or backup. So I turn to the watch command in Linux. The watch command gives the user the ability to re-run a command and see the updated output.

The syntax is ‘watch -n -d .

So for watching a file downloads progress you could run the following:

watch -n 30 ls -l filedownloading

This would show the ls output and you could watch the size of the file grow as it is dowload progress.

Testing perl modules

Wednesday, January 5th, 2011

A quick way to test a perl module with a new method is to use a command like the following:

$ perl -MeTechTips::IssueTracking::ClearQuest -we '$tr =  eTechtips::IssueTracking::ClearQuest->new();'

This will test the new and ensure that you have the correct syntax if you do not have an easy test environment for your code, for example as I do not have a ClearQuest instance to test with.