Ch 2 Using Essential Tools Flashcards
How do you create an alias?
alias newcommand=’oldcommand’
Ex:
Alias ll=’ls -l –color=auto’
Are commands or aliases executed first?
Aliases are, unless a full path for a command is used, e.g., /usr/bin/reboot
What is an internal vs external command?
Internal command is a PART of the shell and doesn’t have to be loaded from disk separately
An external command exists as an executable file on the disk of the computer (slower to load on first use)
When a command is run, the shell first looks to see if its internal. If not, where does it look for external commands?
Looks on the disk for a similar named command that is executable
What command will tell you if a command is internal or external?
Type
E.g. type pwd
This also reveals if it’s an alias
How can external commands be found by the shell?
The $PATH variable
How does the $PATH variable work?
It defines a list of directories that can be searched for a matching filename when a user enters a command
What does the which command do?
Figures out which command the shell will use
E.g. which ls - where will ls command come from? (The external directory that an external command will come from?) But type is a stronger command, since it works for internal commands and aliases
Why doesn’t the $PATH variable include the current directory?
For security reasons
What must prepend a command if you are trying to execute a command that is in your current directory but NOT in the $PATH?
./
What does ./ mean?
The . is for current directory, and the / is for the rest
When does ./ become necessary for execution? An example case?
For a homeade script you wrote in your working directory
Can the $PATH be user specific?
Yes, most users will use the same $PATH. The root user is an exception.
Which I/O goes to the computer monitor, and is also the default destination?
STDOUT
What is the default outout destination errors in the shell?
STDERR
Default shell source for input?
STDIN
Default destination for STDERR?
Computer monitor
Default destination for STDIN?
Computer keyboard
What character is used to redirect the destination for STDIN?
< (same as 0<)
What is the File Descriptor Number for STDIN?
0
What is the File Descriptor Number for STDOUT?
1
What is the character for redirecting STDOUT?
> (same as 1>)
What is the character for redirecting STDERR?
2>
What is the File Descriptor Number for STDERR?
2
How would you redirect a command’s input to being from a file and not the keyboard?
Cmd < file
./myscript < myfileforscript.txt
What’s the difference between using a | for output vs STDOUT redirection with > ?
I think it’s that a pipe redirects output to another command, whereas > is more for redirecting output to a file?
How do you redirect I/O when it’s coming from a background command, a command from a different terminal session, or a monitor? And what if there’s no keyboard to accept input from?
Using I/O redirection. This is why I/O redirection exists.
A command reads from file descriptor 0 for stdin, and writes to file descriptor 1 for non error output. By default, std in and stdout…
Connect to keyboard and monitor.
Stderr by default goes to…
File descriptor 2
These characters connect file descriptors to files and commands in the shell
> < and |
What does > (same as 1>) do?
Redirects STDOUT. If to a file, it overwrites the file’s original contents
What does»_space; (same as 1») do to STDOUT?
Redirects it in append mode. If output goes to a file, output appends to the existing file content
What command clears or wipes a file of content?
/Dev/null > file
What does 2> do?
Redirects stderr. Can redirect to a file
What does 2>&1 do?
Redirects stderr to the same destination as stdout. Stderr output has to be redirected with stdout redirection?
Example of redirecting stderr with stdout
Ls whuhiu > errout 2>&1
What does the character < (same as 0<) do?
Redirects stdin (usually a file)
What can be used to replace the default destinations for stdout, stdin, and stderr?
Files, and also device files
What is a device file?
A file that is used to access specific hardware
Example device files…
/dev/sda - hard disk
/dev/tty1 or /dev/console for the server console
/dev/null to discard I/O
Accessing device files needs what user level of privilege?
Root
what key do you press in vim to switch to input mode with the cursor exactly at your position?
i
what key do you press in vim to switch to input mode with the cursor directly after your current cursor position?
a
what key do you press in vim to switch to input mode that starts the cursor in a new line below the current cursor position?
o
What vim command deletes the current line and places its contents into memory?
dd
Bash’s default configuration saves how many of the last commands?
1000
When a shell session closes, what happens to the shell history?
It updates the history file, .bash_history, in the home directory of the user of the shell session
What happens to shell memory before the shell is closed and written to .bash_history?
It’s held in memory
What command shows bash history?
History
What keyboard shortcut lets you type a word and match it with a string in the command history?
Ctrl + R
How do you get to a specific line number from history?
!number
How do you delete a command from history?
History -d number
This also renumbers all the history commands.
What command will execute a command from history containing a word starting with something you just typed?
!sometext
When might it be necessary to delete bash history?
If a password were typed into clear text
How do you delete command history entirely?
History -c
When you use the clear history command (history -c) does it save anything from your current session when it ends?
No
How do you wipe history of your current session AS WELL AS the .bash_history file?
History -c followed by history -d
What are the different vim modes?
Command mode
Input mode
Visual mode
Refreshes a file in vim while you’re in it
:edit
How to remove line numbering in vim
:set nonu or :set nonumber
How to number the lines in vim
:set nu or :set number
forward search in vim, and for specific text
/text
backward search in vim
?text
what vim command would you use inside vim to rename the file you’re in? (Save as..)
:w newfilename
can the $PATH variable be set for specific users?
yes, but generally, most users use the same. However, the root user has its own access
how do you set an environmental for just your current shell session?
by entering it on the command line instead of a configuration file
what command lets you see all your current environmental variables?
env
what file shows all current environmental variables?
/etc/environment
what’s the template for setting an environmental variable?
VAR=’/path/to/var’
/dev/sda is what?
your hard disk
/dev/tty1 is what?
usually the console of your server
/dev/console is what?
the console of your server
What is /dev/null often used for?
To discard the output of a command (can also be used to wipe a file like IV has done)
What vim command copies (yanks) the current line?
yy
What pastes whatever contents are in memory, in vim?
p (paste was was dd-ed or yy-ed)
What keyboard character switches vim to visual mode?
v
What keyboard in character does undo for the last action?
esc+u or :u
What redos the last action in vim? (but only once)
ctrl+r
What keyboard characters move your cursor to the very beginning of a file in vim?
gg
What keyboard characters move your cursor to the very end of a file in vim?
G
What keyboard character moves you to the first character in the line, in vim?
esc+^
What keyboard character moves you to the last character in the line, in vim?
esc+$
What adds the output of ls or any other command into the current file, in vim?
!ls or !command (I actually found that it brought this up but didn’t actually put it in the file for some reason)
Replace all occurrences of old with new, in vim
:%s/old/new/g
the g means do it globally, or for every instance, ad the %s means substitute
What is the environment for a shell? (shell environment)
It consists of variables that define the user environment, such as the $PATH variable
In the context of a shell environment, what is a variable?
fixed names that can be assigned dynamic values
Explain what the environmental variable $LANG is for
can be set to en_US.UTF-8
this means you can work in the english language with the settings that are the most commond for English ( like time and date)
Can environmental variables be used in scripts and programs?
yes
True or false, can different users have different shell environments, and thus different environmental variables?
true
name some common environmental variables
$MAIL
$PATH
$PWD
$LANG
$HISTCONTROL
$SHLVL (shell level)
$HOME
$LOGNAME
$LESSOPEN
$_
$OLDPWD
How can you find the value of an environmental variable from the command line?
env to show all of them, or echo $ENVVAR to see
When are shell environments created?
When a user logs in
When the shell environment is created, what file determines how it is made?
/etc/profile
/etc/bashrc
~/.bashrc
~/.bash_profile
shell environment config file for all users upon login (generically)
/etc/profile
shell environment config file for when subshells are made
/etc/bashrc
shell environment config file for user-specific login shells
~/.bash_profile
shell environment config file for subshells on a user-specific level
~/.bashrc
What is the difference between a login shell and a subshell?
login shell - first shell that is opened after a user logs in
subshell - created when running a script form the login shell (for the script to run in )
What configuration file determines what is displayed after you login? (like an information banner)
/etc/motd and /etc issues files
How can sysadmins take advantage of the /etc/motd file?
They can use it to inform users of an issue or security policy
Other than /etc/motd, what other way can sysadmins inform users of issues and security policies and such?
/etc/issue
The contents of what file display before you login in a text console interface?
/etc/issue
What file can be used to give users login instructions before they login?
/etc/issue
What command will help you learn more about a command’s syntax and usage?
man
What’s a more succint version of providing what the man command provides?
the –help option flag (gives you all possible options for a command)
A man page includes what sections?
An Examples area and a See Also area (related man pages)
What keyboard key will take you to the end of the man page?
G
What part of the man page are the examples and the See Also?
the end
How can you find examples in a man page?
/example
How can you find the man page you want? (search the mandb man database)
apropos and man -k
What’s a weakness of using man -k for finding a man page?
The text you use to search, e.g. “man -k partition” will only find commands with that term (partition) in its summary
How are man -k and apropos different?
they aren’t
What other system provides info about command usage, other than man pages?
the info system
When do you use the info system instead of the man page?
It depends. Some commands have short man pages and most of their info is in the info page. If this is the case, the “See Also” part of a man page will say this.
The info system for a command is also called what?
Texinfo manual
How do you full up the info page for a command?
pinfo or info
How are pinfo and info different?
pinfo has a menu and easier navigation
How are info pages organized?
like web pages, that is to say, hierarchical
What are the navigation shortcuts for an info page, for next, previous, and up (up in the hierarchy/file/directory tree)
n, p, u
What denotes a menu item in an info page?
An * (*** underline?)
To advance in an info page (drill down into file/directory/page tree) what do you use?
arrow keys
What is a third source of info on commands beside man pages and info pages (and not the –help flag)?
/usr/share/doc
How is /usr/share/doc different from man and info pages?
Tends to be better for services and systems (something more complex than a singular command)
What services have very useful /usr/share/doc pages?
rsyslog
bind
Kerberos
OpenSSL