$ command output
In this exercise you will learn:
To use the command line in the Linux operating system
To use Emacs to edit C programs
To compile C programs under Linux
At this point, you have Linux installed on your laptop. Login and open a terminal window
Note that commands you need to type will appear in a box like the following:
$ command output
The $ represents the prompt (yours may look something like [username@machine ~]$) — you do not need to type that part. Lines that don't start with the prompt are things the command outputs to the screen.
In this section you will learn some simple Linux utilities. These utilities will allow you to maneuver the Linux file system, create new entries (files and directories), and copy, move and remove entries. Before continuing, download the file HelloWorld.c to your home directory at /home/<username>.
This command allows you to clear the screen. Type clear (Linux is case-sensitive) at the prompt. Press enter to run the command
$ clear
This command (“print working directory”) outputs the current folder you are in. Type pwd at the prompt and press enter; you will see the path to your home directory, which has the following structure
$ pwd /home/<username>
This command (“list”) lists the contents of the current working directory. Type ls at the prompt and press enter; you will see the contents of your home directory. You should see something like the following entries.
$ ls Desktop/ Document/ HelloWorld.c
ls comes with a number of flags that let you customize the ways it lists the directory contents.
The -l flag stands for long listing; that is, it lists the directory entries with more details. The details include the type (file or directory), owner, size, permissions, modification date, and more.
$ ls -l drwxr-x--- 15 root root 2048 Mar 3 17:05 Desktop/ drwx------ 3 root root 2048 Nov 16 20:55 Documents/ -rw-r--r-- 1 root root 264 Nov 26 18:36 HelloWorld.c
![]() |
|
The -a flag stands for all; that is, it lists all files including hidden files that begin with “.”.
$ ls -a ./ ../ Desktop/ Documents/ HelloWorld.c
Flags can be combined; for example:
$ ls -al drwxr-x--- 15 root root 2048 Mar 6 17:05 ./ drwxr-xr-x 2 admin root 26624 Mar 5 08:40 ../ drwxr-x--- 15 root root 2048 Mar 6 17:05 Desktop/ drwx------ 3 root root 2048 Nov 16 20:55 Documents/ -rw-r--r-- 1 root root 264 Nov 26 18:36 HelloWorld.c
![]() |
|
This command (“change directory”) changes the working directory. Try switching to the Documents directory, and then run pwd to check your new working directory path.
$ cd Documents $ pwd /home/<username>/Documents
There are several ways to return to your home directory. Since your home directory is the parent of your current directory, you can switch to ... The symbol ~ also refers to your home directory, so switching to ~ from any location will switch to your home directory. You can also type out the path of your home directory in full. Finally, running cd with no path defaults to the home directory. All the following commands switch to your home directory:
$ cd .. $ cd ~ $ cd /home/<username> $ cd
This command (“make directory”) creates a new directory. The path can be either a relative or absolute pathname. Spaces, “*”, “?”, “\” and “/” are some of the characters not allowed in Linux paths. The following example switches to your Documents directory, creates a new directory (csse332), and then creates another new directory (NewDir) within it.
$ cd ~/Documents $ mkdir csse332 $ ls csse332/ $ cd csse332 $ pwd /home/<username>/Documents/csse332 $ mkdir NewDir
This command (“remove directory”) removes a directory. The following example removes the empty NewDir directory created in the mkdir example
$ rmdir NewDir
![]() |
Warning rmdir can only remove empty directories. Attempting to remove non-empty directories will result in an error |
This command (“copy”) copies a file from one place to another. The first argument is the source path, the second is the destination where you want to make a new copy. This example makes a copy of HelloWorld.c, named hello_bkup.c. Run ls afterwards to ensure the copy was successful.
$ cp HelloWorld.c hello_bkup.c
This command (`concatenate`) displays the contents of a file. This example displays the contents of HelloWorld.c to the screen.
$ cat HelloWorld.c
This command (“remove”) deletes the specified file or directory. This example removes the backup file hello_bkup.c created in the cp example. Use ls or cat to ensure the file was removed
$ rm hello_bkup.c
This command (“move”) moves a file from one place to another. It can also be used to rename a file. Try moving HelloWorld.c to the csse332 directory. Use cp to copy HelloWorld.c to a new file named helloagain.c, and then use mv to rename helloagain.c to trial.c. Use ls and cat to test this.
This command (“manual”) displays instructions about a command. Use the man command to learn more about the commands you've seen today
![]() |
The manual is split into numbered sections, and some of the commands fall under multiple sections of the manual. You can use the -s flag to specify a section number. For example, to display the manual for ls in section 1: $ man -s 1 ls |
Use the spacebar or the up and down keys to scroll through the page. Use q to quit the manual. Use / to search the manual for a phrase; n and p find the next and previous matches
Use the exclamation/bang key to re-execute commands. Running !<string> will run the most recently executed command that starts with <string>. Running !! will run the last command you executed. You can also use the up and down arrow keys to scroll through previously typed commands
Pressing tab will auto-complete file and directory names.
$ cd De<tab>
Follow these instructions to setup emacs:
Download the my.emacs file to your home directory. Move my.emacs to a file called .emacs — note the dot at the beginning. The file is a hidden file and should be proceeded with a dot. This file must be in your home directory; switch to your home directory and use ls -a to make sure it's there
Open the Emacs reference card
Make sure you're in your home directory; switch to it if necessary
Type emacs & at the prompt and press enter. You should see emacs running now. The & means you want emacs to start in the background; your terminal will immediately be available again for additional use
To start the emacs tutorial, press Ctrl+H, and then t
Complete the tutorial
Exit emacs and start it again
Use cd to switch to the folder containing HelloWorld.c (this should be your home directory). Using Emacs, open the HelloWorld.c file (at the prompt, type emacs HelloWorld.c &. You should see the following:
/* * HelloWorld.c 1.0 03/14/2003 * */ /* * main: The traditional first program when learning C. * * author David Mutchler (and many others before me). * */ #include <stdio.h> int main(int argc, char* argv[]) { printf("Hello, World!\n"); }
This is a simple C program file that, when compiled and executed, will display the string “Hello, World!” onto the console.
If you don't have a shell window open in emacs, open one now.
Compile to the default executable a.out. Use gcc to compile, and then ls to make sure a.out exists
$ gcc HelloWorld.c $ ls HelloWorld.c a.out
To run the program, execute the file a.out
$ ./a.out Hello, World!
Now compile and create a named executable. Instead of using the default filename a.out we can use the -o switch to give it a more meaningful name. Type the following at the prompt:
$ gcc -o HelloWorldOutput HelloWorld.c
There should be an executable named HelloWorldOutput you can run, just as you did for a.out
You have now gone through the steps of using Emacs to edit C program files, working with the Linux command line environment and compiling a C program from the Linux command line. Do the following as a self-test. If you have problems with any steps, refer to the instructions given earlier
Open a terminal after logging into your linux machine
Create a folder, and use emacs to create a file within it named MyFirstCProgram.c
Edit MyFirstCProgram.c and make it print the string “This is my first program” to the console. See HelloWorld.c if you need help
Save the file
Use ls to make sure the file exists
Compile MyFirstCProgram.c using gcc with and without the -o flag to create the executables a.out and myfirstprog
Run each executable