Sunday, 21 August 2011

What is SFTP, and how do I use it to transfer files?

SFTP, or secure FTP, is a program that uses SSH to transfer files. Unlike standard FTP, it encrypts both commands and data, preventing passwords and sensitive information from being transmitted in the clear over the network. It is functionally similar to FTP, but because it uses a different protocol, you can't use a standard FTP client to talk to an SFTP server, nor can you connect to an FTP server with a client that supports only SFTP.
You can use SFTP with a graphical SFTP client or at the command line.

Graphical SFTP clients

Using graphical SFTP clients simplifies file transfers by allowing you to transmit files simply by dragging and dropping icons between windows. When you open the program, you will have to enter the name of the host (e.g., quarry.uits.indiana.edu) and your username and password. At Indiana University, students, faculty, and staff can download two SFTP clients (CyberDuck for Mac and WinSCP for Windows) from IUware.

Command line SFTP

You can use command line SFTP from your Unix account, or from your Mac OS X or Unix workstation. To start an SFTP session, at the command prompt, enter:
sftp username@host For example, to connect to your dvader account on the host sithlord.org, you would enter:
sftp dvader@sithlord.org Enter your password when prompted.
Some standard commands for command line SFTP include:
cd Change the directory on the remote computer
chmod Change the permissions of files on the remote computer
chown Change the owner of files on the remote computer
dir (or ls) List the files in the current directory on the remote computer
exit (or quit) Close the connection to the remote computer and exit SFTP
get Copy a file from the remote computer to the local computer
help (or ?) Get help on the use of SFTP commands
lcd Change the directory on the local computer
lls See a list of the files in the current directory on the local computer
lmkdir Create a directory on the local computer
ln (or symlink) Create a symbolic link for a file on the remote computer
lpwd Show the current directory (present working directory) on the local computer
lumask Change the local umask value
mkdir Create a directory on the remote computer
put Copy a file from the local computer to the remote computer
pwd Show the current directory (present working directory) on the remote computer
rename Rename a file on the remote host
rm Delete files from the remote computer
rmdir Remove a directory on the remote host (the directory usually has to be empty)
version Display the SFTP version
 !  In Unix, exit to the shell prompt, where you can enter commands. Enter exit to get back to SFTP. If you follow  !  with a command (e.g., !pwd), SFTP will execute the command without dropping you to the Unix prompt.

Talk to another user in unix

talk - talk to another user

talk person [ttyname ]

Talk is a visual communication program which copies lines from your terminal to that of another user. Options available:
person
If you wish to talk to someone on your own machine, then person is just the person's login name. If you wish to talk to a user on another host, then person is of the form `user@host'
ttyname
If you wish to talk to a user who is logged in more than once, the ttyname argument may be used to indicate the appropriate terminal name, where ttyname is of the form `ttyXX' or `pts/X'

When first called, talk contacts the talk daemon on the other user's machine, which sends the message
Message from TalkDaemon@his_machine...
talk: connection requested by your_name@your_machine.
talk: respond with: talk your_name@your_machine
to that user. At this point, he then replies by typing

talk your_name@your_machine
It doesn't matter from which machine the recipient replies, as long as his login name is the same. Once communication is established, the two parties may type simultaneously; their output will appear in separate windows. Typing control-L (^L) will cause the screen to be reprinted. The erase, kill line, and word erase characters (normally ^H, ^U, and ^W respectively) will behave normally. To exit, just type the interrupt character (normally ^C); talk then moves the cursor to the bottom of the screen and restores the terminal to its previous state.

As of netkit-ntalk 0.15 talk supports scrollback; use esc-p and esc-n to scroll your window, and ctrl-p and ctrl-n to scroll the other window. These keys are now opposite from the way they were in 0.16; while this will probably be confusing at first, the rationale is that the key combinations with escape are harder to type and should therefore be used to scroll one's own screen, since one needs to do that much less often.

If you do not want to receive talk requests, you may block them using the mesg(1) command. By default, talk requests are normally not blocked. Certain commands, in particular nroff(1), pine(1), and pr(1), may block messages temporarily in order to prevent messy output.

mesg [y|n]  

Mesg controls the access to your terminal by others. It's typically used to allow or disallow other users to write to your terminal

y
Allow write access to your terminal.
n
Disallow write access to your terminal.
If no option is given, mesg prints out the current access state of your terminal.

nroff - emulate nroff command with groff  


nroff [ -h ] [ -i ] [ -m name ] [ -n num ] [ -o list ] [ -p ] [ -r cn ] [ -S ] [ -t ] [ -T name ] [ -U ] [ -v ] [ file ... ]

The nroff script emulates the nroff command using groff. Only ascii, ascii8, latin1, utf8, nippon, and cp1047 are valid arguments for the -T option. If an invalid or no -T option is given, nroff checks the current locale to select a default output device. It first tries the locale program, then the environment variables LC_ALL, LC_CTYPE, and LANG, and finally the LESSCHARSET environment variable.
The -h and -c options are equivalent to grotty's options -h (using tabs in the output) and -c (using the old output scheme instead of SGR escape sequences). The -C, -i, -n, -m, -o, and -r options have the effect described in troff(1). In addition, nroff silently ignores the options -e, -q, and -s (which are not implemented in troff). Options -p (pic), -t (tbl), -S (safer), and -U (unsafe) are passed to groff. -v shows the version number.  

Saturday, 20 August 2011

'jobs' and 'process'

You run a "job" (or schedule it for later running) While its running, its a "process".
 
The ps command can show you the process ID number that the server assigned to your job which is useful for things like shutting it down early if you want to.

The lsof command is mostly useful to see what processes, files, directories, libraries, etc are being accessed by a job.

There is not much difference between jobs and processes. A job may consist of one ore more processes. Not all processes are jobs.

ps lists running processes with various statistics per process


lsof lists open files and the processes that opened them



ps
Typing ps alone would list the current running processes. Below is an example of the output that would be generated by the ps command.
PID   TTY   TIME   CMD
6874  pts/9   0:00     ksh
6877  pts/9   0:01     csh
418    pts/9   0:00     csh


ps -ef
Display full information about each of the processes currently running.
UID PID PPID C STIME TTY TIME CMD
hope 29197 18961 0 Sep27 ? 00:00:06 sshd: hope@pts/87
hope 32097 29197 0 Sep27 pts/87 00:00:00 -csh
hope 7209 32097 0 12:17 pts/87 00:00:00 ps -ef


% ps -gx
To find out all the process ID numbers of processes you are running, type:
        
Here is an example of output from this command:
          PID TT STAT  TIME COMMAND
        12030 p5 S     0:01 -csh (csh)
        12068 p5 T     1:46 emacs foo
        12788 p5 R     0:00 ps -gx

ps -l
Displays processes including those that are in a wait state, similar to the below example.
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 0 T 0 12308 29722 0 80 0 - 16136 finish pts/0 00:00:00 pico 0 R 0 12530 29722 0 80 0 - 15884 - pts/0 00:00:00 ps 4 S 0 29722 29581 0 80 0 - 16525 wait pts/0 00:00:00 bash
 
To find the job numbers of all the background jobs you are running, type:
% jobs

 

Pipes

To see who is on the system with you, type
% who 

One method to get a sorted list of names is to type,
% who > names.txt
% sort < names.txt 

This is a bit slow and you have to remember to remove the temporary file called names when you have finished. What you really want to do is connect the output of the who command directly to the input of the sort command. This is exactly what pipes do. The symbol for a pipe is the vertical bar |
For example, typing
% who | sort 
will give the same result as above, but quicker and cleaner.

To find out how many users are logged on, type
% who | wc -l

grep

grep is one of many standard UNIX utilities. It searches files for specified words or patterns.

% grep science science.txt 

As you can see, grep has printed out each line containg the word science.
Or has it ????
Try typing
% grep Science science.txt 

The grep command is case sensitive; it distinguishes between Science and science.

To ignore upper/lower case distinctions, use the -i option, i.e. type
% grep -i science science.txt 

To search for a phrase or pattern, you must enclose it in single quotes (the apostrophe symbol). For example to search for spinning top, type

% grep -i 'spinning top' science.txt 

Some of the other options of grep are:

-v display those lines that do NOT match
-n precede each matching line with the line number
-c print only the total count of matched lines


Try some of them and see the different results. Don't forget, you can use more than one option at a time. For example, the number of lines without the words science or Science is
% grep -ivc science science.txt

clear (clear screen),less,wc (word count)


Clears the screen.

Syntax
clear

Examples
clear

Clear the screen.

Additional information
Many newer Unix systems and Unix variants will also accept the cls command or have cls as an alias for clear.


less

The command less writes the contents of a file onto the screen a page at a time. Type
% less science.txt 

Press the [space-bar] if you want to see another page, and type [q] if you want to quit reading. As you can see, less is used in preference to cat for long files.


Simple searching using less

Using less, you can search though a text file for a keyword (pattern). For example, to search through science.txt for the word 'science', type

% less science.txt
then, still in less, type a forward slash [/] followed by the word to search
/science

As you can see, less finds and highlights the keyword. Type [n] to search for the next occurrence of the word.

wc (word count)

A handy little utility is the wc command, short for word count. To do a word count on science.txt, type

% wc -w science.txt

To find out how many lines the file has, type
% wc -l science.txt




Head And Tail

head is a program on Unix and Unix-like systems used to display the first few lines of a text file or piped data. The command syntax is:

head [options] <file_name>
By default, head will print the first 10 lines of its input to the standard output. The number of lines printed may be changed with a command line option.

The following example shows the first 20 lines of filename:
head -n 20 filename 
This displays the first 5 lines of all files starting with foo:
head -n 5 foo* 
 
Tail Command 
tail [options] <file_name>

By default, tail will print the last 10 lines of its input to the standard output. With command line options the number of lines printed and the printing units (lines, blocks or bytes) may be changed. The

following example shows the last 20 lines of filename:
tail -n 20 filename
This example shows the last 15 bytes of all files starting with foo:
tail -c 15 foo*
This example shows all lines of filename from the second line onwards:
tail -n +2 filename

File monitoring

tail has a special command line option -f (follow) that allows a file to be monitored. Instead of just displaying the last few lines and exiting, tail displays the lines and then monitors the file. As new lines are added to the file by another process, tail updates the display. This is particularly useful for monitoring log files. The following command will display the last 10 lines of messages and append new lines to the display as new lines are added to messages:

tail -f /var/adm/messages 
In cases when the user is following a log file that rotates then it  is advisable to use the -F option as it keeps folllowing the log even  when it is recreated, renamed, or removed as part of log rotation.

tail -F /var/adm/messages 
To interrupt tail while it is monitoring, break-in with Ctrl+C. This command can be run "in the background" with &,