How to Use a Linux (or Mac OS X) Terminal

(0 votes, average 0 out of 5)

Every computer hobbyist, at some point, learns to use Linux.  The transition from using a UI based interface in Windows or Mac to performing many functions through the terminal can be a jarring experience for many people.  Many users find the terminal difficult to use and inferior to navigating a UI.  However, the Linux terminal is an extremely powerful device that many learn to love and use often.  Here is a basic introduction to get your feet wet with a terminal.  It should also be noted that many of these steps also apply to a Mac terminal, is OS X is a Unix based operating system as well.

 

Representing a Directory from the Root

First and foremost, you want to be able to change the directory you're in and look at the contents of directories.  Of course in order to do this, you need to know how to represent a directory.  Each directory of course has a name, and contents. Suppose our file system consists of the following, where a is the top most directory in the file system:

       a
      / \
     b   c
    /     \
   d       e

The / character has multiple meanings in a path.  If it is the at the very beginning of the path, it refers to the root directory.  Do not confuse a in the example above!  We could have other files in the same directory as a, for instance, or even other directories.  To represent a we would type:

/a

Note that simply typing this into a terminal will not do anything -- your terminal needs to know what to *do* with the file or directory.  We will cover that in a bit.  If you place a / in any other place in a directory string, it represents moving into a directory.  Think of it as opening a folder in Windows.  For instance, if I had a file named foo.txt inside of b, I would reference it by typing

/a/b/foo.txt

 

 

Representing a Directory from Another Directory

By omitting the leading slash, Linux intereprets the first file or directory you specify as being located in your current directory.  Suppose we are in directory b. We can reference to a file in d simply by typing

d/foo.txt

We can reference a directory above by using the .. string.  We can also reference the current directory using the ./ string.  If we were in directory b, we can reference a file in a by typing:

../foo.txt

 

Using Terminal Commands

Now we can use our directory representations to actually navigate the terminal.  All terminal commands look something similar to this:

1
<command> <arguments>

Of course this is a very simple representation of something that can get very complex.  The command is either a shell command or a program to run; for instance, "python" or "emacs".  The arguments are strings that the program needs to run.  Sometimes arguments are prefaced by "flags", which are typically optional arguments that toggle some option in the program.  For instance, we can open a file in emacs by typing:

emacs foo.c

And emacs will open the file foo.c.  Note that you cannot use the terminal while emacs is open!  If you'd like to open a program in a seperate thread, such that you can continue using the terminal while the program is running, simply type & at the end of your call.

emacs foo.c &

We could have just well not passed the foo.c argument, and emacs would have opened without any file already open.  Knowing this very basic format is truly all you need to know to run commands from the terminal.  Everything else boils down to knowing useful commands and programs, and how to call them from the terminal.  We will cover some of the essentials here.

 

Navigating the File System

The most useful thing to know is how to move around the file system.  The command you will use the most is cd -- change directory.  cd takes one argument, which is the path to the directory you'd like to switch to.  Using are basic file system above, we can move to directory c from a simply by typing (where everything after a $ is a user input):

[/a] $ cd c
[/a/c] $

You can check the contents of a directory by using the ls (list) command.

[/a] $ ls
b/	c/	foo.txt
[/a] $
 

Creating and Changing Files and Directories

There are a good deal of commands for creating, removing, and copying files and folders.  Listed below are the most useful ones, and how to use them.

 

Make a Directory

Making a directory is simple.  Simple call mkdir and pass the directory name as an argument.

mkdir foo

 

Removing a file or directory

The rm keyword can be used to remove a file. 

rm foo.txt

You will be prompted if you are sure you want to delete the file.  You can pass the -f flag to eliminate these warnings, but be careful!  You don't want to accidentally delete something you wanted to keep.  Directories only count as files if they are completely empty.

rm -f foo.txt

You can remove a directory by passing the -r flag, and instead of a file name, pass the directory name.  This will delete the directory AND all of its contents.

rm -r -f b

Of course,the -f flag is not needed here, but the terminal will prompt for verification for every file within the directory.

 

Copying a File

Files can be copied by calling cp.  The first argument is the source file, and the second argument is the path of the destination file.  You can once again copy entire directories by passing the -r flag. 

cp foo.txt b/copy.txt

If you wish to copy the file with the same name, you may omit the filename for the destination.

cp foo.txt b/

 

Other/Advanced Commands

For more commands, browse the Tech Repo for more Linux command articles.  This article should have given you a good start on learning to use the terminal.  Good luck!


Partner Links:
Last Updated on Thursday, 22 July 2010 15:51  
Related Articles

» Make the Linux or Mac OS X Command Line Easier with .bashrc

Starting to use the command line (or terminal) in Linux or Mac OS X can be very daunting. It is indeed a complex task to learn all of the commands and how to use them together properly. Despite the learning curve, using the command line can make a lot of tasks significantly faster and easier. One way to make the terminal easier to use is by creating a .bashrc file.Most Linux distributions (and Mac OS X) use the as the default terminal shell (although I've heard some ). When initializing, Bash...

» How to connect an Apple Bluetooth Keyboard to Ubuntu (Troubleshooting)

Note: This guide uses programs specific to Ubuntu 10.04 (Lucid Lynx) and newer. For older versions of Ubuntu, please refer to .Sometimes the Ubuntu bluetooth stack can behave erratically and prevent you from connecting your Apple Bluetooth Keyboard (or other bluetooth device). The normal procedure for connecting your bluetooth keyboard involves the following steps:First, make sure the bluetooth applet is running by looking for the bluetooth icon on your panel:If it is not running, press ALT+F2,...

» Redirect STDOUT And Other File Descriptors To A File in Bash

It is possible to redirect the output of a single command in Bash to a file using the > convention. For example, to redirect the STDOUT output to a file, use: ls -l > myfile.txt  However sometimes it would be better to redirect all output from multiple commands to a single file. This can be used by modifying the file descriptor, 1. First, we save a backup of the file handle for STDOUT and then change it to point to the desired file. We can then later restore STDOUT from the...