Thursday, December 3, 2009

How to download directory using ftp or scp?

When you try to download a directory using ftp, it wont work properly. We need to some tricks to get it. Here is one of such kind.

Let "aaaa" be the directory you want to download from a remote machine(consider this directory also has lot of subdirectories).

Step 1) tar -cvf directory.tar aaaa
- This step will create a tar file "directory.tar" containing directory "aaaa" and it subdirectory.

Step 2) Now you can use the normal ftp or scp(I feel it better than ftp) to download this file to your local machine. The step results in having the directory.tar file in your local machine.

2.a) local machine> ftp remotemachine_ip
password in remote machine : xxxx
remote machine ftp> cd "path to the file to be downloaded"
remote machine ftp> mget directory.tar
directory.tar? give "y"

(or)
2.b) scp -r username@remotemachine_ip:"path to the tarfile in remote machine" "path in local machine where you need the file"
password in remote machine:yyyy

step 3) In your local machine, untar the tar file. You will have what you need.
tar -xvf directory.tar

How to create Recursive Directories?

Here is a trick to create recursive directories

linux > mkdir -p root/subdir1/subdir2/subdir3/

This will create a given directory hierarchy in present working directory(PWD). If you are bored to type "mkdir -p" every time. Add the following in your .bash_profile in home directory.

alias mkdir="mkdir -p"

How to add a path in linux/unix

Although simple, most of us were sometimes confused about the method to append a path to $PATh variable in Linux/Unix.

Ofcourse, the method differs with different type of shells. Here is list a methods for different shells

Eg : shell prompt> echo $PATH ----> Gives existing search path list
/usr/bin:/usr/lib/:/usr/sbin

Let us assume you want to add /usr/site/bin (Path to be added) to the search path.

1) Bash shells :

bash shell prompt> export PATH=$PATH:Path to be added
bash shell prompt> export PATH=$PATH:/usr/site/bin

2) C Shell :

C shell prompt> setenv PATH Existing list of paths:Path to be added
C shell prompt> setenv /usr/bin:/usr/lib/:/usr/sbin:/usr/site/bin