Lecture 5: CSE 384(System and Network
Programming)The Linux Command Line, Fifth Internet Edition
Chapters 6-10
Overview
• I/O Redirection• STDIN, STDOUT, STDERR
• Concept Overview
• Examples
• Disposing Output• /dev/null
• Pipelines • Concept Overview
• Examples
I/O Redirection: based on page 54 of the online version the Linux CommandLine• redirect the input and output of commands to and from files
• connect multiple commands together into powerful command pipelines• In keeping with the Unix (Linux) model, everything is a file….
• programs (such as ls) send output to a file called standard output: called stdout
• Status (error) messages to another file called standard error: stderr• by default, both stdout/stderr are linked to the screen and not saved into a disk file.
• Many programs take input from yet another file: stdin (called standard in) • By default stdin is connected to the keyboard
• Redirection allows us to change where output (stdout/stderr) goes (e.g. the screen), and where input (stdin) comes from (e.g. the keyboard)
stdin, stdout, stderr
https://github.com/kennyyu/bootcamp-unix/wiki/stdin,-stdout,-stderr,-and-pipes
• In python: “print”• In Java: “System.out.print”• In C: “printf’• fprintf(stderr)• In C++ “std::cout”• std::cerr
0 1
2
STDIN, STDOUT, and STDERR are files…
• Operating systems (under the hood), are often a tables with indices that point to resources.• Files (and other resources) are often referenced using descriptors (integer
numbers) that serve as a handle to the resources.
• STDIN == file descriptor 0
• STDOUT = file descriptor 1
• STDERR = file descriptor 2
Source: The C Programming Language 2nd Edition , page 176
What are files anyway?Original UNIX “FILE” concept implementation by Ritchie
Examples
Examples• Redirect STDOUT
• ls -l /usr/bin > ls-output.txt• ls -l /usr/bin 1> ls-output.txt
• Redirect STDERR• [me@linuxbox ~]$ ls -l /bin/usr > ls-output.txt
• What happened? • Zero length! The destination file is always rewritten from the beginning.
• [me@linuxbox ~]$ ls -l /bin/usr 2> ls-error.txt
• [me@linuxbox ~]$ > ls-output.txt• using the redirection operator with no command preceding it will truncate
an ex-isting file or create a new, empty file.
• [me@linuxbox ~]$ ls -l /usr/bin >> ls-output.txt
• [me@linuxbox ~]$ ls -l /usr/bin >> ls-output.txt• append redirected output to a file instead of overwriting the file
Examples• Redirecting Standard Output and Standard Error to One File
• [me@linuxbox ~]$ ls -l /bin/usr > ls-output.txt 2>&1
• Using this method, we perform two redirections.
• First we redirect standard output to the file ls-output.txt
• then we redirect file descriptor 2 (standard error) to file descriptor 1 (standard output) using the notation 2>&1.
• [me@linuxbox ~]$ ls -l /bin/usr &> ls-output.txt
• Streamlined notation &> in (modern BASH implementations) to redirect both standard output and standard error to the file
• append the standard output and standard error streams to a single file
• [me@linuxbox ~]$ ls -l /bin/usr &>> ls-output.txt
Disposing Output (/dev/null) (page 58)
• Sometimes its appropriate to disregard output from a command, we just want to throw it away.• error and status messages.
• [me@linuxbox ~]$ ls -l /bin/usr 2> /dev/null
• Often called the bit bucket • Old Unix concept and because of its universality
• The old Unix joke: If I want to ignore this lecture, you might say that you’re sending everything I say to /dev/null
Redirecting STDIN (page 59)• Consider the Unix cat program – Concatenate Files
• The cat command reads one or more files and copies them to standard output (note wildcard expand is sorted order)
• Now run cat with no file arguments (assumes stdin)
• [me@linuxbox ~]$ cat
• the quick brown fox jumped over the lazy dog.
• Type Ctrl-d (i.e., hold down the Ctrl key and press “d”)
• to tell cat that it has reached end of file (EOF) on stdin
Redirecting STDIN
• Now run cat with no arguments again….
• [me@linuxbox ~]$ cat
• Type: the quick brown fox jumped over the lazy dog.
• Type Ctrl-d (signal EOF)
• Using the < redirection operator
the source of stdin changed from the keyboard to the file lazy_dog.txt. We see that the result is the same as passing a single file
Pipelines
• read data from stdin and send to stdout is utilized by a shell feature called pipelines.
• Using the pipe operator | (vertical bar)• standard output of one command can be piped into the standard input of
another.
command1|command2
[me@linuxbox ~]$ ls -l /usr/bin | less
Pipe (|) Versus Redirection (>) - page 62• The Difference Between > and |
• the redirection operator connects a command with a file
• command1 > file1 -> echo “hello” > temp
• the pipeline operator connects the output of one command with the input of a second command
• command1 | command2 -> ls -l /usr/bin | less
• Be carful when you are learning about pipelines
• What does command1 > command2
• Answer: sometimes something really bad.
• # cd /usr/bin
• # ls > less
• Notice the # indicates root, (wiped out the less program)
Pipeline (concept)
Source: https://github.com/kennyyu/bootcamp-unix/wiki/stdin,-stdout,-stderr,-and-pipes
Tee command
ls
[me@linuxbox ~]$ ls /usr/bin | tee ls.txt | grep zip
stdoutstdintee
stdin stdoutgrep
stdout
Console
stderrstderr
Console
Filters - piping command together
• https://mwcorley79.github.io/MikeCorley/presentations/TLCL-19.01.pdf#page=86 (The Linux Command Line, William Shotts)• sort (page 62) --
• uniq (unique) -- removes duplicates
• wc (word count)
• grep (page 63)
• head and tail (page 64)
• tee
Chapter 7: The Shell – Expansion and substitution
• Expansion• arithmetic• Brace• Parameter (shell variables)
• Substitution
• Quoting
• https://mwcorley79.github.io/MikeCorley/presentations/TLCL-19.01.pdf#page=86
Chapter 8: Keyboard shortcuts
• https://mwcorley79.github.io/MikeCorley/presentations/TLCL-19.01.pdf#page=86
• Command history
• Completion
• script
Chapter 9: Permissions
• https://mwcorley79.github.io/MikeCorley/presentations/TLCL-19.01.pdf#page=86
• id – Display user identity
• chmod – Change a file's mode
• umask – Set the default file permissions
• su – Run a shell as another user
• sudo – Execute a command as another user
• chown – Change a file's owner Permissions
• chgrp – Change a file's group ownership
• passwd – Change a user's pass
umask
• controls the default permissions given to a file when it is created.
• octal notation to express a mask of bits to be removed from a file's mode attributes.
chmod examples
• chmod g+rx a.out• - give the group read + execute permissions
• chmod g+rwx,o-x a.out• - give the group read + write + execute permissions, take way execute for the
world
• chmod u+rwx,g+rx,o-rwx a.out == chmod 750• Give the owner full access (rwx), the group (rx), take away all access to the
world
Special permissions: setuid, setgid, sticky bit • setuid bit (octal 4000).
• When applied to an executable file,it sets the effective user ID from that of the real user (the user actually running the program) to that of the program's owner.• chmod u+s program
• setgid bit (octal 2000), which, like the setuidbit, changes the effective group ID from the real group ID
• sticky bit (octal 1000) • prevents users from deleting or renaming files unless the user is either the
owner of the directory, the owner of the file, or the superuser• often used to control access to a shared directory, such as /tmp.
Essential security related commands
• id – Display user identity
• chmod – Change a file's mode
• umask – Set the default file permissions
• su – Run a shell as another user
• sudo – Execute a command as another user
• chown – Change a file's owner90
Owners, Groups, and Everyone else (the world) • user may own files and directories.
• user has control over the files and directories owned
• Users can, in turn, belong to a group consisting of one or more users who are granted access to files and directories by their respective owners.
• an user/owner may grant some set of access rights to everybody, which in Unix terms is referred to as the world.
File Attributes: Read, Write, Execute Permissions
• chmod - change the mode (permissions) of a file or directory• only the file’s owner or the superuser can change the mode of a file or direc-
tory
• supports two distinct ways of specifying mode changes: octal number repre-sentation, or symbolic representatio
• Octal (base 8), hexadecimal (base 16) • number systems often used to express numbers on computers.
rwx-r--r111010
chmod examples
chown
SU and SUDO