What's the first thing you think of when you hear this? Go to www.menti.com with code 9132 7354
<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.
-
A lot of great tools are command line only!
-
Different tools can be combined easily.
-
Tasks can be automated easily using shell scripts.
- CMD tools are easier and faster to create.
- They're also more flexible.
/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
- When they start with
/
they're absolute i.e
/bin/ls
Relative paths are specified relative to the current directory.
tjones/notes.txt
../hpc-0123/data.hdf5 #goes one level up
pwd # print your 'present working directory'
# i.e where you are right now.
ls # List the files and folders in the pwd
$ 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
$ 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
$ 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!
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
Ok now you try!
tomhodson.github.io/command_line_slides/task1
Slides at
tomhodson.github.io/command_line_slides
$ git commit --dry-run -m "My commit message"
#!/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
#!/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}
bash my_script.sh
Or
chmod +x my_script.sh # just the first time
./my_script.sh
#!/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
apt-get
(linux)brew
(mac)pip
(python)conda
(python + other)npm
(javascript)- and many many others
# this prints any line that contains string_to_find
cat *.py | grep string_to_find
Like global variables in python.
$ export name=value
$ echo $name
value
$ echo $HOME
/Users/tom
$ env #prints all variables
$ curl url # make almost any kind of HTTP request
$ wget url # many differenet ways to download things
$ youtube-dl # automated youtube video downloader