touch argument: file(s) who chmod mv argument: at least two files rmdir pwd ls passwd mkdir cd finger argument: username cat vi argument: filemame sudo vi modes command mode mobility save file to disk or save a copy cut/paste to Mabel search visual selects texticographically line mode block mode insert type characters into file
Some Unfinished Business from Yesterday
Relative and Absolute Paths A relative path is a
path that is relative to your cwd
. An absolute path
is a path that points to an absolute location in the file system.
Here are the types of absolute paths.
- A path that starts with a
/
specifies an absolute location in the file system. - A path that starts with a
~
specifies an absolute location in your home directory. - A path that starts with a
~username
specifies an absolute location inusername
's home directory.
All other paths are relative. Relative to what. Relative to the cwd
.
Here we list with an absolute path starting with /
.
~> ls /home 2017 2022 anglin gannCheryl lobuglio rebecca.conrad 2018 2023 charles.robinson gotwals menchini rex.jeffries 2019 2024 compact jennifer.betz morrison sedans 2020 2025 cooper keethan.kleiner newbauer vazquez 2021 2026 cs linsey.morrison rash webster ~> ls /home/2022 alexander22d delosreyes22m raganath22d vester22j atalla22l fake22z schrader22a wang22m benjamin22s pham22l sharma22p warner22p bhattacharya22s pollard22l singhvi22k yete22r
You should try these.
ls ~ ls ~morrison/public_html ls ~cs/data
Processes have a cwd
. Can a process change its cwd
?
This C program shows us the answer is, "Yes!"
#include<stdio.h>
#include<unistd.h>
int main()
{
char s[100];
printf("%s\n", getcwd(s, 100));
chdir("..");
printf("%s\n", getcwd(s, 100));
return 0;
}
Can you do this in Python?
Globbing You can create wildcards in arguments using the
*
character. Here are examples.
mv *.tex texfiles
This will move all files with extension.tex
into the directorytexfiles
ls a*.py texfiles
This list all python program starting with the lettera
in long format.ls */*.py
will list all Python files that are one directory down from your cwd.cp ~cs/data/*.txt .
will copy all files in~cs/data
with extension*.txt
into yourcwd
. `
You can use a ?
to create a one-character wildcard.
You can use a list character class (list of chars inside of []
to create a wildcard tht will match the listed characters.
Here is globbing with an absolute path.
~> cd public_html/ ~/public_html> ls -d /home/2021/b* /home/2021/babu21k /home/2021/bit21m /home/2021/bauernfeind21d /home/2021/boyer21n ~/public_html> ls /bin/ls* /bin/ls /bin/lscpu /bin/lsipc /bin/lsmem /bin/lsattr /bin/lsdiff /bin/lslocks /bin/lsns /bin/lsblk /bin/lsinitrd /bin/lslogins /bin/lsscsi
The Environment Variable $PATH
and friends
$PATH
tells the system where to look when you enter a
command.
Comments in BASH A one-line comment is a space then
a pound sign(#
). Do not forget the space or you will get
an ugly surprise. That means: deliberately make that mistake and
see what happens.
Aliases
.bashrc
and .bash_profile
.vimrc
Redirection
Fact of UNIX Life Everything is a file!
For most UNIX commands, the default input stream is stdin
, which
by default is the keyboard. The default output stram is stdout
, which
is the terminal screen. The default error stream is stderr
, which
by default is also the terminal screen. All three of these can be redirected.
All three are files. Also, any printer or peripheral you have attached to your
computer is also a file.
Note that running a BASH script or a an executable program is really just another UNIX command.
Here is a little puzzler. What does this do?
unix> ls > cows.txt
unix> ls -l >> cows.txt
What did this do?
Now run this again. What happened?
unix> ls > cows.txt
Suppose heffalump
is a nonexistent file
unix> ls heffalump 2> junk.txt
Now try this. At the command line type cat
type a line of
text and hit enter. Repeat several times. To exit, hit control-d.
What is the default input for cat
?
Make a small test file foo.txt
and do this.
unix > cat < foo.txt
Can you explain what happened? Now do this.
unix > cat <
Type in a few lines of text, hitting ENTER after each one. Then hit control-D to exit. Let us discuss what happened.
Here we see redirection at work wiht the ls
command. The second
redirection is using append to append its output to the end of a file.
You use the "double funnel" >>
to do this.
~> ls /bin > file.txt ~> vi file.txt ~> ls -la >> file.txt
printf This is similar to C's printf
and can
be used to format output.
Here is a guide to Bash's [clunky] arithmetic.
#!/bin/bash
printf "My name is %s %s\n" "Boffer" "Bings"
Here is a script using variables.
#!/bin/bash
echo This is a shell script.
echo Here is a calendar showing today.
cal
PURSUER="cat"
QUARRY="dog"
printf "A %s is chasing a %s\n" "$PURSUER" "$QUARRY"
Redirecting stderr
Put a 2 in front of
the funnel to redirect stderr
.
~> ls heffalump 2> errs.txt ~> ls bin demolish.sh public_html roster.txt.out submitted build.sh errs.txt returned stayHome.sh test ~> cat errs.txt ls: cannot access heffalump: No such file or directory
Command-line Arguments vs. stdin
#include<stdio.h>
int main(int argc, char** argv)
{
for(int k = 1; k < argc; k++)
{
printf("cows %s\n", argv[k]);
}
return 0;
}
Pipes
It is possible to chain the action of UNIX commands usiing a device called
a pipe. Pipes allow you to make the output of one command the input for
the next. However, we have to be careful. Here is why. Consider the command
ls
. Its arguents are not stdin
; they are
command-line arguments in the C program that is ls
. Here we show
how command-line arguments are handled in C.
The object argv
is an array of strings and argc
is
its size.
Filters A filter takes a character stream from standard input, operates on it, and puts it to stdout.
Let's learn about a few simple ones,cat
, head
,
and tail
, and cut
.
This Cut Video explains the basics.