Chapter 3 Flashcards

Managing Files, Directories, and  Text

1
Q

Explain basic commands for handling files and directories.

A

Typical basic file and directory management activities include viewing and creating files, copying and moving files, and deleting files. For viewing and creating files and directories, use the ls, touch, and mkdir commands. When needing to duplicate, rename, or move files, employ one of the mv, cp, or rsync commands. For local large file copies, the rsync utility is typically the fastest. You can quickly delete an empty directory using the rmdir utility, but for directories full of files, you will need to use the rm -r command. Also, if you need to ensure that you are removing the correct files, be sure to use the -i option on the rm utility.

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

Describe both structures and commands involved in linking files.

A

Linking files is easy to do with the ln command. However, it is important for you to describe the underlying link structure. Hard-linked files share the same inode number, whereas soft-linked files do not. Soft or symbolic links can be broken if the file they link to is removed. It is also useful to understand the readlink utility to help you explore files that have multiple links.

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

Summarize the various utilities that can be employed to read text files.

A

To read entire text files, you can use the cat, bat, and pr utilities. Each utility has its own special features. If you need to read only the first or last lines of a text file, employ either the head or tail command. For a single text line out of a file, the grep utility is useful. For reviewing a file a page at a time, you can use either the less or the more pager utility.

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

Describe how to find information on your Linux system.

A

To determine two text files’ differences, the diff utility is helpful. With this utility, you can also employ redirection and modify the files to make them identical. When you need to quickly find files on your system and want to use simple tools, the which, whereis, and locate commands will serve you well. Keep in mind that the locate utility uses a database that is typically updated only one time per day, so you may need to manually update it via the updatedb command. When simple file location tools are not enough, there are more complex searching utilities, such as find and grep. The grep command can employ regular expressions to assist in your search.

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

You are looking at a directory that you have not viewed in a long time and need to determine which files are actually directories. Which command is the best one to use?

mkdir -v
ls
ls -F
ls -i
ll
A

C. Option C will append an indicator code of / to every directory name, so therefore it is the best choice. The mkdir -v command creates a directory and lets you know whether or not it was successful, but it does not indicate directories, so option A is a wrong answer. The ls command only displays file and directory names, so option B is also a wrong answer. The ls -i command will display filenames along with their inode number, but it does not indicate directories, so option D is incorrect. While option E will work on some distributions to produce a long listing that can indicate directories, this command is not aliased to ls -l on every distribution, so therefore it is not the best command to use.

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

You are using the ls command to look at a directory file’s metadata but keep seeing metadata for the files within it instead. What command option will rectify this situation?

-a
-d
-F
-l
-R
A

B. The -d switch on the ls command will allow you to view a directory file’s metadata instead of seeing metadata for the files managed by that directory. Therefore, option B is the correct choice. Option A is a wrong answer because the -a switch forces the ls command to display hidden files, which are files starting with a dot (.). The -F switch will append an indicator code to each file but not allow you to view a directory’s metadata, so option C is a wrong choice. The -l option is already being employed because you are viewing metadata, so it does not need to be added. Therefore, option D is an incorrect answer. The -R switch allows you to view file information recursively through a directory tree, and thus option E is also a wrong choice.

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

You have just created an empty directory called MyDir. Which command did you most likely use?

mkdir -v MyDir
touch MyDir
cp -R TheDir MyDir
mv -r TheDir MyDir
rmdir MyDir
A

A. The mkdir -v command creates a directory and lets you know whether or not it was successful, so option A is the correct answer. The touch command creates blank and empty files, so option B is incorrect. The cp -R command will recursively copy an entire directory full of files to another directory. Since you do not know if the directory TheDir is empty or not, you most likely did not use this command, so option C is a wrong answer. The mv -r command will rename a directory to a new directory name. Again, you do not know if the directory TheDir is empty or not, so you most likely did not use this command, and thus, option D is also a wrong answer. Option E is an incorrect answer because the rmdir command deletes empty directories.

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

You have a file that is over 10 GB in size, and it needs to be backed up to a locally attached drive. What is the best utility to use in this situation?

readlink -f
mv
cp
scp
rsync
A

E. The rsync utility allows you to perform fast local copies, so for a big file it is the best utility to use in this case. Therefore, option E is the correct answer. The readlink -f command finds the file being pointed to via a series of soft links, so option A is an incorrect answer. The mv command will rename a file instead of creating a backup copy, so option B is incorrect. The cp command does create a file copy. However, it is not as fast as the rsync utility and therefore is not the best choice, making option C a wrong answer. The scp command also creates a file copy; however, it also is not as fast as the rsync utility and therefore is not the best choice, making option D a wrong answer.

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

A long-time server administrator has left the company, and now you are in charge of her system. Her old user account directory tree, /home/Zoe/, has been backed up. Which command is the best one to use to quickly remove her files and still indicate that you are removing the correct directory, but without forcing you to confirm every file deletion?

cp -R /home/Zoe/ /dev/null/
mv -R /home/zoe/ /dev/null/
rm -Rf /home/Zoe/
rm -ri /home/Zoe/
rm -rI /home/Zoe
A

E. The rm -rI command will recursively delete the files in the /home/Zoe directory tree, and it will ask before it starts, so you know you are deleting the correct tree. Therefore, option E is the best answer. Option A is incorrect because the cp command simply copies files; it does not remove them. Option B is incorrect because not only is part of the directory name using the wrong case, but there is no verification the correct directory is being moved to the black hole device, /dev/null/. The rm -Rf command would work, but it is not the best command to use because it does not ask before it starts, so you do not know if you are deleting the correct tree. In fact, the -f option suppresses error messages, so option C is wrong. Option D would also work, but it is not the best answer because it employs the -i option. If Zoe has years of files in her home directory, you may be sitting there for a long time deleting files due to the fact that you must confirm each file before it is deleted. Therefore, option D is an incorrect answer.

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

There is a large directory structure that needs to be renamed. What mv command options should you consider employing? (Choose all that apply.)

-f
-i
-n
-r
-v
A

B, C, E. When renaming a directory, you only need to employ the mv command. However, it is wise to use the -i option, so if the new directory name already exists, the utility will stop and ask permission prior to proceeding. Even better is to use the -n option; that way, if the new name you select is already being used, the mv command does not allow you to overwrite it. Also, the -v option is worthwhile, so the mv command operates in verbose mode, telling you what is going on. Therefore, options B, C, and E are all correct choices. The -f option is not a wise choice because it forces the directory’s renaming, even if a directory already exists that has that name. Therefore, option A is a wrong answer. Also, there is no -r switch, because renaming a directory using the mv command does not require any recursive action. Thus, option D is an incorrect choice.

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

You are trying to decide whether to use a hard link or a symbolic link for a data file. The file is 5 GB, has mission-critical data, and is accessed via the command line by three other people. What should you do?

Create a hard link so that the file can reside on a different filesystem for data protection.
Create three hard links and provide the links to the three other people for data protection.
Create three symbolic links and protect the links from the three other people for data protection.
Create a symbolic link so that the file can reside on a different filesystem.
Create a symbolic link so that the links can share an inode number.
A

B. Option B is the correct answer because the hard links will prevent the three other command-line interface users from accidentally deleting the data. If they delete their link’s name, they will not delete the data. Option A is an incorrect choice because hard-linked files must reside on the same filesystem together. Option C is also an incorrect choice because if you do not provide the symbolic links to the other three data users, they will have to access the data file directly and could delete it. While creating symbolic links will protect the data by letting it reside on a different filesystem, if it is mission-critical data, the filesystem employed should be rigorous enough to protect the data, and therefore your only threat would be human. Thus, option D is an incorrect answer. Symbolic linked files do not share an inode number. Therefore, option E is an incorrect choice.

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

A short text-based control file is no longer working properly with the program that reads it. You suspect the file was accidentally corrupted by a control code update you performed recently, even though the file’s control codes are all correct. Which command should you use next on the file in your problem investigation?

cat -v
cat -z
cat -n
cat -s
cat -E
A

A. The cat -v command will show any nonprinting characters that may have gotten embedded in the control file causing it to be corrupted, and therefore option A is the correct answer. The -z option only lets you see end-of-line characters if they are NULL, and thus, option B is a wrong choice. The -n option only numbers the text lines in output, so option C is also a wrong answer. The cat -s command will display multiple blank lines in the file as one blank line. This will not help in the investigation, so option D is an incorrect answer. The -E option displays a $ whenever a newline linefeed is encountered, and while possibly helpful, it is not the best option to use in this case. Therefore, option E is a wrong answer.

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

You have two short text files that have maximum record lengths of 15 characters. You want to review these files side by side. Which of the following commands would be the best to use?

pr -m
pr -tl 20
cat
pr -mtl 20
pr -ml 20
A

D. Option D is the correct answer because the best command to use is the pr -mtl 20 command. This will display the files side by side, remove any file headers or trailers, and set the page length to 20 lines so the files do not scroll off your screen. Of course, you may need to adjust the line length depending on your screen’s resolution. Option A is incorrect because, while it will display the files side by side, the display may scroll off your screen. Option B is also incorrect, because the command will not display your files side by side. Option C is a wrong answer choice because the cat command will not display the files side by side. Option E’s command may work for you, but it is not the best choice because file headers or trailers will not be removed. Therefore, option E is an incorrect answer.

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

You have a lengthy file named FileA.txt. What will the head -15 FileA.txt command do?

Display all but the last 15 lines of the file.
Display all but the first 15 lines of the file.
Display the first 15 lines of the file.
Display the last 15 lines of the file.
Generate an error message.
A

C. The head command can use either the -n 15 switch or the -15 switch to display a file’s first 15 lines. Therefore, option C is the correct answer. To display all but the last 15 lines of the file, you would need to employ the -n -15 switch, so option A is incorrect. To display all but the first 15 lines, you need to use the tail command instead of the head command, so option B is a wrong answer. Also, you need to use tail to display the last 15 lines of the file, so option D is also an incorrect answer. Option E is a wrong choice because the command will not generate an error message in this case.

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

You have issued the command grep Hal on a text file you generated using information from a failed login attempts file. It returns nothing, but you just performed a test case by purposely failing to log into the Hal account prior to generating the text file. Which of the following is the best choice as your next step?

Employ the tail command to peruse the text file.
Employ the cat command to view the text file.
Delete the text file and regenerate it using information from the failed login attempts file.
Issue the grep -d skip Hal command on the text file.
Issue the grep -i Hal command on the text file.
A

E. It is possible that the account name Hal is listed in the generated text file as hal. Therefore, your best next step is to employ the -i option on the grep command. This will quickly search the text file for the word Hal while ignoring case. Thus, option E is the correct answer. Option A is a wrong choice because the tail command will not allow you to search the file. Option B is also an incorrect answer, because the cat command will just display the entire text file to the screen and is not an efficient method for finding a few text lines. While you may end up having to regenerate the text file, prior to doing so you should check for Hal, ignoring case. If you find the records, then you have saved yourself some time. Thus, option C is also a wrong choice. Finally, the -d skip option on the grep command allows the search to skip any directory files. This option is useless in this situation, and therefore option D is an incorrect answer.

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

You are trying to peruse a rather large text file. A coworker suggests you use a pager. Which of the following best describes what your coworker is recommending?

Use a utility that allows you to view the first few lines of the file.
Use a utility that allows you to view one text page at time.
Use a utility that allows you to search through the file.
Use a utility that allows you to filter out text in the file.
Use a utility that allows you to view the last few lines of the file.
A

B. A pager utility allows you to view one text page at a time and move through the text at your own pace. Therefore, option B is the correct answer. A utility that only allows you to view the first few lines of a file would not be useful in this case, and these utilities are not called pagers. Therefore, option A is a wrong answer. While the less utility is a pager and will allow you to search through the text file, the coworker mentioned pagers, which includes the more utility. With the more utility you cannot search through text, so option C is an incorrect choice. You do not need to filter out text in the file, and filter utilities are not called pagers, so option D is a wrong answer. A utility that only allows you to view the last few lines of a file would not be useful in this case, and these utilities are not called pagers. Therefore, option E is an incorrect choice.

17
Q

Which of the following does not describe the less utility?

It does not read the entire file prior to displaying the file's first page.
You can use the up and down arrow keys to move through the file.
You press the spacebar to move forward a page.
You can use the Esc+V key combination to move backward a page.
You can press the X key to exit from the utility.
A

E. You need to use the q key to exit from the less pager utility; therefore, only option E does not describe less and is the correct answer. Option A is a wrong answer because less does not read the entire file prior to displaying the file’s first page. You can also employ the up and down arrow keys to traverse the file as well as the spacebar to move forward a page and the Esc+V key combination to move backward a page, so options B, C, and D are incorrect answers.

18
Q

Which diff option is the best option to allow you to quickly determine if two text files are different from one another?

-e
-q
-s
-W
-y
A

B. The -q (or –brief) option used with the diff command will allow you to quickly determine if two text files are different from each other. Thus, option B is the correct answer. The -e switch allows you to create an ed script to use later on, so option A is an incorrect choice. The -s option does allow you to see if the files are the same and shows a simple message stating this fact. However, it is focused on file differences, so it is not the best switch to use. Therefore, option C is also a wrong answer. The diff command’s -W option allows you to set a display width, and thus, option D is an incorrect choice. The -y switch displays the two files in two columns, so option E is also a wrong selection.

19
Q

You are working on a Linux server at the command line, and you try to issue a diff command and receive a response stating that the command was not found. What is the next best step to take in order to start the troubleshooting process?

Hit your up arrow key and press Enter.
Log out, log back in, and retry the command.
Enter the which diff command.
Enter the whereis diff command.
Reboot the server and retry the command.
A

C. Option C is the correct answer because the which command will allow you to quickly see the location of the program you provide as an argument. If you see no response, you can go on to the next troubleshooting step of determining if the program is not installed. Option A is not correct because these actions will simply recall the original diff command and try it again. Logging out and then back in again may reset some variables you accidentally created, but it is not a good first step in this troubleshooting process. Therefore, option B is a wrong answer. Entering the whereis diff command will provide additional information concerning the diff command, such as program location and source code file locations as well as man page locations. This additional information is not needed, so option D is an incorrect choice. Rebooting a server just because a command does not work is not a good first troubleshooting step. Therefore, option E is also a wrong answer.

20
Q

You are trying to find a file on your Linux server whose name is conf. Employing the locate conf command for your search shows many directories that contain the letters conf. What is the best description for why this is happening?

The locate utility searches for only for directory names.
You did not employ the -d skip switch.
It is most likely because the locate database is corrupted.
You did not employ the appropriate regular expression.
It is due to file globbing on the pattern name.
A

E. By default, the locate command uses file globbing, which adds wildcards to the pattern you enter. Thus, conf is turned into conf. Therefore, option E best explains the results and is the correct answer. The locate command will search for both file and directory names for specified patterns unless options are provided to modify this behavior. Therefore, option A is an incorrect answer. The locate command does not use the -d skip switch (the grep command does use it, though), and thus, option B is a wrong answer. Because the command operated normally, there is not a problem with the locate database, so option C is an incorrect choice. Also, a regular expression switch was not used in the locate command, so option D is also a wrong choice.

21
Q

You downloaded a large important file, fortytwo.db, from your company’s local website to your Linux server but got interrupted by an emergency. Now you cannot remember where you stored the file. What is the best first step to fixing this problem?

Issue the sudo updatedb command.
Issue the locate -b fortytwo.db command.
Issue the locate -b 'fortytwo.db' command.
Download the file from the company's local website again.
Issue the locate fortytwo.db command.
A

A. The locate utility searches the mlocate.db database, which is typically only updated one time per day via a cron job. Therefore, for this newly created file, the first thing you should do is update mlocate.db via the updatedb command, using super user privileges. Thus, option A is the correct answer. After you have updated the database, any of the commands in option B, C, or E should work fairly well, with option B’s command being the best choice. However, for the first step, options B, C, and E are wrong answers. Downloading the file again is tedious and time-consuming and can possibly consume disk space unnecessarily. Therefore, option D is an incorrect choice.

22
Q

You want to search for a particular file, main.conf, using the find utility. This file most likely is located somewhere in the /etc/ directory tree. Which of the following commands is the best one to use in this situation?

find -r /etc -name main.conf
find / -name main.conf
find /etc -maxdepth -name main.conf
find /etc -name main.conf
find main.conf /etc
A

D. When using the locate command, the path argument is listed first, which is a starting point directory. The find utility will search through that directory and all its subdirectories (recursively) for the file or files you seek. Also, the -name switch allows you to search for a file by name, so option D is the correct answer. Option A is incorrect because there is no -r switch, and no need for one. Option B is not the best command to use in this case because the starting directory is /, which is the root of the virtual directory structure. It is much better to start at the /etc directory, since the file is most likely located somewhere in that directory tree. Using the -maxdepth switch may hamper the search because it sets the subdirectory level to stop the search. Therefore, option C is a wrong answer. Option E is an incorrect choice, because the path and filename are flip-flopped and the -name switch is missing.

23
Q

Yesterday a coworker, Michael, was fired for nefarious behavior. His account and home directory were immediately deleted. You need to audit the server to see if he left any files out in the virtual directory system. Which of the following commands is the best one to use in this situation?

find / -name Michael
find / -user Michael
find / -mmin 1440
find ~ -user Michael
find / -nouser
A

E. The find / -nouser command will search through the entire virtual directory structure looking for any files that do not have a username associated with them. Since Michael’s account and home directory were deleted, any files he owned out in the virtual directory structure will not have a username associated with them, only a user ID (UID). Thus, option E is the best answer. Option A is incorrect because the -name switch is for file names, not usernames. Option B is also an incorrect answer, because the -user switch is used to search for files owned by a particular account. Since Michael’s account was deleted, his username would no longer be associated with any files. Option C is a wrong answer because you do not know when his files may have experienced data changes, as indicated by the -mmin switch, and thus this is a bad method for trying to identify them. Option D is an incorrect choice because the find command is starting the search process in the user’s home directory instead of the root (/) of the virtual directory structure.

24
Q

You need to figure out what configuration file(s) hold a hostname directive. Which of the following commands is the best one to use?

which
whereis
grep
locate
find
A

C. The grep utility will allow you to search file contents quickly and effectively. Therefore, option C is the correct answer. The which utility can help you locate a program’s location by its name, but it does not search its contents, so option A is an incorrect choice. The whereis command will search for a file’s program location, source code files, and man pages, but it cannot search a file’s contents, so option B is also a wrong choice. The locate utility will search for a file’s location using its name, but it cannot search a file’s contents, so option D is an incorrect answer. The find command can find files using a file’s metadata, but it does not search inside a file, so option E is a wrong choice.

25
Q
A