jump to navigation

Linux : lesson1-3 : The filesystem October 29, 2010

Posted by Tournas Dimitrios in Linux course.

In Linux, information and programs are stored on disks as files.To be more precisely everything is handled as a file by the Linux kernel (sockets , network-connections , mount points etc …)  Files are grouped into directories, which can contain files and other directories. (Other operating systems often refer to directories as “folders”). This hierarchy of directories containing directories is often referred to as the “directory tree”.

The base of the directory tree is a directory named simply /, and referred to as “the root directory”. Files within the root directory can be referred to as /filename. In  Linux, the root directory contains mostly other directories, such as /etc. Files within these subdirectories may be referred to as /etc/filename, and the process continues for each new level of subdirectories. For example, the file network found in the directory sysconfig, which is in turn found in the directory etc, which is in the root directory /, can be referred to as /etc/sysconfig/network.

Obviously, starting at the root directory every time you refer to a file is a lot of work. Fortunately, Linux provides an easier way. Every process, including a user’s shell, uses a “current working directory” for context. Files in a process’s current working directory can be referred to as simply filename, without the leading slash. Files in subdirectories of the current working directory can be referred to as dirname/filename, again without the leading slash. For example, if a process’s current working directory were /etc, the network file referred to above could be referred to as sysconfig/network. If the working directory was /etc/sysconfig then the file could simply be referred to as network.

In summary, there are always two ways to refer to a file. File references relative to the root directory always start with a leading /, and are called absolute references. File references relative to the current working directory start with something other than a /, and are referred to as relative references.

Listing directory contents with ls

From a shell, users use the ls command to list the contents of a directory. (Think of the ls as a shortening of the verb “list”.) In the following example, the user elvis wants to list the contents of the /etc/sysconfig/rhn directory.

[elvis@station elvis]$ ls /etc/sysconfig/rhn
rhn-applet  systemid  up2date-keyring.gpg  up2date-uuid
rhnsd       up2date   up2date.rpmnew

The ls command, when called without arguments (i.e., without specifying a directory), lists the contents of the shell’s current working directory. If using a color terminal, the ls command also colorizes the filenames to help distinguish which of the directory contents are regular files (white), and which are directories (blue).

The ls command is a very flexible command that can provide a lot of different information. It will be discussed in more detail in later lessons.

Viewing the contents of a file with cat

While the ls command lists the files contained in a given directory, it does not reveal the contents of the files. While several commands are available for viewing files, the simplest command is cat. The cat command, when given a list of files, concatenates the files to the terminal output. If given one filename, the contents of that single file is displayed as output.

In the following example, the user elvis wants to view the contents of the /etc/hosts configuration file.

[elvis@station elvis]$ cat /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.       localhost.localdomain   localhost   server1.example.com  server1     station1.example.com station1

For now, don’t worry about what the contents mean, just realize that the cat command displayed the entire contents of this 5 line file.

Note that if you ask cat to display a very long file, or a binary (non text) file, cat will happily comply. There are more sophisticated commands for browsing large files, a screen full at a time, which will be introduced later.

The less Pager

Linux uses the less pager for viewing man pages and long files. When viewing files (including man pages) in less, navigation is provided by single letter keystrokes: space is used to view the next page, b is used to go back a page, q is used to quit. less will be covered in more detail in a later lesson, but the table below summarizes some of the most useful navigation commands when viewing man pages with less.

Basic less Navigation

Command Action
space View next page
b View previous page
q Quit
/ text RETURN Search for word text
n Find next occurrence of previously used search term

Redirecting command output to files

When the previous ls and cat commands were performed, their output was displayed on the terminal. In Linux, most commands which generate text output use a common Unix concept called the “standard out” stream. By default, this stream is connected to the terminal. The bash shell allows users to “redirect” the standard out stream to other locations. For now, we will only learn the simplest case: using the > character to redirect standard out into a file.

In the following example, the user elvis is again going to list the contents of the /etc/sysconfig/rhn directory, but redirect the output into a newly created file.

[elvis@station elvis]$ ls /etc/sysconfig/rhn > lsout.txt
[elvis@station elvis]$ ls
[elvis@station elvis]$ cat lsout.txt

The output of the ls /etc/sysconfig/rhn command was not displayed on the terminal, but instead placed into the newly created file lsout.txt. elvis next takes a ls of his current working directory, and sees the newly created file. He then uses the cat command to observe the contents of the file. In “Unix speak”, elvis “redirected the output from the ls command into the file lsout.txt“.

Permissions, and a user’s home directory.

Notice what happens when elvis tries to redirect output to a file somewhere other than his shell’s current working directory.

[elvis@station elvis]$ ls /etc/sysconfig/rhn > /etc/lsout.txt
-bash: /etc/lsout.txt: No such file or directory

The user elvis has encountered another common Linux concept, that of file ownerships and permissions. elvis tried to create the new file /etc/lsout.txt, but elvis does not have permissions to create files in the /etc directory.

By default, in Red Hat Enterprise Linux, users are not able to create files just anywhere. In fact, there are only a few places where files can be created. Every user has a home directory, where they can create new files (and new subdirectories). Fortunately, when user’s log in to a Linux session, their shell uses their home directory as its current working directory. By default in Red Hat Enterprise Linux, a user’s home directory is named /home/username, where username is replaced with the user’s username.

For now, just realize users are generally only allowed to create files in their home directory.





No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s