Chapter 3 Notes

Shells

Every user that logs into a Linux system has a shell associated with their account. The shell is a program that allows the user to interact with the system. There are a few different shells in Linux, each one does the same job, but each understand a different command syntax and provides different built-in functions. The default shell in Linux is bash.
  • To find out your current shell, type echo $SHELL.
  • To find all available shells, type cat /etc/shells.
  • To change your shell, type chsh.
  • To change a user's shell, type chsh -s /bin/<shell> <user> as root.

Text Streams and Redirection

In Linux, there are three streams: standard input (stdin; stream 0), standard output (stdout; stream 1), and standard error (stderr; stream 2).
  • Type cat and hit enter. Now type text, and you will see that the text is repeated; cat is getting input from the keyboard.
  • Type cat /etc/passwd and notice that the contents of the /etc/passwd file are displayed to the screen; cat is now getting input from a file.
  • Assume we have a file called todo that contains a to-do list, and we want to email the list to user damaya on the system. We could type mail damaya@host.example.com and then paste the contents from the file; but, there's an easier way to do it with input redirection. Instead, we type mail damaya@host.example.com < todo. This takes the content of the todo file and redirects it as input to the mail command. Input redirection makes STDIN a file rather than the keyboard.
  • As a user on the system (not root), type the following command find / -name passwd. Both streams (1 and 2, stdout and stderr respectively) are displayed to the screen.
  • We can redirect stdout with the > operator. For example, find / -name passwd >/tmp/stdout redirects stdout to the file /tmp/stdout. The default behavior of the output redirection operator (>) is to redirect stdout, but not stderr. This is the same thing as 1>/tmp/stdout.
  • To redirect stderr type find / -name passwd 2>/tmp/stderr, and that would redirect only the errors from the find command to the file /tmp/stderr.
  • If  we want to redirect both stdin and stderr output to a file, we would combine the output by typing find / -name passwd &>/tmp/stdboth. In a few bullet points, we will see an interesting caveat to using &>.
  • We can redirect stdout to one file, and stderr to another file by typing find / -name passwd 1>/tmp/stdout 2>/tmp/stderr.
  • Say we're not interested in errors; Linux has a garbage disposal called /dev/null. To disregard (throw the output in the garbage) the output from stderr, type find / -name passwd 2>/dev/null. To disregard the output of stdout, type find / -name passwd >/dev/null. To disregard all output (both stdout and stderr), type find / -name passwd &>/dev/null.
  • We can append output to a file with the >> operator. If you type find /etc -name passwd >/tmp/append and then cat /etc/passwd >/tmp/append, the output from cat will be the only thing in the file. The > operator overwrites what already exists in a file with whatever you're redirecting. If you type find /etc -name passwd >/tmp/append and then cat /etc/passwd >>/tmp/append, you will now see that the output from both commands is in the file.
  • We can also redirect output to another command with the pipe (|) operator. For example, if we type the command dmesg we will see a huge amount of text scroll by. To scroll through the file we can pipe the output to the pager less by typing dmesg | less.
  • Here's the caveat of using &>: The &> operator redirects stdout and stderr to a file, and can only redirect to a file. If you need to pipe both stdout and stderr to another command, you will need to use 2>&1, which says to redirect stderr to stdout, thus combining the two.

File and Directory Concepts

Everything in Linux is a file. For an example of this, as root, type cat /dev/input/mice and then move your mouse around the screen. 
  • Use the cd command to change directories. For example, to change to the /tmp directory, type cd /tmp.
  • To find out what directory you're currently in, type pwd.
  • The tilde (~) is a shortcut for your home directory. So, cd ~ will change to your home directory (typing cd without any arguments will do this as well). You can also switch to a directory in your home directory by typing cd ~/directory.
  • If there is a user named damaya on the system, you can type cd ~damaya to go to that user's home directory.
  • Type cd - to change to the previous working directory. For example, if I am in /tmp and I cd to /home/damaya, I can type cd - to go back to /tmp.
  •  When you create a directory in Linux, the . and .. directories are automatically created. The . represents the current directory, and the .. represents one directory up. So, to copy a file from /tmp to the current directory, you can type cp /tmp/file . rather than typing out the entire path. To copy a file from /tmp to one directory up on the hierarchy, you'd type cp /tmp/file .. (assuming we are in /home/damaya/temp, that command would copy the file from /tmp/file to /home/damaya (one directory up from where we are at)).

Comments

Popular posts from this blog

Introduction