Headlines | Linux | Apps | Coding | BSD | Admin | News
Information for Linux System Administration 

MiniLesson: An introduction to Linux in ten commands


This tutorial is the first in a series of introductory Linux lessons. This first article will cover navigating around a Linux filesystem along with a brief passage -- with examples -- on using ten of the most essential GNU/Linux commands.

You should have access to a Linux system in order to perform the example commands as we progress through the tutorial. If you don't have a dedicated Linux box, you can use a Live Linux CD-ROM-based distribution such as Knoppix. Knoppix will let you run Linux directly from the CD without modifying anything on your hard drive.

Once you're logged in to a Linux system, open a terminal session. Each of the commands covered here will be typed directly into a command line terminal window. Under Red Hat Linux, terminal is found in the 'system tools' section of the menu. (Your system may, alternatively, have a terminal program called 'konsole', 'xterm', or 'shell'. Look around your system for a menu with 'tools' or 'utilities' in the name if necessary.)

The first command we will use is 'pwd' -- which stands for 'print working directory'. The pwd command shows you your current position within the Linux filesystem. The position is known as your 'current working directory'. Type pwd now. The example below shows my command prompt and the pwd command followed by the output from the pwd command:
 [rayy@barton0 rayy]$ pwd
[rayy@barton0 rayy]$
From the output (/home/rayy) we can tell that I am in my 'home directory' -- the directory where I keep my personal files and the directory where I always start out in a new session.

The ls command lets you list files. For example, here is the (shortened) output of an ls command on my system:
 [rayy@barton0 code]$ ls
artdir countdir machine
tsardir sortdir
[rayy@barton0 code]$
Alternatively, you can get a 'long listing' that shows file sizes, timestamp, ownership, and permissions as follows:
 [rayy@barton0 code]$ ls -l
drwxr-xr-x 2 rayy rayy 4096 Feb 3 2002 artdir
drwxr-xr-x 2 rayy rayy 4096 Feb 3 2002 countdir
drwxr-xr-x 2 rayy rayy 4096 Feb 3 2002 machine
drwxr-xr-x 2 rayy rayy 4096 Feb 3 2002 sortdir
drwxr-xr-x 2 rayy rayy 4096 Feb 3 2002 tsardir
[rayy@barton0 code]$
You can also supply a target directory to the ls command. For example, to view the contents of the /tmp directory, I enter the following:
 [rayy@barton0 code]$ ls /tmp
flp kde-rayy mcop-rayy
[rayy@barton0 code]$
For more information on the ls command you can reference the manual page for ls with the following command:
 [rayy@barton0 code]$ man ls
This next command, 'cd', lets you change your current working directory. for example, you can change your current working directory to /usr/bin by entering the following command:
 [rayy@barton0 rayy]$ cd /usr/bin
[rayy@barton0 bin]$
Note that after I entered the cd command, my command prompt changed to reflect the change in the last node of my current working directory. Your command prompt may not be configured to do that.

Change your current working directory to /usr/bin now and enter the ls command.
 [rayy@barton0 code]$ cd /usr/bin
[rayy@barton0 bin]$ ls
addr2line mcheck
addr2name.awk mcomp
addresses mcookie
[rayy@barton0 bin]$
The preceding is a partial listing. There are many, many files in the /usr/bin directory on most Linux systems.

If you have a background in Windows or are familiar with DOS, you are used to file extensions that signify the file type. Linux (and Unix) have no such requirement. That is, an executable program can be named anything. Therefore, a handy command is supplied with Linux named 'file'. For example, I have a file named 'sample.c' in my code directory. I can learn a bit about that file by entering the following command:
 [rayy@barton0 code]$ file sample.c
sample.c: C++ program text
[rayy@barton0 code]$
Alternatively, I can use the '*' wildcard -- which represents all filenames -- to examine all of my code files at once. The following is a shortened example:
 [rayy@barton0 code]$ file *
bbsdir: directory
code.tar: GNU tar archive
genart.c: ASCII C program text
sample.c: C++ program text
xor: ELF 32-bit LSB executable
[rayy@barton0 code]$
The file command can be very useful to avoid minor annoyances -- such as when using one of the following three commands.

The cat command is useful for concatenating multiple files -- or just for dumping a single text file to the screen. Before you use the cat command to dump a file to the screen, use the file command to make sure it's some variety of text file such as ascii text, commands/text, C source code, html/text, etc. The following is a shortened example of using file and cat to identify and dump a text file:
 [rayy@barton0 code]$ file xor.c
xor.c: ASCII C program text
[rayy@barton0 code]$ cat xor.c
int x;
unsigned char buff[128],
[rayy@barton0 code]$
The more command is useful when a text file is larger than a single screen. The following is a shortened example of using more to view a large C program:
 [rayy@barton0 code]$ more xor.c
int x;
unsigned char buff[128],
Note the '--More--(29%)' at the end of the screen. That means that 29% of the file is above that line, implying that another 71% of the file is below. Press the space bar to page through the file, a screenful at a time. Press the b key to back up. If you finish looking before reaching the end of the file, press the q key to quit.

The grep command, short for 'get regular expression and print', is useful for finding occurances of a particular string in a text file. To find the 'printf' statements in the example C program above, enter the following command:
 [rayy@barton0 code]$ grep printf xor.c
[rayy@barton0 code]$
The grep command has far more capability than I describe here and, as usual, enter
 [rayy@barton0 code]$ man grep
for more information.

The cp command will let you copy files. Unlike the commands used above, this one includes a hazard; if you copy filename1 to filename2 and filename2 already exists, you will destroy the original filename2 file. Use cp with caution!

To make a duplicate copy of my xor.c file I could enter the following command:
 [rayy@barton0 code]$ cp xor.c xor.c.bak
[rayy@barton0 code]$ ls xor.c*
xor.c xor.c.bak
[rayy@barton0 code]$
Note that the cp command returned no output -- I had to enter an ls command to see the results of the copy. [By adding the * wildcard to the original filename, I asked for a listing of all files that started with xor.c -- including those with no additional characters in the name.]

The rm command is used for removing files. To remove the duplicate file I created in the cp command example, I would enter the following:
 [rayy@barton0 code]$ rm xor.c.bak
[rayy@barton0 code]$ ls xor.c*
[rayy@barton0 code]$
Again, note the absense of any feedback from the rm command. I had to enter an ls command to verify that the xor.c.bak file had really been removed.

As with other commands, rm can remove multiple files at once when used with wildcards or with the -r (recursive) option. See the man page for more information on rm.

Ok, this is really two commands, but they are complementary. Use the mkdir command to make a new directory and use the rmdir command to remove an empty directory. For example:
 [rayy@barton0 tmp]$ mkdir testdir
[rayy@barton0 tmp]$ ls
[rayy@barton0 tmp]$ rmdir testdir
[rayy@barton0 tmp]$ ls
[rayy@barton0 tmp]$
In the preceding series of commands I first created a new directory named 'testdir'. I then used the ls command to verify its presence. Then, I removed 'testdir' and verified that it was gone by using ls again.

For more information on the commands covered in this article, take a look at the general commands man pages over at the website.
mail this link | score:8893 | -Ray, February 19, 2004 (Updated: April 18, 2007)
More Linux articles...

Tallahassee Abstract Art

Selected articles

Testing the Digital Ocean $5 Cloud Servers with an MMORPG

VPS: Xen vs. OpenVZ

Apple DIY Repair

Librenix T-Shirts and Coffee Mugs!

Linux dominates Windows

How to install Ubuntu Linux on the decTOP SFF computer

Microsoft to push unlicensed users to Linux

Space Tyrant: Multithreading lessons learned on SMP hardware

Apple to Intel move no threat to Linux

Space Tyrant: A multiplayer network game for Linux

Linux vs. Windows: Why Linux will win

Scripting: A parallel Linux backup script

Space Tyrant: A threaded C game project: First Code

Space Tyrant: A threaded game server project in C

Missing the point of the Mac Mini

Tutorial: Introduction to Linux files

Mono-culture and the .NETwork effect

The Network Computer: An opportunity for Linux

Hacker Haiku

Closed Source Linux Distribution Launched

The Supreme Court is wrong on Copyright Case

No, RMS, Linux is not GNU/Linux

Why software sucks

Why Programmers are not Software Engineers A simple directory shadowing script for Linux

The Real Microsoft Monopoly

Programming Language Tradeoffs: 3GL vs 4GL

Download: Linux 3D Client for Starship Traders

Graffiti Server Download Page

Beneficial Computer Viruses

The life cycle of a programmer

The short life and hard times of a Linux virus


Firefox sidebar

Site map

Site info

News feed


(to post)


Articles are owned by their authors.   © 2000-2012 Ray Yeargin