$
cat
abc
abc
def
def
control-D
< input > output >> append
$ cat <myfile
hello
there
$ date >xyz
$ cat >xyz
have a nice
day
control-D
$ cat myfile xyz
hello
there
have a nice
day
$ cat <myfile >def
$ cat myfile >def
Q) Create a file called theusers containing a list of those users currently logged in.
A)
$ cat myfile nofile
hello
there
cat: nofile: No such file or directory
Q) $ cat myfile nofile 2>error 1>output
$ cat output
$ cat error
$ more /usr/dict/words
Space
next page b prev page
Ret scroll
forward one line
?
print page of help q quit
A pipe can join 2 streams together.
Q) Send email msg to sam which will inform sam of current time and date??
date send to stdout the time and date and mailx sends a msg from input
stream.
$ date | mailx -s "Todays time and date" sam
$ sh whenandwho
$ echo Hello there (Useful for writing scripts)
Hello there
Q) A script called niceday which will output today's time and day with
a user friendly message.
The time and date are now:
$ cat >niceday
echo
The time and date are now:
date
echo Have a nice day!
control-D
$ sh niceday
The time and date are now:
Thu Jan 7 11:17:05 EST 1999
Have a nice day!
Script can have redirections and pipes
Q) Mail sam a message which says running my script now
$ echo running my script now | mailx -s "What I'm doing" sam
$ tee "copy_of_input" | "command_name"
This command has the same effect as command_name, except a copy of standard
input
is sent to copy_the_input.
Q) Send user jo and keep a copy of the message in file jo_message.
$ tee jo_message | mailx jo
hi,
this is a test.
^D
$ cat jo_message
To use tee to copy standard output, pipe the output of a command through
tee.
Q) Run the command who, but store a copy of the output in file who_out
$ who| tee who_out
Q) Find
out how to print the current h/w type on which your system is running by
using uname
$
man uname and -m option does it
Q) To
find command which will print out just your login name
$ man -k login
logname
$ cd /usr/bin
$ cd /squiggle
$ ls / ( lists files in root)
$ cd /bin
$ ls
Q) What files does sam have in his home directory?
$ ls ~sam
$ cp myfile foo (creates new inode)
$ mv myfile bar
$ ls
$ ln /cs/grad/sam/datafile samsdata (links -s )
$ ls -i
total 561
241563 myfile
43532 dir1 562241 foo
562241 bar
Q)
What is the inode of your home directory?
$ cd
$ ls -i
$ ls -id
File Access Control
Each file divides the users of the machine into 3 categories : owner, group, other
Access Priveleges: read, write, execute$ ls -l
$ ls -R dir (Lists all subdirectories recursively)
Wildcards: * matches any character, ? matches any single character. , [...] matches any character in the encoded list or range
$ ls *.txt
udil.txt wais_www.txt$ ls letter?.*
letter1.txt lettera.com$ ls report_[a-c]
report_A report_b report_c$ chmod go-w myfile
$ chmod u+x myfile
$ chmod g=r-x myfile
Q) Create a file which no-one can read and confirm that you yourself cannot read it.
File Contents
What does the file contain? c program, library, text??
$ file myfile
Q) What type of file is /usr?
$ head -n 3 myfile
$ tail -n 4 myfile
$ wc myfilefile 1: A file 2: A
test testing
file file$ diff file1 file2
2c2
< test
--
> testing$ uniq A (To find out how often repetitions occur or to filter out the repetitions)
(Check out options)$ paste A B (Concat 2 files so that output is like table columns)
17.04.61 Smith Fred
22.01.63 Jones Susan
03.11.62 Bloggs Zach$ cut -c7-8 myfile
61
63
62$ cut -f2 -d' ' myfile (with delimiter option)
Smith
Jones
Bloggs$ sort A
$ sort A | uniq (Filter out repeated lines and prints in sorted order)
Q) Find out how many separate inodes are represented by the files(excluding 'dot' files)
$ ls -il | cut -c1-6 | sort -u | wc -l$ lp -n 2 -d def myfile (line printer)
$ pr -2 -l 50 abc | lp -d def (stdout, columns, lines)
$ du (will print the number of kilobytes used to store the data in the files in that directory)
Chapter 4: Processes and Devices
Process: Machine code program (suspended/running)$ ps
$ date &
$ date
$ sleep 15
$ date
$ sleep 100 &
$ ps$ kill -s KILL "pid"
Job: Collection of processes grouped together and identified by a small integer
$ jobs
$ cat testfile | wc & (2 processes, process # of wc)
[2] 2374Q) Arrange to check mail in 2 minutes.
$ cat >mymailer
sleep 120
echo Checking mail
mailx
control-D
$ sh mymailer&Default job: If jobs are running or stopped, one of them is default and indicated by +(Also %%). It is the job most recently created.
$ bg %1 (Any stopped job can be reactivated to run in the background)
[1]+ testA &$ fg %%
Command History list:
$ fc -l (Lists the commands u have already run)
$ fc 2 4
$ fc -s 3 (rerun commands)Homework: at, batch, nice, nohup, time.
Environment
A collection of variable names together with an associated value for each one.$ echo LOGNAME
LOGNAME
$ echo $LOGNAME$ ADDRESS="9250 Verree Road"
$ echo I live at $ ADDRESS
I live at 9250 Verree RoadEG: EDITOR, HOME, PATH, PRINTER, PS1, PS2, SHELL, TERM
$ echo PATH (When UNIX encounters a command not built in the shell, it looks at PATH, which is a sequence of pathnames. Each path is checked to ee if the executable is present)
$ env | more (list all variables)
$ PS1="type in a command:" (PS1 controls the prompt that he shell gives u)
type in a command: echo $PS1
type in a command:Homework: Global & Local variables
Executable Scripts
$ sh myfile
or
$ chmod a+x myfile
$ ./myfile or $HOME/myfileExamine the value of your PATH $echo $PATH
If there is a dot in your path, then $ myfile should work
Q) Find out the name of the terminal u are using
$ echo $TERMQ) Change your prompt from $ to "enter command"
$ PS1 ="enter command"
enter command: export PS1Q) Write a script which will prompt the user for the name of a file and then print the file on their terminal
$ echo Type in a filename$ read FILENAME
$ cat $FILENAMEQ)Update PATH do that it includes the subdirectory MYbin of your home dir
$ PATH=$PATH:$HOME/MYbin
$ export PATHQ) Write a command changeprompt which will request you to enter a new shell prompt and will then reset it to its new value
$ echo Type in new prompt:
$ read PS1
$ export PS1Q) Write a script which will read in the name of a file and display a message only if it
cannot be read
$echo Type the name of the file
read FILENAME
cat $FILENAME > /dev/nullQ) Reset your shell prompt to the name of the shell followed by a > symbol.
$ echo $SHELL
$ PS1=$( basename $SHELL)">"
sh>