Basic UNIX Commands

Now that you've logged onto the acme system and you know how to get some basic information on its commands, here is a set of useful, basic commands.

ls This command gives you a directory listing. It is very similar to DOS's dircommand. It lists all the files and subdirectories contained in the directory you are currently working in. Like many commands in UNIX, this command has many options and arguments that can be used to show more information.

One of the most useful options is the -F flag. Try typing:

ls -F

and you'll see that there are some characters at the end of some of the file names. Read the man pages for ls to find out what the -Fflag does.

You can also use this command to list only a subset of information in the current directory. Let's say that you want to look through the current directory for all files starting with the letters "da". To do this, you would perform a wildcard search:

ls blah*

The character '*' indicates a wildcard. This means that the operating system will look through the current directory to find any filenames that start with the letters "blah" and list them, no matter what the filenames end with.

cd This command will change your working directory. For instance, if you were looking at the "Part1" folder in the example from the UNIX File Organization section, and you wanted to open the "Lion" folder, you could type

cd Lion

to make the "Lion" folder the current one. If you type

cd ..

it will move you up one directory (back to Part1, in this case). If you type

cd

alone, it will take you back to your home directory -- the one you start out in whenever you log in. Also, typing

~

automatically stands for your home directory. Therefore cd ~ will get you there, and pico ~/filename.txt will let you edit, for instance, "filename.txt" in your home directory.

rm This command allows you to remove a file from your account. The format is:

rm filename

which would remove the file called "filename" from your account. Please note that it does not remove directories that are located inside the current directory without proper flags. Please consult the man pages for more information about the flag s.

Unlike Windows and Macintosh, there is no "Garbage" folder or "Recycle Bin". When you remove a file, it is a permanent decision, so be careful when using this command (OIT does backup all student accounts nightly, but they will only restore files in extreme circumstances)!

rm supports wildcard arguments, much like ls does. To use the last example, let's say that you wanted to remove all files inside the current directory starting with "da". Simply type

rm da*

and all files starting with "da" will be removed. As was noted above, this can be somewhat dangerous. You might delete some files that you don't mean to if they also fit your criteria. To counteract that, we recommend that you use the -i option when performing mass deletions. This option tells UNIX to ask you if you want to delete each file that it is about to delete. The dialog would look something similar to this:

%prompt% > rm -i da*

Remove daniel? (y/n) y

Remove daniel.txt (y/n) n

Remove daniel.doc(y/n) y

%prompt% >

After this command is completed, both daniel and daniel.doc will be removed from your directory. daniel.txt will remain.

Note that using rm -r will recursively delete directories with subdirectories, so that you don't have to delete all the files manually.

pwd This command will print out the current "working directory." (i.e. what directory you are in right now)
passwd (Not to be confused with pwd) This lets you change your gt account password. You are required to change your password every 80-90 days, and you will be plagued with daily e-mails if you do not change it, or if you change it to something that can be easily guessed (yes, OIT *will* try to crack your password!). Make sure your password is at least 6-8 characters long and includes both lowercase and uppercase letters, as well as numbers.
mkdir This command will create a directory.
rmdir This command will remove an empty directory. Use rm -r to delete all the files first.
mv This command allows you to move a file to a new location or give a file a new name. To rename a file, the format is:

mv old_filename new_filename

In this case, "old_filename" is the old name of the file, and "new_filename" is the name of the new file. For example,

mv homework1.backup homework1

would rename "homework1.backup" to "homework1". Note that if you already had a file called "homework1", you would have erased it and replaced it with the contents of homework1.backup!

To move a file into a directory, the format is:

mv filename directory_name

where "filename" is the name of the file you are moving, and "directory_name" is the name of the directory that you want to put the file in.

cp This command allows you to copy a file to a new location, without erasing the old location. If you wish to copy entire directories, use the -r flag, like this:

cp -r ~/public_html/images .

This command means: "Copy all files in my home directory under public_html/images, to *this* current directory (the . dot always means "current directory" by the way)"

more This command allows you to view the contents of any text file without altering the contents of the file.

more my_file

displays the contents of the file one screenful at a time. In other words, it displays information using as much as the screen as possible until it has filled the entire screen, at which point it pauses. To advance the display one line at a time , press Enter. To advance the display a whole screenful at a time, press the Space key. To quit, press 'q', or continue viewing until you reach the end of the file. You cannot scroll back up the file with more, so it is quite outdated. Here is the replacement:

less "Less is more and more is less."

That statement essentially sums up the functionality of less. Less is another program that allows you to view text files, and behaves almost exactly like more except for two key differences. Less allows you to scroll back up the file using the up-directional key on your keyboard. In addition, you can only exit a file by pressing 'q', not by simply reaching the end of the file.

Side note: Both less and more allow you to search the text file you are viewing to find instances of a specific word or phrase. Simply type '/' and the phrase you want to search for (keeping in mind that this is case-sensitive) and press Enter. Both less and more will bring you to the next instance of that phrase in the file. People who are using more to view documents might have difficulty using this functionality with smaller text files. If more has reached the end of the text file you are viewing, it brings you back to the UNIX prompt without pausing. Be sure that you are using less or more before trying to perform your search.

who Used without arguments, who lists the login name, terminal name, and login time for each current user. Who can also be used to find out what your user name is. Just type either

who am I

or

who is who

For information regarding output when using single arguments, consult the man pages.

cat Displays a file to the screen all at once:

cat my_file

It's very useful for checking your files for strange characters which may appear if the file is ftp'd in the incorrect mode. You can see these characters by using the -v flag:

cat -v my_file

For more information, check the man pages.


Return to Lab 2.