1/113
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Where was Unix designed and when?
1970 at Bell Labs
Who created Linux?
Linus Torvalds
Who developed C and when?
Dennis Ritchie and Brian Kernighan in 1972
Unix is built on
-Simplicity => KISS Principle
- Focus => Pick one thing to work on
- Filters => Filter info you don't want
- Open File Format => Linux, unlike windows, has open file format
- Flexibility => Users can do anything
Linux Programs broken into 2 different types
Binary- Compiled into machine code
- Ends in .bin, .run
Scripts
- Are interpreted
- Python, bash, perl
Both are executable
~
Absolute path to directory
echo ~ returns the current user's home directory
echo ~dinh returns dinh's current home directory
PATH
Environment variable that specifies a set of directories where executable (binaries) are found.
- export PATH=/new/directory:$PATH
- Sets PATH variable to new/directory, followed by existing path variable
- $PATH is the current PATH
cat
used to display content of file or join multiple files
cat file1.txt file2.txt > new.txt
- joins file1.txt and file2.txt combined into new.txt
rename
renames file
rename .out.txt .csv *
- Changes all .out.txt with .csv
ln
make links between files
ln -s foo sFoo
- cat sFoo will print what foo has
- if foo Removed, sFoo removed
- if ln foo nFoo, still printsHardlinks point file name to data on deviceSoftlinks point filename to another filename
wc
The wc (word count) command in Linux is a simple utility that displays the number of lines, word count, and bytes (or characters) in a file or files specified as input.
wc [options] [files]
Count lines: To count the number of lines in a file:
- wc -l filename
Count words: To count the number of words in a file:
- wc -w filename
Count bytes: To count the number of bytes in a file:
- wc -c filename
Count characters: To count the number of characters in a file:
- wc -m filename
Multiple files: You can also use wc with multiple files. It will display the counts for each file individually and then a total count for all files:
- wc filename1 filename2
If you run wc without any options, it will display the line count, word count, and byte count in that order.
- wc filename
The Shell
Program that lets us interact w/ kernal
- windows explorer and mac finder
sh - Bourne Shell -> original
ksh - korn shell
csh - cshell (written in c)
bash - Bourne again shell
Which
returns location of file
which cd
- returns /usr/bin/cd
Filename expansion (globbing)
Specify patterns that can match multiple file names or paths.
? globbing
Matches any single character
EX: file?.txt matches "file1.txt", "file2.txt", but not "file10.txt".
[ ]
matches any single character within specified range or set
EX: [abc]*.txt matches files starting with "a", "b", or "c" and ending with .txt
EX: [0-9]*.txt matches all files starting with a digit and ending with ".txt"
EX: [0-9].txt matches all files that has a number in it, and ending with ".txt"
EX: [a-zA-Z]*.txt matches all files starting with a letter (either uppercase or lowercase) and ending with ".txt".
{ }
Specifies alternatives for patterns
EX: {file1,file2}.txt matches "file1.txt" and "file2.txt"
Suppress
echo "" prints
but echo * prints everything
echo "*" is suppressed
Command Substitution
Shell feature that allows you to execute a command and use its output as part of another command
result=`command`
result=$(command) -> PREFERRED
EX: current_date=$(date +%Y-%m-%d)
echo "Today's date is: $current_date"
EX: echo "The # of files in the current directory is:
$(ls | wc -l)"
EX: current_user=$(whoami)
echo "Current user is: $current_user"
EX: for file in $(ls *.txt); do
echo "Processing $file"
done
Iterate all '.txt' files and prints a message
Arithmetic Expansion
Perform arithmetic calculations directly on command line
EX: result=$((5 + 3))
echo "5 + 3 = $result"
$(())
Concatenation of Strings
str1="Hello"
str2="World"
result="$str1$str2"
echo $result => HelloWorld
result2="$str1 $str2"
echo $result2 => Hello World
>>
Appends
"this is some text" >> file.txt
^ above appends the text to file.txt
- if file doesn't exist, it will be created
Standard Streams
C++ C Linux
cin stdin 0
cout stdout 1
cerr stderr 2
infile infile 3
cut
remove sections from each line of files
EX: cut -d ' ' -f 1 data.txt
- `-d ' '` specifies the delimiter is the space
- -f 1 specifies the field you want to extract, so 1st field
- data.txt is the input file
Alice 25 Alice
Bob 30 -> Bob
Charlie 28 Charlie
awk
Used to process column text
awk 'pattern { action }' file
EX: awk '{ print $1, $3 }' file.txt
- prints first and third column of file.txt
Ex: awk '$3 > 50 { print }' file.txt
- prints out everything in 3rd column that is greater than 50
sort
sort lines of text files
EX: sort file.txt
- words line in file.txt in ascending order
less
navigate through the pages of a file
EX: less file.txt
- opens file.txt for viewing
lpr
print files on a printer
EX: lpr document.pdf
lpstat
display status of print queue on system
EX: lpstat -p- displays list of all printers and their status
lprm
remove print jobs from print queue
EX: lprm 123- removes print job with ID '123'
$HOME
Home variable
Equivalent to ~
$SHELL
Tells you where your shell is
$USER
Gets username
Suppression of Expansion
Prevents shell from interpreting stuff
echo "This is a \$ character"
- prints out "This is a $ character"
paps
"PostScript to ASCII", convert PostScript files to plain text
EX: paps input.ps > output.txt
- takes postscript file (input.ps) as input and redirects ASCII
text output to text file output.txt
Tarball
A compressed archive of files containing scripts that install Linux software to the correct locations on a computer system.
Create a tarball: tar -czvf archive.tar.gz /path/to/directory
Regular Expression (regex)
Sequence of characters that define a search pattern. Examples to follow below
grep
Search files for text patterns line by line. Always use -E after
- grep [options] pattern [file ...]
-i: Ignore case distinctions in both the pattern and input files.
-v: Invert the match, displaying lines that do not match the pattern.
-n: Prefix each line of output with the 1-based line number within its input file.
-l: Display only the names of files containing matches, instead of the matching lines themselves.
-c: Display only a count of the lines that contain matches, rather than the lines themselves.
-r or -R: Recursively search subdirectories listed in the command line, treating them as directories rather than simple files.
-w: Match whole words only. The expression is searched for as a word (as if surrounded by [[:<:]] and [[:>:]]).
-E: Interpret the pattern as an extended regular expression (ERE), which supports additional features such as +, ?, (), etc.
-F: Interpret the pattern as a list of fixed strings, separated by newlines, any of which is to be matched.
grep examples
grep "pattern" file.txt
- searches for lines containing "pattern" in file.txt
- prints out all lines that contain "pattern"
grep -i "pattern" file.txt
- case insensitive search for pattern
- prints out all lines that contain case insensitive "pattern"
sed
Used to edit and display edited text files
- doesn't actually edit them unless you use -i or redirect
output to another file
sed [options] 'command' [filename]
-e 'command': Specify a command directly on the command line. Multiple -e options can be used to specify multiple commands.
-f script-file: Read commands from the specified script file. Multiple -f options can be used to specify multiple script files.
-n: Suppress automatic printing of pattern space. Only explicitly printed output will be displayed.
-i[SUFFIX]: Edit files in-place. Modifies the input files directly. Optionally, you can provide a backup file suffix to -i option.
-r: Use extended regular expressions in the script.
-E: Same as -r, but provided for compatibility with some other versions of sed.
-s: Treat the input files as separate streams. Normally, sed processes input files one by one, but with this option, each file is treated as a separate stream.
--follow-symlinks: Follow symbolic links when processing input files.
-u: Use unbuffered I/O, which is useful when processing large files.
sed examples
sed 's/old/new/g' file.txt
- substitutes all occurrences of 'old' with 'new' in file.txt
sed '/pattern/d' file.txt
- deletes all lines that contain 'pattern'
sed -i 's/old/new/g' file.txt
- actually edits the file to replace 'old' with 'new'
- 's' stands for substitute, and 'g' stands for global, meaning it will substitute for all findings. without g, it will only replace first one
sed -n '/pattern/p' file.txt
- prints lines containing 'pattern' in file.txt
[] regex
Matches any single character inside:
- [dDb] matches either lowercase 'd' or uppercase 'D'.
- grep "[dDb]og" file.txt matches lines containing "dog" or "Dog" or "bog" in the file file.txt.
^ regex
Matches the beginning
- ^start matches lines starting with "start".
- grep "^start" file.txt matches lines starting with "start" in the file file.txt.
$ regex
Matches the end
- end$ matches lines ending with "end".
- grep "end$" file.txt matches lines ending with "end" in the file file.txt.
{n,m} regex
Range from n to m:
- a{2,4} matches "aa", "aaa", or "aaaa".
- grep "a{2,4}" file.txt matches lines containing 2 to 4 consecutive 'a's in the file file.txt.
{n} regex
Exactly n:
- x{3} matches exactly three consecutive 'x's.
- grep "x{3}" file.txt matches lines containing exactly three consecutive 'x's in the file file.txt.
{n,} regex
At least n
{,m} regex
At most m
+ regex
1 or more preceding expression:
- go+gle matches "gogle", "google", "gooogle", etc.
- grep "go+gle" file.txt matches lines containing "gogle", "google", "gooogle", etc., in the file file.txt.
- Matches that has 1 or more 0's
* regex
0 or more preceding expression:
- go*gle matches "ggle", "gogle", "google", "gooogle", etc.
- grep "go*gle" file.txt matches lines containing "ggle", "gogle", "google", "gooogle", etc., in the file file.txt.
- Matches 0 or more 0's
? regex
0 or 1 preceding expression:
- colou?r matches "color" or "colour".
- grep "colou?r" file.txt matches lines containing "color" or "colour" in the file file.txt.
| regex
Or
preceding or succeeding expression:
- (cat|dog) matches "cat" or "dog".
- grep "\\(cat\\|dog\\)" file.txt matches lines containing "cat" or "dog" in the file file.txt.
() regex
Capture group:
- (good){2} matches "goodgood".
- grep "\\(good\\)\\{2\\}" file.txt matches lines containing "goodgood" in the file file.txt.
. regex
Matches any single character except for newline
- a.c would match "abc", "axc", "a1c", etc., but not "ac"
- grep "b.t" file.txt matches lines containing "bat", "but", "bit", etc., in the file file.txt.
-rw-r—r—
The file owner has read and write permissions (rw-).
The group that owns the file has only read permission (r--).
Other users have only read permission (r--).
Shebang line
#! /bin/bash
The path to the interpreter for the script
chmod
"change mode"
- allows you to specify who can read, write, and execute file or directory
- chmod [options] mode file
- chmod 755 file
- recursively sets permissions of all files to read and write for owner only
- chmod u=rwx,g=rx,o=r file
- This command explicitly sets permissions for the owner (u), group (g), and others (o) to read, write, and execute, read and execute, and read only, respectively.
For loop
for var in $@; do
echo $var
done
Scripting comparisons for arithmetic
-gt, -ne, etc.
Scripting comparisons for strings
==, =, !=, etc.
-n -not NULL
-z -not empty
CASE Statement
case $val in
red) echo "Its red";&
bl*) echo "Its bl + anything";;
blue) echo "Its blue";;&
esac
________ changes the working directory to the root directory.
cd /
________ changes the working directory to the user's home without using tilde expansion.
cd $HOME
______ changes the working directory to the user's home directory using tilde expansion.
cd ~
______ changes the working directory to the user bob's home directory using tilde expansion.
cd ~bob
Explain the differences between a static C library and a C shared library.
Shared libraries exist outside of the file they are referenced in, while static libraries are compiled as part of the app.
Write a command that concatenates the values found in classNumber and className into a new environment variable named class.
class="${classNumber}${className}"
Write a command that changes the permissions of a file named output rwxr-xr— using characters (not octal numbers) in combination with chmod (without octal numbers).
chmod u=rwx,g=rx,o=r output
Write a command that displays all exported environment variables.
env
Explain what less does
Less allows you to open a file in a navigatable environment.
Write a command that displays the first 5 lines of a file named classList.txt.
Write a command that displays the last 10 lines of a file named classList.txt.
head -n 5 classList.txt
tail -n 10 classList.txt
What command can be used to copy files from one system to another (e.g. your local computer to the department Linux boxes)?
scp
Explain how bash manages execution of commands with regards to processes.
bash will create a new bash process using a fork
Provide the proper filename expansion for the following (assume local directory) for all files ending in .sh.
*.sh
Provide the proper filename expansion for the following (assume local directory) for all files beginning with assign.
assign*
Provide the proper filename expansion for the following (assume local directory) for all files beginning with assign, containing any number of characters, and ending with .tar.gz.
assign*.tar.gz
Provide the proper filename expansion for the following (assume local directory) for all files in bob's home directory ending with .csv.
~bob/*.csv
Provide the proper regular expression (regex) for the following (assume single match per line) for all items that contain the string "file".
.(asterisk)file.(asterisk)
.* any char up to file
.* any char after
Provide the proper regular expression (regex) for the following (assume single match per line) for all items that begin with the string "file".
^file.*
Provide the proper regular expression (regex) for the following (assume single match per line) for all items that end with the string "ing".
.*ing$
Provide the proper regular expression (regex) for the following (assume single match per line) for all items that contain the same 4 or 5 letter sequence twice.
(.....?).*\1
Difference between soft and hard link for files
soft link is a reference to the file (like a pointer, if the pointer is deleted, the data is fine)
hard link is another name for the file (if the alternative name is deleted, the data is deleted)
Formatted vs unformatted input
Formatted - uses strings to represent data and works with bytes meant to be strings (scanf, printf)
Unformatted - uses raw data (put, write)
Errno
Global variable that holds the numeric code of the last system-call error
System calls
Uses file descriptor
open
read
write
Formatted input functions
fopen
fread
fwrite
What shells can custom environment variables be used in?
The current shell and any children
What does time() get?
The amount of time since the last epoch 1/1/1970
What's the difference between strftime and strptime?
strftime - formats the date and timestrptime - parses a string into a struct tm
execl
executes a file and doesn't return if successful
fork()
Creates a child process
The return differentiates whether it's a child or parent
Zombie process
A process that has terminated, but whose parent has not yet called wait()
Wait()
Function that performs blocking
Stops the program until something is done
What are pipes and what are they used for
Communication channel between a parent and child process
They are shared resources so they can create a race condition
How do you do polymorphism in C?
Function pointers
When is synchronization necessary?
When there is a shared resource
What is mutex?
A mutually exclusive access to a resource. It's a special case of a semaphore with a maximum concurrency of 1.
What are semaphores?
Semaphores are a generalization of mutexes that allow access of a resource up to a limit, not just 1
What is sigaction?
A function to examine and change a signal action
Signal communication is what?
Asynchronous meaning processes stop and deal with the signal before doing anything else