Basic Linux Commands
The Linux operating system is currently very similar to the Microsoft Windows Operating System. The primary differences are that Linux is free (thus it is very popular with researchers, universities, and many private corporations), Linux can run on many different hardware architectures (from servers to microcontrollers), and Linux is still strongly controlled form the command-line. While most will find that file management and program execution from the GUI is straight-forward, the equivalent commands at the terminal will be useful when compiling programs. The following sections detail basic file management commands and informational tips that can be executed from the terminal.
File System
The file system of Linux is laid out similarly to that of Microsoft Windows. Both systems use a tree structure where directories (or folders, as they are called in Windows) contain files and other folders. Unlike Windows, however, there are no drive letters. Instead, Linux uses a path system that is similar to a complete URL path. For example, instead of having the C-drive, the Linux operating system has the root directory, denoted as "/".
All directories/folders are contained within the root directory. Floppies, CDs, flash memory devices, and all other removable and non-removable storage devices are accessed as (what appear to be) subdirectories of the root directory. For example, the home directory for most CSE students is "/cs//". Since the details of accessing other removable devices are cryptic and non-intuitive, they will be omitted for the time being, though they can be researched online and may be covered in later classes.
One additional point of importance to those familiar with the Windows operating system is that file extensions (e.g. ".exe") are not used. While binary files may be executed directly, there is no association that tells the operating system to execute, for example, ".mp3" files with a media player. The program that handles a specific file must be called directly, with the data file as an argument (if that is how the program operates).
Terminal Commands
All Linux commands can be issued from a terminal window and will be executed by the system in the current terminal process. While you can have multiple terminal windows open, it is often useful to understand how to issue commands and what options ara available to make terminal sessions cleaner and more productive.
The first thing that students should be aware of is that the command-line options available for use with any given command or program can be accessed by following the command with the "--help" option, as in:
ls --help
Another source of information about virtually any aspect of the Linux operating system - not just commands - is the infamous "man page", or "manual page", for the command. Most versions of Linux install with the "man pages" automatically, allowing users to look up information about any installed application, feature, or other question they may have. The "man pages" are accessed via the man command, followed by the keyword(s) related the information sought. The following is an example that searches for information on Xemacs:
man xemacs
One additional point is that programs can be executed from the terminal in two ways. The first is by simply typing the program name, as in "xemacs". The other option is to execute the program in a spearate process/window that keeps the current terminal free for use. To do this, simply end your command with an ampersand ("&"). The following example shows how to call the Xemacs editor to open a file in another process:
xemacs myfile.cpp&
The execution of built-in Linux commands, programs, and applications is accomplished by simply typing the name of the executable. For user programs, it is often necessary to specify that the program is located in teh current directory by prefixing the name of the executable with "./" as in:
./testing
Creating Directories
Directory creation is accomplished using the mkdir command, followed by the name that you wish the directory can have. The following example illustrates the creation of a new, short-names directory:
mkdir cs202
Since the modern Linux operating system supports long filenames, as in Windows, directories can contain spaces. If the directory does contain spaces, you must enclose it in quotation marks, as in the following example:
mkdir "Computer Science 202"
Please keep in mind that, just like commands in Linux, the names of files and directories are case-sensitive.
Accessing Directories
Accessing subdirectories of the current directory (or folder) is a simple task. To change the current directory location, use the cd command, followed by the directory to which you want to be, in relation to the current directory. For example, we use the following command to access a subdirectory of the current directory:
cd cs202
To access the parent directory of the current directory, we simply replace the directory name with .., as in
cd ..
You can also change your location to any other directory by specifying the absolute path of the directory where you want to be. For example, to switch to your web directory from any location, use the following command:
cd /cs//public_html/
Listing Directory Contents
The contents of any directory may be listed using the ls command. While there are several arguments that allow you to see the hidden files (those that begin with a ".") and other information, such as the following:
ls -al
Copying Files
Copying files, whether from one location to another or within the current directory, is done with the cp command. The arguments, in order, are the source filename and the destination filename. As one would expect, since long filenames are supported, any filenames containing spaces should be enclosed in quotation marks. The following is an example that copies one file to a subdirectory, giving it another name in the process:
cp sourcefile.cpp ./data/targetfile.cpp
Moving Files
The process of moving files is similar to that of copying files, except that the source file is actually removed. The mv command is useful for moving files, renaming them, and renaming directories. When moving files to directories, it is important to specify that the destination is a directory by ending the directory name with a backslash ("/"). In the following example, a file is moved to a subdirectory:
mv sourcefile.cpp ./data/
Deleting Files
The removal of files is also done using a simple command: rm. The only required parameter is the filename that you wish deleted. While many other parameters can be used, such as the "-r" option to remove directories, the basic usage is:
rm sourcefile.cpp
On This Page...
File System
Terminal Commands
Creating Directories
Accessing Directories
Listing Directory Contents
Copying Files
Moving Files
Deleting Files