Managing Files and Directories Flashcards
Editing files with vi Working with files Locating files Searching with grep Manipulating text
In vim, how can you open up a file to a particular line number?
vi +# filename
ex: vi +12 file.txt
In vim, how can you delete multiple lines or a range of lines?
:#,#d
ex: :1,10d
In vim, how can you move to a particular line?
:#
ex: :10
In vim, what does ‘I’ do?
Insert at the beginning of a line
In vim, what does ‘A’ do?
Insert at the end of a line
in vim, what does ‘dd’ do in command mode?
Deletes the entire line
In vim, how do you perform a search?
In command mode, type / and then search (case sensitive)
In vim, what does ‘dw’ do in command mode?
Deletes the word in front up to the cursor
When you perform a search, how can you go to the next and previous occurrence of the search?
‘n’ goes forward, and ‘N’ goes backward
How can you perform a search above your cursor?
?
How can you do a search and replace just once?
:s/find/replace
How can you do a search and replace throughout the whole file?
:%s/find/replace
How can you do a search and replace within a range in the file?
:#,#s/find/replace
What does ‘D’ do in command mode?
Deletes to the end of the line
What does ‘y’ do in command mode?
Yanks (copies)
What does ‘yy’ do in command mode?
Yanks (copies) the current line
What does ‘p’ do in command mode?
pastes
What does ‘ZZ’ do in command mode?
Exits and saves changes
What does ‘:w!’ do?
Saves overwriting the file
What does ‘:q!’ do?
Exits ignoring any changes
In vim, how can you undo an edit?
u
What does ‘:e’ do?
Switches to another file
What does ‘:e!’ do?
Switches to another file without saving current file
How can you open a file in vi and search for a specific string at the same time?
vi +/search file.txt
What does the command ‘cat’ do?
Displays file contents to stdout
What does the command ‘more’ do?
This command is the same as cat, but it is paginated
What does the command ‘less’ do?
This command is the same as ‘more’, but it allows bidirectional scrolling
What does the command ‘head’ do?
This command displays the first 10 lines.
What does the command ‘tail’ do?
This command displays the last 10 lines.
What does the command ‘tail -f’ do?
This command will display the last 10 lines of the file, but also ‘follow’ (watch) for any new messages that may appear.
What does the command ‘tail -n 5’ do?
This command will show you the last 5 lines
What does the command ‘head -n 5’ do?
This command will show you the first 5 lines
How can you copy a file to another location?
Use the command cp
What would this command do? Assume you are in your home directory.
cp /var/log/apt/history.txt history.txt
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.
If you moved a directory to another path, would you have to use the -r flag to move everything within the directory as well?
No. When you use the ‘mv’ command on a directory, everything within it is also moved.
What are two commands you can use to find files in Linux?
Find and locate
What is the difference between the commands find and locate?
find searches the drive, and locate searches the mlocate database. Locate can be much faster
What is the command you would run to find a specific file in your current directory?
find . -name
What is the command you would add to get rid of possible errors during a file system search using the command find?
2>/dev/null
What is the command you would run to find files by permission?
find -perm
What is the command you would run to find files by size?
find -size
What is the command you would run to find files by user?
find -user
Find searches in all sub-directories of the given path. How can you control how many sub-directories are searched?
-maxdepth
What does the command ‘whatis’ do?
This displays a one-line manual page description
What does the command ‘whereis’ do?
locate the binary, source, and manual page files for a command
What does the command ‘which’ do?
locates a command, however, it only returns the 1st match it finds by default
What does the command ‘type’ do?
This command displays how a command is interpreted
What is a command you could use to search with regex?
grep
In regex, what does the ‘[ ]’ wildcard do?
This allows you search for a list of possible values
Ex: [a, b, c]
In regex, what does the ‘ - ‘ wildcard do?
This allos you search a range of values
Ex: 1-9
In regex, what does the ‘ . ‘ wildcard do?
This will match any single character
In regex, what does the ‘ * ‘ wildcard do?
This will match any number of characters
In regex, what does the ‘ ^ ‘ wildcard do?
This will match your criteria at the beginning of a line
In regex, what does the ‘ $ ‘ wildcard do?
This will match your criteria at the end of a line
In regex, what does the ‘ | ‘ wildcard do?
This allows you to give a logical ‘or’
In regex, what does the ‘( )’ wildcard do?
Allows you to have a sub-expression or slice
In regex, what does the ‘ \ ‘ wildcard do?
This is an escape character
Assume you ran the following command on a file - ‘wc’. What does the following output mean?
7 17 147 cal-2019.txt
7 = line count from the txt file 17 = word count from the txt file 147 = character count from the txt file
What command would you use to interactively manipulate text?
Grep
What command would you use in Bash scripting to manipulate text?
awk or sed
What does the command ‘wc’ do?
This command give you a word count of the input file
what are two command you can use to print information to stdout? What’s the difference between the two?
echo and printf. printf gives you more control/options
What is the command you would use to sort information in a file?
sort
What does the ‘-t’ flag do with the command sort?
Allows you to specify a delimiter
What does the ‘k’ flag do with the command sort?
Allows you to choose a column number in the file
What does the ‘r’ flag do with the command sort?
reverses the sort order (descending)
What does the command ‘cut’ do?
extracts columns or lines of data from text
What does the command ‘diff’ do?
Compares contents of two files for differences
What is the command ‘awk’ used for primarily?
pattern scanning and text processing
What is the command ‘sed’ used for primarily?
stream editor for filtering and transforming text