LINUX - MANAGING FILES AND DIRECTORIES
LINUX - MANAGING FILES AND DIRECTORIES
Creating Text Files
Enter vim software_list.txt to open the file. Remember that Vim opens in Command mode.
Use the arrow keys to move the cursor down to the first instance of the text "Friefox".
Position the cursor under the "i" in "Friefox".
Press x to cut the letter "i".
Move the cursor under the letter "F" and press p to paste the cut letter.
Verify that the line now correctly says "Firefox".
Use the search functionality built into Vim to find and correct the other instance of the spelling error
Enter "/Frie" to search for the next occurrence of the misspelled name.
You use the / to create a command prompt at the bottom of Vim and then you enter the text you wish to search for.
Correct the name so that it says "Firefox".
Fix the casing of one of the software names
Press k to go up line-by-line until you reach the line that says "openVAS" (note the lowercase "o").
Press ^ (Shift+6) to go to the beginning of the line.
Press x to delete the first letter.
Press i to enter Insert mode, then type an uppercase O
Press Esc to exit Insert mode and return to Command mode
Delete a duplicate line and save the file
Press j to go down line-by-line until you reach the second line that mentions "Apache".
Press d twice to delete the entire line.
Enter :wq to write your changes and quit the file
Open the file in GNU nano and make a correction
Enter nano software_list.txt to open the file in the nano text editor.
The nano text editor is common on many Linux distros. You should know the basics of both Vim and nano.
Use the arrow keys to move to the "Y" under the "Configured?" column for "Eclipse".
Press Delete.
Type N
Remove a duplicate line
Navigate down to the second instance of "LibreOffice".
Press Ctrl+K to cut the duplicate line.
Note that some of the most common key commands for nano are displayed at the bottom of the window. There are many other key commands as well.
Add another entry to the file
Navigate to the beginning of a new line at the bottom of the file.
Type Apache-Tomcat
Press Tab until the cursor is under the "Version" column.
You can also use the spacebar for more precise alignment.
Type 9.0.12
Place the cursor under the "Installed?" column and type N
Type N under the "Configured?" column.
Press Ctrl+O "write out" or save you changes to the file.
Press Enter to save the file.
Press Ctrl+X to exit GNU nano.
Enter cat software_list.txt to display the file.
Searching for Files
Search for the location of system log files.
Enter sudo find / -type d -name 'log' to search the root of the filesystem / for a directory d with a name that includes the string log.
Verify that there are several locations on the root volume that contain the word "log".
Enter sudo find / -type f -name 'messages' to search the root of the filesystem / for a file f with a name that includes the string messages.
Verify that the location of the messages log is identified as /var/log/messages
Enter sudo find /var/log -type f -size +100k to search for log files that are greater than 100 KB in size.
Enter sudo find /var/log -type f -size 0 -or -size +100k -mmin -30 to search for log files that are either empty or above 100 KB, and have been updated in the last 30 minutes.
Verify that these conditions are accurate for at least one of the files.
Manipulating Files and Directories
Enter cp -r /opt/linuxplus/managing_files_and_directories/aups ~ to copy the aups directory to your home directory.
This copies the aups directory to your home directory, leaving its original location intact.
Enter cd aups to change directories.
Enter ls -l and verify there are five files, three of which are marked as "OLD" and have inconsistent file names.
Create a new directory and move the most recent policy files into it
Enter mkdir ../policies to create a new directory.
Enter mv aup_v1.txt ../policies to move the file.
Enter mv aup_v2.txt ../policies to move the file.
Enter ls -l and verify that these two files are no longer in this directory.
Enter cd ../policies to change to the policies directory.
Enter ls -l and verify that the two recent files are now in this directory.
Create placeholder files for future policies
Enter touch user_sec_policy.txt to create a new empty file named user_sec_policy.txt
Enter ls -l and verify that a blank file with this name was created.
Use touch to create three more blank files in ~/policies with the following names:
server_sec_policy.txt
email_policy.txt
clean_desk_policy.txt
Enter ls -l and verify that the files exist.
Delete the aups directory and its contents as it is no longer needed
Enter rmdir ../aups
Verify that you cannot remove this object because it is a directory with contents.
You need to specify the -R (recursive) option with rm in order to delete non-empty directories.
Enter rm -R ../aups to delete the directory and its contents.
Enter ls .. and verify that the aups directory is gone, as are the old policy files.
Processing Text Files
Sort the software list file by name, then by which packages need to be installed and/or configured.
Enter cd ~ to return to your home directory.
Enter cat software_list.txt to review the column structure of this file.
Enter sort -k1 software_list.txt
Verify that the list was sorted by the first column, which is the name of each software package. However, the sort operation was not perfect, as the column headers were included. There are several ways to stop this from happening, one of which you'll perform in a later topic.
Enter sort -k3 software_list.txt to sort by the "Installed?" column.
Sort by the "Configured?" column.
Retrieve the word count of the AUP files.
Enter cd policies to change directories.
Enter wc -w aup_v1.txt
Verify that you can see the word count of version 1 of the AUP policy file.
Enter wc -w aup_v1.txt aup_v2.txt
Verify that you can see the word counts of both versions of the file, as well as a combined total.
Enter diff aup_v1.txt aup_v2.txt to display the differences between the two files.
Verify that you are presented with the differences between each file, as well as suggested actions.
The differences are as follows:
33a34,41 means that after line 33 in the first file (version 1), lines 34–41 from the second file (version 2) need to be added in order for that chunk of text to be the same.
The multiple > symbols indicate each line that must be added to the first file in order to be the same as the second file.
In other words, the HR lead added this entire new section to version 2 of the policy.
35a44 means that at line 35 in the first file, line 44 from the second file needs to be added in order for the text to be the same.
In other words, the HR lead added an entry to the revision history explaining her changes.
Search the authentication log for failed login attempts.
Enter sudo cat /var/log/secure to display the contents of the secure log file.
Verify that there are many entries in the authentication log.
Rather than read the entire log or search term-by-term for failure entries, you can use grep to bring all of the relevant information to the forefront with one command.
Enter su - ariley and provide an incorrect password to simulate an authentication failure.
Do not actually sign in. The purpose of this step is to generate a message in the log file.
Enter sudo grep failed /var/log/secure to search for the string failed in the secure log file.
Verify that you are presented with all lines in the log containing the text string "failed".
Enter sudo ln auth/secure year/secure
This creates a hard link to the file in the auth directory.
Enter ls -l year and verify that a file was created in the year directory.
Enter sudo cat year/secure and verify that its contents are the same as the authentication log.
You can run diff auth/secure year/secure if you want to be sure.
Make a change in one file and see it reflected in the hard link file
Enter sudo nano auth/secure
Press Enter to start a new line at the top.
Type BEGIN LOG ##-#### where the hashes are the current month and year.
For example: BEGIN LOG 01-2019
Press Ctrl+O then ENTER to save.
Press Ctrl+X to quit.
Enter sudo head year/secure and verify that the header you just added was also added to the hard link file.
Remove one file and verify that the hard link is still intact
Enter sudo rm auth/secure
Enter sudo cat year/secure and verify that the hard link file's contents are still intact.
Attempt to create a link from your home directory to a log file in the backup directory
Enter cd ~ to return to your home directory.
Enter sudo ln /backup/log/year/secure auth-log
Verify that the operation failed.
You cannot create hard links across different file systems, and the home directory and the backup log directory are on different file systems. To get around this, you must create a soft (symbolic) link.
Create a symbolic link to the log file
Enter ln -s /backup/log/year/secure auth-log
Enter sudo cat auth-log and verify that your link has the expected log contents.
Delete the original log file and verify that the symbolic link was affected
Enter sudo rm /backup/log/year/secure
Enter sudo cat auth-log and verify that so such file exists.
Enter ls -l and verify that the file is a broken link.
You should see red text pointing to text with a black background indicating that the link is broken.
Enter rm auth-log to delete the symbolic link.
se output redirection to start adding text to the laptop inventory file.
Enter touch laptop_inv.txt to create a blank file.
Enter echo "User Make Serial No." > laptop_inv.txt
Separate each column by four spaces.
Enter cat laptop_inv.txt and verify that the text output to the file.
Use output redirection to append text to the file.
Enter echo "jsmith Asus S489124" > laptop_inv.txt
Enter cat laptop_inv.txt and verify that the header was replaced by this new row.
This is because the > operator replaces any existing text with the provided string. You need to append that text.
Reenter echo "User Make Serial No." > laptop_inv.txt
Remember, you can press the Up Arrow to return to a command you previously entered.
Enter echo "jsmith Asus S489124" >> laptop_inv.txt
Again, separate each column by four spaces.
This time, you're using the append operator >>.
Verify that the file has both the header and the first row.
Use input redirection to replace all instances of a mistyped character in the file.
Enter cp /opt/linuxplus/managing_files_and_directories/laptop_inv.txt laptop_inv.txt
This will update your copy with a filled-in one.
Examine the file and verify that the Asus serial numbers incorrectly start with the capital letter "S".
Enter tr S 5 < laptop_inv.txt
Verify that the instances of "S" were replaced with "5" and that the file was printed to the CLI.
Use both input and output redirection at the same time to create a new file with the corrections.
Enter tr S 5 < laptop_inv.txt > laptop_inv_fix.txt
Examine the corrected file and verify that the appropriate correction was made.
Use piping to sort the inventory list without the header.
Enter sort -k1 laptop_inv_fix.txt to sort the contents.
Observe that, just like sorting the software list earlier, the header is included in the sort when it shouldn't be.
Enter tail -n +3 laptop_inv_fix.txt | sort -k1
The tail -n +3 command outputs everything after and including the third line, which is when the header ends. You are piping the output of this command to the sort command, which takes it as input.
Verify that the inventory is now sorted by user name, but does not include the header.
Use the tee command to redirect output to both the CLI and a file at the same time.
Enter sudo ls -lR /backup > backup_report
Verify that ls didn't print its results to the CLI.
Enter sudo ls -lR /backup | tee backup_report
Verify that ls did print its results to the CLI.
This is because piping the ls command to tee instead of doing a stdout redirect ensures that the results will appear at both the CLI and the specified file.
Examine the backup_report file and verify that it also lists directory information.
Use grep and cut together to make log analysis easier.
Enter sudo grep 'password check failed' /var/log/secure
This prints all instances of the text "password check failed" from the authentication log. However, it also prints every single part of the line, much of which isn't relevant and just adds to the noise.
Enter sudo cut /var/log/secure -d " " -f5-12
The cut command, using the -d option, trims each line using a space as a delimiter. The -f5-12 option specifies the range of the delimiter to extract. So, you're only extracting approximately the middle chunk of each line. However, you're still seeing every line of the log.
Enter sudo grep 'password check failed' /var/log/secure | cut -d " " -f5-12
Verify that you extracted all lines matching the provided string, as well as only the portion of the line that is relevant to your needs.
Comments
Post a Comment