ITS 2320 - Linux II

Week 5, Chapter 4, Managing Files

Objectives:

This lesson reviews issues relating to files under Linux. Objectives important to this lesson:

  1. File commands
  2. File ownership
  3. File access
  4. Finding files
Concepts:

This chapter is about files, rights, and file commands. It begins with a review of some file commands you should remember from Linux I.

  • ls - displays a simple list of files and directories in the current working directory
  • ls -l - displays a long list (wider, actually) with more details about the files and directories
  • ls -a - shows all files in a folder, including hidden ones
  • pwd - displays the path to the present working directory, starting at the root directory
  • file filename - The file command takes a filename as an argument. It reports what kind of file it seems to be. Especially useful for files whose actual nature is suspect.
  • mkdir - make a directory
  • cp - copy

The text provides some examples of these commands and several others. Note the refresher in this section abut wildcards. The main two are * (which matches anything, of any length, including nothing) and ? (which matches one character in the position indicated by your search command). Review this section as well.

The text mentions four compression utilities that can be used on Linux system. It warns that you typically need the same utility to decompress a file that was used to compress it, but that is not necessarily so, as discussed in this article about such products. It discusses all four from the chapter and eleven more.

  • gzip
  • bzip2
  • xz
  • zip

You may also wish to check into 7zip and rar.

The text continues with some tools for making archives. I had to read the description of the tar utility twice before I realized that a tar file does not necessarily use compression. This seems odd to me, since any such program I have ever used on a PC also made the resultant file smaller than the initial file or files. Maybe saving space is not important to some people? The video below discusses gzip, bzip2, and tar.


The text turns to a more troublesome topic on page 213, links to files and folders. Links come in two types: hard and soft. Hard links are discussed first.

Hard links are created with the ln command. They are created to give a file or folder more than one name. This should feel wrong to you. In the example in the text, a new file is created in a folder, Let's call it file1. A second filename is created in the same folder with a command like this:

ln file1 file2

Both filenames point to the same file, as can be seen with the command ls -i, which shows the inodes that filenames are connected to. In the example below, note that the two filenames I used are connected to the same inode.

example of ln command


This example shows that creation of a hard link is easy. You should review these rules for creating them:

  • The original file must exist when the link is created.
  • The new filename (the hard link) to be linked to the original file must not exist until the ln command creates it.
  • The hard link and the original file will point to the same inode and the same data.
  • The hard link does not need to exist in the same folder as the original file, but it must be on the same filesystem.

Soft links (symbolic links) are a bit trickier, because they can point to data on a different filesystem. The also have different inode numbers. In the example below, I have created a soft link with the ln -s option, and displayed the inode information.

Example of ln -s

The rules for soft links are different in three respects:

  • The inode numbers for the original file and the soft link are different.
  • The original file and the soft link point to different data.
  • The original file and the soft link can be in different filesystems.

The text continues with some advice about using links, which is welcome but not very informative. Let's try a friendly video.


As the presenter says, if you delete either the original filename or a hard link to it, you still have the other filename to access the data/executable. If you delete a soft link, you still have the original. If you delete the original filename, the soft link is useless because it points only to the original filename which no longer exists..

The next topic is file ownership, also called file permissions. You should be familiar with the chmod and chown commands.You may not have seen the chgrp command which is handy for a hacker. If you use chgrp on a file you own, you can change the assigned group to any group you belong to. A root level user, however, can change the assigned group to any group, which makes it more possible for a hacker to create a group that will be assigned rights to files as the hacker pleases.

If you are not familiar with file permissions, review the notes about them in my Linux I course.

The text also mentions some useful file commands that are not always covered.

  • which filename - reports the path to the version of a command that will be executed if the command is typed without a path qualifier
  • whereis filename - reports the path to the binary version of a command, its source files (if found), and manual pages that relate to it
  • locate filename - locate is more of a search engine that can find particular kinds of files, or search on regular expressions
  • find filename - uses regular expressions, like locate, but has switches to search for files based on last modified date and more interesting criteria
  • type filename - this one reports what kind of file (type) a file is, how it will be treated by a shell command to use it

You should review the parameters listed for locate and find as they are probably less familiar to you.