Friday, 23 October 2009

Extract zip files in Unix-like environment

To extract a tar.bz2 file, use the command (note the j option)

Code:
tar -jxvf filename.tar.bz2
To extract a tar.gz file, the the command (note the z option)

Code:
tar -zxvf filename.tar.gz

Monday, 19 October 2009

Thursday, 4 June 2009

Useful Linux Disk Commands

When we need to manipulate the disks under control of Linux, it is more difficult to get the basic information of them, such as which file point is corresponding which partition among those disks and which directory in which partition or disk. This situation is more common if we are working in a network environment namely some Network File Systems are involved.

Here is some handful Linux Commands which are helpful if you were undergoing this terrible situation.

1. fdisk -l
used to list the several partitions in the disks of the system

2. parted /dev/sda print
This is the same as fdisk. It just concentrate on a specific disk.

3. df -h
used to report file system disk space usage

Tuesday, 3 March 2009

scp and sftp for file transfers

Copyright Phillip Farrell. Last revision November 24, 2004

Table of Contents:

  1. Getting ssh programs for your computer
  2. ssh for remote login
  3. ssh authentication methods
  4. scp and sftp for file transfers
  5. ssh to run remote commands

The ssh protocol is designed to create encrypted "channels" for communication between computers. In addition to remote shell logins, these channels can be used for other types of connections. In particular, there are standard programs for copying files between computers using ssh encrypted channels. These program are called scp and sftp on Unix systems. GUI based programs that implement the same protocols are available for Macintosh and Windows PCs.

scp is a direct replacement for the old Berkeley rcp command, and is used to copy one or more files between computers with a single command line. sftp is a direct replacement for the standard ftp program, and allows you to interactive browse a remote file system and select files for copying (in either direction) with interactive commands.

scp

scp works just like the standard local Unix copy command, cp, except that you put the name of the remote system, followed by a colon, in front of the pathname specification for the remote file.

You can copy either direction, to or from the remote system, with scp. If no directory is specified for the remote system, your home directory there is assumed.

scp will prompt you for the password on the remote computer, or not, depending upon the type of authentication system you have set up for ssh.

Just like cp, scp will silently remove any existing file that is "in the way" of making its copy, at either end. But unlike cp, there is no -i option that you can use with scp to force it to ask you first before overwriting an existing file. If you think that there is any chance that an existing file may have the same name as the one you trying to copy, you need to check first yourself.

If you are copying from a remote computer to the local computer, you can simply use ls to make sure that your desired new file name is not already taken locally. If you are copying from the local computer to a remote computer, run a remote ls with ssh in remote command mode to check if your desired name is already being used remotely, for example,

ssh remotehost ls

Examples:

  • scp myfile toquima:
    Copies "myfile" to my home directory on toquima.
  • scp myfile toquima:/newdir/newname
    Copies "myfile" to "/newdir/newname" on toquima.
  • scp toquima:myotherfile .
    Gets a copy of "myotherfile" from my home directory on toquima and puts it in my current working directory on the local machine (designated in standard UNIX fashion by the "dot" (.) character).

Just like the cp command, scp has a -p option to propagate the permission settings of the original file to the copy (otherwise the copy is made with the normal settings for new files), and a -r option to copy an entire directory tree with one command.

scp creates a completely transparent encrypted data channel between the two machines, so binary data (such as images or executable programs) is preserved correctly. This also means that scp is unable to perform automatic end-of-line termination conversion between different types of operating systems, as can be done with ftp in "ascii" mode. That will not be a problem when copying between Unix systems, which all use the same end-of-line convention.

sftp

ftp has been a standard for file transfer since the beginning of the Internet. It gives you a special interactive login on a remote computer with a limited set of commands that let you perform file operations, such as browsing the remote file system, copying files back and forth, making directories on the remote system, and removing files from the remote system, all subject, of course, to the permissions available to your account on the remote system. The problem with ftp is that it passes your password, and all file contents, across the network in plain-text.

sftp is a direct replacement for the ftp program. It gives you the same type of interactive login with special file manipulation commands. In fact, it provides more file operations than plain ftp. Like ssh and scp, it completely encrypts your password when logging into the remote system and also encrypts all files while in transit, so your password and data are safe from spying hackers.

sftp only works with servers running ssh version 2. Pangea and all Sweet Hall workstations run version 2. If the server you want to use only runs version 1 of ssh, you must use scp instead for file transfers.

Like scp, sftp creates a completely transparent encrypted data channel between the two machines, so binary data (such as images or executable programs) is preserved correctly. Unfortunately, this means that sftp is unable to perform the automatic end-of-line termination conversion between different types of operating systems that can be done with ftp in "ascii" mode. That will not be a problem when copying between Unix systems, which all use the same end-of-line convention. When using an sftp client program on a Windows or Macintosh PC, there may be a local option to perform this end-of-line termination conversion.

Syntax:

sftp remotehostname

Start sftp by simply giving the remote computer hostname or IP address on the command line. After prompting for your remote computer password (skipped if you have setup password-less authentication), you will get a new prompt that looks like

sftp>

At this point, you can give commands to browse the remote file system and copy files back and forth. It is easiest to change the working directory on the local system to the location where you want to find or put files first, before starting sftp, although it does have special commands to move around in the local file system as well as the remote file system.

The basic file manipulation commands are the same as ftp. See the TCP standards - telnet and ftp web page for a description of those commands, or use the on-line manual entry.


REFERENCE: http://pangea.stanford.edu/computerinfo/unix/netcommands/scpsftp.html

Sunday, 22 February 2009

Java Miscellaneous - 1

The differences between final and static?

A final instance field must to be initialized when the object is constructed. This must be guaranteed. And the final instance field can not be modified.

A static instance field will be shared among all instance objects of the class.


How to declare a Constant?
e.g. public static final double PI = 3.14159265358979323846;


Static Characteristic:

A static method can not access a instant field and can only access static fields.

Use the Class name to invoke static methods and static fields, instead of a Object of the class.

Thursday, 12 February 2009

Miscellaneous of Video problem in Linux

The Video problem is a quite complex problem usually. Not because of it is hard to resolve. On the contrast, there are far more different solutions available in most cases for a same problem on the Internet. They usually confused most people. It often happens that after the problem is solved when as much as the solutions have been tried, we even don't know which steps make it happens.

So I really need to figure out my own problem solving procedure.

Firstly, Check the Log of the X window.
That should be in the file of "/var/log/Xorg.0.log".
Something should be useful there.

Saturday, 1 November 2008

Dynamically Allocating Multidimensional Arrays

Dynamically Allocating Multidimensional Arrays

We've seen that it's straightforward to call malloc to allocate a block of memory which can simulate an array, but with a size which we get to pick at run-time. Can we do the same sort of thing to simulate multidimensional arrays? We can, but we'll end up using pointers to pointers.

If we don't know how many columns the array will have, we'll clearly allocate memory for each row (as many columns wide as we like) by calling malloc, and each row will therefore be represented by a pointer. How will we keep track of those pointers? There are, after all, many of them, one for each row. So we want to simulate an array of pointers, but we don't know how many rows there will be, either, so we'll have to simulate that array (of pointers) with another pointer, and this will be a pointer to a pointer.

This is best illustrated with an example:

#include

int **array;
array = malloc(nrows * sizeof(int *));
if(array == NULL)
{
fprintf(stderr, "out of memory\n");
exit or return
}

for(i = 0; i < nrows; i++) { array[i] = malloc(ncolumns * sizeof(int)); if(array[i] == NULL) { fprintf(stderr, "out of memory\n"); exit or return } }

array is a pointer-to-pointer-to-int: at the first level, it points to a block of pointers, one for each row. That first-level pointer is the first one we allocate; it has nrows elements, with each element big enough to hold a pointer-to-int, or int *. If we successfully allocate it, we then fill in the pointers (all nrows of them) with a pointer (also obtained from malloc) to ncolumns number of ints, the storage for that row of the array. If this isn't quite making sense, a picture should make everything clear:



Once we've done this, we can (just as for the one-dimensional case) use array-like syntax to access our simulated multidimensional array. If we write

array[i][j]

we're asking for the i'th pointer pointed to by array, and then for the j'th int pointed to by that inner pointer. (This is a pretty nice result: although some completely different machinery, involving two levels of pointer dereferencing, is going on behind the scenes, the simulated, dynamically-allocated two-dimensional ``array'' can still be accessed just as if it were an array of arrays, i.e. with the same pair of bracketed subscripts.)

If a program uses simulated, dynamically allocated multidimensional arrays, it becomes possible to write ``heterogeneous'' functions which don't have to know (at compile time) how big the ``arrays'' are. In other words, one function can operate on ``arrays'' of various sizes and shapes. The function will look something like

func2(int **array, int nrows, int ncolumns)
{
}

This function does accept a pointer-to-pointer-to-int, on the assumption that we'll only be calling it with simulated, dynamically allocated multidimensional arrays. (We must not call this function on arrays like the ``true'' multidimensional array a2 of the previous sections). The function also accepts the dimensions of the arrays as parameters, so that it will know how many ``rows'' and ``columns'' there are, so that it can iterate over them correctly. Here is a function which zeros out a pointer-to-pointer, two-dimensional ``array'':

void zeroit(int **array, int nrows, int ncolumns)
{
int i, j;
for(i = 0; i < nrows; i++) { for(j = 0; j < ncolumns; j++) array[i][j] = 0; } } Finally, when it comes time to free one of these dynamically allocated multidimensional ``arrays,'' we must remember to free each of the chunks of memory that we've allocated. (Just freeing the top-level pointer, array, wouldn't cut it; if we did, all the second-level pointers would be lost but not freed, and would waste memory.) Here's what the code might look like: for(i = 0; i < nrows; i++) free(array[i]); free(array); Read sequentially: prev next up top This page by Steve Summit // Copyright 1996-1999 // mail feedback