Chapter 3 Flashcards
Managing Files, Directories, and Text
Explain basic commands for handling files and directories.
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.
Describe both structures and commands involved in linking files.
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.
Summarize the various utilities that can be employed to read text files.
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.
Describe how to find information on your Linux system.
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.
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
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.
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
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.
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. 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.
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
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.
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
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.
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
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.
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.
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.
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. 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.
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
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.
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.
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.
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.
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.