Sep 12
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.
- 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 820
drwxr-xr-x 32 ramakris softlab 4096 Aug 19 15:37 Codes
drwxr-xr-x 5 ramakris softlab 4096 Jul 18 21:25 Datasets
drwxr-xr-x 3 ramakris ramakris 4096 Sep 12 16:16 Desktop
drwxr-xr-x 22 ramakris ramakris 4096 Aug 30 20:58 Documents
-rwxrw-r-- 1 ramakris ramakris 37 Sep 12 09:48 lines4to5
-rw-rw-r-- 1 ramakris ramakris 342001 Sep 11 19:44 MuraliActiveNetworksproposal.pdf
drwxr-xr-x 16 ramakris ramakris 4096 Aug 4 14:10 Personal
drwxrwxr-x 18 ramakris softlab 4096 Aug 30 21:06 Projects
-rw------- 1 ramakris ramakris 2273 Sep 11 20:04 README
-rw-rw-r-- 1 ramakris ramakris 158550 Sep 12 15:59 sep12
drwxrwxr-x 2 ramakris ramakris 4096 Sep 12 16:16 temp
-rw------- 1 ramakris ramakris 283094 Sep 11 19:44 tods03.pdf
- 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).
- 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.
But still cannot edit it in emacs.
- 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 temp has "2" listed because it has two hard links,
one is itself, and the other is "temp/..". This should also tell you
that there are no subdirectories inside temp (why?)
- In contrast, the Codes directory has 30 subdirectories. Now each
of them 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
The basics of shell scripting
- Suppose we wanted to spit out lines three to five for several
files, not just README. We dont have to laboriously type the pipe
again and again with different filenames. We can create our own
customized command, called a shell script. Here is how: