Friday, February 5, 2016

Week 18: Day 049 - Essential File Management Tools


Hello, today I will be continuing my blog posts, after a long snowstorm which kept us out of school for two weeks. In this chapter, I will take the exercises put it on here, and then comment on what I do. In the end, I will answer the review questions.

To understand the way that the Linux file system is organized, knowing the concept of mounting is important. Sometimes it's not a good idea to store everything in one place. It decreases system performance, and it makes it harder to make additional storage space available. So there are actually advantages to mounting. Let's look at the directories. "/boot" contains files required for booting your computer. "/var" should be put on a dedicated device, because it will take up storage on your server. "/home" is the directory which contains user directories, and should be placed on a dedicated device like /var, for security reasons. "/usr" contains the Operating System files. The mount command shows all of the mounted devices. "df -Th" shows available disk space on the mounted devices. The command "findmnt" does the same thing, except in a nicer fashion. The simplest, best command which shows mounted devices is "df -hT".

Moving on, there's also a thing called "wildcards" which I should know. The * is basically everything. If you were to do ls * it would show you every file in your working directory.

In this exercise, you learn how to work with directories.

1. Open a shell as a normal user. Type cd. Next, type pwd, which stands for print working directory. You’ll see that you are currently in your home directory, a directory with the name /home/<username>.

They were right, that happened.

2. Type touch file1. This command creates an empty file with the name file1 on your server. Because you currently are in your home directory, you can create any file you want to.

It created that file in my home directory.

3. Type cd /. This changes the current directory to the root (/) directory. Type touch file2. You’ll see a “permission denied” message. Ordinary users can create files only in directories where they have the permissions needed for this.

I was denied permission. You must be sudo to do anything in the root directory I guess.

4. Type cd /tmp. This brings you to the /tmp directory, where all users have write permissions. Again, type touch file2. You’ll see that you can create items in the /tmp directory (unless there is already a file2 that is owned by somebody else).

I'm in the temp directory. Users of all kinds can do whatever they want there, cause it's temporary!

5. Type cd without any arguments. This command brings you back to your home directory.

Quite obvious.

6. Type mkdir files. This creates a directory with the name files in the current directory. The mkdir command uses the name of the directory that needs to be created as a relative pathname; it is relative to the position you are currently in.

Already knew that.

7. Type mkdir /home/$USER/files. In this command, you are using the variable $USER, which is substituted with your current username. The complete argument of mkdir is an absolute filename to the directory files you are trying to create. Because this directory already exist, you’ll get a “file exists” error message.

What was the point of that? It already exists!

8. Type rmdir files to remove the directory files you have just created. The rmdir command enables you to remove directories, but it works only if the directory is empty and does not contain any files.

Codecademy taught me this.

Since the majority of stuff I already learned in Codecademy, I will skip a bunch of this. Continuing though, there are things called links. They create links, similarly to creating a shortcut on Windows. There are two different types of links, hard links and symbolic links.

In this exercise, you work with symbolic links and hard links:

1. Open a shell as a regular (nonroot) user.

Use screen for this!

2. From your home directory, type ln /etc/passwd .. (Make sure that the command ends with a dot!) This command gives you an “operation not permitted” error because you are not the owner of /etc/passwd.

Do sudo for this, it requires that kind of permission.

3. Type ln -s /etc/passwd .. (Again, make sure that the command ends with a dot!) This works; you do not have to be the owner to create a symbolic link.

They're right, no owner privileges needed.

4. Type ln -s /etc/hosts. (This time with no dot at the end of the command.) You’ll notice this command also works. If the target is not specified, the link is created in the current directory.

I've created a symbolic link with hosts. It highlights the directory in a certain color.

5. Type touch newfile and create a hard link to this file by using ln newfile linkedfile.

Now both words within the home directory are highlighted in blue.

6. Type ls -l and notice the link counter for newfile and linkedfile, which is currently set to 2.

7. Type ln -s newfile symlinkfile to create a symbolic link to newfile.

This worked.

8. Type rm newfile.

Interestingly, linkedfile remains, while symlinkedfile is there, but it doesn't have any file to "shortcut", so it's kind of just dead.

9. Type cat symlinkfile. You will get a “no such file or directory” error message because the original file could not be found.

True.

10. Type cat linkedfile. This gives no problem.

Already said that.

11. Type ls -l and look at the way the symlinkfile is displayed. Also look at linkedfile, which now has the link counter set to 1.

Yes, this means that linkedfile is just itself, it has no connection newfile, since newfile is dead/deleted. So basically linkedfile is a clone of newfile, and newfile still exists, but inside of linkedfile.

12. Type ln linkedfile newfile.

This brought newfile, back to life!

13. Type ls -l again. You’ll see that the original situation has been restored.

Final note, I will focus an entire blog post on Tar since I have to make a presentation about it. But for now This is where it ends. Time for Review Questions!

Review Questions

1. Which directory would you go to if you were looking for configuration files?

/etc

2. What command enables you to display a list of current directory contents, where the newest files are listed first?

ls -alt

3. Which command enables you to rename the file myfile to your file?

mv myfile yourfile

4. Which command enables you to wipe an entire directory structure, including all of its contents?

rm -rf

5. How do you create a link to the directory /tmp in your home directory?

ln -s /tmp

6. How would you copy all files that have a name that starts with a, b, or c from the directory /etc to your current directory?

cp /etc/[abc]

7. Which command enables you to create a link to the directory /etc in your home directory?

ln -s /etc ~

8. What is the safe option to remove a symbolic link to a directory?

rm symlink is the SAFEST.

9. How do you create a compressed archive of the directories /etc and /home and write that to /tmp/etchome.tgz?

tar zcvf /tmp/etchome.tgz /etc /home
10. How would you extract the file /etc/passwd from /tmp/etchome.tgz that you have created in the previous step?

tar xvf /tmp/etchome.tgz /etc/passwd

No comments:

Post a Comment