Linux Essentials
To be successful in this class, students should be able to:
Describe basic functions of essential Linux commands
Use Linux commands to navigate a file system and manipulate files
Transfer data to / from a remote Linux file system
Edit files directly on a Linux system using a command line utility (e.g. vim, nano, emacs)
Topics covered in this module include:
Creating and navigating folders (
pwd
,ls
,mkdir
,cd
,rmdir
)Creating and manipulating files (
touch
,rm
,mv
,cp
)Looking at the contents of files (
cat
,more
,less
,head
,tail
,grep
)Network and file transfers (
hostname
,whoami
,logout
,ssh
,scp
,rsync
)Text editing with vim (insert mode, normal mode, navigating, saving, quitting)
Log in to the Class Server
To log in to login-coe332.tacc.utexas.edu
, follow the instructions for your operating
system or ssh client below.
Note
Replace username
with your TACC username.
Mac / Linux
Open the application 'Terminal'
type: ssh username@login-coe332.tacc.utexas.edu
(enter password)
(enter token)
Windows (use WSL or an SSH client like PuTTY)
Open the application 'PuTTY'
enter Host Name: login-coe332.tacc.utexas.edu
(click 'Open')
(enter username)
(enter password)
(enter token)
If you can’t access the class server yet, a local or web-based Linux environment will work for this guide. However, you will need to access the class server for future lectures.
Try this Linux environment in a browser.
Creating and Manipulating Files
We have seen how to navigate around the filesystem and perform operations with
folders. But, what about files? Just like on Windows or Mac, we can easily
create new files, copy files, rename files, and move files to different
locations. First, we will navigate to the home directory and create a few new
folders and files with the mkdir
and touch
commands:
$ cd # cd on an empty line will automatically take you back to the home directory
$ pwd
/home/wallen
$ mkdir folder1
$ mkdir folder2
$ mkdir folder3
$ touch file_a
$ touch file_b
$ touch file_c
$ ls
file_a file_b file_c folder1 folder2 folder3
These files we have created are all empty. Removing a file is done with the
rm
(remove) command. Please note that on Linux file systems, there is no
“Recycle Bin”. Any file or folder removed is gone forever and often
un-recoverable:
$ touch junkfile
$ rm junkfile
Moving files with the mv
command and copying files with the cp
command
works similarly to how you would expect on a Windows or Mac machine. The context
around the move or copy operation determines what the result will be. For
example, we could move and/or copy files into folders:
$ mv file_a folder1/
$ mv file_b folder2/
$ cp file_c folder3/
Before listing the results with ls
, try to guess what the result will be.
$ ls
file_c folder1 folder2 folder3
$ ls folder1
file_a
$ ls folder2
file_b
$ ls folder3
file_c
Two files have been moved into folders, and file_c
has been copied - so
there is still a copy of file_c
in the home directory. Move and copy
commands can also be used to change the name of a file:
$ cp file_c file_c_copy
$ mv file_c file_c_new_name
By now, you may have found that Linux is very unforgiving with typos. Generous
use of the <Tab>
key to auto-complete file and folder names, as well as the
<UpArrow>
to cycle back through command history, will greatly improve the
experience. As a general rule, try not to use spaces or strange characters in
files or folder names. Stick to:
A-Z # capital letters
a-z # lowercase letters
0-9 # digits
- # hyphen
_ # underscore
. # period
Before we move on, let’s clean up once again by removing the files and folders we have created. Do you remember the command for removing non-empty folders?
$ rm -r folder1
$ rm -r folder2
$ rm -r folder3
How do we remove file_c_copy
and file_c_new_name
?
$ rm file_c_copy
$ rm file_c_new_name
Looking at the Contents of Files
Everything we have seen so far has been with empty files and folders. We will
now start looking at some real data. Navigate to your home directory, then issue
the following cp
command to copy a public file on the server to your local
space:
$ cd ~ # the tilde ~ is also a shortcut referring to your home directory
$ pwd
/home/wallen
$ cp /usr/share/dict/words .
$ ls
words
Try to use <Tab>
to autocomplete the name of the file. Also, please notice
the single dot .
at the end of the copy command, which indicates that you
want to cp the file to .
, this present location (your home directory).
This words
file is a standard file that can be found on most Linux operating
systems. This version contains 654,895 words, each word on its own line. To see
the contents of a file, use the cat
command to print it to screen:
$ cat words
A
A'asia
A's
AATech
AATech's
AAeE
AAeE's
AAgr
AAgr's
AAvTech
This is a long file! Printing everything to screen is much too fast and not very
useful. We can use a few other commands to look at the contents of the file with
more
control:
$ more words
Press the <Enter>
key to scroll through line-by-line, or the <Space>
key
to scroll through page-by-page. Press q
to quit the view, or <Ctrl+c>
to
force a quit if things freeze up. A %
indicator at the bottom of the screen
shows your progress through the file. This is still a little bit messy and fills
up the screen. The less
command has the same effect, but is a little bit
cleaner:
$ less words
Scrolling through the data is the same, but now we can also search the data.
Press the /
forward slash key, and type a word that you would like to search
for. The screen will jump down to the first match of that word. The n
key
will cycle through other matches, if they exist.
Finally, you can view just the beginning or the end of a file with the head
and tail
commands. For example:
$ head words
$ tail words
The >
and >>
shortcuts in Linux indicate that you would like to redirect
the output of one of the commands above. Instead of printing to screen, the
output can be redirected into a file:
$ cat words > words_new.txt
$ head words > first_10_lines.txt
A single greater than sign >
will redirect and overwrite any contents in
the target file. A double greater than sign >>
will redirect and append
any output to the end of the target file.
One final useful way to look at the contents of files is with the grep
command. grep
searches a file for a specific pattern, and returns all lines
that match the pattern. For example:
$ grep "banana" words
banana
banana's
bananaquit
bananas
cassabanana
Although it is not always necessary, it is safe to put the search term in quotes.
Network and File Transfers
In order to login or transfer files to a remote Linux file system, you must know the hostname (unique network identifier) and the username. If you are already on a Linux file system, those are easy to determine using the following commands:
$ whoami
wallen
$ hostname -f
login-coe332.tacc.utexas.edu
Given that information, a user would remotely login to this Linux machine using ssh in a Terminal:
[local]$ ssh wallen@login-coe332.tacc.utexas.edu
(enter password)
(enter token)
[login-coe332]$
Windows users would typically use the program PuTTY (or another SSH client)
to perform this operation. Logging out of a remote system is done using the
logout
command, or the shortcut <Ctrl+d>
:
[login-coe332]$ logout
[local]$
Copying files from your local computer to your home folder on the class server would
require the scp
command (Windows users use a client “WinSCP”):
[local]$ scp my_file wallen@login-coe332.tacc.utexas.edu:/home/wallen/
(enter password)
(enter token)
In this command, you specify the name of the file you want to transfer
(my_file
), the username (wallen
), the hostname
(login-coe332.tacc.utexas.edu
), and the path you want to put the file
(/home/wallen/
). Take careful notice of the separators including spaces,
the @
symbol, and the :
.
Copy files from the class server to your local computer using the following:
[local]$ scp wallen@login-coe332.tacc.utexas.edu:/home/wallen/my_file ./
(enter password)
(enter token)
Instead of files, full directories can be copied using the “recursive” flag
(scp -r ...
). The rsync
tool is an advanced copy tool that is useful for
synching data between two sites. Although we will not go into depth here,
example rsync
usage is as follows:
$ rsync -azv local remote
$ rsync -azv remote local
This is just the basics of copying files. See example scp usage and example rsync usage for more info.
Text Editing with VIM
VIM is a text editor used on Linux file systems.
Open a file (or create a new file if it does not exist):
$ vim file_name
There are two “modes” in VIM that we will talk about today. They are called
“insert mode” and “normal mode”. In insert mode, the user is typing text into a
file as seen through the terminal (think about typing text into TextEdit or
Notepad). In normal mode, the user can perform other functions like save, quit,
cut and paste, find and replace, etc. (think about clicking the menu options in
TextEdit or Notepad). The two main keys to remember to toggle between the modes
are i
and Esc
.
Entering VIM insert mode:
> i
Entering VIM normal mode:
> Esc
A summary of the most important keys to know for normal mode are:
# Navigating the file:
arrow keys move up, down, left, right
Ctrl+u page up
Ctrl+d page down
0 move to beginning of line
$ move to end of line
gg move to beginning of file
G move to end of file
:N move to line N
# Saving and quitting:
:q quit editing the file
:q! quit editing the file without saving
:w save the file, continue editing
:wq save and quit
Review of Topics Covered
Part 1: Creating and navigating folders
Command |
Effect |
---|---|
|
print working directory |
|
list files and directories |
|
list files in column format |
|
make a new directory |
|
navigate into a directory |
|
remove an empty directory |
|
remove a directory and its contents |
|
refers to the present location |
|
refers to the parent directory |
Part 2: Creating and manipulating files
Command |
Effect |
---|---|
|
create a new file |
|
remove a file |
|
remove a directory and its contents |
|
move a file into a directory |
|
change the name of a file |
|
change the name of a directory |
|
copy a file |
|
copy a directory |
|
autocomplete file or folder names |
|
cycle through command history |
Part 3: Looking at the contents of files
Command |
Effect |
---|---|
|
print file contents to screen |
|
redirect output to new file |
|
scroll through file contents |
|
scroll through file contents |
|
output beginning of file |
|
output end of a file |
|
search for ‘pattern’ in a file |
|
shortcut for home directory |
|
force interrupt |
|
redirect and overwrite |
|
redirect and append |
Part 4: Network and file transfers
Command |
Effect |
---|---|
|
print hostname |
|
print username |
|
remote login |
|
logout |
|
copy a file from local to remote |
|
copy a file from remote to local |
|
sync files between local and remote |
|
sync files between remote and local |
|
logout of host |
Part 5: Text editing with VIM
Command |
Effect |
---|---|
|
open “file.txt” and edit with |
|
toggle to insert mode |
|
toggle to normal mode |
|
navigate the file |
|
quit ending the file |
|
quit editing the file without saving |
|
save the file, continue editing |
|
save and quit |
Additional Resources
Practice VIM on the command line by typing
vimtutor