User Tools

Site Tools


notes:unix:spring2024:classnotes

UNIX class notes

Week 1

Welcome to the best class you have ever taken

This page includes some notes, typically major things, from each class session

Please add notes whenever you can = )

Week 2

*Re-went over group and pwd command

*we went into the /bin folder and found we can find new commands to play with

*we spoke about kernels, the foundation of the operating system. the kernel is what boots the operating system

*we went into other root folders, the dev folder contains many special files such as the zero file, which, similarly to the yes command, spits and endless string of 0's

*talked about full and null, check man files

*used the watch command, it repeats the following command with a delay ie, watch ls repeats ls continuously until exited

*you can check your messaging status with mesg

*to change messaging status follow mesg with y, or n depending on how you wish to change it

*went into /etc and checked out the passwd and shadow files, had a conversation about encryption

*we explored the group file in /etc

*we went into the /lib directory

*used ldd command, shows what libraries are needed for given argument

*went into the /proc directory, one thing in there is the cpuinfo file, which can show the hardware info from far away

*went into the /var directory

*/var/public is where projects are found when not using grabit

*went into /usr and subdirectories within


Week 3


Tuesday

Wildcards are a set of symbols that can be used to match files or directories. The four main wildcard symbols are: ?, *, [], [^]. The '?' symbol matches any single character, the '*' symbol represents 0 or more characters, the '[ ]' symbol represents a range of values, and the [^] symbol will exclude the range of values inside.

Wildcard examples:

ls -1d ??? This lists all files that contain only three letters in their name.

ls -1d c*n” This lists all files that start with a c and end with an n but has any combo of characters in the middle.

ls -1d ?[aeiou]??” This lists all four-letter files, but the second letter must be a vowel.

ls -1d ?[^a-j]?” This lists all three-letter files, but the second character must exclude all letters ranging from a through j. As you can see, you can represent a range of values either using a dash or entering the specific characters inside when using the '[]' or '[^]' symbols.

You can also “wc -l” to find how many files fit the criteria of your wildcard symbols.

For example: “ls -1d ?[d-m]?? | wc -l

Thursday

File permissions get more complex, you can now have s, S, t, and T covering the x in each category user and group execute permission would be replaced with s or S, other execute permissions can be replaced with T, or t. If it is capital there is no execute permission, if it is lowercase there is an execute permission s in user is setUID, s in group is setGID, t is other is sticky the new categories numbers are added on the front, so -rwSr-s–t implies user has read and write, no execute and it has setUID, group has read, execute and setGID, other has execute and sticky s (user) is 4, s (group) is 2, t (other) is sticky, so the previous example would be 7651, 7 because it has Sst, 651, because afformentioned basic permissions in unix double quotes are called half quotes, they allow expansion in unix single quotes are called full quotes/literal quotes, they have no expansion backtick is called a command expansion a word preceded by a dollar sign is a variable typically environment variables are in all caps backslash toggles functionality of the next character, ie /$varname does not call the variable


Week 5


Tuesday

The first portion of class was focused on different number bases, and solving our first octal pct. We went over what next week's projects will be, tpb0 and gfo0. We started using Vi in class which is like the original version of Vim (vi improved). Vi is a bi-moded text editor (meaning that there is a distinction between putting text in and running commands), and some text editors like Notepad and nano are unimoded text editors (meaning that there is no distinction between putting text in and running commands). Vi has command mode and insert mode, it starts in command mode

Insert commands (put you in insert mode):

  • i types before the cursor
  • a types after the cursor
  • I inserts at the start of the line
  • A appends at the end of the line
  • o insert on new line below
  • O insert on new line above

To get out of insert mode press the esc key

Navigation commands:

  • h moves left one character
  • l moves right one character
  • j moves down one line
  • k moves up one line
  • w moves forward by word (seperated values)
  • W moves forward by word chunk (seperated by spaces specifically)
  • b moves backwards by one word (seperated values)
  • B moves backwards by one word chunk (seperated by spaces specifically)
  • { moves backwards a paragraph
  • } moves forwards a paragraph
  • #G move you to the line of the given number
  • #gg is the same as #G
  • $G takes you to the last line in the file

(prefixing the navigation commands with a number moves by the number of characters, ie 6h moves left 6 characters) If you want to open Vim on a certain line you can type vim +line# textfilename

i.e. vim +57 project.c

Manipulation commands:

  • x is similar to the delete key by character
  • X is similar to backspace
  • dW delete by word forward
  • dB delete by word backward
  • d{ delete by paragraph backwards
  • d} delete by paragraph forwards
  • dd delete line
  • p paste
  • P paste
  • y copies, has yw, y{, yy
  • c is change, which is a delete and insert command as one, has cw, cb, c{, c}, c^, c$ (carat is start of file, $ is end of file so c^ is from here to beginning, c$ is from here to end)
  • . repeats last manipulation command

Exit commands

  • :w writes, if no file name is given you need to give it a file name, so :w filename
  • :q! quits without saving
  • :wq writes and quits
  • ZZ saves and quits, checks if there is enough space in the file system to save. This is technically safer than using wq only because wq does not check for space and if there was ever not enough memory, then whatever you tried to save would be gone.

You can see why vi is bimoded because we can switch between two different modes (command and insert).

Thursday

More basics in Vi

  • ~ toggles the case of letter, ie if cursor is on n ~ changes it 2 N
  • u undoes what was just done
  • q records your Vi experience
  • / search keyword, when followed by n goes to next, when followed by N goes to previous
  • s substitutes character / deletes character and places in insert mode
  • S substitutes line / deletes line and places in insert mode
  • ^ moves to beginning of line
  • $ moves to end of line

–Extended VI command mode–

to enter type a colon :

  • :#co# copies the first numbered line to the 2nd numbered line
  • :#,#co$ copies range of lines to end
  • :#m# moves numbered line to numbered line
  • :set number enables line numbers
  • :set nonumber disables line numbers
  • :syntax on turns on syntax coloring
  • :syn on same as syntax on
  • :set tabstop=# changes length of tabs
  • :set shiftwidth=#
  • :set cursorline puts a line where your cursor is horizontally
  • :set cursorcolumn puts a line where your cursor is vertically
  • :set smartindent adds smart indents in curley braces
  • :set scrolloff=# sets distance where
  • :colorscheme changes the color of your vim
  • :%s/word1/word2/g search and replace, searches for word1 and replaces with word2

You can edit your default settings in Vim by adding to your ~/.vimrc file ie by adding colorscheme torte to your ~/.vimrc file it changes the default colorscheme

–Other stuff– In unix the date command tells you the date and time, you can also specify dates, you can format the data, check the man page for further use

You can use cal to show a calendar, you can specify month and year ncal shows the calendar in a different orientation

head gives you the lines from the top of the file ie head -3 gives the first 3 lines tail gives you the lines from the bottom of the file ie tail -5 gives you the last 5 lines sed can do a search and replace


Week 6


Tuesday

whereis command shows the path to the executable given, ie whereis who responds with /usr/bin/who

for loops in bash may look like:

for VARIABLE in list OR command OR anothervariable; do
    something that the loop is doing;
done

ie

LIST=$(command that gives us some list)
for user in ${LIST}; do
whatever we want to do with the list;
done

Thursday


It is good practice to begin your scripts with #!path to interpreter so for our shell scripts, if we are using bash it would be useful to begin the script with #!/usr/bin/env bash

if statements in bash look like

if [ Condition to check ]; then
whatever we are doing
fi

numeric for loops

for (index=somenumber; index > OR < some other thing; )

in VI shift » and « change the indents by one level

Week 7


Tuesday

We did our first base 9 puzzle

More modification to our script, now it lists out time in days, hours and minutes

The script now accepts arguments to sort by most logins or most time

There are many ways to pass a script an argument, the way we did is by giving a variable the arguments defined by ${#}, ie the first argument would be ${1}

Thursday


The dawn of the final puzzle box is upon us, enjoy its brain expanding power

Field trip, went to the binary clock, which runs on an older gen raspberry pi

We found multiple ways to reset/backup an sd card

Field trip resulted in many great discussions pertaining to engineering and the limitations of creating technology, including software

Week 8


Tuesday

No class, free day for experimentation/completing assignments

Thursday


No class, free day for experimentation/completing assignments

Week 9


Tuesday

Virtual class session day

Started discussing wpa0

in a man page, or vi, or a less pager you can use a forward slash to search for something

Remember wildcards are specifically filenames, regex's are for text

REGULAR EXPRESSIONS!!!!!!!

Basic REGEX (in Unix)

  • . match any symbol (similar to ? in wildcards)
  • * match zero or more of the previous (similar, but different to * in wildcards)
  • ^ match start of line
  • $ match end of line
  • \< match start of word
  • \> match end of word
  • [ ] character class; match any one of enclsed (similar to use as wildcard)
  • [^ ] inverted character class; do not match any of enclosed (similar to use as wildcard)

Extended REGEX

  • + match 1 or more of the previous
  • | logical or
  • ( ) group together
  • \( \) REGEX group
  • ? match previous item at most once (0-1)
  • {n} match previous item exactly n times ie “ {3}” looks for exactly 3 spaces
  • {n,} match previous n or more times
  • {,m} match previous at most m times
  • {n,m} match previous at least n, but no more than m times

3 types of grep used to be used based on what REGEX's you need

  • fgrep - fast grep; no REGEX use
  • grep - regular grep; basic REGEX use
  • egrep - extended grep; extended REGEX use

you can still use extended regex with a regular grep, but they need to be preceded by a \, ie a grep “\?” should work like egrep “?”

diff - a command that can show differences in files, assuming they are sorted

Thursday


No class, free day to experiment and work on projects

Weeks 10-14


Mostly given time to work on projects, the last major discussion was Regex's, and a quick base 11 pct Work on your EoCE

Cleaned VI Notes

(Images courtesy of Corey)


There currently is more above, check Week 5 for a more complete list

notes/unix/spring2024/classnotes.txt · Last modified: 2024/04/16 13:19 by cgrant9