You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

324 lines
39 KiB

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My Learning Website</title>
<link href="/styles/styles.css" rel="stylesheet" type="text/css">
<link href="/linux/styles/styles.css" rel="stylesheet" type="text/css">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="banner">
<h1 class="courselink">Learning Linux Command Line</h1>
<h2 class="lecturer">LinkedIn Learning : Scott Simpson</h2>
<h2 class="episodetitle">Files, Folders and Permissions</h2>
</div>
<article>
<h2 class="sectiontitle">Files, Folders and Navigation</h2>
<p>There are two useful commands, file and stat, which allow us to obtain more information on a file. To demonstrate this, I will look at some of the files in the Exercise Files folder which we had placed inside the Documents folder which itself is inside my Home folder.</p>
<p>Starting from my Home folder, I will cd into the Documents folder. The Exercise Files folder is actually in another folder called commandlinebasics and I can cd into that directory easily enough.</p>
<p>Cd .. Now, from here I would like to cd into the Exercise Files folder but the space in the name gives me a problem. If I try to type</p>
<pre class="inset">$cd Exercise Files</pre>
<p>I get an error stating that there are two many arguments and this is because the name of the directory consists of two words and this makes the shell think I am providing two directory names. There are two ways to avoid this problem. I could either put the name of the directory in quotes (single or double both work) as in:</p>
<pre class="inset">$cd “Exercise Files”</pre>
<p>or I can use an escape character to represent the space as in:</p>
<pre class="inset">$cd Exercise/ Files</pre>
<p>Actually, the tab autocomplete is useful here because it allows me to type just</p>
<pre class="inset">$cd Ex</pre>
<p>And press tab to autocomplete and it completes the name of the directory with the space character escaped so this can be an option when there is no ambiguity with the name.</p>
<p>Now, I can use ls to get a list of files in that folder as shown in figure 3.</p>
<figure>
<img src="images/figure3.png" alt = " Figure 3 - a directory listing of the Exercise Files folder">
<figcaption> Figure 3 - a directory listing of the Exercise Files folder
</figcaption>
</figure>
<p>If I use file with the argument test.sh, the terminal displays this information:</p>
<pre class="inset">test.sh: Bourne-Again shell script, ASCII text executable</pre>
<p>So this tells me that test.sh is an executable shell script encoded as ASCII text. If I look at the same file with stat, I can see some of the information I would get using -l as an option with ls, specifically there is information relating to permissions on the file and when it was most recently accessed or modified.</p>
<p>Within the exercise files folder, we have a folder called departments which contains folders for several departments. Each of these folders contain a number of files relevant to that department. If we wanted to list the files in each of these folders, we can do this by navigating to the appropriate folder and typing ls. However, it might be time-consuming to do this for each department but there is a way in which we can list the contents of all the departments with a single command.</p>
<p>If we are in the Exercise Files folder (which contains the departments folder, we can do this with the command:</p>
<pre class="inset">$ls -R ./departments</pre>
<p>Note that we had specified the departments folder here. Had we not done that, we would have performed this type of recursive listing for all folders in the Exercise Files folder. However, if we navigate to the departments folder before executing the command, we can omit the name of the folder as an argument.</p>
<p>Something I had ignored previously since it was not new to me is the use of a . to represent the current directory. There are a couple of other similar characters which I will mention now and then illustrate using the departments folder.</p>
<pre class="inset">
&bull; As mentioned, the single . represents the current folder
&bull; A double .. represents the parent folder for the current folder
&bull; A – (dash or hyphen) represents the last folder you were in</pre>
<p>In the departments folder, we will navigate to the hr folder and then the policies folder with the
command</p>
<pre class="inset">$cd ./hr/policies</pre>
<p>So here we are specifying the pathname for the directory we want to navigate to with ./ representing the current directory path to which we are adding hr/policies. In this instance, we can omit the ./ and simply type</p>
<pre class="inset">$cd hr/policies</pre>
<p>If we want to go back, we can type</p>
<pre class="inset">$cd ../..</pre>
<p>or simply</p>
<pre class="inset">$cd -</pre>
<p>We can also use these characters to do some slightly more complex navigation and to demonstrate this we will navigate back to the policies folder (reminder: this is in the hr folder). Now, we may have noticed when we recursively listed the contents of the departments directory that there is also a directory called finance which in turn contains a directory called spreadsheets. We can navigate to this folder easily enough from the departments folder. If we are currently in the policies folder, we can also quite easily navigate back to the departments folder and then to the spreadsheets folder using two (or even three or four) simple commands.</p>
<p>However, we can also accomplish this using the single command</p>
<pre class="inset">$cd ../../finance/spreadsheets</pre>
<p>To break this down, if we had done this in four steps, they would have been</p>
<pre class="inset">
&bull; cd ..
&bull; cd..
&bull; cd finance
&bull; cd spreadsheets</pre>
<p>Bear in mind that the current working directory before we navigate to spreadsheets (which can be obtained with the command pwd) is</p>
<pre class="inset">~/Documents/commandlinebasics/Exercise Files/hr/policies</pre>
<p>Note that in this pathname the ~ character represents the current user’s home directory so full pathname is actually a little bit longer than this (most likely /home/philip/Documents/commandlinebasics/Exercise Files/hr/policies). The steps are doing the following</p>
<pre class="inset">
&bull; Navigates back to the parent folder (hr)
&bull; Navigates back to hr’s parent folder (departments)
&bull; Navigates to the finance folder
&bull; Navigates to the spreadsheets folder</pre>
<p>And finally, just a quick reminder that wherever you are in the folder structure, there are two useful shortcuts you will probably use quite often.</p>
<pre class="inset">
&bull; cd – cd on its own will take you straight to your home directory
&bull; cd / - this isn’t really a shortcut but it is used very often if you are exploring a Linux installation or if you are carrying out system admin tasks and this takes you straight to the root folder</pre>
<h2 class="sectiontitle">A Little More About ls</h2>
<p>We may have notice throughout the previous exercises that when we obtain a directory listing with ls, we see that the files listed have different colours. Note that in this context, files can mean conventional files (text files, spreadsheets, images, executables and so on). However, it is important to bear in mind the Linux philosophy that everything is a file and this includes directories, devices and other things which we might not normally think of as files. In this example, files simply refers to the contents of the directories within the Exercise Files folder. As a result, when I refer to different coloured files, bear in mind that some of these files are folders.</p>
<figure>
<img src=" images\figure4.png" alt=" Figure 4 - the results of using ls-l to list the contents of the Exercise ">
<figcaption> Figure 4 - the results of using ls-l to list the contents of the Exercise</figcaption>
</figure>
<p>If we look at figure 4, the first thing to note is that he have used -l to provide a long version of the directory so that we can see both the names of the files and additional information which I will mention briefly since the course will cover this in more depth later.</p>
<p>If we look at the first line, we can see that the very first character on the line is a d and on each subsequent line, it is a – character. This signifies that departments is a directory or folder. In comparison, a – here generally means that this is a file (in the conventional sense!). Following this, we have the permissions for who can access the file or folder then the name of the owner of the folder and the group that owns the folder. Note that in this case, the user is myself (philip) and the group is philip.</p>
<p>Next, we have the size of the file in bytes. If we add a h to our command (so, ls -lh) this gives us the listing in a more human-readable format and in this context, means that the size would be shown in kb if the file is large enough (for instance 4096 bytes becomes 4kb whereas 52 would show simply as 52 so we can infer that the size is shown in bytes is no unit is expressed). If the files were large enough, we might also see these sizes expressed as mb for megabytes or even gb for gigabytes.</p>
<p>Next, we have the date and time for the files creation and lastly the filename itself. Note that in this case, we seen to be displaying directories in blue, a zipped file in red, a shell script in green and some text Files and another shell script in white. This seems a little confusing so for now I will just mention that it is normal convention to display directories in blue and other files in light grey or black (depending on the shell’s background colour).</p>
<p>It is important to remember that the colouration is useful but not critical and depending on your installation, ls may not be set to use it. If you find that your shell is not set to colour code the listing, you can try ls with –color=always.</p>
<h2 class="sectiontitle">Create and Remove Folders</h2>
<p>The basic command for creating a folder is mkdir and its usage is simply the command followed by a directory name such as</p>
<pre class="inset">$mkdir new_folder</pre>
<p>Now, let’s return to the departments example in the Exercises Folder to look at ways in which this can be more powerful. We will start inside the Exercise Files folder (not actually in the departments folder) and we will create a folder inside the departments folder called customerservice with the command</p>
<pre class="inset">$mkdir departments/customerservice</pre>
<p>This demonstrates the fact that we can create a new folder in a location other than the one we are currently in. In fact, if we provide a full pathname, we can create a new folder inside any pre-existing folder regardless of which folder we are currently in.</p>
<p>We can also make several folders in one command by specifying each separately.</p>
<pre class="inset">$mkdir departments/customerservice/documents departments/customerservice/cases departments/customerservice/awards</pre>
<p>This creates folders called documents, vases and awards inside the customerservice folder.</p>
<p>Next, we will create a folder called contracts inside a folder called legal in a similar way to the way in which we created the folders inside customerservice. The difference here is that legal itself does not exist so we want to create both the legal folder (inside departments) and the contracts folder (inside legal) at the same time. We do this by adding the option -p to our command which gives us</p>
<pre class="inset">$mkdir -p departments/legal/contracts</pre>
<p>Essentially, the -p means create the parent folder as well. Actually, if we take a look at the man page for mkdir, we can see that p is short for parents and the description of the option is that it does not create an error if a parent folder already exists but it will create parent folders if needed. In this case, since the parent folder (legal) did not already exist, it was created and contracts was created inside it.</p>
<p>We can also remove a directory with the rmdir command but this is rather limited if we use it on its own because it will only delete an empty directory. So we can, for instance, delete the contracts folder we just created with the command</p>
<pre class="inset">$rmdir /departments/legal/contracts</pre>
<p>We will see a better way for removing folders later.</p>
<h2 class="sectiontitle">Copy, Move and Delete Files and Folders</h2>
<p>Copying a file is quite straightforward and we can do this with the cp command which I will
demonstrate with a file on the Exercise Files folder called poems.txt. We can copy this to a new file called poems2.txt (this is a copy, so we will still have the original file) with</p>
<pre class="inset">$cp poems.txt poems2.txt</pre>
<p>We can also provide a new location for the copy so let’s say we want to place a copy of the file ,
dimple_dat.txt in the employee info folder (which is in the hr folder). We can do that with the command</p>
<pre class="inset">$cp simple_data.txt departments/hr/employee\ info</pre>
<p>I am curious to know (and I don’t think this is covered in the course which may mean it is not a valid option) about whether we can use the -p option with copy so that folders are created as required during the operation. To test this, I will attempt to copy the file poems2.txt into a folder called philip (which does not exist beforehand) using the -p option.</p>
<pre class="inset">$cp -p poems2.txt departments/hr/employee\ info/philip/poems2.txt</pre>
<p>Actually, a more sensible approach to this type of enquiry would be to check the man page and in doing so, I can see that cp does have an option -p but it does not do what was expected here. In fact, there does not seem to be an option to create required directories when copying. Googling this seems to suggest that something similar is possible (using the option -rip) if the folder already exists in the source directory since it copies recursively but that it is not possible to create an entirely new folder via the cp command so essentially, a directory must already exist if you want to copy something into it.</p>
<p>Of course. It would be simple enough to create the directory in the target directory before attempting the copy operation.</p>
<p>We will be looking at the mv command which, although it is a move command, is often used as a method of renaming a file but I want to note, first, that the cp command can also rename the copied file but again, it leaves the original file intact.</p>
<p>To illustrate this, I will copy the simple_data.txt file into the hr folder again, but I will rename it to philip.txt in the process. The command for this is</p>
<pre class="inset">$cp simple_data.txt departments/hr/employee\ info/philip.txt</pre>
<p>We can use cat with the path and filename to examine simple_data.txt and philip.txt in the target folder to confirm that they contain the same text which is sufficient to confirm that the file has been copied and renamed in a single operation.</p>
<p>We can do the same thing with mv but the difference is that the original file will no longer exist so if we had used mv in the previous example, rather than copy and rename we would have move and rename combined in a single operation and if the source and target directories are the same, the effect would be to rename the file only.</p>
<p>Both cp and mv can operate on more than one file at a time by using a pattern. For instance, we might want to move all of the files with extension .txt from one folder to another so rather than specify a filename such as poems.txt, we would specify a pattern such as *.txt.</p>
<p>We can delete a file with the rm command</p>
<pre class="inset">$rm poems2.txt</pre>
<p>Again, we could use a pattern here and to illustrate this, I will use cp to copy poems.txt, creating poems2.txt again and also poems3.txt. Now, let’s say I want to delete these copies, I can do this with a pattern</p>
<pre class="inset">$rm poems?.txt</pre>
<p>The question mark will match with any character and the result is that poems2.txt and poems3.txt are both deleted but poems.txt is not. This is because poems.txt does not have any character at the position marked by the question mark and therefore does not match the pattern.</p>
<p>Note, however, that </p>
<pre class="inset">$rm poems*.txt</pre>
<p>would also delete poems2.txt and poems3.txt, but in this case will also delete poems.txt. This is because the asterisk (*) matches with any number of characters and this includes 0 and therefore poems.txt does match this pattern.</p>
<p> We saw previously that it was not possible to delete a directory using rmdir if that directory was not empty. So, for instance</p>
<pre class="inset">$rmdir ./departments/customserservice</pre>
<p>returns an error reporting that the directory is not empty. Similarly</p>
<pre class="inset">$rm ./departments/customerservice</pre>
<p>returns an error reporting that this
is a directory. To remove the directory, we need to delete
everything inside it and so we need rm to operate recursively and we
can do this by using the option -r</p>
<pre class="inset">$rm -r ./departments/customerservice</pre>
<p>This deletes everything inside the folder, including other folders and everything inside them, and
deletes the customerservice folder as well.</p>
<p>Finally, it is important to note that when you are working with the command line, there is no trash can and so any file you delete is essentially lost permanently. It is therefore important to be very careful when deleting anything, particularly if you are using a pattern or if you are deleting a
directory using the -r option.</p>
<p>Note that if you use the option -i with rm, you will be prompted when deleting a file to confirm that you want to do that and this can help to avoid deleting a file (or folder) accidentally.</p>
<p>If we want this to happen automatically when we use rm, we can do that by creating an alias</p>
<pre class="inset">$alias rm=’rm -i’</pre>
<p>However, if we want this to be permanent, we can add it to either the .bashrc or .profile file in our home directory. In Ubuntu, there is actually a recommendation in the .bashrc file that user-defined aliases should be placed in a separate file called .bash_aliases. The .bashrc file checks to see if this file exists and if it does, it creates the required aliases.</p>
<p>It is important to note, however, that if you use an alias in this way, when you use rm recursively, you may see a prompt for every file and folder which would be deleted by the command.</p>
<p>To get around this, you can use the -f option to avoid multiple prompts</p>
<pre class="inset">$rm -rf ./departments/customerservice</pre>
<p>Which negates the effect of the -I option and will delete the entire folder and its contents without any prompts.</p>
<p> There is a suggestion in the course video that it is actually impossible to recover a file accidentally deleted at the command line and I don’t think that is actually true. I assume that the file is never permanently deleted until the disk space it occupies is user for another file so I guess that recovery is possible, if not easy. For more information on this subject, see <a href="https://www.tecmint.com/recover-deleted-file-in-linux/">https://www.tecmint.com/recover-deleted-file-in-linux/</a> .</p>
<p>Just a quick aside, if the arrow keys are behaving erratically in vi, this can be solved by creating a file called .vimrc to your home folder and adding the line, set nocompatible to this file.</p>
<h2 class="sectiontitle"> Find Files from the Command Line</h2>
<p>The find command cam be quite useful for finding a particular file in a complicated file structure. The basic syntax is</p>
<pre class="inset">$find scope options pattern</pre>
<pre class="inset">
&bull; where scope is the location you want to start searching from. The find command operates recursively by default so it will search through the entire directory structure starting from the directory provided here as scope. Some shortcuts here are:
&bull; . if you want to search in the current directory and any subdirectories
&bull; ~ if you want to search in your home directory and any subdirectories
&bull; / if you want to search everywhere
&bull; Options are primarily used to specify the search parameters so, for instance, here we are searching based on the filename so the option is -name but we can also search based on size, file creation date and so on. There are lots of options and these are probably worth exploring.
&bull; Pattern essentially specifies what we are actually looking for.</pre>
<h2 class="sectiontitle">User Roles and Sudo</h2>
<p>Normal users cannot install software or browse other users home folders, this type of access requires root (or superuser) access. In general, it is considered to be bad practice to log in as the root user (or to su to root). Preference is generally given to the use of sudo to temporarily ‘borrow’ the access that the root user normally has.</p>
<p>I would guess this is because you will be normally asked for a password before any command is executed so in a sense this can act a little like the user account control in Windows. However, if you do provide a password, some Linux systems will allow you a few minutes grace period where a password is no longer required.</p>
<p>If you no longer require sudo access, you can (and this is considered good practice) relinquish the higher level of access with</p>
<pre class="inset">$sudo -k</pre>
<p>If you do need to switch over to root user, you can so this with the command</p>
<pre class="inset">$sudo -s</pre>
<p>To do this, you will be required to provide your own password. Alternatively, you can log in as root with the command</p>
<pre class="inset">$su</pre>
<p>For this, you will be required to provide the root users password.</p>
<h2 class="sectiontitle">File Permissions</h2>
<p>Figure 5 shows a terminal displaying a directory listing (the listing is of my home folder on my CentOS 7 VM) in the long format.</p>
<figure>
<img src="images\figure5.png" alt=" Figure 5 - a terminal displaying a direction listing with options -al ">
<figcaption> Figure 5 - a terminal displaying a direction listing with options -al</figcaption>
</figure>
<p>Note that on the left of the screen, the permissions for each folder and file are displayed. For instance, the permissions for the Documents folder are</p>
<pre class="inset">drwxr-xr-x</pre>
<p>and for the file, .bash_profile</p>
<pre class="inset">-rw-------</pre>
<p>The first character denotes the file type which is d for directory in the first example and a hyphen denoting a file in the second example.</p>
<p>After that, we have the permissions in three groups which are, in order, user, group and others. The permissions are represented by letters</p>
<pre class="inset">
&bull; r for read
&bull; w for write
&bull; x for execute</pre>
<p>So we can see from this that the for the documents folder, I (as the owner or user for this folder) have full access (rwx) and everyone else has read and execute access (r-x). In the context of a folder, execute access is required to allow a user to browse a folder. I can illustrate this by changing the permissions for this folder so that I no longer have the execute permission. I can then try to list the contents of the folder or navigate to the folder and in both cases, the attempt fails with a permission denied message.</p>
<p>If the file is an executable file, the executable permission relates to the ability to execute a file on
its own, without loading it into another program first. For example, let’s say we have a file called test.sh which prints out a simple string. We could execute it with</p>
<pre class="inset">$./test.sh</pre>
<p>However, if we do not have execute permission for the file, this will not work. On the other hand, the command</p>
<pre class="inset">$bash ./test.sh</pre>
<p>will execute the script because in this case, the script is being loaded into bash before being
executed.</p>
<p>We can change the permissions on a file with chmod and an octal value. The three entries for each set of permissions are converted from a 3 bit binary number (where 1 indicates the permission is granted and 0 indicates that it is not) to an octal value (of course, since there are only three bits, you could convert it to a decimal number and the effect would be the same).</p>
<p>To demonstrate this, please see figure 6. This shows a terminal where two commands have been executed. The first</p>
<pre class="inset">$touch permissions.txt</pre>
<p>creates a file called permissions.txt. Actually, creating the file in this instance is a side effect. The purpose of the touch command is to update the files timestamp, but if the file does not exist already, it is created so touch is a useful command for quickly creating a file.</p>
<p>Secondly, the command</p>
<pre class="inset">$ls -al p*</pre>
<p>displays a directory listing in the long format. The a option is actually superfluous here since the aim was simply to display the permissions.txt file, hence the pattern used (p*) so any other files displayed would have been superfluous but it is probably a good habit to use the a as a default option unless you have a good reason not to.</p>
<figure>
<img src ="images\figure6.png" alt=" Figure 6 - a directory listing in long format displaying the permissions.txt ">
<figcaption> Figure 6 - a directory listing in long format displaying the permissions.txt </figcaption>
</figure>
<p>So here, the permissions are defaults and have not been modified since the file was created and we can see that the user has read and write access as does the group. For everyone else, access is read only.</p>
<p>Aside – when a user creates a file in his own home folder, for instance with a command such as <em>touch</em>, these are the default permissions (rw- r-- r--)</p>
<p>A simple formula for converting these to an octal (or decimal) number is that:</p>
<pre class="inset">
&bull; read = 4
&bull; write = 2
&bull; execute = 1</pre>
<p>So let’s say we want to modify the permissions so that everyone has write access as well as read access. Read + write can be translated to 4 + 2 so we can update the permissions with the command</p>
<pre class="inset">$chmod 666 permissions.txt</pre>
<p>Note that this is a blanket approach so we don't need to know what the permissions are already before we execute such a command. However, if, for example, the user also had the execute permission, this command would remove it so not knowing in advance what the permissions are before you make a change in this way can have unintended side effects. In general, with a command like this, you can say what the permissions will be after executing the command, but you don't necessarily know what they were beforehand.</p>
<p>If we wanted to simply change one permission, we can use symbolic notation instead. In this scenario, the appropriate command might be</p>
<pre class="inset">$chmod o+w permissions.txt</pre>
<p>This has the same effect, as it happens, as the previous chmod command but in this case, no permissions other than the one specified are changed so there are no unintended side effects.</p>
<p>For this command, o represents others or the third set of permissions. If we wanted to change permissions for the group, we would use a g rather than an o (and u for user).</p>
<figure>
<img src ="images\figure7.png" alt="Figure 7 - Symbolic file permissions">
<figcaption>Figure 7 - Symbolic file permissions</figcaption>
</figure>
<p>As you can see in figure 7, although we used a plus sign to add the permission, we can also remove a permission with a minus sign and we can set the overall permissions for the user, group or others by using the equals sign which sets all three permissions according to the parameters you supply. For example, if we want others to have read only access to the permissions.txt file, we can do that with the command</p>
<pre class="inset">$chmod o=r permissions.txt</pre>
<p>This has the effect of setting the permission for others to r - - so after executing the command and regardless of what the permissions had been previously, others have read access only.</p>
<p>If we execute the command</p>
<pre class="inset">$chmod +x permissions.txt</pre>
<p>this has the effect of adding (that is, granting) the execute permission, but we have not specified whether this is for user (u), group (g) or others (o) and the result is that it is added for everyone, user, group and others.</p>
<p>Consider a command like</p>
<pre class="inset">$chmod 007 permissions.txt</pre>
<p>This gives read, write and execute permission to others but no permissions to either the user or the group. This might be seen as a catch-all since both the user and group belong to everyone. In this context, however, everyone should be taken to mean everyone <em>except</em> the user or anyone belonging to the group owning the file.</p>
<p>In this example, I am the user (or owner) of the permissions.txt file, but this command prevents me from reading or writing to the file so for a specific user, only the user permissions apply. It is not enough to simply giver these permissions to others (which is often understood to mean everyone but in reality means everyone else).</p>
<p>Another way to change permissions without actually changing the permissions for the file directly is to change owner. This requires sudo access so the command, for instance, that will change the ownership of test.sh to root is</p>
<pre class="inset">$sudo chown root test.sh</pre>
<p>Similarly, we can change the group to root with</p>
<pre class="inset">$sudo chgrp root test.sh</pre>
<p>Figure 8 shows a directory listing in the long format after executing these commands.</p>
<figure>
<img src ="images\figure8.png" alt="Figure 8 - long version of a directory listing of the home folder">
<figcaption>Figure 8 - long version of a directory listing of the home folder</figcaption>
</figure>
<p>Now, if we look at the long version of the directory listing, we will see that for the file test.sh, both user and group show as root. As the user philip, this means that I now only have read and execute permissions for the file, I can no longer write to it. Bear in mind that if the permissions were different, for instance if others had only read access or no access at all, then the user philip would similarly have only read access or no access.</p>
<p>If I now execute the command</p>
<pre class="inset">$vi test.sh</pre>
<p>I can see that the file is being reported as read only and if I tried to edit, I would be notified of the fact that I am trying to edit a read only file. However, since the owner is root, I can edit the file by ‘borrowing’ root access with</p>
<pre class="inset">$sudo vi test.sh</pre>
<p>Of course, I could also switch over to root access as described in the section, User Roles and Sudo.</p>
<h2 class="sectiontitle">Create Hard and Symbolic Links</h2>
<p>Links are used to create a reference to a file in another location and are used to avoid having multiple copies of a file. For example, if you want to access a file from your home directory without having to navigate to the folder it is stored in, you can create a link to it inside your home directory and then use the file as though it were located there.</p>
<p>Links can be hard or symbolic. Hard links point to data on the disk and symbolic links (also referred to as soft links) point to files on the disk.</p>
<p>To illustrate this, we will go back to the Exercise Files (recall that these are inside a folder called commandlinebasics which itself is inside the Documents folder in my home drive – for information on downloading these files see under the section Creating a Linux Virtual Machine). There is a file here called poems.txt.</p>
<p>The command to create a link is ln and it can be followed by -s (for a symbolic or soft link, this is omitted if we are creating a hard link), the name of the file that we want to create a link to and finally the name of the link.</p>
<p>If we execute the command</p>
<pre class="inset">$ln -s poems.txt writing.txt</pre>
<p>this creates a soft link called writing.txt which points to poems.txt and this can be seen quite clearly in a long directory listing as seen in figure 9.</p>
<figure>
<img src ="images\figure9.png" alt="Figure 9 - a long version of a directory listing of the Exercise Files folder showing the symbolic link we created">
<figcaption>Figure 9 - a long version of a directory listing of the Exercise Files folder showing the symbolic link we created</figcaption>
</figure>
<p>Now, we can view and edit writing.txt but it is important to remember that this is a link to the original file (poems.txt) so if we do edit writing.txt, we are actually editing poems.txt.</p>
<p>This type of link is relative so if either the poems.txt file or the writing.txt link are moved, the link will no longer work. In this case, however, the link will always work provided the files are in the same directory. We can demonstrate this by creating a directory called test and copying both files into this new directory. We can then attempt to edit writing.txt within the test directory and it still works.</p>
<p>We could also have created the link by specifying the full path as in</p>
<pre class="inset">$ln -s poems.txt ~/Documents/commandlinebasics/Exercise\ Files/writing.txt</pre>
<p>Now , a little care is required here because if we now try to edit the writing.txt file, it will still work. However, the link is pointing to the poems.txt file in the Exercise Files folder, not the poems.txt file in the test folder. So now this link will work if we move the writing.txt link as long as the poems.txt files is still located in the folder pointed to by the link.</p>
<p>Note that if you use ls without the -l option, the link will not show the file it points to in the directory listing and the only indication that it is a link is that it is (assuming coloration is set) a different colour. You may also have noticed in the long directory listing in figure 9, the link is also indicated by an l in the first position of the permission bits and it is also interesting to note that the default permissions appear to be different.</p>
<p>As an experiment, I have removed permissions from the poems.txt file and again tried to edit the writing.txt (which still has full permissions) and I found that permission was denied so clearly the permissions on the link do not override the permissions on the original file.</p>
<p>We can create a hard link with</p>
<pre class="inset">$ln poems.txt</pre>
<p>If we now obtain a long directory listing we will see the results as shown in figure 10.</p>
<figure>
<img src ="images\figure10.png" alt=" Figure 10 - a long directory listing after creating a hard link to poems.txt called words.txt ">
<figcaption>Figure 10 - a long directory listing after creating a hard link to poems.txt called words.txt </figcaption>
</figure>
<p>In practice, the link seems to work in the same way as a soft link but notice that in figure 10, there is no indication that words.txt is a link at all, the first permission bit is missing (set to -) and the link to poems.txt is not shown.</p>
<p>However, if we edit words.txt, we are still editing the poems.txt file so it is important to remember that this is a link rather than a copy of the file.</p>
<p>The hard link is actually pointing to the file on the hard drive so if we change the location of either poems.txt or words.txt, as long as the file is still on the hard drive, the link will continue to work. As a matter of fact, Linux uses hard links to point to files on the hard drive so when you are looking at a directory listing such as that in figure 10, this is actually a listing of hard links so poems.txt for instance, is also a hard link to the data stored on the drive. In fact, all files in a Linux system are hard links to the underlying data.</p>
<h2 class="sectiontitle">The Linux Filesystem</h2>
<p>Most Linux distributions follow a standard called the filesystem hierarchy standard which defines where certain types of files are stored. Each Linux system has one filesystem and everything else is represented within it and this includes files, folders, external drives and network shares.</p>
<p>At the top of this hierarchy is the root folder, represented by a slash - /. Within this folder, there are several common folders including:</p>
<pre class="inset">
&bull; home – the home folders of regular users are stored here. Note that for root, the home folder is the root folder.
&bull; etc – the configuration files for most programs are kept here.
&bull; bin and sbin – most programs the system relies on to work are kept here.
&bull; lib – this is where shared libraries and modules are stored.
&bull; dev – this is where the system keeps references to all the hardware it has including hard drives, memory, the CPU and so on.
&bull; mnt – this is where local or network filesystems are mounted into the overall system.
&bull; media – this is where removable systems like USB drives and optical drives are mounted.</pre>
<p>There are some others and within the standard, several subfolders to these are specified. There are also some folders outside of the standard which are used by Linux and these include:</p>
<pre class="inset">
&bull; proc – this is where references to running processes are stored.
&bull; sys – this is where files representing different kernel parameters and system information are kept.</pre>
<p>More information on the Linux filesystem can be found in Joe Collins YouTube video, <a href="https://www.youtube.com/watch?v=HIXzJ3Rz9po">Learning the Linux Filesystem</a>.</p>
</article>
<div class="btngroup">
<button class="button" onclick="window.location.href='basics.html';">
Previous Chapter - Command-Line Basics
</button>
<button class="button" onclick="window.location.href='common.html';">
Next Chapter - Common Command-Line Tasks and Tools
</button>
<button class="button" onclick="window.location.href='learninglinuxcommandline.html'">
Course Contents
</button>
<button class="button" onclick="window.location.href='/linux/linux.html'">
Linux Page
</button>
<button class="button" onclick="window.location.href='/index.html'">
Home
</button>
</div>
</body>
</html>