CS246-F20-01-UnixShell
Lecture 1.5
• More UNIX commands:
– man
– ls
– mkdir
– cp, mv, rm
– cat/less
CS246
man
• man prints (very terse) information about commands, option
names, and functions
$ man bash
… # information about “bash” command
$ man chsh
… # information about “chsh” command
$ man man
… # information about “man” command
• man is short for manual; it gives you a dense reference page
for usage, not a friendly tutorial
– Look to the web for tutorials, tips, tricks, FAQs for UNIX commands
LS(1) BSD General Commands Manual
LS(1)
NAME
ls — list directory contents
SYNOPSIS
ls [-ABCFGHLOPRSTUW@abcdefghiklmnopqrstuwx1]
[file …]
DESCRIPTION
For each operand that names a file of a type
other than directory, ls displays its name as
well as any requested, associated information.
For each operand that names a file of type direc-
tory, ls displays the names of files contained
within that directory, as well as any requested,
associated information.
If no operands are given, the contents of the
current directory are displayed. If more than
one operand is given, non-directory operands are
displayed first; directory and non-directory op-
erands are sorted separately and in lexicographi-
cal order.
The following options are available:
-@ Display extended attribute keys and sizes
in long (-l) output.
-1 (The numeric digit ”one”.) Force out-
put to be one entry per line. This is
the default when output is not to a ter-
minal.
-A List all entries except for . and …
Always set for the super-user.
-a Include directory entries whose names
begin with a dot (.).
ls
• ls gives a listing of the contents of a directory (folder),
by default the current directory
– It is perhaps the most common UNIX command I use in a
given day
– Most common usage:
ls [-l] [-a] []
stuff inside square brackets is optional
-a also show “dotfiles” (which are typically not shown)
-l gives long listing for each file
ls
• Some other options:
-R recursively list contents of directories too
-t sort by time modified (instead of alphabetically)
-F append * onto names of executable files, / onto names of
directories, @ onto names of sym-links, etc.
-G colour code files (red for executables, etc.) on some systems
(use “–color” on others)
• Note that for –F, the characters appended onto the filenames
are not actually part of the filenames
– They are just there to visually indicate the type of file
– In general, it’s a really good idea avoid globbed characters (like *,?,[,])
in filenames
$ pwd
/Users/migod/temp
$ ls
Hearts balloon.cc
balloon oldStuff
$ ls -a
. .DS_Store balloon oldStuff
.. Hearts balloon.cc
$ ls -l
total 48
lrwxr-xr-x 1 migod staff 46 11 Sep 16:45 Hearts ->
/Users/migod/Class/138/2011-Winter/code/Hearts
-rwxr-xr-x 1 migod staff 15212 11 Sep 16:48 balloon
-rw-r–r– 1 migod staff 769 11 Sep 16:47 balloon.cc
drwxr-xr-x 13 migod staff 442 11 Sep 16:47 oldStuff
$ ls -F
Hearts@ balloon.cc balloon* oldStuff/
$ ls -G
Hearts balloon balloon.cc oldStuff
$ ls -FG
Hearts@ balloon* balloon.cc oldStuff/
$ ls -l b*
-rwxr-xr-x 1 migod staff 15212 11 Sep 16:48 balloon
-rw-r–r– 1 migod staff 769 11 Sep 16:47 balloon.cc
$ ls oldStuff # if arg is a directory, list its contents
README.txt doc jx temp.c
a.out jar-run.sh salsa-fx.jar vowel.c
count.c javaHearts search.c
mkdir
• mkdir creates new directories at specified locations in the file
hierarchy
• Usage:
mkdir
• Example
$ ls -F
Hearts@ balloon* balloon.cc oldStuff/
$ mkdir flurble bazz
$ ls -F
Hearts@ balloon.cc flurble/
balloon* bazz/ oldStuff/
cp, mv, rm
• Copy, move/rename, and remove files
• I strongly suggest that you use the “-i” option for all three
– At the UNIX command line, there is no Trash to store recently deleted files.
Deletes are usually immediate + permanent; “-i” asks if you’re sure
– I also use the “-v” option, which verbosely says what it does, so at least you
know what files got deleted
• In my ~/.aliases file:
alias rm=’rm –iv’
alias cp=’cp –iv’
alias mv=’mv –iv’
cp
• cp can be used to copy an existing file into a file with the
same name (if in another dir) or different name
– Can copy many files to another dir (keep same names)
– Can copy whole directory(ies), but must use “-r” option
• Usage:
cp [ -iv ] source-file target-file
cp [ -iv ] source-file-list target-dir
cp [ -iv ] -r source-dir-list target-dir
mv
• mv moves / renames an existing file/dir
• Usage:
– If target-file does not exist, the src-file is renamed; o/w the
target-file is replaced.
mv [ -iv ] src-file target-file
mv [ -iv ] src-file-or-dir-list target-dir
rm
• rm remove (delete) files;
– with the –r option, remove directories (recursively)
• Usage :
-f (“force”, the default) do not prompt for verification for
each file/directory being removed
-r recursively delete the contents of any specified directories
rm [ -ivfr ] file-or-dir-list
$ cp f1 f2 # copy file f1 to f2 (overwrites old f2)
$ cp f1 f2 f3 d # copy files f1, f2, f3 into dir d
$ cp -r d1 d2 d3 # copy dirs d1, d2 recursively to dir d3
$ mv f1 foo # rename file f1 to foo
$ mv f2 f3 # delete file f3 and rename file f2 to f3
$ mv f3 d1 d2 d3 # move file f3 + dirs d1, d2 into dir d3
$ mv d1 d2 # If d2 is a normal file, then error
# if d2 is an existing dir,
# d1 is moved into it
# If no existing file/dir named d2,
# then d1 is renamed to d2
$ rm f1 f2 f3 # file list
$ rm -r d1 d2 # dir list, and all subfiles/dirs
$ rm -r f1 d1 f2 # file and dir list
• cat / less “print” file contents to the terminal
• Usage:
• cat shows the contents in one continuous stream (useful for pipes)
• less paginates the contents one screen at a time (useful for reading)
cat / less
$ cat file-list # or std input, terminated by ^D)
$ less file-list # (same)
$ cat q1.h
… # print file q1.h completely
$ less q1.h
… # print file q1.h one screen at a time
# type “space” for next screen, “q” to stop
$ cat ~/hello.cc
#include
using namespace std;
int main (int argc, char* argv[]) {
cout << "Hello world" << endl;
}
$ cat /usr/share/dict/words
A
A's
AA's
AB's
ABM's
^C
% cat
hello there
hello there
world
world
^D
$
^C (CNTL-C) means interrupt the currently executing program
(i.e., kill it NOW)
^D (CNTL-D) sends the END_OF_FILE (EOF) signal to the currently
executing program, so it knows there's no more input
coming
$ cat ~/hello.cc
#include
using namespace std;
int main (int argc, char* argv[]) {
cout << "Hello world" << endl;
}
$ cat < ~/hello.cc
#include
using namespace std;
int main (int argc, char* argv[]) {
cout << "Hello world" << endl; } The cat program is passed ~/hello.cc as a command line arg; it opens the file itself and reads input from it These are slightly different in how they operate, but they have the same effect on the output stream The shell opens the file ~/hello.cc and sends its contents to the standard input; the cat program reads input from the standard input, not directly from the file End CS246