/** * CS149 - Programming Fundamentals * Department of Computer Science * James Madison University * @version Fall 2019 */
The workstations in the JMU CS Computer labs run Linux Mint. Both Mint and OS X are based on Unix and come with a powerful suite of general purpose computing tools. Many of these tools are only available via the command line or "terminal" interface. You will learn some of these commands this semester and others throughout your career. Today we will focus on basic survival skills for navigating the command line and a Unix-based file system.
Download the UnixLab.txt worksheet and open/edit the file as plain text. Submit your answers via Canvas by the deadline.
You may be most familiar with the Windows file system, which uses a "drive letter" and backslashes to identify files. The following example shows the "path" or location of the file Hello.java, starting from the C: drive.
C:\Users\John\Desktop\Hello.java
In Unix-based systems, there are no drive letters. All files (and disk drives) can be reached from the "root" directory, designated with a forward slash ( / ).
/Users/John/Desktop/Hello.java
(on OS X)
/home/john/Desktop/Hello.java
(on Linux)
All three of these examples are "fully qualified" or absolute paths, because they indicate the exact location of the file within the computer's file system.
You can alternatively specify a relative path, based on your
current location in the file system. For example, if you are working
in the /home/john/
directory on Linux, you can simply use the name
Desktop/Hello.java
to identify that file.
Study the following example file system diagrams and answer the questions below.
Windows File System
john.doc
? What is the path
for the file 3dMaze.scr
?Unix File System
QUESTION: In the Unix diagram, what is the absolute path to donations
in the jones
directory
QUESTION: You are now working in the /home/smith/
directory. What is the relative path to week1.txt
?
QUESTION: Where are the "games files" installed on this Unix-based system? Name the path absolute path.
The following instructions will lead you through some basic exercises involving the command line. Open a new Terminal window to get started. Stop and ask your neighbor or a lab assistant for help anytime you don't understand something.
The Unix file system is hierarchical. It starts at the root,
designated by /
. Every user has a home directory, designated by
~
. To see a list of files, use the "ls
" command.
ls
|
list the current directory |
ls ~/Desktop
|
list your desktop directory |
ls /
|
list the root directory |
Try the above commands in your terminal.
The commands "mkdir
" and "rmdir
" will make a new directory and remove
a directory from the file system, respectively. Try creating a new
subdirectory of your home directory:
mkdir UnixLab
The "ls" command should now show a new directory named "Lab02". Remove the new directory with:
rmdir UnixLab
Verify that it has been removed using the "ls
" command.
While you are logged in, you are attached to a "current directory" in the file system. To see what your current directory is, enter the "print working directory" command:
pwd
To change your current directory to a different directory, use the "change directory" command:
cd directory
For example:
cd /
|
change to the root directory |
cd ~/Desktop
|
change to the Desktop subdirectory within your home directory |
cd
|
change to your home directory (default working directory) |
Try the above commands on your workstation. Do an "ls
" command after
each step to list the files in that directory.
The special names ".
" and "..
" refer to the current directory and
parent directory, respectively.
ls .
|
list current directory |
ls ..
|
list parent directory |
ls ../..
|
list parent of parent directory |
Try the above commands on your workstation.
../..
?Unix keeps track of the commands you have recently entered. Use the up arrow to recall the previous command you wrote, and press enter to run it again. Now press up several times to go back several commands, followed by the down arrow to move forward to more recent commands.
Nano is a simple text editor that can be used on most Unix platforms. When nano is invoked, a control-key menu appears at the bottom of the screen. Try creating a new file with nano and copy a small section of this lab into it:
nano hello.txt
Practice using the control keys to save (write), exit, search, cut (delete a line), uncut (paste the last cut line), go forward, and go backward.
Raise your hand when you have completed this step, and demonstrate your nano skills to a lab assistant.
When working on the command line, you don't need to type every single
character! Instead, use the tab key to complete the current word. For
example, type "nano he
" without pressing enter, and then press
tab. The terminal should complete the word "hello.txt
" for you.
Try typing the following command with as few keystrokes as possible. (Hint: Use the tab key sometime after every slash.)
nano /usr/share/info/dir
Here are some basic file manipulation commands. Practice using them with the file that you created in the last step.
cp fromfile tofile
|
Copy a file. The "from file" and "to file" can be relative or absolute file names. |
mv file directory mv oldname newname
|
Move a file. This command can also be used to change the name of a file. |
rm filename
|
Delete (remove) a file. You might want to make a copy of it first. |
cat filename
|
Show the contents of a file (technically, "catenate" it to the standard output). |
less filename
|
View the file contents one page at a time. Use the arrow keys and page up/down to scroll through the file. Press 'q' to quit (exit the less program). |
QUESTION: What commands would you type to do the following? (Test them out on your workstation.)
question8
.question8
.question8
to delete_me
.You now know something about ls
, mkdir
, rmdir
, pwd
, cd
,
nano
, cp
, mv
, rm
, cat
, and less
. Each of these programs
have optional command line arguments. For example, type ls -l
(ls
with long formatting) in your terminal. You can use the "man
"
command to view the manual page for any command.
man ls
QUESTION: Use "man
" to answer the following questions:
clear
do?grep
do?man
do?There are two "wildcard" symbols that can be used in most
commands. Practice using them with the files in your directory. Create
more files if necessary, using "cp
" or "nano
".
*
|
match any series of 0 or more characters |
?
|
match any 1 character |
For example:
ls a*
|
list files starting with 'a' |
rm *.log
|
delete all files ending ".log" |
ls ??text.*
|
list files with names containing any two characters followed by "text" and ending with any suffix |
Files that have names beginning with '.' are not normally shown in
directory listings. These files are typically used for configuration
purposes, or to store personal settings for applications. To see the
hidden files in a directory, add "-a
" (all) to the ls
command.
ls -a
One of the useful features of Unix is the ability to transfer the output of one command to the input of another. The symbol "|
" (vertical bar) pipes output between commands.
ls -l | sort -k 8
sorts output from "ll" by the 8th field of the output (time)
ls -la | less
displays the output of ls one page at a time
The symbol '>' redirects standard output (normally directed to the screen) to a file.
ls -l > list.txt
|
sends output of the "ls" command to a file named "list.txt" |
ls -l | sort -k 8 > sortedlist
|
sorts the directory listing and saves it in the file "sortedlist" |
Check out http://ss64.com/bash/ for a longer list of Unix commands.
Note: Some of the content in this activity is taken from the UNIX Tutorial for Beginners originally developed by Michael Stonebank at the University of Surrey.