Lecture 2 ● Log into Linux ● Questions? ● Homeworks 1 & 2 posted ● Homework 1 due Tuesday next week ● Homework 2 due Thursday next week Tuesday, September 1 CS 375 UNIX System Programming - Lecture 2 1 Outline ● Files and directories ● UNIX philosophy ● Essential commands ● grep ● find Tuesday, September 1 CS 375 UNIX System Programming - Lecture 2 2 File Information ● Use “ls -l” to display complete file info: $ ls ­l drwxrwxr­x 11 hwang faculty 4096 Jul 25 10:43 qt ­rw­r­­r­­ 1 hwang faculty 6455 Aug 19 14:53 readme.txt lrwxrwxrwx 1 hwang faculty 23 Aug 8 12:59 rt ­> a.txt prw­rw­r­­ 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 p Named pipe Tuesday, September 1 d Directory l Soft link c Char device b Block device CS 375 UNIX System Programming - Lecture 2 3 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". Tuesday, September 1 CS 375 UNIX System Programming - Lecture 2 4 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) . Tuesday, September 1 CS 375 UNIX System Programming - Lecture 2 5 File Links name1 DATA name2 File with two hard links name1 DATA name2 File with one hard link and one soft link Tuesday, September 1 CS 375 UNIX System Programming - Lecture 2 6 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 ../../../../etc Tuesday, September 1 CS 375 UNIX System Programming - Lecture 2 7 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) Tuesday, September 1 CS 375 UNIX System Programming - Lecture 2 8 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. Tuesday, September 1 CS 375 UNIX System Programming - Lecture 2 9 A Few Standard Device Files /dev/hda /dev/hdb /dev/hdc /dev/hdd /dev/cdrom /dev/sda /dev/sr0 /dev/st0 /dev/hda1 /dev/hda2 /dev/hda5 Tuesday, September 1 Master IDE drive on first controller Slave IDE drive on first controller Master IDE drive on second controller (often is CDROM/DVD drive) Slave IDE drive on second controller (often was used for ZIP drive) Link to CDROM device First SCSI drive CDROM device Tape drive First primary partition on hda drive (C: drive in Windows ­ partitioning sda is similar) Second primary partition on sda drive First logical partition CS 375 UNIX System Programming - Lecture 2 10 A Few Standard Device Files /dev/tty /dev/tty1 /dev/pts/1 /dev/fd/0 /dev/stdin /dev/ttyS0 /dev/lp0 /dev/audio /dev/null /dev/zero /dev/random /dev/mem /dev/psaux Tuesday, September 1 Current terminal device First console device First pseudo­terminal (xterm) device File descriptor 0 or standard input (1 is stdout, 2 is stderr) Link for /dev/fd/0 (also /dev/stdout, /dev/stderr) First serial port (COM1) First parallel port Sound card i/o Null device (bit bucket) Zero device (all zeroes) Random number generator Physical memory PS/2 mouse port CS 375 UNIX System Programming - Lecture 2 11 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 16­bit 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 Tuesday, September 1 CS 375 UNIX System Programming - Lecture 2 12 Cool Device Tricks // File: random.cpp // Print 10 random 32­bit 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; } Tuesday, September 1 CS 375 UNIX System Programming - Lecture 2 13 Filesystems ● Disk devices (e.g. /dev/sda1) 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): Tuesday, September 1 $ mkfs ­t ext3 /dev/sdc1 CS 375 UNIX System Programming - Lecture 2 14 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/sdc1 /home $ mount /dev/fd0 /media/floppy $ mount /dev/hdc /media/cdrom $ mount # Display mounted filesystems Tuesday, September 1 CS 375 UNIX System Programming - Lecture 2 15 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) detaches a filesystem Many systems have an automounter program running to automount removable media (USB sticks, CD-ROMs, etc.) Tuesday, September 1 CS 375 UNIX System Programming - Lecture 2 16 Standard Directory Tree / /bin /lib /sbin /etc /home /root /dev /var /tmp /usr/bin /usr/lib /usr/local Tuesday, September 1 root directory (don't confuse with /root) essential utilities essential libraries essential admin tools configuration files contains user HOME directories root user HOME directory device directory system files that change (log, spool files) for temp file usage (avail to all users) user applications user application libraries contains applications added by local admin CS 375 UNIX System Programming - Lecture 2 17 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: ● ­rw­r­­r­­ User can read and write, group other just read ● ­rwxr­xr­x All can read and execute, user can write Tuesday, September 1 CS 375 UNIX System Programming - Lecture 2 18 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: ● drwxr­xr­x User has full access, others can use ls, cp ● drwxrwx­­­ User and group have full access, others none Tuesday, September 1 CS 375 UNIX System Programming - Lecture 2 19 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 Tuesday, September 1 CS 375 UNIX System Programming - Lecture 2 20 Additional Permission Info ● ● Special permissions: setuid, setgid, sticky. See 'man chmod' and 'info “File Permissions”' ● ­rwsr­xr­x setuid (run as owner), chmod u+s ● ­rwxrwsr­x 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. Tuesday, September 1 CS 375 UNIX System Programming - Lecture 2 21 File Ownership ● Use chown to change file ownership $ chown newusername readme.txt ● ● You must be administrator ● Newusername becomes the owner of 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 Tuesday, September 1 CS 375 UNIX System Programming - Lecture 2 22 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. Tuesday, September 1 CS 375 UNIX System Programming - Lecture 2 23 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. Tuesday, September 1 CS 375 UNIX System Programming - Lecture 2 24 Essential Commands COMMAND DESCRIPTION ls Displays directory listing cd directory Change the current (working) directory cd Return to HOME directory pwd Display the current directory mkdir dirname Create a directory file filename Display file type cat textfile Display file contents less textfile Page file contents to display (also more) exit or ^D Leave this session passwd Change your password man command Display man pages on command info command Display info pages on command Tuesday, September 1 CS 375 UNIX System Programming - Lecture 2 25 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 command ps aux Display all processes kill Kill a process (requires ownership) du foo Display disk usage by file or dir foo df Display free disk space grep Search for character strings find Find files meeting criteria tar Standard UNIX archive tool Tuesday, September 1 CS 375 UNIX System Programming - Lecture 2 26 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 -i -l -v -E Tuesday, September 1 count lines that match ignore case list names of files w/o line invert matching, selects non-matching lines extended expressions (slower) CS 375 UNIX System Programming - Lecture 2 27 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:] [:alpha:] [:digit:] [:space:] Tuesday, September 1 alphanumeric chars letters digits whitespace CS 375 UNIX System Programming - Lecture 2 28 grep ● Examples: $ $ $ $ $ cd /usr/share/dict/ grep in words grep ^in words grep in$ words grep ^in[[:alpha:]]in$ words $ grep ^[cd] words $ grep ^[^a]*$ words $ grep ^[^ae]*$ words Tuesday, September 1 # # # # contains "in" begin with "in" end with "in" begin and end with "in" # begin with c or d # no a's # no a's or e's CS 375 UNIX System Programming - Lecture 2 29 find ● Syntax: find [path] [options] [tests] [actions] ● Search for files starting at path. Often / or . ● Main options; always return true -follow -maxdepths N -mount Tuesday, September 1 Follow symbolic links Search at most N levels Don't search other file systems CS 375 UNIX System Programming - Lecture 2 30 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 Tuesday, September 1 CS 375 UNIX System Programming - Lecture 2 31 find ● Combine tests with logical connectives !, -not -a, -and -o, -or ● ● invert test both test must be true 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 -ls Tuesday, September 1 print the name of matching file use ls -dils on matching file CS 375 UNIX System Programming - Lecture 2 32 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 Tuesday, September 1 CS 375 UNIX System Programming - Lecture 2 33 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" Tuesday, September 1 CS 375 UNIX System Programming - Lecture 2 34