Archive for the 'Linux' Category

Cron Job entry format

Friday, August 26th, 2011

If you ever need to schedule a job regularly on a Unix system, they have a great utility called cron. This will run a job at a regularly scheduled time.

Here is the format:

* * * * *  command to be executed
_ _ _ _ _ 
| | | | |
| | | | +--- day of week (0 = Sunday - 6 = Saturday)
| | | +----- month (1 - 12)
| | +------- day of month (1-31)
| +--------- hour(0-23)
+----------- minute (0-59)

example of a script that runs every day at 7:

* 7 * * * sendmeupdate

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: , , , ,

Ddrescue saves the day…eventually

Saturday, August 13th, 2011

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

Tags: , ,

Mounting drives in Linux

Tuesday, August 9th, 2011

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.

Tags: , ,

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

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.

Edit the hosts file

Monday, November 1st, 2010

If you have every wanted to set certain IP addresses in your network without depending on a nameserver, you can edit the ‘hosts’ file in your operating system.  This can be done to stop access to any inappropriate sites as well.

The hosts file in Windows is:

%systemroot%System32driversetchosts

This is typically  in:

c:windowsSystem32driversetchosts

On linux this file is:

/etc/hosts

The common layout of the file is

<IP address> hostname [hostname]

So if would look something like this:

127.0.0.1  localhost
192.168.2.222 homeserver homeserver.mydomain.net
127.0.0.1 badlocation

Remember that these lookups happen before looking to a nameserver. So this will override any name lookup. So if you happen to do this on a laptop that you regularly use outside of your home network, make sure that you do not override an address that might be needed when you are not at home.

You will need appropriate privileges to access the file. So you may have to be root or use sudo on Linux and you may have to open an editor as administrator before opening the file on Windows.

Tags: ,