Redirection and Pipes
Contents
More Commands
sort
This command can be used to sort lines.
$ cat 1.txt
second
first
third
fourth
$ sort 1.txt
first
fourth
second
third
This command can be used to take standard
input from the console. Once we enter the lines we
can hit "Ctrl-d" .
$ sort
second
first
third
fourth --Ctrl-d
first
fourth
second
third
more and less
$ more 2.txt
first
fourth
second
third
The "more" command can be used to view a large file
with page by page views. We use the space bar to go down
the document and q to come out of the display.
type
The "type" command displays how it's argument
would be interpreted if used as a command.
$ type echo
echo is a shell builtin
$ type if
if is a shell keyword
The "if" is a keyword in shell scripting.
The following are the different options
the command can output.
alias – if command is a shell alias
keyword – if command is a shell reserved word
builtin – if command is a shell builtin
function – if command is a shell function
file – if command is a disk file
help
The "help" command can be used to get more
information about the command.
$ help echo
echo: echo [-neE] [arg ...]
Write arguments to the standard output.
Typing "help" by itself lists all the commands.
apropos
The "apropos" command searches the man pages looking
for the matching argument and lists those commands.
[amittal@fog ~]$ apropos echo
echo (1) - display a line of text
echo (1p) - write arguments to standard output
lessecho (1) - expand metacharacters
pam_echo (8) - PAM module for printing text messages
ping (8) - send ICMP ECHO_REQUEST to network hosts
ping6 (8) - send ICMP ECHO_REQUEST to network hosts
[amittal@fog ~]$ apropos moun
mount.cifs (8) - mount using the Common Internet File System (CIFS)
mount (2) - mount file system
btrfs (5) - topics about the BTRFS filesystem (mount options, supp...
btrfs-check (8) - check or repair an unmounted btrfs filesystem
btrfsck (8) - check or repair an unmounted btrfs filesystem
doveadm-mount (1) - Manage the list of mountpoints where mails are stored
The above shows that we do not have to give the whole word. The
"apropos" means "about" or roughly.
info
This is smilar to the "man" command"
info
This is smilar to the "man" command"
whatis/B>
This gives a short description of the command.
alias/B>
We can use "alias" to create a shortcut
$ touch 5.txt
$ rm 5.txt
If we use |rm" by itself then it will delete
the file without confirmation.
$ touch 5.txt
$ rm -i 5.txt
rm: remove regular empty file '5.txt'? y
The "-i" option forces us to confirm before
the file is deleted.
Wouldn't it be nice if we didn't have to type the
option every time we delete a file.
$ alias rm='rm -i'
$ rm 5.txt
rm: remove regular empty file '5.txt'? y
We can also use another name for alias.
alias RM='rm -i'
$ touch 5.txt
Deller@DESKTOP-DNEP4MC ~/cs160/copy/first
$ alias RM='rm -i'
Deller@DESKTOP-DNEP4MC ~/cs160/copy/first
$ RM 5.txt
rm: cannot remove '7.txt': No such file or directory
Multiple Commands
We can run multiple commands on the command line separated
by semi colons.
$ date ; echo "Another command."
Tue, Dec 26, 2023 7:46:46 PM
Another command.
Redirection
We have run commands on the console. The command will take input from the console and the output will also be written to the console. However we can "redirect" the commands to take their input from some other place or write to somewhere else.Normally the "date" command outputs to the console but we can redirect the output to go to a file instead. $ date > t1.txt Deller@DESKTOP-DNEP4MC ~/cs160/redirect $ cat t1.txt Sun, Dec 24, 2023 1:58:12 PM We can use the ">>" to append to a file. $ cat t1.txt Sun, Dec 24, 2023 1:58:12 PM Sun, Dec 24, 2023 2:01:28 PM We can also redirect the input. Search the file "t1.txt" for the word "Dec" . $ grep Dec < t1.txt Sun, Dec 24, 2023 1:58:12 PM Sun, Dec 24, 2023 2:01:28 PM Search the file "t1.txt" for the word "Nov" . $ grep Nov < t1.txt If we type the following command from the root folder of hills we will see a bunch of permission errors. find . -name "*.sh" -print 2>/dev/null We can suppress those errors by redirecting the error output represented by the number 2 to "/dev/null" file. The "/dev/null" is a special file. The system will discard anything redirected to this file. We can also merge 2 files using the "cat" and the redirect command. $ cat 1.txt 2.txt > 3.txt We can combine both redirects in a single command. Using brackets. $ cat 2.txt first fourth second third $ ( sort < 1.txt ) > 2.txt $ cat 2.txt first fourth second third
Pipes
Let's say we have 2 commands and we want the output of the first command to go as the input to the second command. We could do this with a temporary file. $ date > t1.txt $ grep Dec < t1.txt Sun, Dec 24, 2023 2:14:10 PM However Unix gives us an easier way. Using pipes we can tell the output of the "date" command should be considered as the "input" of the grep command. $ date | grep Dec Sun, Dec 24, 2023 2:14:10 PM We can keep repeating the "|" as many times as we want in a single command. [amittal@hills ~]$ date | grep Dec | wc -l 1 The "wc -l" counts the number of lines in the input. We can also combine the redirect with pipe. date | grep -v Oct > data2.txt Assuming we are not in the month of October this will create the file "data2.txt" with the following sample contents. $ cat data2.txt Sun, Dec 24, 2023 2:18:05 PM