Shell Flashcards

https://www.datacamp.com/courses/introduction-to-shell-for-data-science

1
Q

Which command lets you find out where you are in the filesystem?

A

pwd

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

Which command lets you list the content of your directory?

A

ls

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

Which command lets you list the content of a specific directory?

A

ls /path/to/file

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

What is the conceptual difference between asbolute and relative paths?

A

An absolute path is like a latitude and longitude: it has the same value no matter where you are.

A relative path, specifies a location starting from where you are: it’s like saying “20 kilometers north”.

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

How does the shell decide if a path is relative or absolute?

A

An absolute path starts with a /.

A relative path doesn’t.

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

Which command lets you move around the filesystem?

A

cd

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

What is the parent of a directory?

A

The parent of a directory is the directory above it. For example, /home is the parent of /home/repl, and /home/repl is the parent of /home/repl/seasonal.

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

Which special path corresponds to the one above the one you’re currently in?

A

..

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

Which path is the root directory?

A

/

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

Which path corresponds to the current directory?

A

.

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

Which path corresponds to the home directory?

A

~

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

Which command lets you copy files?

A

cp path/original.txt path/duplicate.txt

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

How do you copy several files to a directory?

A

cp original1.txt original2.txt path/to/directory

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

Which command lets you move files from on directory to another?

A

mv path/to/file1.txt path/to/file2.txt path/to/directory

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

Which command lets you rename a file?

A

mv old_name.txt new_name.txt

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

Which command lets you remove files?

A

rm file/to/file1.txt file/to/file2.txt

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

What happens if you move or copy to an existing filename?

A

The original file with that filename is overwritten.

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

Which command lets you remove an empty directory?

A

rmdir directory_name

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

Which command lets you create a directory?

A

mkdir directory_name

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

Where should you store files and programs you only need briefly?

A

/temp

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

Which command lets you look at the content of files?

A

cat file1.txt file2.txt

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

When printing the content of a file, how do you go to the next page?

A

Press the spacebar

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

When printing the content of a file, how do you quit?

A

Press q

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

When printing the content of several files, how do you move to the next file?

A

Type :n

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

When printing the content of several files, how do you move to the previous file?

A

Type :p

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

When printing the content of several files, how do you quit?

A

Type :q

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

Which command lets you print the first 10 lines of a csv file?

A

head file.csv

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

What are command-line flags?

A

They are a way to specify options for command-line programs. For example, head -n 5 file.csv specifies head should print 5 lines instead of the default 10.

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

By convention, where should command-line flags be placed?

A

By convention, command-line flags should be placed before any filenames.

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

How do you display everything that is underneath directory?

A

ls -R

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

When listing files, how do you make it clear which ones are directory names and which ones are runnable programs?

A

ls -F

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

Which command lets you obtain documentation on a specific command?

A

man command

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

Which command lets you display columns?

A

cut

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

When selecting columns, which flag lets you specify the column delimiter?

A

-d ,

(for columns separated by commas)

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

When selecting columns, which flag lets you specify the columns or range of columns to select?

A

-f 1-4 7

(columns 1 to 4 and 7)

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

When displaying the content of a file, how do you skip the first x lines?

A

-n +x

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

Which command lets you display a list of commands you have run recently?

A

history

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

How do you rerun your most recent use of a command?

A

!command

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

How do you rerun the xth command in your history?

A

!x

40
Q

How do you display lines of files containing a specific piece of text?

A

grep keyword path/to/file

41
Q

With grep, which flag lets you print a count of matching lines rather than the lines themselves?

A

grep -c

42
Q

With grep, which flag lets you not print the names of the files when searching multiple files?

A

grep -h

43
Q

With grep, which flag lets you ignore case?

A

grep -i

44
Q

With grep, which flag lets you print the names of the files that contain the matches, rather than the matches themselves?

A

grep -l

45
Q

With grep, which flag lets you print only lines that don’t match?

A

grep -v

46
Q

Which command lets you combine files?

A

paste file1.csv file2.csv

47
Q

How do you save a command’s output?

A

Use redirection:

tail -n 5 file.csv > last5.csv

48
Q

Which command lets you print the number of characters, words, and lines in a file?

A

wc

49
Q

Which wc flag lets you print the number of characters only?

A

wc -c

50
Q

Which wc flag lets you print the number of words only?

A

wc -w

51
Q

Which wc flag lets you print the number of lines only?

A

wc -l

52
Q

What is a wildcard and why are they useful?

A

A wildcard is a character that can be used as a substitute for any of a class of characters in a search, thereby greatly increasing the flexibility and efficiency of searches.

53
Q

Which wildcard matches 0 or more characters?

A

*

54
Q

Which wildcard matches a single character?

A

?

55
Q

Which wildcard matches any one of the characters passed?

A

[characters]

56
Q

Which wildcard matches any of the comma-separated patterns passed?

A

{pattern1, pattern2}

57
Q

Which command lets you order values?

A

sort

58
Q

Which sort flag lets you sort in numerical order instead of the default alphabetical order?

A

-n

59
Q

Which sort flag lets you sort in descending order instead of the default ascending order?

A

-r

60
Q

Which sort flag lets you ignore leading blanks?

A

-b

61
Q

Which sort flag lets you fold case (be case-insensitive)?

A

-f

62
Q

Which command lets you remove adjacent duplicated lines?

A

uniq

63
Q

Which uniq flag lets you display the count of duplicated lines?

A

-c

64
Q

In a command expression, where should the command writing to a file appear?

A

At the very beginning or at the very end.

65
Q

How do you interrupt a runing command?

A

Ctrl + C

66
Q

Which environment variable corresponds to the user’s home directory?

A

HOME

67
Q

Which path corresponds to HOME?

A

/home/repl

68
Q

Which environment variable corresponds to the present working directory?

A

PWD

69
Q

Which environment variable corresponds to the shell program being used?

A

SHELL

70
Q

Which path corresponds to SHELL?

A

/bin/bash

71
Q

Which environement variable corresponds to the user’s ID?

A

USER

72
Q

Which command lets you print the value of an environment variable?

A

printenv ENVIRONMENT_VARIABLE

or

echo $ENVIRONMENT_VARIABLE

73
Q

Which command lets you display a list of all environment variables?

A

set

74
Q

How do you create a shell variable?

A

variable=value

without spaces before or after =

75
Q

How do you print the value of a variable?

A

echo $variable

76
Q

How do you define a for loop?

A

for variable in list ; do command ; done

77
Q

How do you indicate a variable name instead of the name you have typed.

A

$

(use a dollar in front of the variable, eg echo $variable)

78
Q

If you have filenames with spaces, how do you work around that so your command interprets filenames correctly?

A

Wrap filenames in single or double quotes (‘’ or “”).

79
Q

How do you separate several commands in the body of a for loop?

A

;

80
Q

How do you open a file with nano?

A

nano filename

81
Q

How do you delete a line with nano?

A

Ctrl + K

82
Q

How do you undelete a line with nano?

A

Ctrl + U

83
Q

How do you save the file with nano?

A

Ctrl + O

84
Q

How do you exit the editor with nano?

A

Ctrl + X

85
Q

How do you create a file with nano?

A

nano filename

86
Q

How do you store commands so you can reuse them later?

A

Write commands to a .sh file.

Although scripts don’t have to have names ending in .sh, it’s good practice as it helps keep track of which files are scripts.

87
Q

Which command lets you execute commands contained in a .sh file?

A

bash name.sh

88
Q

How do you call a file full of shell commands?

A

A shell script.

89
Q

How do you provide a placeholder to a command in a script?

A

$@

90
Q

How do you execute a shell script with a placeholder?

A

bash file.sh placeholder

91
Q

How do you provide several placeholders to a shell script?

A

Use $1, $2…

92
Q

How are several placeholders passed to a shell script?

A

In the order in which they appear in the bash command calling the script.

You need to pay extra attention to the order of the placeholders in the script and in your commands call, to ensure you’re passing the appropriate value at the appropriate placeholder.

93
Q

How do you write loops in scripts?

A

Use semi-colons, or declare them on several lines without semi colon following this format:

for value in variable
do
command1
command2
done

Commands do not have to be indented, but doing so makes things clearer.

94
Q

How do you add comments to a script?

A

Use # at the beginning of the line.

95
Q
A