Linux Fundamentals
In the EECS department at the University of Tennessee, we use Linux operating systems.
Navigating the File System
There are two main ways to navigate the Linux file system. You can use the File Browser, accessible by clicking on Places and then Home Folder.
Otherwise, you can use the terminal and the following commands to navigate the file system. To access the terminal, click on Applications, then Accessories, then Terminal. A terminal window will open and you will be in your home area.
The following sequence of commands will allow you to view the files that are currently in your file system:
- ls: lists directory contents. There are a variety of flags associated with ls. The following are the most commonly used:
- ls -A lists all files in the directory
- ls -l lists information about files, including their modification times and permissions
- For more options of ls, type man ls into the terminal. Typing man followed by any command will show the entry for that command in the manual.
- cd: This command, followed by any directory name, changes the current directory in the terminal.
- Typing cd .. changes to the parent directory.
- Typing cd alone changes to your home directory.
- Type the tab key on the keyboard to auto-complete directory or file names.
The following commands allow you to change the structure of the file system:
- mkdir: This command allows you to create a directory. It should be followed by the name of a directory.
- Example: mkdir images
- This would create the directory images within the directory in which you called the command.
- cp: This command allows you to copy a file. It takes two file names as arguments. The first file name is the file to be copied and the second file name is the destination of the file.
- Example: cp f1.txt f2.txt
- This command would copy f1.txt to f2.txt.
- If f2.txt already existed, its contents would be lost, so use cp carefully.
- mv: This command allows you to move a file. It takes two file names as arguments. The first file name is the file to be moved and the second file name is the destination of the file.
- Example: mv f1.txt f2.txt
- This command would move f1.txt to f2.txt.
- If f2.txt already existed, its contents would be lost, so use mv carefully.
- rm: This command allows you to delete or remove a file. It takes the file to be removed as an argument.
- Example: rm f1.txt
- This command would delete f1.txt.
- Note that rm deletes a file forever, so use it carefully.
Text Editors
We will primarily use text editors to create and edit our code. There are two main text editors: vi and emacs. Most in our department use vi to create and edit their code, but emacs is also very popular. We will start using vi. Emacs will be introduced later in the semester. If you are already comfortable using a particular editor, feel free to continue using that editor.
Link to xkcd comic on text editors
To open vi, simply open a terminal and type vi filename where filename is the name of the file you want to open.
- command mode: this is the mode you are in when vi begins.
- insert mode: This is the mode you use to type your code.
To enter insert mode, type I. -- INSERT -- will appear at the bottom of your page. If you are copying and pasting into Vi, Vi must be in insert mode.
To exit insert mode, click ESC, which will return you to command mode.
To save a file in Vi, return to command mode and type :w. W is this instance stands for write. In this instance, write and save are synonyms.
To exit Vi, return to command mode and type :q. You can also save and quit at the same time by typing :wq.
Though Vi's commands may seem difficult to learn initially, the goal of using Vi is to minimize the transfer of hands from keyboard to mouse. The more Vi commands you learn, the faster you will be able to type in Vi.
A few of the more handy commands in Vi (all of which are used in command mode) are:
- gg -- This will move your cursor to the beginning of the document.
- G -- This will move your cursor to the end of the document.
- 0 -- This will move your cursor to the beginning of the current line.
- $ -- This will move your cursor to the end of the current line.
- dd -- This will delete the current line. If you include a number before dd, such as 5dd, this will delete 5 lines.
- yy -- This will yank or copy the current line. Again, like dd, if you include a number before yy it will copy that many lines.
- p -- This will put or paste the lines you most recently deleted using dd or copied using yy.
- u -- This will undo the last thing you did in vi.
- :redo -- This will redo the last thing you undid in vi.
- == -- This will auto-indent the current line. Again, if you include a number before ==, it will auto-indent that many lines.
- % -- If the cursor is over any parenthesis or brace, entering % will move the cursor to the corresponding closing parenthesis or brace.
- :# -- where # is a line number. This will take the cursor to line #, e.g., :50 will take you to line 50 in the document.
For more information about vi, you can visit one of the following tutorials:
Tar
Tar is an archiving program designed to store and extract files into an archive file. We will use tar to create a tar file in our lab submissions. For our purposes there are two types tar commands we will use:
- tar cvf tarfilename.tar f1.txt f2.txt -- This command is used to create a tar file. The c stands for create, the v stands for verbose, and the f indicates that you will be providing a tar file name. The next argument is the name of the tar file you are creating, with file extension .tar. The next arguments will be the list of files or directories you want to be added to the tar file. In the example command, only f1.txt and f2.txt will be added to the tar file.
- tar xvf tarfilename.tar -- This command is used to extract files from a tar file. The x stands for extract, the v stands for verbose, and the f indicates that you will be providing a tar file name. In this example, tarfilename.tar will be extracted into the current directory.
Exercise
In this exercise you will a directory, several files, and a tar file. You will email me the completed tar file.
- Open a terminal and navigate to your home directory by typing:
- Make a directory called cs594csf using the following command:
- Navigate to your new directory:
- Make a new directory called lab1, where you will do all of your work for lab1:
- Make a new directory called test, where you will complete the remainder of this test:
- Navigate to the test directory:
- Create a new file called f1.txt using vi:
- In f1.txt, enter insert mode and type your name, what you preferred to be called, and what you are interested in learning in this course.
- Save and exit when you are finished by re-entering command mode and typing :wq.
- Copy f1.txt to f2.txt:
- Move f1.txt to intro.txt.
- List the files in the directory and note that f1.txt is no longer there.
- Open f2.txt in vi:
- Delete all of the lines in f2.txt using dd and enter your current degree information (where you received your undergrad, in what, and when, and what year and degree you are pursuing at UT).
- Save and exit when you are finished.
- Move f2.txt to edu.txt:
- Create a tar file called username_test.tar where username is replaced by your username. This tar file should contain intro.txt and edu.txt.
- tar cvf username_test.tar intro.txt edu.txt
- Email me the tar file at cdschuman@gmail.com. Make the subject of the email CS 594 -- Linux Basics.
- Navigate to the lab1 directory you created earlier:
- Make a directory in lab1 called submit.
- When you complete Lab 1, copy all of the relevant files to the submit directory, navigate to that directory and follow the submission instructions on the class main page.
- If you are unfamiliar with C++, let me know and check out the following sites:
- Get started on Lab 1.
- If you are interested in learning how to log into our lab machines from home, please let me know.