Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 1 Lecture 2 Log into Linux Questions? Homework 1 posted, due next Tuesday
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 1
Lecture 2
Log into Linux Questions? Homework 1 posted, due next Tuesday
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 2
Outline
Files and directories UNIX philosophy Essential commands
grep find
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 3
File Information
Use “ls -l” to display complete file info:$ ls ldrwxrwxrx 11 hwang faculty 4096 Jul 25 10:43 qtrwrr 1 hwang faculty 6455 Aug 19 14:53 readme.txtlrwxrwxrwx 1 hwang faculty 23 Aug 8 12:59 rt > a.txtprwrwr 1 hwang faculty 0 Aug 27 15:18 mypipe
Shown: file type, permissions, # of hard links, owner, group, size, last modification time, and name.
File type is indicated by first character Regular file d Directory l Soft link p Named pipe c Char device b Block device
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 4
Soft (Symbolic) LInks
Use “ln -s” to create soft or symbolic links. A soft link is a pointer to a “real name” for a file. These are similar to Windows shortcuts but are supported by both the CLI and the GUI.
$ ln s target linkname The source file may be on another filesystem. You can create soft links to directories. Deleting the link has no effect on the file. Deleting the file can leave a soft link to nothing. I.e.
a "dangling reference".
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 5
Hard Links
Use ln to create hard links. Creating a hard link creates another “real name” for a file.
$ ln target newname Data is not deleted until all hard links are deleted.
Names are aliases, so changes are reflected. Data must be on the same filesystem as the link. System call to delete a file is actually “unlink”. Only the administrator can create hard links to
directories (to prevent filesystem corruption due to directory “loops” by unwitting users) .
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 6
File Links
DATA
name1
name2
File with two hard links
DATA
name1
name2
File with one hard link and one soft link
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 7
Special File Names
A filename whose first character is a period will not show up in normal directory listing.
Use 'ls -a' to list hidden files This has NOTHING to do with security.
Every directory automatically includes . and .. which are links to itself and its parent directory.
$ cp /etc/passwd .$ cd ../../../datadir
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 8
Device Files
Device files provide access to hardware. There are two types: character and block
They reside in the /dev directory. They can be created with mknod by the administrator. Permissions determine access to corresponding
hardware. Character devices provide sequential access (serial
ports) while block devices are used for random access (drives)
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 9
Device Files
See the MAKEDEV man page and the devices.txt file in the kernel source archive for a list of devices. See also man pages for hd (hex dump), od (octal dump), random, etc.
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 10
A Few Standard Device Files
/dev/hda Master IDE drive on first controller/dev/hdb Slave IDE drive on first controller/dev/hdc Master IDE drive on second controller (often is CDROM/DVD drive)/dev/hdd Slave IDE drive on second controller (often was used for ZIP drive)/dev/cdrom Link to CDROM device/dev/sda First SCSI drive/dev/sdc0 SCSI CD drive/dev/st0 SCSI tape drive/dev/hda1 First primary partition on hda drive (C: drive in Windows partitioning sda is similar)/dev/hda2 Second primary partition on hda drive/dev/hda5 First logical partition
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 11
A Few Standard Device Files
/dev/tty Current terminal device/dev/tty1 First console device/dev/pts/1 First pseudoterminal (xterm) device/dev/fd/0 File descriptor 0 or standard input (1 is stdout, 2 is stderr)/dev/stdin Alias for /dev/fd/0 (also /dev/stdout, /dev/stderr)/dev/ttyS0 First serial port (COM1)/dev/lp0 First parallel port/dev/audio Sound card i/o/dev/null Null device (bit bucket)/dev/zero Zero device (all zeroes)/dev/random Random number generator/dev/mem Physical memory/dev/psaux PS/2 mouse port
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 12
Cool Device Tricks
$ dd if=/dev/cdrom of=cdrom.iso # Create ISO image of CD$ cat /dev/audio > sound.au # Record from microphone$ cat sound.au > /dev/audio # Play sound$ program 2> /dev/null # Redirect program error # messages to bit bucket$ program /dev/stdin # Pass stdin as filename argument$ od A n N 8 t u2 < /dev/random # Generate 4 16bit unsigned random #s
You can create, format, and mount disk images!$ dd if=/dev/zero of=f.img bs=1k count=1440 # Create 1.44MB zero file$ mkfs t msdos f.img # Create a file system$ mkdir a # Create a mount point$ mount o loop f.img a # Mount the file system
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 13
// File: random.cpp// Print 10 random 32bit integers#include <fstream>#include <iostream>using namespace std;
int main(){ int randnum;
ifstream randstr("/dev/random");
for(int count=0; count<10; count++) { randstr.read((char *)&randnum,sizeof(randnum)); cout << count << ": " << randnum << endl; } randstr.close(); return 0;}
Cool Device Tricks
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 14
Filesystems
Disk devices (/dev/sdc1) contain raw disk data that is not normally accessed directly by the user.
A filesystem contains inode lists and data blocks. An inode structure (on disk) contains all file info except the
file name (owner, group, perms, times, link count, and indexes to the data blocks that make up the file).
A directory block has only filename and inode # pairs. Use “ls -i” to display inode numbers. Use mkfs (as administrator) to create a filesystem (format):
$ mkfs t ext3 /dev/sdc1
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 15
Filesystems
All files in a UNIX systems are arranged in a tree rooted at /.
No C:, D:, etc drives as in Windows. Easily add disk space and allow files to maintain the
same position in directory tree. mount is used to attach a filesystem to the tree$ mount t ext3 /dev/sda3 /home$ mount /dev/fd0 /mnt/floppy$ mount /dev/hdc /mnt/cdrom$ mount # Display mounted filesystems
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 16
Filesystems
List of automatically mounted devices is in /etc/fstab
Use df (diskfree) to display the amount of freespace on each filesystem.
umount (not unmount) detachs a filesystem Many systems have an automounter program
running to automount removable media (USB sticks, CD-ROMs, etc.)
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 17
Standard Directory Tree
/ root directory (don't confuse with /root)/bin essential utilities/lib essential libraries/sbin essential admin tools/etc configuration files/home contains user HOME directories/root root user HOME directory/dev device directory/var system files that change (log, spool files)/tmp for temp file usage (avail to all users)/usr/bin user applications/usr/lib user application libraries/usr/local contains applications added by local admin
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 18
File Permissions
Regular file permissions are specified for user (owner), group and other (anyone else).
Permissions are r (read), w (write) and x (execute) Write permission allows you to alter a file's contents. It
does not give you permission to delete the file. Permissions also apply to named pipes and devices.
Here are some examples: rwrr User can read and write, group other just read rwxrxrx All can read and execute, user can write
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 19
Directory Permissions
Permissions apply to user, group and other. Read implies permission to get a directory listing. Write indicates permission to create and delete files in the
directory. (Write permission on the file is not required!) Execute means permission to access files in the directory. This makes more sense if you think of a directory as a file
that contains filenames. (Actually, that's what it is.)
Here are some examples: drwxrxrx User has full access, others can use ls, cp drwxrwx User and group have full access, others none
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 20
Changing Permissions
Use the chmod command You must be the owner (or root) to change
permissions. Symbolic mode examples
$ chmod u=rw,og=r readme.txt
$ chmod uog+rx datadir
Numeric mode (r=4, w=2, x=1) $ chmod 644 readme.txt
$ chmod 755 datadir
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 21
Additional Permission Info
Special permissions: setuid, setgid, sticky. See 'man chmod' and 'info “File Permissions”'
rwsrxrx setuid (run as owner), chmod u+s
rwxrwsrx setgid (run as group), chmod g+s
drwxrwxrwt sticky (only allow owner to delete), o+t
Look at perms on /usr/bin/chsh (setuid) and /tmp (sticky)
Linux also supports ACLs (access control lists) for fine-grained control, i.e. to give individual users special access to directories and files.
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 22
File Ownership
Use chown to change file ownership$ chown newusername readme.txt
You must be administrator Copier become the owner of copied files
Use chgrp to change file group$ chgrp newgroupname readme.txt
You must belong to both old group and new group
Can use chown to change both at once$ chown newusername.newgroupname readme.txt
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 23
File Timestamps
There are three times associated with a file: last access, modification, and change
Modification time is when file data was altered. Change time is when owner, group, permission, or hard link count was altered.
“ls -l” shows modification time by default. Use “ls -l --time=atime” or “ls -l --time=ctime” to see
others
You can update access and modification times using touch.
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 24
UNIX Philosophy
Here are a few notes on UNIX programming philosophy:
Write small programs that do one thing well. Read data from standard input (scanf, cin) and write
data to standard output (printf, cout). Such programs are known as filters. Very complex applications can be created combining filters using pipes.
Move core routines into well-documented libraries so that they can be reused.
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 25
Essential Commands
COMMAND DESCRIPTION
ls Displays directory listingcd directory Change the current (working) directorycd Return to HOME directorypwd Display the current directorymkdir dirname Create a directoryfile filename Display file typecat textfile Display file contentsless textfile Page file contents to display (also more)exit or ^D Leave this sessionpasswd Change your passwordman command Display man pages on commandinfo command Display info pages on command
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 26
Useful Commands
COMMAND DESCRIPTION
apropos string Search the whatis (manual page) database for string (same as “man k string”)whatis command Display brief description from whatis database (same as “man f command”)command ––help Display brief help on commandps aux Display all processeskill Kill a process (requires ownership)du foo Display disk usage by file or dir foodf Display free disk spacegrep Search for character stringsfind Find files meeting criteriatar Standard UNIX archive tool
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 27
grep
general regular expression parser Used to find strings inside files; prints file name and
line by default Syntax:
grep [options] pattern [files] Options change the way command behaves
-c count lines that match-i ignore case-l list names of files w/o line-v invert matching, selects non-matching lines-E extended expressions (slower)
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 28
Regular Expressions
Patterns use regular expressions
^ anchor to beginning of line$ anchor to end of line. match any single character[ ] match one of a set of chars, can be a range, prefix with ^ for inverted match? optional - match 0 or 1 time* match 0 or more times+ match 1 or more times
Special match patterns inside [ ]
[:alnum:] alphanumeric chars[:alpha:] letters[:digit:] digits[:space:] whitespace
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 29
grep
Examples:$ cd /usr/share/dict/$ grep in words # contains "in"$ grep ^in words # begin with "in"$ grep in$ words # end with "in"$ grep ^in[[:alpha:]]in$ words # begin and end with "in"
$ grep ^[cd] words # begin with c or d$ grep ^[^a]*$ words # no a's$ grep ^[^ae]*$ words # no a's or e's
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 30
find
Syntax:
find [path] [options] [tests] [actions]
Search for files starting at path. Often / or . Main options; always return true
-follow Follow symbolic links-maxdepths N Search at most N levels-mount Don't search other file systems
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 31
find
Tests return true or false. If false, stops. If true, does next test/action
-atime N last accessed N days ago; +N for greater than; -N for less than-mtime N last modified N days ago-name pattern file name matches pattern; must be in quotes-newer otherfile file is newer than otherfile-type C file is type C – e.g. f for regular file-user username file is owned by username
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 32
find
Combine tests with logical connectives
!, -not invert test-a, -and both test must be true-o, -or one test must true
Use escaped parentheses to group Actions are at the end and say what to do with
each file that matches
-print print the name of matching file-ls use ls -dils on matching file
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 33
find
Any command can be executed
-exec command execute command on match
-ok command ask user confirmation before
executing command on match
These actions must end with \; sequence in order for find to know where the embedded command ends
Use { } to represent the matching file
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 34
find
Examples:$ find / -name test -print # find all files named "test"$ find . -mtime 2 -print # modified exactly 2 days ago$ find . -mtime -2 -type f -print # regular files modified in last 2 days$ find . \( -mtime -2 -o -atime -2 \) -print # modified or accessed in last 2 days$ find . \( -mtime -7 -a -name "*.cpp" \) -print # C++ source files modified in last week
$ find . \( -mtime -7 -a -name "*.cpp" \) -exec ls -l {} \; # execute ls -l on them$ find . \( -mtime -7 -a -name "*.cpp" \) \ -exec grep ^int {} \; # print out lines starting with "int"