elementary shell and sftp

You need to be a Power User!

version 0.1
August 2011
author: smh

If you understand the console-based commands for invoking or controlling applications, you have a POWERFUL advantage over those who are limited by knowing only GUI-based control. When you know what's really going on, then you are more easily able to ramp up your knowledge when confronted with a new GUI interface that simplifies (i.e., "dumbs down") interaction to an application you already know.

To that end, we have here a golden opportunity to learn some console-based shell commands (the shell is the part of an operating system that interacts with a user) and some sftp commands.

Keep in mind, a GUI interface interprets your mouse clicks into something rather like shell commands, then those commands are executed. It may look like the mouse click does something directly -- but that would be wrong.

If, after this little paper, you feel you need more work in this area, there are a huge number of tutorials on the Web and there are a myriad of technical books. Get yourself comfortable and look them up.

Files, Directories, Paths

The file system is organized into a tree-like structure. At the top of the the file system is the root directory. All files and directories in the file system can be reached from the root directory. A directory can "hold" from zero to many directories and zero to many files. A file cannot "hold" anything (in the file structure).

From a specific directory or file looking "up" there is one path to the root. From a specific data file looking "down", there is no where else to go -- a file cannot "hold" anything in the file structure. From a specific directory looking down, there are from 0 to many possible branches.

A picture may help here. insert picture of approximately:

/
	Stuff/
		HisStuff/
			f1.txt
			Pictures/
				p1.jpg
				p2.jpg
			f2.txt
		MyStuff/
			file1.txt
			file2.txt
			/Projects
				P1/
					p1.java
					p1.class
				P2/
					p2a.java

Speaking casually, we say a directory contains directories and files, and a file contains data.

A directory or a file is identified in the file system by its path. A complete path for a directory or file is the complete specification of all directories from the root to the desired destination. A relative path is the specification of directories to a desired destination but starting at a location different from the root. The path to the desired file (directory) relative to a specific starting directory.

There are two special names used in the file system. '.' refers to the current working directory. '..' refers to the working directory's parent.

Complete path examples
Using the picture already given, the complete path for the file file1.txt is /Stuff/MyStuff/file1.txt. The complete path for the directory named P1 is /Stuff/MyStuff/P1.
Relative path examples
Using the picture already given, if the current working directory is P1 (e.g., pretend you're editing p1.java), then the relative path to Projects is .., and the relative path to P2 is ../P2. If the current working directory is P1, then the relative path to f1.txt is ../../../HisStuff/f1.txt.

Open a shell

to Mac OS X

The Terminal application opens a shell. Terminal is typically located at Applications | Utilities . If you can't find it there, then search (the icon is a magnifying glass in the upper-most, right-most corner of the screen) for it.

to Windows

The I forget application opens a shell. ... is located at Start | All Programs | Applications. Another way to invoke the shell in Windows is to choose Run... and type cmd or is it command.com?to the buffer.

A table of a few shell commands

This table gives a few useful shell commands and "flags" to the commands that are particularly useful.
OS X OS X flags Win Win flagsaction
man cmd on line documentation of shell command cmd
ls -l give more information dir /w wide output formatlist contents of current directory
ls path -l dir path list contents of path
pwd print path of current working directory
cp f1 f2 copy f1 f2 create a copy of file f1 and place in file f2
mv f1 path move file f1 to location given by path
cd path change working directory to path
pwd print (on screen) path of working directory
rm f1 del f1 delete file f1
mkdir d1 create a new directory named d1 in current working directory
rmdir d1 delete directory named d1
vi f1 edit f1 open plain text editor on file f1
sftp username@hostnameopen sftp client to host hostname using user username
ssh username@hostnameopen ssh connection to host hostname using user username

Open an sftp connection

Type sftp username@hostname to invoke an sftp client. The client will open an sftp connection to the sftp server that is running on the host machine named hostname.

Your machine (running the sftp client for you) is the local computer. The machine running the sftp server for you (at hostname) is the remote computer. The local computer uploads data to the remote computer and downloads data from the remote computer.

A table of useful sftp commands

command action
? print a list of ftp commands
put f1 uploads file f1 in local working directory to remote working directory
get f1 download file f1 to local working directory from remote working directory
mkdir d1 create new directory in remote working directory
pwd print path of remote working directory
cd path change remote working directory
ls print contents of remote working directory
lpwd print path of local working directory
lcd path change local working directory to path
lls print contents of local working directory
bye end sftp connection and close sftp client

A small gotcha

If you are sftp-ing to people.emich.edu, access permissions on files you upload are automatically set properly (read and execute for directories, and read for files) because people.emich.edu is configured to serve as a web server for EMU people. However, if you are trying to set up a website on another, perhaps more general purpose machine, then you may have to set access permissions yourself.

Setting access permission with sftp is not as intuitive as setting them with a shell. The same command chmod is used, but sftp requires numeric arguments rather than symbolic. To give read access to index.html for all users, use
chmod 644 index.html
To give read and execute access to directory Dir1 for all users (you need both read and execute to get the behavior you probably want) use
chmod 755 Dir1

Self-Test

What do the following shell commands do? What do the following sftp commands do?