Friday, 7 April 2017

Linux Tutorial : Working with Files and Directories Part 1


Introduction

When working in a Linux Operating System, you will need to know how to manipulate files and directories. Some Linux distributions have GUI-based applications that allow you to manage files, but it is important to know how to perform these operations via the command line.
The command line features a rich collection of commands that allow you to manage files. In this chapter you will learn how to list files in a directory as well as how to copy, move and delete files.
The core concepts taught in this chapter will be expanded in later chapters as more file manipulation commands are covered, such as how to view files, compress files and set file permissions.

Understanding Files and Directories

Files are used to store data such as text, graphics and programs. Directories (AKA, "folders") are used to provide a hierarchical organization structure. This structure is somewhat different than what you might be used to if you have previously worked on Microsoft Windows systems.
On a Windows system, the top level of the directory structure is called My Computer. Each physical device (hard drive, DVD drive, USB thumb drive, network drive, etc.) shows up under My Computer, each assigned a drive letter, such as C: or D:. A visual representation of this structure:
The directory structures shown below are provided as examples only. These directories may not be present within the virtual machine environment of this course.
Like Windows, a Linux directory structure has a top level, however it is not called My Computer, but rather the root directory and it is symbolized by the /character. There are also no drives in Linux; each physical device is accessible under a directory, not a drive letter. A visual representation of a typical Linux directory structure:
This directory structure is called the filesystem by most Linux users.
To view the root filesystem, type ls /:
sysadmin@localhost:~$ ls /
bin dev home lib media opt root sbin selinux sys usr
boot etc init lib64 mnt proc run sbin??? srv tmp var
Notice that there are many descriptive directories including /boot, which contains files to boot the computer.

1 Directory Path

Using the graphic in the previous section as a point of reference, you will see that there is a directory named sound under a directory named etc, which is under the / directory. An easier way to say this, is to refer to the path.
Consider This:
The /etc directory originally stood for “et cetera” in early documentation from Bell Labs and used to contain files that did not belong elsewhere. In modern Linux distributions, the /etc directory typically holds static configuration files as defined by the File Hierarchy Standard (FHS).
A path allows you to specify the exact location of a directory. For the sounddirectory, the path would be /etc/sound. The first / character represents the root directory, while each other / character is used to separate the directory names.
This sort of path is called an absolute path. With an absolute path, you always provide directions to a directory (or a file) starting from the top of the directory structure, the root directory. Later in this chapter, we will cover a different sort of path called a relative path.
Note: The directory structures shown below are provided as examples only. These directories may not be present within the virtual machine environment of this course.
The following graphic demonstrates three additional absolute paths:

2 Home Directory

The term home directory often causes confusion to beginning Linux users. To begin with, on most Linux distributions there is a directory called home under the root directory: /home.
Under this /home directory there will be a directory for each user on the system. The directory name will be the same as the name of the user, so a user named "bob" would have a home directory called /home/bob.
Your home directory is a very important directory. To begin with, when you open a shell, you should automatically be placed in your home directory, as this is where you will do most of your work.
Additionally, your home directory is one of the few directories where you have the full control to create and delete additional files and directories. Most other directories in a Linux filesystem are protected with file permissions, a topic that will be covered in detail in a later chapter.
On most Linux distributions, the only users who can access any files in your home directory are you and the administrator on the system (the root user). This can be changed by using file permissions.
Your home directory even has a special symbol that you can use to represent it: ~. If your home directory is /home/sysadmin, you can just type ~ on the command line in place of /home/sysadmin. You can also refer to another user's home directory by using the notation ~user, where user is the name of the user account whose home directory you want to refer to. For example, ~bobwould be the same as /home/bob. Here, we will change to the user's home directory:
sysadmin@localhost:~$ cd ~
sysadmin@localhost:~$ ls
Desktop Documents Downloads Music Pictures Public Templates
Videos
sysadmin@localhost:~$
Note that a listing reveals subdirectories contained in the home directory. Changing directories requires attention to detail:
sysadmin@localhost:~$ cd downloads
-bash: cd: downloads: No such file or directory
sysadmin@localhost:~$
Why did the command above result in an error? That is because Linux environments are case sensitive. Changing into the Downloads directory requires the correct spelling - including the capital D:
sysadmin@localhost:~$ cd Downloads
sysadmin@localhost:~/Downloads$

3 Current Directory

Your current directory is the directory where you are currently working in a terminal. When you first open a terminal, the current directory should be your home directory, but this can change as you explore the filesystem and change to other directories.
While you are in a command line environment, you can determine your current directory by using the pwd command:
sysadmin@localhost:~$ pwd
/home/sysadmin
sysadmin@localhost:~$
Additionally, most systems have the default user prompt display the current directory:
[sysadmin@localhost ~]$
In the graphic above, the ~ character indicates your current directory. As mentioned previously, the ~ character represents your home directory.
Normally the prompt only displays the name of the current directory, not the full path from the root directory down. In other words, if you were in the /usr/share/doc directory, your prompt will likely just provide you with the name doc for the current directory. If you want the full path, use the pwd command.

4 Changing Directories

If you want to change to a different directory, use the cd (change directory) command. For example, the following command will change the current directory to a directory called /etc/sound/events:
sysadmin@localhost:~$ cd /etc/sound/events
sysadmin@localhost:/etc/sound/events$
Note that there is no output if the cd command is successful. This is one of those "no news is good news" type of things. If you try to change to a directory that does not exist, you will receive an error message:
sysadmin@localhost:/etc/sound/events$ cd /etc/junk
-bash: cd: /etc/junk: No such file or directory
sysadmin@localhost:/etc/sound/events$
If you want to return to your home directory, you can either type the cdcommand with no arguments or use the cd command with the ~ character as an argument:
sysadmin@localhost:/etc/sound/events$ cd
sysadmin@localhost:~$ pwd
/home/sysadmin
sysadmin@localhost:~$ cd /etc
sysadmin@localhost:/etc$ cd ~
sysadmin@localhost:~$ pwd
/home/sysadmin
sysadmin@localhost:~$

5 Absolute vs. Relative Pathnames

Recall that a pathname is essentially a description of where a file or directory is located in the filesystem. You can also consider a pathname to be directions that tell the system where to find a file or directory. For example, the cd /etc/perl/Net command means "change to the Net directory, that you will find under the perl directory, that you will find under the etc directory, that you will find under the / directory".
When you give a pathname that starts from the root directory, it is called an absolute path. In many cases, providing an absolute path makes sense. For example, if you are in your home directory and you want to go to the /etc/perl/Net directory, then providing an absolute path to the cd command makes sense:
sysadmin@localhost:~$ cd /etc/perl/Net
sysadmin@localhost:/etc/perl/Net$
However, what if you were in the /etc/perl directory and you wanted to go to the /etc/perl/Net directory? It would be tedious to type the complete path to get to a directory that is only one level below your current location. In a situation like this, you want to use a relative path:
sysadmin@localhost:/etc/perl$ cd Net
sysadmin@localhost:/etc/perl/Net$
A relative path provides directions using your current location as a point of reference. Recall that this is different from absolute paths, which always require you to use the root directory as a point of reference.
There is a handy relative path technique that you can use to move up one level in the directory structure: the .. directory. Regardless of which directory you are in, .. always represents one directory higher than your current directory (with the exception of when you are in the / directory):
sysadmin@localhost:/etc/perl/Net$ pwd
/etc/perl/Net
sysadmin@localhost:/etc/perl/Net$ cd ..
sysadmin@localhost:/etc/perl$ pwd
/etc/perl
sysadmin@localhost:/etc/perl$
Sometimes using relative pathnames are a better choice than absolute pathnames, however this is not always the case. Consider if you were in the /etc/perl/Net directory and then you wanted to go to the /usr/share/docdirectory. Using an absolute pathname, you would execute the cd /usr/share/doc command. Using relative pathnames, you would execute the cd ../../../usr/share/doc command:
sysadmin@localhost:/etc/perl/Net$ cd
sysadmin@localhost:~$ cd /etc/perl/Net
sysadmin@localhost:/etc/perl/Net$ cd /../../../usr/share/doc
sysadmin@localhost:/usr/share/doc$ pwd
/usr/share/doc
sysadmin@localhost:/usr/share/doc$
Note: Relative and absolute paths are not just for the cd command. Any time you specify a file or a directory you can use either relative or absolute paths.
While the double dot (..) is used to refer to the directory above the current directory, the single dot (.) is used to refer to the current directory. It would be pointless for an administrator to move to the current directory by typing cd .(although it actually works). It is more useful to refer to an item in the current directory by using the ./ notation. For instance:
sysadmin@localhost:~$ pwd
/home/sysadmin
sysadmin@localhost:~$ cd ./Downloads/
sysadmin@localhost:~/Downloads$ pwd
/home/sysadmin/Downloads
sysadmin@localhost:~/Downloads$ cd ..
sysadmin@localhost:~$ pwd
/home/sysadmin
sysadmin@localhost:~$
Note: This use of the single dot (.) as a reference point is not to be confused with using it at the beginning of a filename. Read more about hidden files in Section 6.4.2.














































































Load disqus comments

0 comments