Lecture III. Highlights and more
Lecture III, full text. PDF
More on "ls"
- Typical options
- ls <filename>
simply spits <filename> back on screen
- ls <directoryname>
lists the contents of the directory
- ls -d <directoryname>
simply spits <directoryname> back on screen
- ls -d
simply spits everything, files or directories
- Regular expressions
- ls *R
lists files and (contents of) directories whose names end in R
"*" matches anything, of any length, including nothing!
- ls R*
lists files and (contents of) directories whose names begin in R
- ls *R*
lists files and (contents of) directories whose names contain R
- ls ???R??
lists files and (contents of) directories whose names have six
characters, fourth of which is an "R"; "?" matches exactly one
character.
- ls [123]R
lists files and (contents of) directories whose names are either
1R, 2R, or 3R. Enclose a "choice" construct inside square brackets.
- Note the "escape" character "\".
When placed in front of the symbol it makes sure that its literal meaning
is used and not the expression meaning.
(i.e. \$ means use the $ , not match everything preceding the $)
- Miscellaneous
- To list only names of directories, not files, use:
"ls -d */"
- Can also use:
"ls -d */." and "ls -d */.." (why?)
- "ls -a" lists all files and directories, including those that
start with ".".
- "ls -R" does a recursive listing
More on regular expressions
- Contexts
- can use them anywhere a name is expected, e.g.
cd C* will try to cd into a directory that starts with a C.
If more than one such directory exists, UNIX will complain. Even if only
one thing exists, and that one thing is a file, UNIX will complain because
you cannot cd into a file.
- On the other hand cat C* will cheerfully concatenate
all files that fit the pattern (here, you will hear complaints
if the pattern matches a directory).
Deciphering the "ls -l" output
total 28
drwxr-xr-x 3 onufriev onufriev 4096 Jan 17 21:09 Desktop/
drwxr-x--x 3 onufriev onufriev 4096 Feb 3 16:52 PAPERS/
drwxr-x--x 2 onufriev onufriev 4096 Jan 30 01:24 STRUCTURES/
-rw-r----- 1 onufriev onufriev 0 Feb 5 20:26 test
- First column lists permissions.
- Second column lists the number of hard links to that entry.
- Third column lists the owner.
- Fourth column lists the group the file/directory is assigned to.
- Fifth column lists the size (bytes. Use ls -lh to get human readable format).
- Sixth column etc., list timestamp and name.
Changing permissions
- What permissions do you have?
- Look at the first column of "ls -l" output
- The first character indicates the type of file:
|
|
d = directory; - = plain file; l = link |
- Characters 2 . . . 10 give the file access permissions:
|
|
r = read; w = write; x = execute. |
- Characters 2, 3, 4 give the owner's permissions,
followed by group and other user's permissions. A -
denotes absence of the permission.
- Updating permissions can be done:
- for "u" (user), "g" (group), or "o" (others)
- towards three types of accesses: "r" (read), "w" (write), and "x" (execute
files;
also used for cd access to directories).
- "+" gives permission; "-" takes away permission.
- Use chmod command, e.g.,
chmod u-rw README
takes
away ("-") read ("r") and write ("w") permission for user ("u") toward README. Cannot read and
cat this file anymore.
- chmod u+r README now allows the user to cat the file.
chmod a+r README The change is for all users.
But still cannot edit (read AND write)
- chmod u+w README gives back the writing capabilities as well.
- lists the number of hard (not soft) links to the file.
- Files usually have "1" listed unless there are new hard links created
using the "ln" command.
- Directories have different numbers listed. In the above "ls -l" output,
the directory STRUCTURES has "2" listed because it has two hard links,
one is itself, and the other is to a file in it.
- The PAPERS directory has 1 subdirectory and 1 file in it. Now this
subdirectory may have their own subdirectories but we cannot infer that from
just this output.
I/O Redirection
- UNIX commands receive their input from the standard input (stdin) and send their output to the standard output (stdout), by default these files are the console or terminal.
- Shells allow input/output (I/O) to be redirected from/to other devices,
thus UNIX commands are unaware from what device their input may originate
or to what device their output may be sent.
command name [args] > filename
- The output redirection symbol, > , sends the command's output
to the specified file instead of the console/terminal screen.
ls -al > ls.out
- The file ls.out is created if it does not exist (or emptied and overwritten
if it exists prior to command execution) and sent the directory listing.
- Programs which send their output to the console/terminal may also be
redirected.
head -5 README > head
- Note that this redirects the output to a file called "head" (not to be
confused with the head command).
- The input redirection symbol, < , sends the command input
from the specified file instead of the console/terminal keyboard.
cat < README > README2
- This reads input from README, passes it to cat, and then sends the
output to README2.
- Pipes | are the logical extension of I/O redirection.
- Pipes allow the stdout of one program to become the stdin of another
program.
ls -R | more
- The above command allows the viewing of the long recursive listing of a large
directory one screen at a time; Type man more to see what more
does. Also see less.
- Can create your own customized "pipes". e.g., here is how you can spit
out just lines three to five of a given file.
cat README | head -5 | tail -3