eTechtips

Tech Tips and Insights

  • Bash: test if file exists

    When you need to do some processing in bash and display if a file exists, the following is a simple script that takes the filename in the $testfile variable and reports if it exists or not.

    if [ -f $testfile ]
    then
      echo $testfile exists!
    else
      echo $testfile does not exist
    fi
    
    0 Comments
  • VI: Display tabs and end of lines

    I was working on a system the other day and needed to see where the end of lines were and where there were tabs instead of spaces. And I found the list option to vi.

    This will turn on the display of “$” to indicate the end of line and “^I” to indicate a tab.

    :set list
    

    This will turn off the option to display the end of line and tabs.

    :set nolist
    
    0 Comments
  • Linux:Script to find files

    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

    0 Comments
  • Using find and grep to search files

    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’).

    0 Comments
  • How to add library paths to perl

    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 
    0 Comments
  • Ddrescue saves the day…eventually

    So, after a Linux crash that caused my previous ddrescue to fail(after 350GB), I started to look into how to speed it up, because 30+ hours did not sound like a good idea to me.
    I look into ways to speed it up and using a larger block size did the trick. I experimented with a few different sizes and figured out that using 2M gave me the best throughput. So instead of a couple of megs a second i was getting over 24 MB/s. So about a 4 1/2 hour recovery after that.

    ddrescue -b 2M /dev/sda2 /media/newdisk/olddata /media/newdisk/logfile
    

    The ddrescue process will attempt to recover any lost sectors and retrim the lost data. Luckily, I only lost about 3100k of data so the most of my data is safe.

    You can attempt to rerun ddrescue with the -r <# of times to retry> and it will continue to attempt to read those failed sectors that were unrecoverable. There is no guarantee that you will get back all the data.

    Once this data is recovered, you can (hopefully) mount this file as a disk under linux.

     mount /media/newdisk/olddata /mnt/newdisk 

    Now remember to back up your data and you will not go through the pain that I did to recover this data.

    Part 1 of article

    1 Comment
  • When a hard drive fails, ddrescue could save the day

    So I was trying to do some work the other morning in my hotel room and my Windows 7 computer gave me the dreaded blue screen of death and would not boot past loading a certain library file. I was able to boot into an Ubuntu disk that I happened to have and found that there were many S.M.A.R.T. errors on the disk and wished I had thought to monitor this. So it seemed that my drive was cooked and I could not get any of the data from it.

        I was disappointed that Spinrite was unable to save me from the problems that I was having and I paid 90 bucks for it. So I turned to tools in Linux that might do the job. I had used the dd utility to copy the contents of my drive to a new drive in the past, but dd will exit on the first error. So I found that there is an addition to the dd program called ddrescue which is part of the GNU Project.

    This utility will let you copy the contents of a disk or partition and allow you to skip the errors or even retry the error spots a number of times.

    The command line is something like:

        ddrescue -r 3 /dev/sda1 /media/newvolume/diskimage.iso /media/newvolume/logfilefordiskimage
    

    The really nice thing is if you have to stop the command for some reason, it will restart from the end of the log file. It does take quite a bit of time. I am 6 1/2 hours into recovering a 500GB disk, but it beats losing all my data. I will follow up this post with the results of the rescue and the next steps I took to recover the data.

    Part 2 of article

    1 Comment
  • Mounting drives in Linux

    Sometimes you have a drive that your want to mount under Linux. The mount command can make the drive available to you. But you need to also create a directory to hold the mount point.

    mkdir /mnt/drivepoint
    

    Then you can mount the drive to that mount point by using the mount command and giving the device identifier, in this case it is /dev/sda1, and the mount point which we created as /mnt/drivepoint. One thing to note, is that the mount will use whatever directory you specify, so if there are files located in the directory you use as a mount point, they will no longer be visible.

    mount /dev/sda1 /mnt/drivepoint
    

    This must be run as root or with the sudo command.

    0 Comments
  • WordPress blank pages after updating

    Recently I upgraded to WordPress 3.2.1 and in the midst of this I was fixing a problem in my functions.php file. It turned out that using the editor happened to leave an extra line at the end of my functions.php file and this caused all of the admin pages to no longer load. I removed the extra space at the end of the file and now I am able to admin my site.

    So check any recently updated files and make sure there are no erroneous spaces at the end of the files.

    0 Comments
  • WordPress fix for 28px margin issue

    I was tearing my hair out the other night trying to figure out why there was a 28px margin at the top of the page that I don’t remember setting up. I searched the for the issue and found that there is an issue with the version I am stuck on and the work around is to put the following code just before the </body> tag in footer.php.

     <?php wp_footer(); ?>;
    0 Comments