Managing Files and Directories Flashcards

Editing files with vi Working with files Locating files Searching with grep Manipulating text

1
Q

In vim, how can you open up a file to a particular line number?

A

vi +# filename

ex: vi +12 file.txt

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

In vim, how can you delete multiple lines or a range of lines?

A

:#,#d

ex: :1,10d

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

In vim, how can you move to a particular line?

A

:#

ex: :10

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

In vim, what does ‘I’ do?

A

Insert at the beginning of a line

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

In vim, what does ‘A’ do?

A

Insert at the end of a line

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

in vim, what does ‘dd’ do in command mode?

A

Deletes the entire line

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

In vim, how do you perform a search?

A

In command mode, type / and then search (case sensitive)

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

In vim, what does ‘dw’ do in command mode?

A

Deletes the word in front up to the cursor

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

When you perform a search, how can you go to the next and previous occurrence of the search?

A

‘n’ goes forward, and ‘N’ goes backward

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

How can you perform a search above your cursor?

A

?

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

How can you do a search and replace just once?

A

:s/find/replace

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

How can you do a search and replace throughout the whole file?

A

:%s/find/replace

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

How can you do a search and replace within a range in the file?

A

:#,#s/find/replace

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

What does ‘D’ do in command mode?

A

Deletes to the end of the line

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

What does ‘y’ do in command mode?

A

Yanks (copies)

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

What does ‘yy’ do in command mode?

A

Yanks (copies) the current line

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

What does ‘p’ do in command mode?

A

pastes

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

What does ‘ZZ’ do in command mode?

A

Exits and saves changes

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

What does ‘:w!’ do?

A

Saves overwriting the file

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

What does ‘:q!’ do?

A

Exits ignoring any changes

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

In vim, how can you undo an edit?

A

u

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

What does ‘:e’ do?

A

Switches to another file

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

What does ‘:e!’ do?

A

Switches to another file without saving current file

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

How can you open a file in vi and search for a specific string at the same time?

A

vi +/search file.txt

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

What does the command ‘cat’ do?

A

Displays file contents to stdout

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

What does the command ‘more’ do?

A

This command is the same as cat, but it is paginated

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

What does the command ‘less’ do?

A

This command is the same as ‘more’, but it allows bidirectional scrolling

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

What does the command ‘head’ do?

A

This command displays the first 10 lines.

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

What does the command ‘tail’ do?

A

This command displays the last 10 lines.

30
Q

What does the command ‘tail -f’ do?

A

This command will display the last 10 lines of the file, but also ‘follow’ (watch) for any new messages that may appear.

31
Q

What does the command ‘tail -n 5’ do?

A

This command will show you the last 5 lines

32
Q

What does the command ‘head -n 5’ do?

A

This command will show you the first 5 lines

33
Q

How can you copy a file to another location?

A

Use the command cp

34
Q

What would this command do? Assume you are in your home directory.
cp /var/log/apt/history.txt history.txt

A

By default, cp will copy the file you choose to your current working directory if you don’t specify one, so this will copy history.txt to your home directory.

35
Q

If you moved a directory to another path, would you have to use the -r flag to move everything within the directory as well?

A

No. When you use the ‘mv’ command on a directory, everything within it is also moved.

36
Q

What are two commands you can use to find files in Linux?

A

Find and locate

37
Q

What is the difference between the commands find and locate?

A

find searches the drive, and locate searches the mlocate database. Locate can be much faster

38
Q

What is the command you would run to find a specific file in your current directory?

A

find . -name

39
Q

What is the command you would add to get rid of possible errors during a file system search using the command find?

A

2>/dev/null

40
Q

What is the command you would run to find files by permission?

A

find -perm

41
Q

What is the command you would run to find files by size?

A

find -size

42
Q

What is the command you would run to find files by user?

A

find -user

43
Q

Find searches in all sub-directories of the given path. How can you control how many sub-directories are searched?

A

-maxdepth

44
Q

What does the command ‘whatis’ do?

A

This displays a one-line manual page description

45
Q

What does the command ‘whereis’ do?

A

locate the binary, source, and manual page files for a command

46
Q

What does the command ‘which’ do?

A

locates a command, however, it only returns the 1st match it finds by default

47
Q

What does the command ‘type’ do?

A

This command displays how a command is interpreted

48
Q

What is a command you could use to search with regex?

A

grep

49
Q

In regex, what does the ‘[ ]’ wildcard do?

A

This allows you search for a list of possible values

Ex: [a, b, c]

50
Q

In regex, what does the ‘ - ‘ wildcard do?

A

This allos you search a range of values

Ex: 1-9

51
Q

In regex, what does the ‘ . ‘ wildcard do?

A

This will match any single character

52
Q

In regex, what does the ‘ * ‘ wildcard do?

A

This will match any number of characters

53
Q

In regex, what does the ‘ ^ ‘ wildcard do?

A

This will match your criteria at the beginning of a line

54
Q

In regex, what does the ‘ $ ‘ wildcard do?

A

This will match your criteria at the end of a line

55
Q

In regex, what does the ‘ | ‘ wildcard do?

A

This allows you to give a logical ‘or’

56
Q

In regex, what does the ‘( )’ wildcard do?

A

Allows you to have a sub-expression or slice

57
Q

In regex, what does the ‘ \ ‘ wildcard do?

A

This is an escape character

58
Q

Assume you ran the following command on a file - ‘wc’. What does the following output mean?

7 17 147 cal-2019.txt

A
7 = line count from the txt file
17 = word count from the txt file
147 = character count from the txt file
59
Q

What command would you use to interactively manipulate text?

A

Grep

60
Q

What command would you use in Bash scripting to manipulate text?

A

awk or sed

61
Q

What does the command ‘wc’ do?

A

This command give you a word count of the input file

62
Q

what are two command you can use to print information to stdout? What’s the difference between the two?

A

echo and printf. printf gives you more control/options

63
Q

What is the command you would use to sort information in a file?

A

sort

64
Q

What does the ‘-t’ flag do with the command sort?

A

Allows you to specify a delimiter

65
Q

What does the ‘k’ flag do with the command sort?

A

Allows you to choose a column number in the file

66
Q

What does the ‘r’ flag do with the command sort?

A

reverses the sort order (descending)

67
Q

What does the command ‘cut’ do?

A

extracts columns or lines of data from text

68
Q

What does the command ‘diff’ do?

A

Compares contents of two files for differences

69
Q

What is the command ‘awk’ used for primarily?

A

pattern scanning and text processing

70
Q

What is the command ‘sed’ used for primarily?

A

stream editor for filtering and transforming text