Darrius Magee

File linking in Linux

Objective

This is a useful tip for system admins we will examine local configuration files to get an idea of user accounts, group accounts, security settings, and other basic configuration information on your systems.

We will display and search a log file using various commands. We will also create a directory structure and populate with files using the touch command.

Stage 1: Displaying only the first and last lines of a log

Before we do anything we first want to switch into a user whos log we want to view. In this case we will view "user1"s logs.

Run: su - user1 (switches you to user1) whoami (shows what user you currently are)

We will read the dnf.log file located in /var/log. The dnf.log contains package information, timestamps. repository information, error messages and more. This file contains a long list of logs, to long to just read and find what you want with just your eyes. We can view the first few lines of any files with the "head" command.

Run: head /var/log/dnf.log

As you see we get a few lines of logs, mainly just debugging information. We can also review the most recent logs or logs at the end by using the "tail" command.

Run: tail /var/log/dnf.log

At the end of user1's logs we see that the debugging proccess has completed.

Stage 2: Use filter commands to make log analysis easier

The grep command matches strings of characters, making it useful to search for specific items in a long list. We will be viewing user2's logs for this example.

Run: cat /var/log/dnf.log | grep "timer"

As you see any log that contains the string "timer" will populate our terminal. You can also add the " -i " switch for grep to remove case sensitivity.

Run: cat /var/log/dnf.log | grep -i "fail"

As you can see even any string with "fail" are populated. It is a good habbit to add the " -i " switch when grepping because you may miss something you didn't know existed.

Stage 3: Manipulate files and directories

We will go over copying files, we will be using user1's account. We will copy the /opt/files to our home directory.

Run: cp -R /opt/files ~ (the -R switch copies everything in the folder files, subfolders, etc. The ~ is the home directory)

We then moved into the files location and viewed it contents. It is good practice to type ls after every movement, or manipulation of files to make sure it is doing what you told it to do.

Stage 4: Create a new directory and move specifics files into it

You can create new directories and able to copy files into them using a terminal. To make a directory use the "mkdir" command.

Run: mkdir documents

Now we will copy the contents that are in the "file" directory into the "documents" directory.

Run: cp * ../documents/ (copies everything in the directory you are currently in and place them into the documents folder)

You can also create blank files by using the "touch" command.

Run: touch File1.txt

Stage 5: Deleting the files directory and its contents as it is no longer needed

When deleting a directory we use rmdir but, this will not work if the directory has files inside.

Run: rm -rf files

The -r switch deletes everything the -f switch deletes without a confirmation BECAREFUL using this switch.

Fin.