Websys-Linuxskillstest PDF

Title Websys-Linuxskillstest
Course Web Systems
Institution University of Technology Sydney
Pages 9
File Size 649.6 KB
File Type PDF
Total Downloads 10
Total Views 106

Summary

E. cat [file] | wc -mWould print out the number of characters in the fileCtrl + dTerminates grep processLesson 2: VIMVim – text editor for modifying ASCII files.h j k l: left, down one, up one, right H: top of screen M: middle of screen L: bottom of screen0 : beginning of line $: end of line ): star...


Description

E.g. cat [file] | wc -m Would print out the number of characters in the file Ctrl + d Terminates grep process

Lesson 1: The Filesystem Working directory: Distinguishes shell from the GUI is you, the user, are located in a directory/folder of the file system. Essential commands Pwd: see where you are located Ls [directory]: display list of files Cd [directory]: change working directory Mkdir [directory]: create a directory Touch [file]: create a file Cp [file1] [file]: copy a file Rm [file]: remove empty directory Cat: print contents of any file to screen. Copies file contents to STDOUT. Operates by characters not lines. Accepts STDIN -> input redirection Echo: displays a line of text. Echo strings to STDOUT. Head: only reads first 10 lines of files (or specify number of lines to print -n3 prints 3 lines) Tail: prints last lines of files (-f to follow files for any new data added, useful for watching system log files) Wc: count chars and lines/words/bytes in a file Saving output to a file When executing Bash command, two types of output on screen STDOUT standard output STDERR standard error  

Redirection: capture STDOUT data and sending to a destination other than screen. Saving STDERR as file: 2> Redirecting STDOUT Echo “hello world” > test.txt Cat test.txt (verify contents) Appending 2 outputs to a file >> for second line Listing -a: list all files incl invisible content -A: almost all, not incl . and .. -l: list with details -> permissions, size, etc -t: list newest to oldest -s: list largest to smallest -r: reverse of the ordering selected

Lesson 2: VIM Vim – text editor for modifying ASCII files. h j k l: left, down one, up one, right H: top of screen M: middle of screen L: bottom of screen 0: beginning of line $: end of line ): start of next sentence (: start of previous sentence Gg: start of file G: end of file # number: #number of line Yy: copies one line Yw: copies a word Y$: copies from cursor to end of line V: highlights one line v: highlights one character P: paste Dd: delete line of text Dw: delete word D: delete from cursor to end of line X: delete a character U: undo .: repeat last action / [keyword]: search n: search in next N: search backwards :%s/[pattern]/[replacement]/g: replace To search for either of {"abc","aBc"} type /a[bB]c To search for any string in the set {"ac", "abc", "abbc", "abbbc", ... } Type /ab*c where b* means zero or more b’s Add word boundaries to get and instead of sandy /\ Search and Substitute: Replace every occurrence of abc to ABC :%s/abc/ABC/g 1. : "change to command mode" 2. %s "substitute on every line in the file"

3. /g means "replace all occurrences in the line - not just the first."

Lesson 3: People and Permissions Less /etc/passwd: see list of users Less /etc/group: see list of groups Users - (read, write, execute) - permissions for owner Group - (read, write, no execute) - permissions for users who are in the owner's group Other - (read, no write, no execute) - permissions for other users chmod [ugo][+-][rwx] /path/to/file u: user g: group o: others 7 = all 3= write &execute 2 = write 1= execute Eg. Others can enter + list chmod o+rx /path/to/directory Eg. Unreadable to others chmod o-r /path/to/file To make a file into a program (executable file): chmod +x file.sh (executable by owner) ./file.sh 3.1 Who Am I – “whoami” tells used id. Store user id in myuserid. Touch myuserid Whoami > myuserid

4=read

Lesson 5: Piping and Wildcards wc: displays a count of lines, words, and characters in a file. wc –m: Count characters. wc –w: Count words delimited by white space characters or new line characters. wc –l: Count lines. tail: Delivers the last part of the file. tail –n: n = number the starting location in the file is measured in lines instead of bytes. The origin for counting is 1; that is, -n+1 represents the first line of the file, -n-1 the last. ls: Lists the contents of a directory. ls –R: Includes the contents of subdirectories. ls –r: Reverses the order of how the files are displayed. ls –t: Shows you the files in modification time. cp: Copies files from one location to another. cp –r: copy directories recursively >> , 2>>: append standard output to a file (command >> file) *: matches any string, incl null ?: matches any single character ^: starting with $: end with ie grep '^a.*e$' .*: repeated from zero to any number of times []: matches a range of characters [!]: negative match (all except) \ "escapes" the next character. So [\[\]] represents a pattern containing [ or ] {}: group patterns separated by commas e.g. {*.doc,*.pdf} match all files ending with .doc or .pdf Eg. [[:upper:]a-z] would match 2 characters which start with an Uppercase letter. touch large_to_small cd /course/linuxgym/gutenberg ls -S > ~/large_to_small

or ls -S course/linuxgym/gutenberg > /large_to_small

Lesson 6: Introduction to Scripting File becomes program by setting permission to executable (chmod 777) #! Tells bash which interpreter to run commands # elsewhere, rest of line is a comment to be ignored $n: nth argument $1, $2, $3 etc - First, second, third... arguments after the script name; $* - a string consisting of all the arguments to the script; $# - the number of arguments to the script. 2>&1 - sends the STDERR to STDOUT 1>&2 - sends the STDOUT to STDERR Eg. Echo Word 1>&2 tail -n "+$X" /path/to/file | head -n "$((Y-X+1))"

Sample Q1 Create a directory called sample in your home directory. Mkdir sample

Sample Q2 First create a directory called sample2 then create a file called nested.txt inside sample2 Mkdir sample2 Touch ~/nested.txt

Sample Q3 List all files in the directory /course/linuxgym/gutenberg starting with the character 3 and store this list into a file called list_3_files.txt . Ensure that the full path of the filenames is in this file. Touch list_3_files.txt Ls /course/linuxgym/gutenberg/3* > list_3_files.txt

Sample Q4 Create a file called permission_2_tweek.txt with permissions as follows: 1. You yourself have permission to read and write. 2. Your group has permission to execute. 3. The rest of the world have permission to execute. Touch permission_2_tweek.txt Chmod 611 permission_2_tweek.txt

Sample Q5 Create a bash script called hello.sh This script should print out the string Hello and the first argument For example, hello.sh Rumpelstiltskin should display Hello Rumpelstiltskin #!/bin/bash Echo Hello $ ./hello.sh

Sample Q6 Count the number of words in the file /couse/linuxgym/gutenberg/0ws0310.txt and store the results in the file count.out Do not include any filenames in the count.out file. cat /course/linuxgym/gutenberg/0ws0310.txt | wc -w > count.out

Sample Q7 Put the first 1000 lines (inclusive) of the file /course/linuxgym/gutenberg/0ws0310.txt into a file called top.txt Then change all occurrences of Queene to Queen in this file. Head -1000 /course/linuxgym/gutenberg/0ws0310.txt > top.txt Vim top.txt

:%s/Queene/Queen/g

Sample Q8 Create a file called let.txt containing the list of names of files in /course/linuxgym/emptystuff which have lines BEGINNING with the string Let Ensure there is one file name per line (WITH FULL PATH NAME). Also sort the names in ALPHABETICAL ORDER, remove all extra blank lines and make sure there are NO REPETITIONS of a file name (ie: file names occurs only once). Each line should be of the form : /course/linuxgym/emptystuff/some_file_name Touch let.txt Grep -l “^Let” /course/linuxgym/emptystuff/* | sort > let.txt Vim let.txt

:%s/\s\+//e...


Similar Free PDFs