Computing & Communications Center
Helpdesk

UNIX File Management

Shell

Every time you log in to your UNIX account, you are presented with a shell. The shell is the most basic program that allows you to interact with a remote computer. Using a shell follows a simple pattern. First, you enter a command into the shell using the keyboard, and then press the Enter or Return key. The remote computer will then execute the command, and the command's output will be displayed in the shell. The shell then waits for more input/commands. Several different shell programs are available on the CCC UNIX machines, and each shell varies slightly.

The shell program called 'bash' is set as the default shell for all new accounts. To see which shell program you are currently using, you can issue this UNIX command printenv SHELL

Changing Your Shell

If you'd like to switch shell programs, a link is available for doing so on WPI's Account Maintenance page. After navigating to this page:

  1. Click the Changing your shell or finger info link under the Modifying/Updating WPI User accounts heading.
  2. Log in with your WPI username and password when prompted.
  3. Choose a new shell from the dropdown menu at the top of the new page that appears.
  4. Click the Set login [username] shell to button to confirm your shell selection.
The next time you log in to your UNIX account, you will be using the shell that you selected.

Path

Absolute path refers to the complete location of a file, or the path the system uses to find it. For instance, given that a username hunter, located on /home, has a file called pizza in a sub-directory called FOOD, the absolute path is /home/hunter/FOOD/pizza. This path is used by the system to find the file, in much the same way that you use a map to find a destination.

Relative path is the path to a file from the directory you are currently located in. For example, if you are in /home, hunter/FOOD/pizza is the relative path from the above example.

You don't know what drive someone is on? No problem, there's a shortcut. The ~ character can be used before a username to tell the system to look for the drive on its own. In this way, /home/hunter can be abbreviated as ~hunter, and your own home directory may be abbreviated as ~/.

Understanding how paths work is important, as they are used in conjunction with many commands. Using the above example, at any time you can type cd ~hunter to change to that directory, or more ~hunter/FOOD/pizza to read that file. cd is the change directory command, and more is a program used to view text files one screen at a time. Use cd path to change to a new directory, and cd alone at any time returns you to your home directory. The pwd command (an abbreviation for 'present working directory') shows you what directory you are currently working in. Note that you can't enter any directory or read any file, just those that have the permissions set to allow you to access them.

Directories

Your home directory is simply the top directory in your account. It is the directory you default to when you login. A sub-directory is simply a directory within another directory. Continuing the example, FOOD is a subdir of ~hunter.

If you want to create a new directory the command is mkdir directory_name.

There are two ways to create a subdirectory in one of your directories. The first is to cd to the directory in which you wish to create the subdirectory and enter mkdir directory_name. This will create the subdirectory with the name you supply.

The other way is to use mkdir /path/to/directory_name from any directory. This will create the new directory at the end of the path specified. If you were to enter mkdir ~/Eris, you would create a new dir called Eris under your home directory.

To remove a directory, the command is rm -r directory_name, used in the same way as mkdir. This command will delete the specified directory, as well as any files and subdirectories inside it.

Files

These are the basics dealt with in any computer system.

A file can represent stored text, a program, a digital picture...basically anything! Even so, all files are handled in the same way. In fact, in UNIX, a directory is simply a special file. To move a file you use the command mv path/file path/newfile. For example, mv ~/stuff ~/subdir/oldstuff will move the file stuff from the home directory into a file named oldstuff in the directory subdir. If you leave the last filename off the file will be moved to the new directory, but retain its original name. UNIX does not have a 'rename' command; files can be renamed simply by moving them to the same location with a different name. For instance, typing mv ~/old_name ~/new_name renames the 'old_name' file to 'new_name'.

Note: Be careful when using mv, as it will overwrite any files with the same name that are in the destination directory!

p>To make a copy of a file use the command cp in the same way as mv. This will create a new copy of the file in the specified location, while leaving the original file in place. You can copy directores in a similar fashion, by using the command cp -r.

You can delete or remove files with the rm command, as in rm filename. If rm prompts you with rm: remove filename?, simply press y and Return.

Note: This prompting is not standard, so don't expect this to be the same at all sites. At WPI all new accounts are created with rm aliased to rm -i which triggers this prompting.

You can list files in your working directory with the ls command. On its own, ls will list your normal named files and directories. There are also dot-files with are normally used by the system to store information used by applications. By default, ls doesn't display these files. To include thes files in the listing, use ls -a. For information such as file size and date of last change, use ls -l filename. These flags can be combined, as in ls -al, which will provide complete information for all the files in the directory.

File listings may be more than a screen in length. To counter this problem you can pipe the output through the more program. This is done with the | symbol, as in ls -al | more.

For information on changing the file permissions under UNIX, please see the Help Desk's chmod command page.

Lost Files

If you accidentally lose a file or directory by some mistaken file commands or other typo, you might be able to get them back, since checkpoints of the file system are taken every few hours. If you make a file and delete it, you will very probably not find it in a checkpoint, but if the file is there overnight and is then lost, you should be able to find it in the file system snapshot.

Wildcards

If you've ever played card games, you probably know the principle behind a wildcard. It is something that can assume a number of differnent values. Wildcards also exist in UNIX and they make performing certain tasks very easy.

The most commonly used wiledcard is the asterisk, *. When an asterisk is used in a command, the system looks for all possible completions from 0 characters up. Examples: rm n* will remove all files that have names starting with 'n', while rm q*m will remove all files that start with 'q' and end with 'm'.

The question mark, ?, is also a wildcard. It is used for single character replacement. Whereas the asterisk replaces a string, ? will replace one (and only one) character. If you had files hw1, hw2, and hw3, you could delete them using rm hw?.

The bracket pair, [ and ], can also be used for wildcards. These are used for pattern matching. Using the previous example, you could also remove the files with rm hw[1-3]. If you wanted to keep hw2, you could use rm hw[1,3]. Or you could use the negation available. Instead of file[pattern] you use file[^pattern]. The caret, ^, means to perform the operation on all the matching files except those named by the user. The ^ must be the first character inside of the brackets In this example you would use rm hw[^2].

Flags

A flag is the term used to refer to options available with commands, such as -r, -l, etc. All the dash-* options are called flags.

Odd Files

At some point you may accidentally create a filename with a special character in it, i.e. home*ork. If you tried normal commands to move or remove the file, they wouldn't work. There are ways around this; simply enclose the filename in single or double quotes, ie rm 'home*ork' or rm "home*ork", or use the backslash, \, as in rm home\*ork. A backslash will de-specialize the character immediately following it, allowing commands to function normally.

A special case is creating a file with a hyphen, - as the first character. This will cause commands, such as rm or mv, to view the filename as a flag. To solve this problem, separate the command and filename with an isolated hyphen, as in rm - -kallisti.

UNIX Manual Pages

If you want to learn more about a command, UNIX includes built-in manual pages, or man pages. Simply enter man command, such as man rm, to display the online manual for that command. Most of the common commands will have a manual page. It is a good idea to do this once you have a basic understanding of a command, as most commands have several flags.

man can also be used to research new commands via the -k flag. Typing man -k keyword will return all commands with keyword in their description. Simply use keywords you feel are related to the topic you're interested in.

Maintained by itweb.
Last modified: Aug 23, 2010, 16:02 EDT
[WPI] [Home] [Back]