Log into Linux
Questions?
Homework 1 posted, due next Tuesday
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 1
Files and directories
UNIX philosophy
Essential commands
grep find
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 2
Use “ls -l” to display complete file info:
$ ls l drwxrwxrx 11 hwang faculty 4096 Jul 25 10:43 qt
rwrr 1 hwang faculty 6455 Aug 19 14:53 readme.txt
lrwxrwxrwx 1 hwang faculty 23 Aug 8 12:59 rt > a.txt
prwrwr 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 3
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 4
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 5
Thursday, January 17 name1
DATA name2
File with two hard links name1
DATA name2
File with one hard link and one soft link
CS 375 UNIX System Programming - Lecture 2 6
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 7
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 8
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 9
/dev/hda
/dev/hdb
Master IDE drive on first controller
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
/dev/sda
/dev/sdc0
/dev/st0
/dev/hda1
/dev/hda2
/dev/hda5
Link to CDROM device
First SCSI drive
SCSI CD drive
SCSI tape drive
First primary partition on hda drive
(C: drive in Windows partitioning sda is similar)
Second primary partition on hda drive
First logical partition
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 10
/dev/tty
/dev/tty1
/dev/pts/1
/dev/fd/0
Current terminal device
First console device
First pseudoterminal (xterm) device
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
/dev/lp0
/dev/audio
/dev/null
/dev/zero
/dev/random
/dev/mem
/dev/psaux
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
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 11
$ dd if=/dev/cdrom of=cdrom.iso # Create ISO image of CD
$ cat /dev/audio > sound.au
$ cat sound.au > /dev/audio
$ program 2> /dev/null
# Record from microphone
# Play sound
# 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
$ mkfs t msdos f.img
$ mkdir a
$ mount o loop f.img a
# Create 1.44MB zero file
# Create a file system
# Create a mount point
# Mount the file system
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 12
// 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;
}
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 13
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):
Thursday, September 2
$ mkfs t ext3 /dev/sdc1
CS 375 UNIX System Programming - Lecture 3 14
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 15
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 16
/
/bin
/lib
/sbin
/etc
/home
/root
/dev 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
/var
/tmp
/usr/bin
/usr/lib system files that change (log, spool files) for temp file usage (avail to all users) user applications user application libraries
/usr/local contains applications added by local admin
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 17
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 18
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 19
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 20
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 21
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 22
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 23
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 24
COMMAND DESCRIPTION ls Displays directory listing cd directory Change the current (working) directory cd pwd
Return to HOME directory
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 passwd
Leave this session
Change your password man command Display man pages on command info command Display info pages on command
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 25
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 kill du foo df grep find tar
Display all processes
Kill a process (requires ownership)
Display disk usage by file or dir foo
Display free disk space
Search for character strings
Find files meeting criteria
Standard UNIX archive tool
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 26
g eneral r egular e xpression p arser
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 27
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:]
Thursday, January 17 alphanumeric chars letters digits whitespace
CS 375 UNIX System Programming - Lecture 2 28
Examples:
$ cd /usr/share/dict/
$ grep in words
$ grep ^in words
$ grep in$ words
# contains "in"
# begin with "in"
# end with "in"
$ grep ^in[[:alpha:]]in$ words # begin and end with "in"
$ grep ^[cd] words
$ grep ^[^a]*$ words
$ grep ^[^ae]*$ words
# begin with c or d
# no a's
# no a's or e's
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 29
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 30
Tests return true or false. If false, stops. If true, does next test/action
-atime N
-mtime N last accessed N days ago;
+N for greater than;
-N for less than 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 31
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
-ls print the name of matching file use ls -dils on matching file
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 32
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 33
Examples:
$ find / -name test -print
$ find . -mtime 2 -print
# find all files named "test"
# 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"
Thursday, January 17 CS 375 UNIX System Programming - Lecture 2 34