COMP30023 Week 01 – CLI
School of Computing and Information Systems
COMP30023: Computer Systems
Copyright By PowCoder代写 加微信 powcoder
Week 01 – CLI Revision
In this activity, you will (re)?gain1 familiarity with basic *nix CLI commands.
This will help you in completing practical/lab classes and projects and interacting with the VM.
You should complete Part 1 and 2 of the prelab before starting this activity.
However, it is possible to complete these activities on dimefox.
If you are already familiar with the CLI, we strongly encourage you to complete the exercises in Section
3.4 and 4.1, towards the end of this document.
1 Upload files with scp
In this task, you will download the cli.tar.gz archive file from the LMS and upload it to your server
using the scp command.
If you have completed the optional SSH config setup in the prelab, execute the following command on
your local machine:
$ scp
If you have not completed the optional setup, use the command:
$ scp -i
Now SSH into your server to confirm that the file has been uploaded ( ls ).
You may also want to download files from your server in later practicals, which can be achieved by
executing the following command on your local machine:
$ scp comp30023:
$ scp -i
Tip: Directories can be transferred using the -r flag.
To synchronise local and remote directories, consider the rsync command.
2 The basics
Command Line Interface utility tools you will use in this section:
• Shell command: cd
• coreutils commands: pwd, mkdir, rm, ls, cat
For all of the utility tools above, you can read about their usage using man. For example, if you wanted
to know how to use mkdir, you can run the following command verbatim:
$ man mkdir
which will give similar output to:
1Regex all the things!
2Do not omit the :
3Do not omit the .
MKDIR(1) User Commands MKDIR(1)
mkdir – make directories
mkdir [OPTION]… DIRECTORY…
DESCRIPTION
Create the DIRECTORY(ies), if they do not already exist.
Mandatory arguments to long options are mandatory for short options
-m, –mode=MODE
set file mode (as in chmod), not a=rwx – umask
-p, –parents
no error if existing, make parent directories as needed
-v, –verbose
print a message for each created directory
-Z set SELinux security context of each created directory to the
default type
–context[=CTX]
like -Z, or if CTX is specified then set the SELinux or SMACK
security context to CTX
–help display this help and exit
output version information and exit
Written by Kenzie.
REPORTING BUGS
GNU coreutils online help:
Report any translation bugs to
Copyright © 2020 Free Software Foundation, Inc. License GPLv3+: GNU
GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Full documentation
or available locally via: info ‘(coreutils) mkdir invocation’
GNU coreutils 8.32 March 2020 MKDIR(1)
Press q to exit.
Now perform the following tasks:
1. Determine which directory you’re in with the pwd command.
2. Use the ls command (with the appropriate flags) to list all the files and directories (including
hidden files and their attributes) that are in your home directory. Are there any hidden files?
3. Use cat (or if you prefer, less / more etc.) to print the contents of the file
.ssh/authorized_keys to the terminal. Does the output look familiar?
Tip: use the tab key to autocomplete file/directory names (e.g. type cd .s , tab key, and tab key
4. Make a new directory named test using mkdir .
5. cd into the test directory that you created, and check pwd again.
Tip: use the up arrow to repeat previous commands. See 4 (optional) for other convenient shortcuts.
6. Remove the test directory with the rm command.
Refer to the sample commands listed in the appendix if you’re stuck.
In this section, you will use the following commands to explore and perform some common operations
• coreutils: cat, cp, mv, head, tail, grep
• file, tar
3.1 Extract the prelab files
Execute $ tar xf cli.tar.gz 5 to extract the files for this activity.
cd into the cli folder.
3.2 File management
In this exercise, you will use cp and mv to complete some basic file operations.
1. Use the ls command to list the files which are in this directory.
2. Copy the file named empty file to a file named empty .
Tip: Use a \ to escape the space or surround empty file with quotation marks.
3. Use the mv command to rename the directory named I6OvLQd0No copying allowed!HanKgUl0No copying allowed!E4gftTS0No copying allowed!c8TV0aG1ETk 6 to
4. Use the mv command to move the file empty into the dir directory.
3.3 Examining files
Debugging often involves the examination of log files and program output.
In this exercise, you will use head , tail and grep to extract information from the file subjects (a
list of BSci-credited subjects taken from the unimelb handbook).
1. But first, let’s run the command $ file * 7. Looking at this output, what do you think the
file command does?
2. Use $ head subjects to print the first 10 lines of the file subjects .
3. Read the man page of head to determine the flag used to modify the number of lines which are
outputted. Use this knowledge to print the first 20 lines instead.
4. Similarly, use the tail command to print the last 20 lines of the file.
5. The grep command can be used to search for and print lines that match patterns.
The basic syntax is as follows: $ grep search_pattern path/to/file .
Use grep to search for subjects starting with COMP.
4https://ss64.com/bash/syntax-keyboard.html
5https://explainshell.com/explain?cmd=tar+xf+filename.tar.gz
6This was randomly generated with pwgen . It is deliberately long, to discourage you from typing it out.
7* is a glob. This pattern is a wildcard which matches all visible files
https://ss64.com/bash/syntax-keyboard.html
https://explainshell.com/explain?cmd=tar+xf+filename.tar.gz
3.4 Line endings
In this exercise, you will use the commands from above to briefly examine the files which are in the cli
directory.
1. Use the cat command to output the file welcome .
What do you notice about the positioning of the $ ?
Why do you think this is the case? Run $ file welcome if you are unsure.
2. Now use cat to concatenate the files welcome and comp30023 , and then concatenate the files
welcome and comp30023_1 . Is there any visible difference?
3. What if we compare the outputs with the diff command?
Redirect the outputs8 of the cat command into files out1 and out2 and run diff out1 out2 .
Note: 1c1 indicates that line 1 in the first file was changed to line 1 in the second file.
Why does diff indicate that out1 and out2 are different?
Hint: try $ file comp30023 comp30023_1 .
Can we make diff ignore differences in line endings?
4 Other useful commands and variations (challenge, extension)
• which to “show the full path of (shell commands)”, e.g. which ssh .
• wc -l or nl to count the number of lines in a file.
• grep with regex, e.g. grep -E ‘COMP|INFO|SWEN’ subjects
• cut to “remove sections from each line of files”, e.g. cut -d’ ‘ -f2- subjects .
• sort to sort lines, uniq to omit repeated lines (once sorted).
• Combining commands with pipes, where the output of the command before the pipe becomes the
input of the command after the pipe (more on this in the later half of the subject). e.g.
cat subjects | grep -E ‘COMP|INFO|SWEN’ | wc -l to count the number of subjects starting
with COMP/INFO/SWEN.
4.1 Exercises
Use commands you learnt above to answer the following questions:
1. How many subjects are there in subjects ?
2. Use command(s) (e.g. cut ) to extract a list of subject codes.
3. How many 10001 subjects are there?
4. How many subjects start with C?
5. When sorted by subject name (not code), what is the last COMP subject that is listed?
6. How many letters of the alphabet are represented in the first character of subject codes?
7. What subject prefix (e.g. COMP, SWEN etc.) is most frequently occurring? What is its count?
5 Additional resources
• https://missing.csail.mit.edu/
• https://linuxjourney.com/lesson/alias-command
• http://www.ee.surrey.ac.uk/Teaching/Unix
8e.g using > or tee
https://missing.csail.mit.edu/
https://linuxjourney.com/lesson/alias-command
http://www.ee.surrey.ac.uk/Teaching/Unix
A Sample solutions
1. $ pwd , output should be /home/
2. $ ls -la . Yes, there should be hidden files and directories, e.g. .bashrc , .ssh/ .
3. $ cat .ssh/authorized_keys , $ cat ~/.ssh/authorized_keys ,
$ cat /home/
be your public key (which gives you access to your VM).
4. $ mkdir test or $ mkdir test/
5. $ cd test or $ cd test/ , then pwd should show that you’re within /home/
6. $ rm -r test , or since the directory is empty, $ rmdir test/
2. $ cp empty\ file empty or $ cp “empty file” empty
3. $ mv I6OvLQdHanKgUlE4gftTSc8TV0aG1ETk dir , use tab completion to autocomplete I6… .
4. $ mv empty dir/ , mv empty dir/empty
1. FILE(1) BSD General Commands Manual FILE(1)
file — determine file type
2. $ head subjects
AGRI20026 Plant Growth Processes
AGRI20036 Ecology and Grazing Management
… 8 more lines
3. $ head -n 20 subjects , head –lines=20 subjects
4. $ tail -n 20 subjects
… 12 more lines
ZOOL30008 Experimental Marine Biology
ZOOL30009 Tropical Field Ecology
5. $ grep COMP subjects
COMP10001 Foundations of Computing
COMP30023 Computer Systems
COMP30027 Machine Learning
Tip: can also use regex, e.g.
$ grep ^COMP subjects to match start of line,
or $ grep -E ‘COMP[0-9]{5}’ subjects to match the numeric code also.
1. $ (to be more specific, your primary Prompt String or PS1) should trail welcome ,
e.g. welcome .
This is because there is no trailing newline.
2. cat welcome comp30023 , cat welcome comp30023_1 , no visible difference.
3. cat welcome comp30023 > out1 && cat welcome comp30023_1 > out2 && diff out1 out2
diff indicates that out1 and out2 are different. This is because comp30023 and
comp30023_1 have different line endings, which should be apparent after running the file
command on both files.
It is possible to make diff strip CR characters, using –strip-trailing-cr
Bonus: How can we inspect files to determine their line endings?
Use xxd to print the hexdump of both files.
$ xxd comp30023
00000000: 746f 2063 6f6d 7033 3030 3233 0a to comp30023.
$ xxd comp30023_1
00000000: 746f 2063 6f6d 7033 3030 3233 0d0a to comp30023..
From this, we can see that comp30023 ends in 0a, which corresponds to LF (\n), while comp30023_1
ends in 0d0a, which corresponds to CRLF (\r\n) (see ASCII table).
4. Bonus: The unimelb file is a GIF (a binary file)
$ file unimelb
unimelb: GIF image data, version 89a, 115 x 111
$ xxd unimelb | head
00000000: 4749 4638 3961 7300 6f00 c400 0000 5c9b GIF89as.o…..\.
00000010: 4576 ac00 5e9a 6487 b7ff ffff d1d9 ea30 Ev..^.d……..0 … truncated
Where does the information from file come from?
GIF 89a is immediately apparent.
For dimensions, look to bytes 6-10 (i.e. 7300 6f00), and convert 0x0073 and 0x006f to decimal.
73 00 becomes 0x0073 and 00 6f become 0x6f00 due to little-endian byte ordering.
https://github.com/file/file/blob/81f15c2b0d6e9eaf524ff7bab37426c21af75fb7/magic/
Magdir/images#L512
1. $ wc -l subjects
2. $ cut -d’ ‘ -f1 subjects , awk -F’ ‘ ‘{print $1}’ subjects
3. $ grep 10001 subjects | wc -l
4. $ grep ‘^C’ subjects | wc -l
5. $ grep ^COMP subjects | sort -k 2 | tail -n 1
COMP30026 Models of Computation
6. $ cut -c 1 subjects | sort | uniq | wc -l
7. $ cut -c 1-4 subjects | sort | uniq -c | sort -n | tail -n 1
https://github.com/file/file/blob/81f15c2b0d6e9eaf524ff7bab37426c21af75fb7/magic/Magdir/images#L512
https://github.com/file/file/blob/81f15c2b0d6e9eaf524ff7bab37426c21af75fb7/magic/Magdir/images#L512
Upload files with scp
The basics
Extract the prelab files
File management
Examining files
Line endings
Other useful commands and variations (challenge, extension)
Additional resources
Sample solutions
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com