question? - wordpress.com · 2015. 9. 18. · -rw-r--r-- 1 mwirth staff 473 31 aug 08:36...
TRANSCRIPT
Lecture 3Unix
Question?
• Denmark is famous for?
a. LEGO.
b. The world’s best restaurant.
c. Being in the top three happiest countries in the world.
d. Having the highest taxes in Europe (57%).
e. All of the above.
Announcements
• There are drop-in help sessions in Reynolds 001/002 (right after lectures).
• There are TAs who are dedicated just to private tutoring sessions. The tutoring times can be booked from the course website and are free to the students.
• There are times available beginning next week and anyone who is feeling even a tiny bit overwhelmed should take advantage of the tutoring.
• If the students don't use it we'll stop offering it.
Q&A
• Do you have a question?(I may have an answer)
Computers are closed boxes, hiding the command line and lower level features.
rm vs.
what’s
?
OS
OS
• The Operating System is a resource manager.
• Many different types of operating system, e.g. Windows, OSX, Unix/Linux.
Unix
Unix
• Unix is a multi-user, multi-tasking environment, offering stability, portability and powerful networking capabilities.
• Unix was created by Ken Thompson and Dennis Ritchie at AT&T in the early 1970s.
• Evolved from UNICS (Uniplexed Information and Computing System) written in assembler.
Unix-history
Incarnations?
• Berkeley Software Distribution (BSD)
• Solaris (Sun)
• Linux - a free open source UNIX OS originally developed by Linus Torvalds in 1991.
• Mac OSX - based on BSD, (Berkeley Software Distribution) version of Unix.
Importance?
• C and Unix evolved together.
• Unix kernel was re-written in C.
Benefits of Unix?
• The kernel itself is relatively small.
• Can be built specifically for the platform it runs on (OSX), or usable on anything, such as a wristwatch (Linux).
• Scales well.
• Can support fully-featured GUI, yet maintains the command-line.
Limitations of Unix?
• Too many choices for distributions (Linux).
• Lacks some widely used software (e.g. Photoshop).
Architecture
Hardware
Kernel
Shell
Applications
Kernel
• Interacts with hardware to perform memory management, task scheduling, and file management.
• Technically, the kernel “is” the OS.
Shells
• Textual command line shells.
• Processes user requests - commands are translated by the shell into something the kernel can understand.
Shells
• OSX, and many versions of Linux use the Bash shell as its default - the Bourne Again Shell.
• Many different types of shells: C shell (csh), Bourne shell (sh), Korn shell (ksh)
System utilities
• Commands such as ls, cp, grep, awk, sed, bc, wc
• Can be combined together to solve problems.
Unix file system
• Organized in a tree structure.
Unix file system
• Unix file systems have directories and files.
• Unix is CASE sensitive.
• This means Vader, vader, VADER, vaDer are all different.
Types of files
• Plaintext - files in human-readable format. e.g. readme.txt
• Binary - files in machine-code. For example executables (a.out), .zips, .pdfs, .jpgs
Commands
• A Unix command line consists of the name of a Unix command, followed by its "arguments" (options and the target filenames and/or expressions).
• The general syntax for a UNIX command is:
$ command -options targets ↵
ls
• ls is used to list files in a directory.
• By itself it just lists the file names, in alphabetical order.
• The command ls -la will list information associated with each file.
ls
$ ls -latotal 72drwxr-xr-x 9 mwirth staff 306 1 Sep 11:25 .drwxr-xr-x@ 272 mwirth staff 9248 1 Sep 11:04 ..-rwxr-xr-x 1 mwirth staff 9640 1 Sep 10:06 a.out-rw-r--r--@ 1 mwirth staff 3094 3 Jul 18:48 gamepig.c-rwxrwxrwx@ 1 mwirth staff 1439 30 Jun 10:02 luhn.c-rw-r--r--@ 1 mwirth staff 1717 3 Jul 12:01 mugwump.c-rw-r--r--@ 1 mwirth staff 490 31 Aug 08:04 paragraph.txt-rw-r--r-- 1 mwirth staff 473 31 Aug 08:36 paragraph2.txt-rw-r--r--@ 1 mwirth staff 3778 3 Jul 10:06 scramble.c$
a directory
permissions
owner group
no. bytes
date and time last modified
file name
ls
$ ls -latotal 72drwxr-xr-x 9 mwirth staff 306 1 Sep 11:25 .drwxr-xr-x@ 272 mwirth staff 9248 1 Sep 11:04 ..-rwxr-xr-x 1 mwirth staff 9640 1 Sep 10:06 a.out-rw-r--r--@ 1 mwirth staff 3094 3 Jul 18:48 gamepig.c-rwxrwxrwx@ 1 mwirth staff 1439 30 Jun 10:02 luhn.c-rw-r--r--@ 1 mwirth staff 1717 3 Jul 12:01 mugwump.c-rw-r--r--@ 1 mwirth staff 490 31 Aug 08:04 paragraph.txt-rw-r--r-- 1 mwirth staff 473 31 Aug 08:36 paragraph2.txt-rw-r--r--@ 1 mwirth staff 3778 3 Jul 10:06 scramble.c$
current directory
parent directory
pwd
• pwd is a command used to tell you which directory you are currently in.
cd
• cd is used to change directories.
• The general form of the cd command is as follows:
cd directory-name
pwd & cd
$ pwd/Users/mwirth/codingC$ cd afall15$ lsa.out! ! luhn.c! ! paragraph.txt! scramble.cgamepig.c! mugwump.c! paragraph2.txt$ cd ..$
change directory to afall15
change directory to the parent (codingC)
current directory
cp
• cp is used to make copies of files.
• The general form of the cp command is as follows:
cp srcFile destFile
cp
• cp can also be used to put a copy of a file into a directory.
• The general form of this cp command is as follows:
cp srcFile destDir
cp
• To copy the file a1Mug.c to a1MugBACKUP.c :
cp a1Mug.c a1MugBACKUP.c
• To copy the file a1Mug.c to a directory named a1BACKUPs :
cp a1Mug.c a1BACKUPS
mv
• mv is used to rename files.
• The general form of the mv command is as follows:
mv srcFile destFile
mv
• mv can also be used to move a file into a directory (this does not make a copy).
• The general form of this mv command is as follows:
mv srcFile destDir
cp
• To rename the file a1Mug.c to a1Mug.old :
mv a1Mug.c a1Mug.old
• To move the file a1Mug.c to a directory named deathStarPlans :
mv a1Mug.c deathStarPlans
rm
• rm is used to delete files.
• The general form of the rm command is as follows:
rm filename
rm
CAREFUL!!
rm is forever.
Other commands
• mkdir - make a directory
• rmdir - remove a directory
• touch - create an empty file, or modify a files timestamp
• cat - concatenate files, and print to screen
• more - similar to cat, but displays one screen at a time.
grep
• The grep command searches for lines matching a specific pattern.
• The general form of the grep command is as follows:
grep pattern files
grep
$ grep main *.cgamepig.c:int main(void)luhn.c: // Return the remainder when divided by 10luhn.c:int main(void)mugwump.c:int main(void)scramble.c:int main(void)$
find
• The find utility is used to search for files in a directory hierarchy.
• The general form of the find command is as follows:
find path conditions
find
$ find . -name mugwump.c./mugwump.c$
history
• All shells store a list of the commands that have been issued.
• The command history allows you to see this list.
history
• Commands in the list can be accessed and re-executed using a single exclamation mark, !, which is often referred to as Bang.
• You can do ! followed by a history number, or ! followed by the first few characters of the command.
history
$ history 503 cd afall15/ 504 ls 505 gcc scramble.c 506 ./a.out$$ !505gcc scramble.c$$ !ggcc scramble.c$
Redirecting output
• stdout mean the standard standard output (screen).
• It is also possible to redirect this using the operator >.
Redirecting output
$ lsa.out!! luhn.c!! paragraph.txt! scramble.cgamepig.c! mugwump.c! paragraph2.txt$$ ls > flist$ Instead of outputting
to the screen this redirects it to a file
named flist.
Pipes
• The pipe creates a channel from one command to another.
• Pipes are created using the |.
• The general form of a pipe is as follows:
command1 | command2
Pipes
$ history | grep cd 352 cd PRAC_inprog/ 383 cd codingC 501 cd codingC 503 cd afall15/ 545 cd .. 568 history|grep cd$
Run the history command, then pipe the results into grep which will return all
lines which contain the search term cd.
Processes
• Everything that runs in an OS has a process.
• A process is an instance of a running program.
• The ps (process snapshot) command tells you about your processes.
$ ps PID TTY TIME CMD 9878 ttys000 0:00.32 -bash11641 ttys001 0:00.02 -bash
Processes
• Every process has a PID, or Process-ID, which identifies it.
• You can have a program running more than once... each will have their own PID.
$ ps PID TTY TIME CMD 9878 ttys000 0:00.32 -bash11641 ttys001 0:00.02 -bash
Processes
• To see everything running in the background, you can use ps -A
$ ps -A PID TTY TIME CMD 1 ?? 2:51.39 /sbin/launchd 11 ?? 0:05.62 /usr/libexec/UserEventAgent 12 ?? 0:09.27 /usr/libexec/kextd
When things go wonky...
• If a program runs for too long... maybe because it has gone into an infinite loop or something, it can be stopped by using:
When things go wonky...
• Sometimes a program will get stuck for some reason. It just stops responding.
• When this happens it’s time to kill the process.
$ ps PID TTY TIME CMD 9878 ttys000 0:00.35 -bash11769 ttys000 0:00.12 ./a.out11641 ttys001 0:00.02 -bash$ kill -9 11769$
Need more info?
• Use man.
• Short for manual page.
• The general form of a man page is as follows:
man command-name
Unix is all powerful
• You can write programs in shell languages, to perform processing that is easier than using a GUI.
• For example:#!/bin/bashfor img in *.jpgdo filename=$(basename "$img") extension="${filename##*.}" filename="${filename%.*}" echo $filename convert "$img" "$filename.png"done
Shell scripting
#!/bin/bashfor img in *.jpgdo filename=$(basename "$img") extension="${filename##*.}" filename="${filename%.*}" echo $filename convert "$img" "$filename.png"done
Use the bash shell
Select all files ending in .jpg
}extract file name with
no extension
Convert the file to .png
Black holes
• Unix has an built-in black hole called
/dev/null
• Any user can write to it.
• Anything written to it just disappears.