An intro to the terminal - SBScodingclub/SBSCodingClub GitHub Wiki
Terminals
This is a direct model of the Griffith lab github but simplified and expanded for understanding
Found here: Griffith lab Unix bootcamp tutorial
This guide is to explain how to use terminals and to give you a basic overview of how terminals work. A terminal simply a basic command line interpreter which allows you to issue simple commands to the machine and string longer sets of commands together. UNIX operating systems all have a terminal present called the Unix shell and the unix shell speaks multiple languages but it's main one we are discussing is Bash. The Unix shell is so ubiquitously used that it has even been introduced to Windows 10.
Some basic terminology to be aware of:
- Directory: this is the unix name for a folder
- e.g. on macOS the home directory for a user, say “Victoria”, would be
/Users/Victoria
- And their documents folder would be found at
/Users/Victoria/Documents
or~/Documents
- In bash the
~
character denotes the home directory.
- e.g. on macOS the home directory for a user, say “Victoria”, would be
- A path is the textual representation of a directory, e.g. the path to Victoria’s documents folder is either
/Users/Victoria/Documents
or~/Documents
Basic Bash commands
-
ls
– lists whats available in the current directory -
ls -F
lists working directories and files / unsure what -f does, but its similar, lists everything -
cd
- changes the directory -
pwd
-prints working directory -
cd ..
- takes you back up a directory -
cd ~
-takes you back to your home directory. a tilde character (`~``) works because Unix uses the tilde character as a short-hand way of specifying a home directory. -
cd -
changes to previous directory -
cd~/directory/seconddirectory
- tells you where you want to run shell stuff -
cd/
- takes you back to the root of all life as you know it -
cd .
goes nowhere,.
points to where I am so its like saying change directory to where I am -
While typing a path / variable / command pressing tab will attempt to autocomplete the name, e.g. typing
/Users/Vic
and then tab would autocomplete toUsers/Victoria
. This is really useful for -
mkdir
make directory command don't put spaces between folder names, use underscores or you have to do some bull shit fuckery with slashes (\
is used to escape whitespace -
rm
e.g.rm – draft.txt
Once melted it’s much harder to recover things than if put into the recycling bin so be careful -
rm –r
is recursive delete- BE CAREFUL it removes directories
rm -r ~
would delete EVERYTHING (maybe)
- BE CAREFUL it removes directories
-
cp
is a copy command.cp -r
to copy directories -
mv
is the move rename and move command -
syntax for
cp
/mv
iscp path/to/file new/location/and/name
- This will overwrite existing files so think about what you’re doing
-
ALSO display file path to picture lets you view images form the terminal
-
*
is a useful wild card — this allows the rest of a name to match any pattern e.g.ls ~/Do*
would list files for bothDownloads
andDocuments
-
wc
is a word count command -
wc *.pdb
word counts all files that have a .pdb file -
wc -l file.txt
tells you how many lines infiles.txt
-
relative vs absolute paths using the tilda
~
to make it an absolute wherever you are it will go to that area -
touch
allows the generation of a empty file. i.eubuntu@:~/workspace/Learning_unix$ touch test.txt
-
echo
is useful to paste inputs to the terminal screen text which by itself isn’t super helpful but you can redirect the text using the '>' character to put files into a txt file i.eubuntu@:~/workspace/Learning_unix$ echo "Call me Ishmael." > opening_lines.txt
-
less
allows us to see the opening lines of a text document. -
cat
The cat command also displays the content of files directly in the command line. You can use cat to quickly combine multiple files or, if you wanted to, make a copy of an existing file:cat opening_lines.txt next_idea.txt > two_ideas.txt
-
>
vs>>
. Notice that we use>>
and not just>
. The>>
operator will append to a file. If we only used>
, we would end up overwriting the file. -
Nano is a basic terminal text edit you could open by typing:
nano example_text_file.txt
. to use the commands at the bottom this symbol '^' means control so control+ a letter -
grep
allows for the searching of files to find lines that match a certain pattern. It has a lot of modifiers that you add after the grep i.e:grep -i “opening_lines.txt”
-i
ignores case while matching
-
|
<- this is the piping command which allows you to pipe one unix commands output to another so you could usegrep
to search for a file and the thewc
command to word count it in one line of code. Whenever making a long pipe, test each step as you build it! -
top
andhtop
look at computer resource usage, this lets you see what is running and how much cpu allocation / memory is currently in use.
Learning shell
- This is an interactive website that builds off the griffith lab tutorial above and teaches you to use shell and write shell programmes
- Found here: http://www.learnshell.org/
- Bash Academy is another resource that is very new — the guide is very in depth and if you want to master bash programming this would be a great place to start. However, just for basic usage it may be more than required.
- Software Carpentery have an introductory tutorial to using the bash shell here