OPS145 Lab 3 Newversion: Difference between revisions

From Littlesvr Wiki
Jump to navigation Jump to search
Line 63: Line 63:
[[File:SimplePaths.png|center]]
[[File:SimplePaths.png|center]]


= Also: =
= Go places, find things =
Linux filesystem
Using the basic knowledge of absolute and relative paths introduced above you can navigate to any directory on your system, open any file, and run any program you have permissions for.


Navigate filesystem
* Go to the directory which has most of the applications on your system (/bin):<syntaxhighlight lang="bash">
cd /bin
ls
</syntaxhighlight>This is vaguely similar to "C:\Program Files\" on Windows, with one big difference: it only has the executables in it, not the other data files which the programs need. In Linux an application is rarly installed in one directory, an application's files are spread over several system directories (e.g. /usr/bin, and /usr/lib, and /usr/share).
* In this directory look for the programs you've run so far (cat, less, ls, pwd). Notice that they're coloured green. That's because they're executable programs.
* You can also open the /bin directory in your graphical file manager, but only as a curiosity (you don't typically run graphical applications from a file manager in Linux):[[File:BinCat.png|center|800x800px]]
* From /bin in the graphical file manager navigate back to your home directory and keep a mental note of the steps you took to get there:
** Go up one directory
** Go into the "home" directory
** Go into the "yourusername" directory
* Do the equivalent in the terminal:<syntaxhighlight lang="bash">
cd ../home/yourusername
pwd
</syntaxhighlight>
* Now do the reverse. In the graphical file manager:
** Go up one directory, you'll end up in /home
** Go up one more directory, you'll end up in /
** Go to the "bin" directory
* And do the equivalent in the terminal:<syntaxhighlight lang="bash">
cd ../../bin
pwd
</syntaxhighlight>
* You'll need a lot of practice to get used to these ideas. Choose some number of random directories in the graphical file manager, and navigate to/between them in the terminal using absolute and relative paths.


Home directory, directories
== Shortcut for your home directory: ~ ==
Your home directory (/home/yourusername) is so frequently used that there's a special character you can use as a shortcut to get to it: the tilde '''~''' (top left of your keyboard).


~
No matter what your PWD is in your terminal: '''~''' will always be automatically converted to /home/yourusername. You can use it alone, or in combination with other path components.


== Sometimes it's complicated ==
As a challenge look at the diagram, and the relative paths, and figure out what the PWD will be after you execute each of the following commands. Then run the commands one at a time to check your mental gymnastics:[[File:FilesystemIntro.png|center|499x499px]]<syntaxhighlight lang="bash">
cd ~
cd Downloads/../Documents
cd ../Documents/../Downloads
cd ../../../bin
</syntaxhighlight>Then write four '''cd''' commands using absolute paths instead of relative paths to do exactly the same operations as the four '''cd''' commands above.
= Also =
mkdir
mkdir



Revision as of 12:27, 26 January 2024

!!!THIS PAGE IS NOT READY YET!!!

Filesystems

A filesystem is a system of organizing files on a storage device. Such a system consists of:

  • A standard specifying the meaning and order of bytes on the storage device, and
  • Driver-like software which hides the filesystem-dependent details, and allows user-level software to perform generic operations (e.g. copy this file to that directory)

Files and directories are very different things from a human point of view, but on a filesystem they are almost the same thing. Each has a name, permissions, modification date, and the location of its contents. The contents of a file are whatever you'd expect they are, it's different for each type of file. The contents of a directory are the list of its contents.

In this course we're only going to look at the user level of Linux filesystems.

We'll start exploring these ideas by paying attention to the directories and files in the following diagram:

FilesystemIntro.png

You should have all of these if you finished lab 2.

Absolute paths start at root

In Linux there are no drive letters. All the storage which is accessible is accessible via a path which starts at the root (a single forward slash).

For example your first.txt file is in the Documents directory. The Documents directory is in the asmith15 directory. The asmith15 directory is in the home directory, and the home directory is in the root directory. The root directory does not have a parent directory, technically it is its own parent.

There might be other first.txt files on the system. In order to specify the path to this specific first.txt you use an absolute path: /home/youruserid/Documents/first.txt

The directories in a path are separated by forward slashes (/). This is different from Windows where backslashes are used.

If a path starts with a slash: it's an absolute path.

  • Open a terminal and use ls, or ls -l to confirm that first.txt exists and you have the correct path to it:
    ls /home/youruserid/Documents/first.txt
    
  • Read the contents of first.txt using an absolute path:
    cat /home/youruserid/Documents/first.txt
    

Relative paths start at PWD

In the last lab you used relative paths when you ran your ls and cat commands. All that means is that the path you used (e.g. first.txt), because it didn't start with a slash, was automatically appended to the present working directory in your terminal (e.g. /home/youruserid/Documents/), resulting in an unambiguous path (e.g. /home/youruserid/Documents/first.txt).

A file or directory name on its own is the simplest type of relative path - you used that last week.

This week we can look at slightly more complicated relative paths so you can navigate the filesystem more efficiently.

  1. Use pwd to confirm you're in /home/youruserid
  2. Change your PWD to the Documents directory using a relative path, and confirm that worked:
    cd Documents
    pwd
    ls
    
  3. Now go back to your home directory (/home/youruserid, not /home):
    cd ..
    pwd
    ls
    
    Two dots (..) means "parent directory" in the shell. Since /home/youruserid is the parent directory of /home/youruserid/Documents: that does what you want in this case. .. is also a relative path.
  4. From your home directory change directly into the SampleFiles directory:
    cd Downloads/SampleFiles
    pwd
    ls
    
    This time you had a path with two directories, but it's still a relative path, not an absolute path. It will get appended to the PWD (which was /home/youruserid), resulting in /home/youruserid/Downloads/SampleFiles/
  5. You can use .. more than once in the same path. It always means "parent directory". Go back to your home directory from where you are now:
    cd ../..
    pwd
    ls
    
  6. Here's a screenshot of the commands from this section so far:
SimplePaths.png

Go places, find things

Using the basic knowledge of absolute and relative paths introduced above you can navigate to any directory on your system, open any file, and run any program you have permissions for.

  • Go to the directory which has most of the applications on your system (/bin):
    cd /bin
    ls
    
    This is vaguely similar to "C:\Program Files\" on Windows, with one big difference: it only has the executables in it, not the other data files which the programs need. In Linux an application is rarly installed in one directory, an application's files are spread over several system directories (e.g. /usr/bin, and /usr/lib, and /usr/share).
  • In this directory look for the programs you've run so far (cat, less, ls, pwd). Notice that they're coloured green. That's because they're executable programs.
  • You can also open the /bin directory in your graphical file manager, but only as a curiosity (you don't typically run graphical applications from a file manager in Linux):
    BinCat.png
  • From /bin in the graphical file manager navigate back to your home directory and keep a mental note of the steps you took to get there:
    • Go up one directory
    • Go into the "home" directory
    • Go into the "yourusername" directory
  • Do the equivalent in the terminal:
    cd ../home/yourusername
    pwd
    
  • Now do the reverse. In the graphical file manager:
    • Go up one directory, you'll end up in /home
    • Go up one more directory, you'll end up in /
    • Go to the "bin" directory
  • And do the equivalent in the terminal:
    cd ../../bin
    pwd
    
  • You'll need a lot of practice to get used to these ideas. Choose some number of random directories in the graphical file manager, and navigate to/between them in the terminal using absolute and relative paths.

Shortcut for your home directory: ~

Your home directory (/home/yourusername) is so frequently used that there's a special character you can use as a shortcut to get to it: the tilde ~ (top left of your keyboard).

No matter what your PWD is in your terminal: ~ will always be automatically converted to /home/yourusername. You can use it alone, or in combination with other path components.

Sometimes it's complicated

As a challenge look at the diagram, and the relative paths, and figure out what the PWD will be after you execute each of the following commands. Then run the commands one at a time to check your mental gymnastics:

FilesystemIntro.png
cd ~
cd Downloads/../Documents
cd ../Documents/../Downloads
cd ../../../bin

Then write four cd commands using absolute paths instead of relative paths to do exactly the same operations as the four cd commands above.

Also

mkdir

rmdir

tree

ls -r

cp

mv

rm

ln -s