Tag Archive
android ant array BAT Browsers C# C/C++ CSS dd ddrescue DOS droid droidx editor editors find Firefox fix formatting GUI Hardware hash html Java Linux OS perl PHP Programming properties Python review Ruby scripting Shell Swing syntax system tools utilitie Utilities utilization vi Video Windows Wordpress
Linux: Processor info and speed
There are times that I get on a Linux box and want to know about some of the resources available to me. There are tools that can accomplish this, but there are some files that contain basic settings about the resources available to the system. One of those file is:
/proc/cpuinfo
This file contains information about the Processors and you can do a simple:
more /proc/cpuinfo
to see all the details or you can grep for certain fields:
processor -- This will show you how many processor entries there are. cpu MHz or MHz -- This will show you the speed of the processors. cache size -- This will show you the cache size available to the processor. model -- This will show you the type and model of the processor you are using.
There are quite a few more fields, but these are the ones that I check the most.
Perl: Hashes
Here are some basics about Perl hashes that can be helpful to be used when an you need to associate a label with some value such as for names and telephone numbers or account numbers with amounts owed.
Create a hash variable.
my %hashvar;
Create a hash reference
my $refhash = {};
Add a value to a hash variable.
$hashvar{'somekey'} = 'someval';
Add a value to a hash reference.
$hashref->{'somekey'} = 'someotherval';
Access a value in a hash.
print $hashvar{'somekey'};
Access a value in a hash reference.
print $hashref->{'somekey}';
Access all Keys in a hash variable.
foreach my $k (keys(%hashvar))
{
print $hashvar{$k};
}
Access all Keys in a hash reference.
foreach my $k (keys(%$hashref))
{
print $k . "\n";
}
Access all Keys and values in a hash.
while (my ($key,$value) = each %hashvar)
{
print "Key: " . $key . " has value:" . $value . "\n";
}
Access all Keys and values in a hash.
while (my ($key,$value) = each %$refhash)
{
print "Key: " . $key . " has value:" . $value . "\n";
}
Assigning multiple values to a hash
%hasvar = ( 'test2' => 'myval', 'test3' => 'myval3' );
Note: This will clear the existing hash.
Delete a value from a hash
delete $hashvar{'key1'};
Delete a value from a hash reference.
delete $hashref->{'key1'};
Linux:Script to find files
I regularly have to find where a file is located and usually use find:
find . | grep
Where
#!/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
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’).
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.
Windows: Grep for windows
If you don’t have access to a Unix environment on your windows box, you can still search files with findstr. The findstr command will allow you to search files close to the same way as grep.
findstr /S "search string"
will recursively find your string in the current directory and subdirectories.
There are more options that you can find if you run the following command:
findstr /?
will show you the help message.
Capturing the screen in Windows
For the longest time, I thought the “Print Screen” or “PrtScr” Key on my keyboard was a throw back to much older computers with sheet fed dot matrix printers and would just print out the current Dos command shell.
Once I figured out how to use it in windows, it has become an invaluable tool.
I used to create presentations of source code issues. It was a great help to be able to grab a copy of the screen and place it in a Powerpoint slide or Word Doc.
If you need a complete screen capture or even just the current window, here is what you can do.
If you need to capture the complete screen, press the “Print Screen” button located near the top of the keyboard with the scroll lock or Pause key. It may seem like nothing has happened, but the complete image of your current desktop has been saved into the clipboard in windows.
If you want to just capture the current window, make sure the window you want is active and press the Alt and “Print Screen” keys together.
Now open Paint, MS Word or any other program capable of handling images, and type Ctrl-V. This will take the image that has been captured into memory and place it in the program. Now you can manipulate the image to crop it or use it in your document.
This is a built in function of the MS Windows Operating system.
If you need more control there are other applications that can let you capture portions of the screen at any time and create a file with a simple keystroke, such as SnagIt!
How to split large files for emailing
I had to send a large file (about 100MB) to a client for analysis. They did not have an anonymous ftp server so I had to figure a way to e-mail the file to them and then have a way to put it back together.
I found the split command very useful. I use Linux when I can but have installed Cygwin on my Windows PC to get the same functionality.
Here is a way to break the file into 5 megabyte chunks:
# split -b 8m veryLargeInputFile
This instance splits veryLargeInputFile 8MB segments named xaa xab xac…xap.
Now put the file back together at the distant end:
# cat xaa xab xac xad xae xaf xag xah xai xaj > veryLargeInputFile
or
# cat * >veryLargeInputFile
Note: ensure xa* are the only files in the directory when using the wildcard
For ASCII files: Split lines — This example splits a document into 1000 line segments.
# split -l 1000 veryLargeTextFile
Use the same process to put the file back together again.
Note: For larger files, find a ftp server or make your filesize increments bigger.
Split options
-b ## — replace ## with the number of bytes you want in a file
-C ## — replace ## with the number of SIZE bytes of lines per output file
-l ## — replace ## with the number of lines per file.
-d — use numeric suffixes for output files instead of alphabetic