Lab 1: Linux Basics
The Linux command line will be used extensively in ECPE 170. This lab is intended to give you a basic introduction to navigating on the command line and performing common tasks. You will build upon these skills for the remainder of the course. By the end of the semester the command line should be second nature to you.
Bring your laptop to class (and to all future labs)
Before the start of this lab, you must complete the Virtual Machine Setup tutorial and build a working Linux system to use. The pre-lab is easy, but the download and installation process is too time-consuming to do in class.
Checkpoint to obtain pre-lab credit - due by start of class:
(1) If you have a laptop: Bring your laptop, boot Ubuntu Linux (via either a virtual machine or by dual-booting), and launch the command prompt
(2) If you have a desktop: Show me a picture on your phone of Ubuntu running on your computer
(3) If you have no computer: Email me in advance of the lab so we can make accommodations
(Alternate checkpoint: If you have errors when installing/building, you must email Dr. Hughes a detailed description of the specific error at least 12 hours before the start of class. "I couldn't get it working" is not sufficient.)
Lab Part 1 - Unix Basic Skills
In Ubuntu, login and launch the command prompt (aka Terminal). The keyboard shortcut is <CTRL><ALT><T>. Or, you can search for "Terminal" by clicking on the Dash button in the upper-left corner (i.e., the Ubuntu equivalent of the Windows "start" button).
For the rest of this course, commands to enter at the terminal are going to be given in the following format:
unix> This is the command that you should enter
The "unix>" part of the line should not be typed in, and it will not look the same on your computer. Rather, it might look something like this: "username@computername:/current-directory$", i.e. a combination of your username, computer name, and the current directory you are in. Thus, it will be constantly changing as you work! To simplify these labs, this is written as "unix>"
List files: ls
When you first login, your current working directory is your home directory. Your home directory has the same name as your user-name, for example, khughes, and it is where your personal files and subdirectories are saved. To find out what is in your home directory, use the ls command. (ls is short for "list") There may be no files visible in your home directory, in which case, nothing will print but the command prompt again.
List contents of your current working directory:
The ls command does not show hidden files by default. Hidden files have file names that begin with a dot (.). To list all files in your home directory including hidden files, use the -a option with the ls command.
List all contents of your current working directory, including hidden files:
unix> ls -a
Make directory: mkdir
To make a directory called ecpe170 inside your current working directory, type
unix> mkdir ecpe170
To see the directory you have just created, type
Change to a different directory: cd
The command cd directory means change the current working directory to 'directory'. The current working directory may be thought of as the directory you are in, i.e. your current position in the filesystem. To change to the directory you have just made, type
unix> cd ecpe170
To go up one level in the directory tree, use two dots:
unix> cd ..
At this point you should be back in your home directory.
To jump back to your home directory (regardless of how deep in the file tree you happen to be), use either of the two following commands:
unix> cd ~
In Unix systems, the tilde (~) character represents your home directory.
Show the current directory: pwd
Pathnames enable you to work out where you are in relation to the whole file system. For example, to find out the absolute pathname of your current directory, use:
The full pathname will look something like /home/khughes which means that khughes (your home directory) is inside the directory home (the directory containing all user accounts).
Lab Reports are to be created in OpenOffice (now "LibreOffice"), and submitted in PDF form at the completion of the lab. You will create a single Lab Report document for each lab.
A Lab Report template is available. Download it now, make a copy, and use it to start your report for this lab.
(1) What command would I type to go to the following directory? /home/khughes/ecpe170/project1/src
(2) In the following file pathname, what is the top-level directory (i.e. highest in the hierarchy) and what is the lowest-level directory? /home/khughes/ecpe170/project2/src/main.c
Copy file: cp
Before doing these commands, enter the ecpe170 directory, and create two empty files using the touch command:
unix> cd ecpe170
unix> touch file1
To make a copy of file1 and give it the name file2 (in the current working directory), use:
unix> cp file1 file2
Now you should have two files: file1 and file2.
Note that you can use relative or absolute paths to identify files. For example, to copy file1 from the current working directory and save it as the name file2 in one directory up from the current directory, use:
unix> cp file1 ../file2
Move file: mv
To move (or rename) file1 into file2, use:
unix> mv file1 file2
Delete file: rm
To delete ("remove") a file, use:
unix> rm file1
Caution! The rm command is very powerful. Once you remove a file, recovering that file can be very difficult, if not impossible. The command line does not provide the safety of "are you sure?" questions like the GUI interface does. Further, no "unremove" or "undelete" command. If you're paranoid about deleting something accidentally, use this instead:
unix> rm -i file1
Delete ("remove") directory: rmdir
unix> rmdir directoryname
Note that the directory must be empty first!
Clear the terminal window: clear
View a (text) file on screen: less
unix> less /var/log/syslog
The command less writes the contents of syslog (located in the /var/log/ directory) onto the screen a page at a time.
- Press the [space-bar] if you want to see another page
- Press the [arrow keys up/down] if you want to scroll
- Press [q] if you want to quit reading.
Read documentation on a program: man
unix> man <<program-name>>
For example, use man ls to view the manual for the ls utility. These are colloquially referred to as "man pages".
- Press [q] if you want to quit reading
Search document for a matching command: apropos
When you are not sure of the exact name of a command, use apropos to search the "man pages" for a command matching a keyword. For example, to search for programs that have something to do with "sort", use:
unix> apropos sort
Note that this is the same as using the "-k" option with man:
unix> man -k sort
Run a command as the administrative ("root") user: sudo
Sudo allows you (a regular user) to run a command as an Administrative ("root") user by typing your password. Everything that comes after sudo is the command to run as an Administrative user. Administrative access is required to install software and read/move/delete files not owned by your current user.
unix> sudo <<command-to-run-as-administrator>>
For example, the apt-get utility that we used to install software packages must be run as the root user.
unix> sudo apt-get update
(3) Copy the first page of the manual for the GCC compiler.
Tip: This program is run with the gcc command at the command line.
(4) What does this command do? sudo rm -rf /
Tip: DO NOT type this at the command line to find out, you will regret it!
Download a file from the Internet: wget
The wget utility can download files (of any kind) via HTTP:
unix> wget http://www.somesite.com/somefile.dat
Edit a text file: gedit
There are many text editors available on Linux/Unix systems.
The ancients - still present on modern computers, but "user friendly" was not a design requirement...
The classics - still widely used!
- vi - Written by Bill Joy, who later founded Sun Microsystems
- emacs - Written by Richard Stallman, who founded the GNU Project and Free Software Foundation
I don't care what text editor you use. Just pick one and learn it well enough to become comfortable.
Gedit is easiest for novices due to its graphical design.
Tip 1: Turn on "Display Line Numbers" in Gedit preferences, so debugging programming errors will be easier later this semester.
Tip 2: Disable "Create a backup copy of files" in Gedit preferences, so your directories won't be littered with temporary files ending in "~" later on.
Vi and Emacs are insanely powerful in the hands of advanced users.
(I had a colleague who would code, compile, jump to a line with a compile error, fix it, re-compile, and run the resulting binary - all without leaving the Emacs editor or touching a mouse).
Run gedit to create an untitled document:
Run gedit to create the document myfile.txt
unix> gedit myfile.txt
Place the following text in myfile.txt and save it (we'll use it next):
Neo: What are you trying to tell me? That I can dodge bullets?
Morpheus: No, Neo. I'm trying to tell you that when you're ready, you won't have to.
Count the number of characters, words, or lines in a file: wc
unix> wc myfile.txt
(5) What do the first three numbers in the output of the wc utility mean?
Redirect input and output: < and >
Many programs take their input from "standard input" (i.e. the console keyboard) and display results to "standard output" (i.e. the console monitor). However, you can change this, so either the input, output, or both are files. This works even if the program was never written to expect a file!
Change the input to a program from standard input to a file: <
unix> sort < myfile.txt
Change the output of a program from standard output to a file: >
unix> man wc > docs_for_wc_program.txt
unix> less docs_for_wc_program.txt
Change the output of a program from standard output to a file, appending to the end: >>
(You should have two copies of the docs in the file now!):
unix> man wc >> docs_for_wc_program.txt
unix> less docs_for_wc_program.txt
Lab Part 2 - What is the Command?
Add the following information to the end of your lab report.
Show, for each task below, the command (or keystrokes) that are needed to performs the desired operation.
In addition, document your command, clearly identifying the program name and describing the purpose of each argument that follows.
|6||Display information on how to use the ls program, such as what optional arguments the program accepts.
(Also, what key do you press to advance to the next page? What do you press to exit?)
|7||Count the number of characters in myfile.txt and save the result in the file myfile_char_count.txt (if the filename is also saved in the output file, that's OK)|
|8||List all files contained in your home directory, including hidden files where the filename starts with a period.
(This command should work for any user, so don't put an explicit path that hardwires it to a specific username.)
|9||Move the file "data.txt" from the current directory to inside the directory "experiment1". The destination directory is located directly under (i.e. inside) the user's home directory.
Note: The current directory is left unspecified in this problem.
|10||Sort the directory listing of the /etc directory by file size|
|11||Download the file http://www.google.com/doodles/roswells-66th-anniversary from the web to your current directory.|
|12||Print the current directory that you are in. (By "print", I want to know *what* directory you are currently in, and where it exists in the filesystem. I do not want a list of the files in that directory).|
|13||Do a long listing for files stored in the /boot directory, and include the size of each file in human-friendly units like megabytes or kilobytes.|
|14||Report the total amount of disk space consumed by the /bin directory.
(This report of the total space should be printed in human friendly units. Further, it should only show the summary (total), not the size of each individual file)
|15||Report the free space available on the disk in human-friendly units like megabytes or kilobytes.|
Lab Part 3 - Q&A
Answer the following questions. Short answers (averaging 4 sentences per question) are sufficient.
(16) What is the Linux kernel?
(17) How is Ubuntu Linux different from the Linux kernel?
(18) What is a Virtual Machine?
(19) How is dual booting different from a virtual machine?
(20) What is a directory tree, and why is it called a tree?
(21) What is the best text editor: vi or emacs?
(22) Beyond inflicting pain and suffering on newbies, what are 3 advantages of using the command line to control a computer?
(23) What does one dot (.) mean in a file path? What do two dots (..) mean in a path?
(24) What does cat /proc/cpuinfo do? (Specifically, what does cat do, and what is in /proc/cpuinfo?)
(25) What does this massive image represent? Explain it in your own words. (from http://futurist.se/gldt/)
Lab Report - Wrapup:
(1) What was the best aspect of this lab?
(2) What was the worst aspect of this lab?
(3) How would you suggest improving this lab in future semesters?
Lab Report Submission: For this lab only, upload your report to the class Sakai site in the Assignments section. Reports must be submitted as PDF files, not as LibreOffice files (Choose File->Export as PDF).