Skip to content

Latest commit

 

History

History
294 lines (254 loc) · 6.8 KB

slides.md

File metadata and controls

294 lines (254 loc) · 6.8 KB

The Command Line

What's the first thing you think of when you hear this? Go to www.menti.com with code 9132 7354

Use Menti code 9132 7354


Results

<iframe sandbox='allow-scripts allow-same-origin allow-presentation' allowfullscreen='true' allowtransparency='true' frameborder='0' height='315' src='https://www.mentimeter.com/embed/c92cb2f99d7edcaa0b0be2a177c5f231/1edf2d837bdd' style='position: absolute; top: 0; left: 0; width: 100%; height: 100%;' width='420'></iframe>
--- ## What is the command line? - A text based interface to a computer. - A programming language. - A tool with strengths and weaknesses.

Quick demo


Why use it?

  • A lot of great tools are command line only!

  • Different tools can be combined easily.

  • Tasks can be automated easily using shell scripts.


Why don't people just make a GUI?

  • CMD tools are easier and faster to create.
  • They're also more flexible.

Using the command line


Walking the filesystem


Paths

/bin/ls
/cm/shared/applications/
/home/tjones/notes.txt
  • A way to specify a file or folder with a text string
  • We use them to tell commands where to look for files and folders
  • Note the similarity to URLs

Absolute Paths

  • When they start with / they're absolute i.e
/bin/ls

These always refer to a specific location. A bit like a GPS coordinate.

Relative Paths

Relative paths are specified relative to the current directory.

tjones/notes.txt
../hpc-0123/data.hdf5 #goes one level up

More like a giving someone directons, 'From here, go left then right'

Looking around: ls and pwd

pwd # print your 'present working directory' 
# i.e where you are right now.
ls # List the files and folders in the pwd

Getting around

$ cd / # go to the root directory
$ cd   # go to your home directory
$ cd ~ # go to your home directory
$ cd ~/foo  # go to some folder in your home directory
$ cd /cm/shared/applications/ # go to an absolute path
$ cd ../  # go up one level from wherever you are
$ cd ../../  # go up two levels from wherever you are

Looking in files

$ ls #see what's in the current directory
$ cat file # outputs a file to the terminal 
$ nano file # opens a file for basic editing

To save the file in nano press CTRL-O (O for output) then press ENTER To exit press CTRL-X


Messing around

$ mkdir folder # makes a directory
$ mv file1 folder/file2 # moves and renames
$ cp file1 folder/file2 # copies and renames
$ rm file #deletes the file permanently
# be careful with rm!

Getting stuck

If you can't figure out how to use a command i.e git

Quick refreshser

git --help
git -h # some older commands only accept -h

Offline Manual

man git # press down to read and then q to exit

Or just google it, that's what I usually do.

Ok now you try!

tomhodson.github.io/command_line_slides/task1

Slides at

tomhodson.github.io/command_line_slides



Demo of task 1

--- ### A more complex command
$ git commit --dry-run -m "My commit message"


Scripting

Putting multiple commands together in a file makes a shell script.

A Simple Script

#!/usr/bin/env bash
cd ~
mkdir filesize
cd filesize
echo "numpy" > requirements.txt
echo "# File Size" > README.md
mkdir filesize
cp ~/somewhere_else/filesize.py filesize/filesize.py
python filesize/filesize.py README.md

Script Arguments

#!/usr/bin/env bash
echo Hello World
echo You invoked this script with $# arguments
echo The first one was $1
variable=42
echo The variable is ${variable}

Running a script

bash my_script.sh

Or

chmod +x my_script.sh # just the first time
./my_script.sh

For loops

#!/usr/bin/env bash
for python_file in *.py
do
    echo $python_file # prints the filename
    echo -------------
    cat $python_file # outputs its contents
done
echo I printed all the python files

Task 2


Demo of me doing task 2


Useful tools


package managers

  • apt-get (linux)
  • brew (mac)
  • pip (python)
  • conda (python + other)
  • npm (javascript)
  • and many many others

Pipes

# this prints any line that contains string_to_find
cat *.py | grep string_to_find

Environment variables

Like global variables in python.

$ export name=value
$ echo $name
value
$ echo $HOME
/Users/tom
$ env #prints all variables

Downloading things

$ curl url # make almost any kind of HTTP request
$ wget url # many differenet ways to download things
$ youtube-dl # automated youtube video downloader

Done!