Non-HiPerGator version
Last updated: September 11, 2024
This version of the handout is for users who do not have a HiPerGator account. There are several options you can use. This should mostly work with any computer with a BASH interpreter.
There are also several free online tools that work. For example:
- GitHub Codespaces
- replit.com
These instructions are focused on using GitHub Codespaces. You will need a GitHub account to use these instructions. Sign up at github.com!
- To run in Codespaces, you will need to be using this repository: https://github.com/UFIT-RC-Linux-Training/Linux_training
- GitHub provides educational Organizations with 3,000 minutes of Codespaces CPU time per month. Anyone can use this repository, but:
- Users are limited to 1 Codespace at a time
- Users are limited to the 2-core VMs
- Idle timeout is 30 minutes
- When the 3,000 minutes a month are used up, no more Codespaces will be able to be launched.
In this documentation, we will use the convention of putting text that needs to be substituted for your specific case in italic font.
- File paths (directories or folders):
/
,/home/Codespace
,/workspaces/Linux_training
pwd
,cd
,ls
(Where am I, change directory, list directory)cp
,mv
,rm
(copy, move (also used for rename), delete)more
,less
,head
,tail
,cat
(examine files)nano
,vim
(text editors in Linux)
Note that most things in Linux are case-sensitive. This applies to commands and file names. e.g.
CD test
will not work to change directories to the "test" folder, whilecd test
will work (assuming there is a folder named "test" in the current folder). Similarly,Test
andtest
are different folders or files.
- Tab completion- type part of a path or file name and hit tab-key, the shell will auto-complete for you.
history
: redo something that you did before without retyping (use ⬆️ arrow key)man
: getting help. Many built-in Linux applications have manual pages that document their use and options that they have. e.g.man ls
will bring up the page for thels
command, documenting the many options that change how thels
command functions.- Another way to get help information about applications is using the name of the application followed by either the
-h
or--help
flags. Many applications use this convention to provide documentation to users.
- Another way to get help information about applications is using the name of the application followed by either the
(Note: some of the data and examples are taken from software-carpentry.org):
-
Login to github.com
-
Navigate to the repository https://github.com/UFIT-RC-Linux-Training/Linux_training
-
Click the "<> Code" button and select the Codespaces tab:
-
Click the "Create Codespace on main" button.
- Connect to a repl.it Bash server:
- Navigate to https://replit.com/
- Click on the
<> Start coding
button - In the Create new repl Language selection window, type "Bash", select that, and click Create repl.
- When the repl opens, for the most part we will focus on the command prompt on the right-hand side. You can resize that pane to get some more room to work.
- Copy and paste (you will probably need to use right-click to paste) this command to get a copy of these notes and the data for the exercise:
git clone https://github.com/UFResearchComputing/Linux_training.git
-
Where are you when you login?
pwd
- In Codespaces, it should be
/workspaces/Linux_training
. - The replit paths are a bit different. As an example, I see:
> pwd /home/runner/WarlikeBeneficialAutoresponder >
- In Codespaces, it should be
-
What files are there?
ls
-
Let’s make a directory to put some data in:
mkdir cli_demo
-
Now what’s there?
ls -l
- Linux commands usually have flags to change how they work
man
,-h
or--help
often give you help
-
Change into cli_demo directory:
cd cli_demo
orcd cl-Tab-key
-
Copy some demo data here (
.
):- In Codespaces:
cp -r ../data/molecules .
- In replit:
cp -r ../Linux_training/data/molecules .
- Note the
-r
to recursively copy, sincecp
won’t copy directories by default - Also note the “
.
” at the end to copy the molecules directory to your current location.You could use the copy in the repository you cloned, but this keeps the directions in parallel with the directions for users on HiPerGator and practices the
mkdir
andcp
commands. This folder originated from the software-carpentry Shell Training materials.
- In Codespaces:
-
Check that the copy worked:
ls
-
Change directories into the molecules directory:
cd molecules
-
Look at these files with
more
,cat
,head
,tail
:more propane.pdb
andcat propane.pdb
head propane.pdb
orhead -n2 propane.pdb
tail propane.pdb
ortail -n2 propane.pdb
-
Redirects: You can redirect the output of a command to a file with the "
>
" character (see below for more information about STDIN, STDOUT and STDERR).Caution: Redirection with "
>
" first erases the file you are redirecting to if it exists, replacing it with the new contents. You can append to a file with ">>
".- As an example, we can get the lengths, in number of lines, of all the files ending in "
.pdb
" using the wildcard "*
" and redirect the output to a file:wc -l *.pdb > lengths.txt
- Let’s see what this file looks like:
cat lengths.txt
Wildcard expansion (e.g.
*.pdb
) is a convenient way to operate on a list of files matching some characteristic: e.g. having the.pdb
ending, starting with "p" (p*
), etc. The wildcard is evaluated and the list of matching files is passed into the command. - As an example, we can get the lengths, in number of lines, of all the files ending in "
-
Sorting: We might want the lengths sorted:
sort -n lengths.txt
- What happens without the
-n
?
- What happens without the
-
Pipes: We can connect commands together by piping the output from one command into the input for the next command using the vertical bar character "
|
":The pipe character, "
|
", is typically located above the Enter-key on US keyboards with the "\
" and is accessed with the Shift-keywc -l *.pdb | sort -n > lengths.txt
- Or if we only want to know the shortest file:
wc -l *.pdb | sort -n | head -n1
Why this works: Most Linux programs can take input from what is called "standard in", often abbreviated as "STDIN". In addition, they typically have two output streams, "standard out", "STDOUT", and "standard error", "STDERR"--both of which print to the screen by default, but can be redirected as we saw above (in fact we only redirect STDOUT with the "
>
"; any STDERR would still print to the screen).So, while you can run a command like
wc -l propane.pdb
, you can also get the same result by running thecat
command and "piping" the STDOUT of that to the STDIN ofwc
:cat propane.pdb | wc -l
. Similarly, you can keep piping commands connecting the output of one command to the input for the next command. -
grep: We can search for text using
grep
:grep ATOM propane.pdb
-
awk:
awk
can do a lot, but one thing it’s good it is pulling out columns from files, in this case the 3rd column:grep ATOM propane.pdb | awk '{print $3}'
-
uniq:
uniq
is a command to find unique entries in a sorted list:grep ATOM propane.pdb | awk '{print $3}' | sort | uniq
-
Loops: One of the great things about the command line is the ability to automate repetitive tasks. Let’s say we want to verify that all our molecules are hydrocarbons (made up of only C and H atoms). Below is what the loop looks like neatly spaced out as if it were in a script:
for molecule in *.pdb do echo $molecule grep ATOM $molecule | awk '{print $3}' | sort | uniq done
As we type this in on the command line, once you hit the Enter-key at the end of the first line, Bash gives you the continuation prompt, "
>
", essentially telling you that you have started a command (a for loop), but not finished it:$ for molecule in *.pdb >
Note: In replit.com, the Bash prompt is a symbol very similar to the
>
sign.You can keep typing the rest of the lines, so that it looks like this:
$ for molecule in *.pdb > do > echo $molecule > grep ATOM $molecule | awk '{print $3}' | sort | uniq > done
Once you hit the Enter-key after the "
done
", that completes the for loop and Bash executes it, showing the output.If you find yourself stuck in the continuation prompt mode, you can use the key combination
Ctrl-C
to cancel and exit back to the main command prompt.Ctrl-C
will usually cancel execution of a program in Bash and is a handy key-combination to know! -
Deleting files: Let’s get rid of the lengths.txt file:
rm lengths.txt
- That file is now gone!! There is no undo, no recycle bin or trash can. As soon as you type the command and hit return, the file is gone!
- Be careful... but don’t keep everything forever either!
- Which molecule has the most H atoms?
- Make a directory in your
cli_demo
folder and copy themethane.pdb
file there (preferably without moving from themolecules
directory) - From the
molecules
directory, get thehead
of themethane.pdb
file in the directory you created above. - Change directories to the directory you made above, rename the
methane.pdb
file tomy_methane.pdb
. - Edit the
my_methane.pdb
file and put your name as the AUTHOR. I typically recommend thenano
text editor on the command line for new users:nano my_methane.pdb
and use the arrow keys to move around. The commands at the bottom of the screen use the Ctrl-key in combination with another key, so^X
means hold down the Ctrl-key and the X-key at the same time to exit. - How many ATOMS does methane have?