Command Line Flashcards
What does the command ‘ls’ do?
Lists the files in a directory
What command id used to find out the directory you are currently in?
pwd
What is the command to create an empty file
touch
What does using the -a flag with ls do?
Shows all files including those which start with a ‘.’ and therefore are hidden
What does using the -t flag with ls do?
Shows all files in a directory in order of most recently modified
What does using the -l flag with ls do?
Shows all files in a directory in the long format.
Here there are four rows, with seven columns separated by spaces. Here’s what each column means:
- Access rights. These are actions that are permitted on a file or directory.
- Number of hard links. This number counts the number of child directories and files. This number includes the parent.
- The username of the file’s owner. Here the username is cc.
- The name of the group that owns the file. Here the group name is eng.
- The size of the file in bytes.
- The date & time that the file was last modified.
- The name of the file or directory. directory link (..) and current directory link (.).
What is the wildcard special character
*
What command can be used to rename a file?
mv
What is the stdin, stdout and stderr?
stdin - standard input, is information inputted into the terminal through the keyboard or input device.
stdout - standard output, is the information outputted after a process is run.
stderr - standard error, is an error message outputted by a failed process.
What is the command for seeing the content of a file?
cat
What is the command for redirecting the standard input to standard output?
>
What command is used to append standard input to standard output without overwriting?
> >
What does the < command do?
e.g. $ cat < lakes.txt
< takes the standard input from the file on the right and inputs it into the program on the left. Here, lakes.txt is the standard input for the cat command. The standard output appears in the terminal.
What is ‘|’ and what is it used for?
e.g.
$ cat volcanoes.txt | wc | cat > islands.txt
Here the output of cat volcanoes.txt is the standard input of wc. in turn, the wc command outputs the number of lines, words, and characters in volcanoes.txt, respectively.
is a “pipe”. The | takes the standard output of the command on the left, and pipes it as standard input to the command on the right. You can think of this as “command to command” redirection.
What command can be used to take the standard input and sort alphabetically?
sort
What does the uniq command do?
eg uniq deserts.txt
uniq stands for “unique” and filters out adjacent, duplicate lines in a file. Here uniq deserts.txt filters out duplicates of “Sahara Desert”, because the duplicate of ‘Sahara Desert’ directly follows the previous instance. The “Kalahari Desert” duplicates are not adjacent, and thus remain.
If you sort before using uniq it will remove all duplicate values.
What command is used to search for a pattern and return results and what does the -i argument do when used with this command?
grep stands for “global regular expression print”. It searches files for lines that match a pattern and returns the results. It is also case sensitive.
grep -i enables the command to be case insensitive.
What does -R and -Rl do when used with grep?
N.B. l here is a lower case L
grep -R searches all files in a directory and outputs filenames and lines containing matched results. -R stands for “recursive”. This outputs the filename and the lines with matched results.
grep -Rl searches all files in a directory and outputs only filenames with matched results. -R stands for “recursive” and l stands for “files with matches”.
What does the sed command do?
$ sed ‘s/snow/rain/’ forests.txt
sed stands for “stream editor”. It accepts standard input and modifies it based on an expression, before displaying it as output data. It is similar to “find and replace”.
Let’s look at the expression ‘s/snow/rain/’:
s: stands for “substitution”. it is always used when using sed for substitution.
snow: the search string, the text to find.
rain: the replacement string, the text to add in place.
In this case, sed searches forests.txt for the word “snow” and replaces it with “rain.” Importantly, the above command will only replace the first instance of “snow” on a line.
$ sed ‘s/snow/rain/g’ forests.txt
The above command uses the g expression, meaning “global”. Here sed searches forests.txt for the word “snow” and replaces it with “rain”, globally. All instances of “snow” on a line will be turned to “rain”.
What command to you use to activate environment settings?
source
When creating a bash environment, what prefix should be added to the file name containing the information?
.
A bash profile is a secret file
When using a bash profile what does alias do?
The alias command allows you to create keyboard shortcuts, or aliases, for commonly used commands.
When using a bash profile what does export do?
Export sets a variable and makes it available to all child sessions started from the session you are in
What does the variable PS1 change?
The PS1 variable is the command prompt variable