Command Line Flashcards

1
Q

What does the command ‘ls’ do?

A

Lists the files in a directory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What command id used to find out the directory you are currently in?

A

pwd

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the command to create an empty file

A

touch

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does using the -a flag with ls do?

A

Shows all files including those which start with a ‘.’ and therefore are hidden

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does using the -t flag with ls do?

A

Shows all files in a directory in order of most recently modified

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does using the -l flag with ls do?

A

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:

  1. Access rights. These are actions that are permitted on a file or directory.
  2. Number of hard links. This number counts the number of child directories and files. This number includes the parent.
  3. The username of the file’s owner. Here the username is cc.
  4. The name of the group that owns the file. Here the group name is eng.
  5. The size of the file in bytes.
  6. The date & time that the file was last modified.
  7. The name of the file or directory. directory link (..) and current directory link (.).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the wildcard special character

A

*

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What command can be used to rename a file?

A

mv

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the stdin, stdout and stderr?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the command for seeing the content of a file?

A

cat

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the command for redirecting the standard input to standard output?

A

>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What command is used to append standard input to standard output without overwriting?

A

> >

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does the < command do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is ‘|’ and what is it used for?

e.g.
$ cat volcanoes.txt | wc | cat > islands.txt

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What command can be used to take the standard input and sort alphabetically?

A

sort

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does the uniq command do?

eg uniq deserts.txt

A

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.

17
Q

What command is used to search for a pattern and return results and what does the -i argument do when used with this command?

A

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.

18
Q

What does -R and -Rl do when used with grep?

N.B. l here is a lower case L

A

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”.

19
Q

What does the sed command do?

A

$ 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”.

20
Q

What command to you use to activate environment settings?

A

source

21
Q

When creating a bash environment, what prefix should be added to the file name containing the information?

A

.

A bash profile is a secret file

22
Q

When using a bash profile what does alias do?

A

The alias command allows you to create keyboard shortcuts, or aliases, for commonly used commands.

23
Q

When using a bash profile what does export do?

A

Export sets a variable and makes it available to all child sessions started from the session you are in

24
Q

What does the variable PS1 change?

A

The PS1 variable is the command prompt variable

25
Q

What is contained in the $PATH environment variable?

A

PATH is an environment variable that stores a list of directories separated by a colon.

Each directory contains scripts for the command line to execute. The PATH variable simply lists which directories contain scripts.

26
Q

What does the env command return?

A

The env command stands for “environment”, and returns a list of the environment variables for the current user. Here, the env command returns a number of variables, including PATH, PWD, PS1, and HOME.

27
Q

What is ssh used for?

A

Secure Shell (SSH) is a cryptographic network protocol for operating network services securely over an unsecured network.[1] The best known example application is for remote login to computer systems by users.

SSH provides a secure channel over an unsecured network in a client-server architecture, connecting an SSH client application with an SSH server.[2] Common applications include remote command-line login and remote command execution, but any network service can be secured with SSH. The protocol specification distinguishes between two major versions, referred to as SSH-1 and SSH-2.

28
Q

What is scp?

A

The SCP is a network protocol, based on the BSD RCP protocol,[3] which supports file transfers between hosts on a network. SCP uses Secure Shell (SSH) for data transfer and uses the same mechanisms for authentication, thereby ensuring the authenticity and confidentiality of the data in transit. A client can send (upload) files to a server, optionally including their basic attributes (permissions, timestamps). Clients can also request files or directories from a server (download).

29
Q

How can you check your connection to the internet?

A

ping
e.g. ping www.google.co.uk
if there is a connection, a response will print to screen every second or so.

30
Q

What does awk do?

A

Awk scans each input file for lines that match any of a set of patterns specified literally

31
Q

How do you apply awk?

A

awk ‘pattern {action}’ input-file > output-file

This means: take each line of the input file; if the line contains the pattern apply the action to the line and write the resulting line to the output-file. If the pattern is omitted, the action is applied to all line.

32
Q

How would you make permanent ‘find and replace’ changes to a file?
How would you make this command case insensitive?

A

Use sed command with -i.

i.e. sed ‘s/snow/rain/g’ forest.txt will print to screen the content of the file showing the replacements mad, but won’t chnage the contents of the file

sed -i ‘s/snow/rain/g’ forests.txt won’t print to screen the changes made but will find and replace all incidences of ‘snow’ with ‘rain’ in the forests.txt file

sed -i ‘s/snow/rain/gI’ forests.txt (addition of a capital i) will find and replace case insensitively (i.e. Snow, SNOw, snow, snoW etc will all be replaced with rain)