├── README.md ├── code ├── .gitignore ├── part1.txt ├── part2.1.txt ├── part2.2.txt ├── part3.1.txt └── part3.2.txt ├── exercises ├── .gitignore ├── part1.exercises.md ├── part2.1.exercises.md ├── part2.2.exercises.md ├── part3.1.exercises.md └── part3.2.exercises.md └── slides ├── .gitignore ├── introduction_to_the_command_line.pdf └── introduction_to_the_command_line.pptx /README.md: -------------------------------------------------------------------------------- 1 | # Introduction to the Command Line 2 | 3 | ## Pre-Requisites 4 | 5 | ### Shell 6 | 7 | You must be using a mainstream shell (bash, zsh or the like). 8 | 9 | ### Basic Shell Knowledge 10 | 11 | ## Layout of this repository 12 | 13 | `exercises` - course exercises 14 | 15 | `code` - the example walkthroughs 16 | 17 | `slides` - course slides 18 | 19 | ## Links 20 | 21 | [This repo](https://github.com/ianmiell/introduction-to-the-command-line) 22 | 23 | [Introduction to the bash shell](https://www.oreilly.com/live-training/courses/introduction-to-the-bash-shell/0636920267348/) 24 | 25 | [Man pages](https://man7.org/linux/man-pages/) 26 | 27 | [Regexes](https://regexone.com/) 28 | 29 | [Readline](https://tiswww.case.edu/php/chet/readline/readline.html) 30 | 31 | [Syscalls](https://chromium.googlesource.com/chromiumos/docs/+/master/constants/syscalls.md) 32 | -------------------------------------------------------------------------------- /code/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ianmiell/introduction-to-the-command-line/2184cac6208725955a2d5987fd2fba5443e0d641/code/.gitignore -------------------------------------------------------------------------------- /code/part1.txt: -------------------------------------------------------------------------------- 1 | # These commands should be types out by hand to get an understanding of what they do. 2 | # They are safe to run if run exactly as they are listed here. 3 | # Note, you do not need to include anything from the comments onward in each line (the parts beginning with #...) 4 | cd # go to home folder 5 | pwd # folder I am in 6 | cd /tmp # move to another folder 7 | cd - # return to previous folder 8 | 9 | mkdir icl-core # make folder for this activity 10 | cd icl-core # change directory to this folder 11 | cd - # return to previous folder 12 | cd - # come back to newly-created folder 13 | mkdir path/to/a/new/folder # try to make a new set of folders 14 | mkdir -p path/to/a/new/folder # use -p to create deeper chain of folders 15 | cd path # move into path folder 16 | 17 | touch afile # create a file 18 | touch .adotfile # create a dotfile 19 | 20 | ls # list files 21 | ls -l # list files with more information 22 | ls -a # list all files, including dotfiles 23 | ls -lrt # most commonly-used in practice: long, reversed time order (most recently changed files first) 24 | ls -lrta # as above, but with dotfiles too 25 | ls -R # show files recursively down the hierarchy 26 | ls -lR # same, but with long output 27 | 28 | rm afile # delete a file 29 | ls 30 | rm to # try and remove the 'to' folder 31 | rmdir to # try to use rmdir to remove a folder 32 | rmdir to/a/new/folder # can remove folder if there is nothing in it 33 | ls -lR # check it worked 34 | rm -r to/a # rm with -r can delete without check 35 | ls -lR # check again 36 | 37 | # The next four lines create a file 38 | cat > afile << EOF 39 | some text in a file 40 | some more text 41 | EOF 42 | # file created 43 | cat afile # output contents of file 44 | tac afile # reverse output of contents of file 45 | 46 | cp afile afile.copied # copy file 47 | cat afile # original still intact 48 | cat afile.copied # copy is the same 49 | 50 | echo some text # output text to screen 51 | echo -n no newline # output with no newline 52 | echo -n no newline; echo -n no newline # demonstrate no newline if in zsh, and semicolon separator 53 | echo -n no newline && echo -n no newline # demonstrate no newline if in zsh with '&&' operator 54 | 55 | less /usr/share/doc/bash/bash.html # less command 56 | 57 | man ls # man command 58 | man man # documentation about man itself 59 | man 1 intro # description of section 1 of man pages 60 | man 2 intro # description of section 2 of man pages 61 | man -k browser # find man pages that mention word in main description 62 | man -K browser # search for man pages that have text anywhere in its text 63 | 64 | sudo ls # get admin privileges (might not work for you) 65 | 66 | mkdir tmp # make a folder 67 | cd tmp # move into the folder 68 | touch afile # create a file 69 | ls -l afile # list the file, showing file modes 70 | chmod -r afile # no-one can read 71 | ls -l afile # list the file, showing file modes 72 | chmod +r afile # everyone can read 73 | ls -l afile # list the file, showing file modes 74 | chmod o-r afile # 'other' users cannot read 75 | ls -l afile # list the file, showing file modes 76 | chmod g+r afile # group owner can read 77 | ls -l afile # list the file, showing file modes 78 | info chmod # info command 79 | 80 | echo $0 # which kind of shell am I in? 81 | pushd /tmp # push folder onto stack, and move to it 82 | popd # pop folder from stack, and return to next on stack 83 | sh # run a `sh` shell 84 | pushd /tmp # try pushd from `sh` shell 85 | 86 | cd && rm -rf icl-core # cleanup (optional) 87 | -------------------------------------------------------------------------------- /code/part2.1.txt: -------------------------------------------------------------------------------- 1 | # These commands should be types out by hand to get an understanding of what they do. 2 | # They are safe to run if run exactly as they are listed here. 3 | # Note, you do not need to include anything from the comments onward in each line (the parts beginning with #...) 4 | cd # move to home folder 5 | mkdir icl-text # create folder to work in 6 | cd icl-text # change to working folder 7 | 8 | git clone --depth=1 https://github.com/kubernetes/kubernetes # clone the repo - this may take a while (1 minute on my host) [git not covered in course] 9 | 10 | cd kubernetes # move into codebase 11 | 12 | du . # disk taken up by each file 13 | du -hs . # how much disk is taken up by all files in and under this folder 14 | 15 | grep network * # look for occurrences of the string 'network' in files 16 | grep -r network * # look for occurrences of the string 17 | grep -r network * | less # pipe the output to 'less' 18 | grep -ri Network * | less # ignore case 19 | grep -riw Network * | less # ignore case, and match 'network' as a whole word only 20 | grep -vrwi Network * | less # match lines that DON'T have the word 'network' in them 21 | grep -nrwi Network * | less # show line numbers as well 22 | grep -rwi Network * | tr 'n' 'p' | less # use 'tr' to replace 'n's with 'p' in the output 23 | grep -rwi Network * | tr 'nN' 'pP' | less # use 'tr' to replace 'n's with 'p's while keeping case 24 | grep -rwi Network * | tr -d '\n \t' | less # remove all newlines, space and tabs from output 25 | grep -rwi Network * | sed 's/Network/FOO/' | less # use sed to replace first match of 'Network' with FOO 26 | grep -rwi Network * | sed 's/[Nn]etwork/FOO/' | less # use sed to replace first match 'Network' or 'network' with FOO 27 | grep -rwi Network * | sed 's/[Nn]etwork/FOO/g' | less # use sed to replace 'Network' or 'network' with FOO everywhere on the line 28 | grep -rwi Network * | sort | less # use sort to sort the output 29 | grep -hrwi Network * | sort | less # use -h to remove filenames from the output 30 | 31 | wc -l README.md # number of lines in README.md file 32 | wc -w README.md # number of words in README.md file 33 | wc -c README.md # number of characters in README.md file 34 | 35 | grep -hrw 'Network' * | wc -l # number of lines that match whole word 'Network' 36 | grep -hrw 'Network' * | wc -w # number of words in lines that match whole word 'Network' 37 | grep -hrw 'Network' * | wc -c # number of characters in lines that match whole word 'Network' 38 | grep -hrwi 'Network' * | wc -l # number of lines that match whole word 'Network' ignoring case 39 | grep -hrwi 'Network' * | wc -w # number of words in lines that match whole word 'Network' 40 | grep -hrwi 'Network' * | wc -c # number of 41 | 42 | head README.md # 43 | tail README.md # 44 | tail -20 README.md # 45 | tail -f README.md # 'follow' file as it is being written to. CTRL+C to quit 46 | -------------------------------------------------------------------------------- /code/part2.2.txt: -------------------------------------------------------------------------------- 1 | # These commands should be types out by hand to get an understanding of what they do. 2 | # They are safe to run if run exactly as they are listed here. 3 | # Note, you do not need to include anything from the comments onward in each line (the parts beginning with #...) 4 | cd # move to home folder 5 | cd icl-file # change to working folder 6 | 7 | find git -name kubernetes # find files with the name 'kubernetes' in the folder 'git' 8 | find git -name kubernetes -type f # find files with the name 'kubernetes' in the folder 'git' that are of type 'file' 9 | find git -name kubernetes -type d # find files with the name 'kubernetes' in the folder 'git' that are of type 'directory' 10 | man find # look for info on types and flags 11 | 12 | touch someuniquefile # create a file that does not exist 13 | locate someuniquefile # not there 14 | sudo updatedb # update the database 15 | locate someuniquefile # file is in the database now 16 | 17 | touch afile # create a file 18 | cp afile copyofafile # simple copying of a file 19 | rm afile copyofafile # remove files 20 | cp -r kubernetes kubernetes-bak 21 | cp -r kubernetes kubernetes-bak # copy contents of folder 22 | ls kubernetes-bak # list contents of folder - kubernetes is in there 23 | ls kubernetes-bak/kubernetes # contents of that folder match 24 | rm -rf kubernetes-bak # remove copy 25 | 26 | rsync --dry-run -avz kubernetes/ kubernetes-bak # dry run, -a basically treats folder and contents as a single unit 27 | # forward slash is important, otherwise it puts kubernetes-bak/kubernetes/CONTENTS 28 | # rather than kubernetes-bak/CONTENTS 29 | rsync -avz kubernetes/ kubernetes-bak # for real 30 | rsync -avz kubernetes/ kubernetes-bak # re-run, with no change to files 31 | touch kubernetes/newfile # create a new file 32 | ls -lrt kubernetes # show new file 33 | rsync -avz kubernetes/ kubernetes-bak # copies over only new file 34 | touch kubernetes/newfile # update file 35 | rsync -avz kubernetes/ kubernetes-bak # copies over updated/touched file 36 | 37 | touch afile # create afile 38 | cp afile afile.copied # copy that file 39 | diff afile afile.copied # diff shows no difference 40 | # next three lines add a line to afile 41 | cat >> afile.copied << EOF 42 | a line added 43 | EOF 44 | 45 | diff afile afile.copied # diff shows line added 46 | # next three lines add a line to afile 47 | cat >> afile << EOF 48 | another line added 49 | EOF 50 | 51 | diff afile afile.copied # diff shows two different lines 52 | 53 | diff kubernetes kubernetes-bak # just looks at contents of the folders 54 | diff -r kubernetes kubernetes-bak # recurses down, looking for differences in files 55 | touch kubernetes-bak/somefile # add a file to kubernetes-bak 56 | diff -r kubernetes kubernetes-bak # file mentioned in diff 57 | # next three lines add a file with some content 58 | cat > kubernetes/somefile << EOF 59 | yet another line 60 | EOF 61 | diff -r kubernetes kubernetes-bak # file diffed 62 | 63 | cd && rm -rf icl-file # cleanup (optional) 64 | -------------------------------------------------------------------------------- /code/part3.1.txt: -------------------------------------------------------------------------------- 1 | # These commands should be types out by hand to get an understanding of what they do. 2 | # They are safe to run if run exactly as they are listed here. 3 | # Note, you do not need to include anything from the comments onward in each line (the parts beginning with #...) 4 | cd # move to home folder 5 | mkdir icl-net # make network folder 6 | cd icl-net # change to working folder 7 | 8 | wget https://bbc.co.uk/news # wget bbc news file 9 | less news # view the file 10 | rm news # remove the file 11 | ls 12 | 13 | curl https://bbc.co.uk/news # get same file with curl 14 | ls # no output, no file? 15 | curl -v https://bbc.co.uk/news # get same file with verbose output 16 | curl -L https://bbc.co.uk/news # follow 301 response to 'correct' location 17 | 18 | telnet bbc.co.uk 80 # use telnet to access 19 | GET /news HTTP/1.1 # type in HTTP request 20 | Host: bbc.co.uk 21 | # hit return twice to finish request and see repsonse 22 | 23 | 24 | nc -l 1234 # set up netcat listening server on host 25 | nc 1234 # in separate terminal, connect to that port 26 | nc bbc.co.uk 80 # works like telnet 27 | GET /news HTTP/1.1 # type in HTTP request 28 | Host: bbc.co.uk 29 | # hit return twice to finish request and see response 30 | 31 | ping bbc.co.uk # ICMP connect to host 32 | 33 | ip addr # all network interfaces 34 | ip -o addr # show one line 35 | ip -o addr | grep -v veth # get rid of veth pairs 36 | ip route # show routes 37 | ip neigh # show ARP table (machines 'nearby') 38 | 39 | dig bbc.co.uk # basic output. Note the SERVER line 40 | dig +trace bbc.co.uk # trace how the info was got 41 | dig @8.8.8.8 bbc.co.uk # use google's DNS server 42 | dig bbc.co.uk NS # who is the nameserver for this address? 43 | dig +short bbc.co.uk # just the IP - get used to this! 44 | dig +short +identify bbc.co.uk # just the IP, and tell me who told me 45 | 46 | nmap bbc.co.uk # get the open ports on that server 47 | 48 | cd && rm -rf icl-net # cleanup (optional) 49 | -------------------------------------------------------------------------------- /code/part3.2.txt: -------------------------------------------------------------------------------- 1 | # These commands should be types out by hand to get an understanding of what they do. 2 | # They are safe to run if run exactly as they are listed here. 3 | # Note, you do not need to include anything from the comments onward in each line (the parts beginning with #...) 4 | cd # move to home folder 5 | mkdir icl-sys # make system folder 6 | cd icl-sys # change to working folder 7 | 8 | ps # show processes running in your shell session 9 | ps -e # show all processes running on your machine 10 | ps -A # same as -e 11 | ps -ef # show fuller details for these processes 12 | 13 | sleep 192837 & # run a process in the background 14 | ps -ef | grep sleep | grep 192837 # get the pid 15 | kill [PROCESS ID] # try and politely terminate the process 16 | sleep 192837 & # run same process in the background again 17 | ps -ef | grep sleep | grep 192837 # get the pid 18 | kill -9 [PROCESS ID] # try to force termination of the process 19 | 20 | top # top command; q to quit 21 | top -n 1 # top with only one iteration 22 | top -b -n 1 # top with only one iteration in 'batch' mode 23 | 24 | watch -d top -b -n 1 # watch for changes running top 25 | 26 | systemctl # items running on your system under systemd control 27 | systemctl status # status of above items 28 | systemctl status postgresql # look at status of specific service 29 | journalctl -u postgresql # try looking at it as a 'normal' user 30 | sudo journalctl -u posgresql # if that doesn't work, try as root 31 | 32 | lsof # a lot of output... 33 | man lsof # look at type of file, q to quit 34 | lsof -c ls # show files open associated with a command 35 | lsof -i :443 # show files open and associated with a port 36 | lsof -p 1 # show files associated with the 'root' or 'init' process 37 | 38 | strace ls # show system calls for ls command 39 | strace -e read ls # show all calls to read system call for ls 40 | strace -e read -s 1024 ls # show all calls to read system call and show 1024 characters of data 41 | strace -e read -s 1024 -o out ls # as above, but write output to a file 42 | strace -e read,write -s 1000 curl bbc.co.uk # show all read and write system calls to a curl of bbc 43 | strace -t -e read,write -s 1000 curl bbc.co.uk # as above with time flag 44 | strace -tt -e read,write -s 1000 curl bbc.co.uk # as above with a further time flag 45 | strace -ttt -e read,write -s 1000 curl bbc.co.uk # as above with another time flag 46 | strace -c ls # summarise system calls 47 | strace -o nofollow curl bbc.co.uk # capture curl to a file 48 | strace -f -o follow curl bbc.co.uk # as above, but follow child processes 49 | 50 | sdiff follow nofollow # side by side diff 51 | 52 | cd && rm -rf icl-sys # cleanup (optional) 53 | -------------------------------------------------------------------------------- /exercises/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ianmiell/introduction-to-the-command-line/2184cac6208725955a2d5987fd2fba5443e0d641/exercises/.gitignore -------------------------------------------------------------------------------- /exercises/part1.exercises.md: -------------------------------------------------------------------------------- 1 | # PART I - EXERCISES 2 | 3 | 1. Create a folder 4 | 1. Create two files containing text from [these course materials pages](https://github.com/ianmiell/introduction-to-the-command-line). Use `cat` to copy and paste. 5 | 1. Use less to page the output of these files 6 | 1. Use ls to output the files in different ways, eg: With single column output; In reverse order; Experiment with other flags 7 | 1. Try man on all the commands seen in this part. Which of them don't have man pages? Why? 8 | 1. Read up on the info command [here](https://zwischenzugs.com/2019/09/04/the-lazy-persons-guide-to-the-info-command/) 9 | 1. Look up various pages with info on your system by going to `info coreutils` (NOTE: you may not have any `info` pages installed, so may get the `man` page by default) 10 | 1. Run through the commands [here](../code/part1.txt) 11 | -------------------------------------------------------------------------------- /exercises/part2.1.exercises.md: -------------------------------------------------------------------------------- 1 | # PART II.I EXERCISES 2 | 3 | 1. Find specific words from files in folder structure on your system. 4 | 1. Run various analyses of the files' content, eg number of lines in your codebase; number of words in your codebase; number of characters 5 | 1. Manipulate files content in streams, eg replace all 'a's with 'z's; replace all 'you's with 'thee's 6 | 1. Go through all the commands listed in the code for this section and study their man pages 7 | 8 | -------------------------------------------------------------------------------- /exercises/part2.2.exercises.md: -------------------------------------------------------------------------------- 1 | # PART II.II EXERCISES 2 | 3 | 1. Diff files in different ways. 4 | 1. Find specific files in your system. 5 | 1. Use rsync to copy folder structure, and determine the amount of disk your files take up. 6 | 1. Go through all the commands listed in the code for this section and study their man pages 7 | -------------------------------------------------------------------------------- /exercises/part3.1.exercises.md: -------------------------------------------------------------------------------- 1 | # PART III.I EXERCISES 2 | 3 | 1. Download the contents of a web page, analyse the content with 'wc', and 'grep' for specific text 4 | 1. Make 'wget' output to the screen (as 'curl' does), and make 'wget' output nothing if the page returns an HTTP redirect 5 | 1. Look up the IP addresses of some of your favourite sites 6 | 1. Which ports are open on those IP addresses? 7 | 1. Try 'ping'ing those IPs. See what happens and try to figure out why 8 | 1. Find out which DNS server your system is using 9 | 1. Find out how many ip addresses your machine has 10 | 1. Go through all the commands listed in the code for this section and study their man pages 11 | -------------------------------------------------------------------------------- /exercises/part3.2.exercises.md: -------------------------------------------------------------------------------- 1 | # PART III.II EXERCISES 2 | 3 | 1. Which processes are using the most memory and CPU on your machine? 4 | 1. Look at the flags to 'ps' in the 'man' page, and see what information you can retrieve about running processes on your machine 5 | 1. Does your system have 'systemctl'? Look at some of the services running on your machine 6 | 1. Research the 'netstat' command, and compare its functionality to 'lsof' 7 | 1. Go through all the commands listed in the code for this section and study their man pages 8 | -------------------------------------------------------------------------------- /slides/.gitignore: -------------------------------------------------------------------------------- 1 | *lock* 2 | -------------------------------------------------------------------------------- /slides/introduction_to_the_command_line.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ianmiell/introduction-to-the-command-line/2184cac6208725955a2d5987fd2fba5443e0d641/slides/introduction_to_the_command_line.pdf -------------------------------------------------------------------------------- /slides/introduction_to_the_command_line.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ianmiell/introduction-to-the-command-line/2184cac6208725955a2d5987fd2fba5443e0d641/slides/introduction_to_the_command_line.pptx --------------------------------------------------------------------------------