|
|
(6 intermediate revisions by the same user not shown) |
Line 1: |
Line 1: |
| =ADVANCED UNIX / LINUX FILE MANAGEMENT= | | = Filesystems = |
| | A filesystem is a system of organizing files on a storage device. Such a system consists of: |
|
| |
|
| ===Main Objectives of this Practice Tutorial===
| | * A standard specifying the meaning and order of bytes on the storage device, and |
| | * Driver-like software which hides the filesystem implementation details, and allows user-level software to perform generic operations (e.g. copy this file to that directory) |
|
| |
|
| :* Understand the difference between '''absolute''' , '''relative''' and '''relative-to-home''' pathnames
| | 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. |
|
| |
|
| :* Become productive at issuing Linux commands with the most appropriate pathname
| | In this course we're only going to look at the user level of Linux filesystems. |
|
| |
|
| :* Use '''Filename Expansion''' ('''FNE''') Symbols: '''*''' , '''?''' , '''[ ]''' , '''[! ]'''
| | We'll start exploring these ideas by paying attention to the directories and files in the following diagram:[[File:FilesystemIntro.png|center|499x499px]] |
|
| |
|
| :* Use '''quotation''' treat special characters as just '''text''' when issuing Linux commands.
| | You should have all of these if you finished lab 2. |
|
| |
|
| :* Understand the quotation symbols: '''Backslash \''' , '''single quotes ' '''' and '''double quotes " "'''
| | = Absolute paths start at root = |
| <br><br>
| | 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)'''. |
|
| |
|
| ===Tutorial Reference Material===
| | 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. |
|
| |
|
| {|width="100%" cellspacing="0" cellpadding="10"
| | 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''' |
|
| |
|
| |- valign="top"
| | The directories in a path are separated by '''forward slashes (/)'''. This is different from Windows where backslashes are used. |
|
| |
|
| |colspan="2" style="font-size:16px;font-weight:bold;border-bottom: thin solid black;border-spacing:0px;padding-left:15px;"|Pathname Type / Filename Expansion / Quoting<br>
| | If a path starts with a slash: it's an absolute path. |
|
| |
|
| |- valign="top" style="padding-left:15px;"
| | * Open a terminal and use ls, or ls -l to confirm that first.txt exists and you have the correct path to it:<syntaxhighlight lang="bash"> |
| | style="padding-left:15px;" |'''Pathname / Filename Expansion:'''
| | ls /home/youruserid/Documents/first.txt |
| * [https://www.cyberciti.biz/faq/understanding-absolute-pathnames/ Absolute]<br>
| | </syntaxhighlight> |
| * [https://www.oreilly.com/library/view/web-design-in/0596009879/ch04s02s02.html Relative]<br>
| | * Read the contents of first.txt using an absolute path:<syntaxhighlight lang="bash"> |
| * [https://stackoverflow.com/questions/24048533/unix-simple-what-is-a-relative-to-home-pathname Relative-to-home]<br> | | cat /home/youruserid/Documents/first.txt |
| * [https://docstore.mik.ua/orelly/linux/run/ch04_08.htm#:~:text=Another%20way%20to%20save%20time,is%20sometimes%20called%20%22globbing.%22 Filename Expansion Symbols]
| | </syntaxhighlight> |
|
| |
|
| | style="padding-left:15px;"|'''Quoting Special Characters:'''
| | = Relative paths start at PWD = |
| * [http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_03.html Backslash '''\''' , Single ''' ' ' ''' , Double '''" "''']<br>
| | 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). |
| '''Linux Commands:'''
| |
| * [https://man7.org/linux/man-pages/man1/echo.1.html echo]
| |
|
| |
|
| |}
| | A file or directory name on its own is the simplest type of relative path - you used that last week. |
|
| |
|
| = KEY CONCEPTS =
| | This week we can look at slightly more complicated relative paths so you can navigate the filesystem more efficiently. |
|
| |
|
| ===Pathname Types=== | | # Use '''pwd''' to confirm you're in /home/youruserid |
| | # Change your PWD to the Documents directory using a relative path, and confirm that worked: <syntaxhighlight lang="bash"> |
| | cd Documents |
| | pwd |
| | ls |
| | </syntaxhighlight> |
| | # Now go back to '''your home''' directory (/home/youruserid, not /home):<syntaxhighlight lang="bash"> |
| | cd .. |
| | pwd |
| | ls |
| | </syntaxhighlight>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. |
| | # From your home directory change directly into the SampleFiles directory:<syntaxhighlight lang="bash"> |
| | cd Downloads/SampleFiles |
| | pwd |
| | ls |
| | </syntaxhighlight>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/ |
| | # 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:<syntaxhighlight lang="bash"> |
| | cd ../.. |
| | pwd |
| | ls |
| | </syntaxhighlight> |
| | # Here's a screenshot of the commands from this section so far: |
| | [[File:SimplePaths.png|center]] |
|
| |
|
| As previously mentioned, a '''pathname is a fully-specified location of a unique filename''' within a file system.<br>The concept of a pathname relates to every operating system including: ''Unix, Linux, MS-DOS, MS-Windows, Apple-Macintosh,'' etc.
| | = 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. |
|
| |
|
| Last week, we used a pathname from our home directory to create and manipulate directories and text files.<br>There are '''different types of file pathnames''' that we can use to access a directory or text file.
| | * 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. |
|
| |
|
| '''For Example:'''<br><span style="font-family:courier">/home/userid/uli101/cars.txt</span> ('''absolute pathname''')<br><span style="font-family:courier">samples/cars.txt</span> ('''relative pathname''')<br><span style="font-family:courier">~/cars.txt</span> ('''relative-to-home pathname''')
| | == 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). |
|
| |
|
| These types of file pathnames can make it more efficient (i.e. less keystrokes for users to type) when issuing Unix and Linux commands.
| | 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. |
|
| |
|
| | = Create directories with mkdir = |
| | For the rest of this lab you'll use the graphical interface to do stuff, and then do the equivalent thing in the command-line. |
|
| |
|
| ====Absolute Pathnames==== | | * Use the graphical file manager to create a '''lab3 directory''' in your home directory. |
| | * In a terminal '''cd''' to your home directory first, then create the '''lab3-cmd''' directory:<syntaxhighlight lang="bash"> |
| | cd ~ |
| | mkdir lab3-cmd |
| | </syntaxhighlight> |
| | * You can open multiple file managers and multiple terminals to convince yourself that other than the name there isn't any difference between the two directories you created. |
| | * In the graphical file manager go into your lab3 directory and create a '''red''' subdirectory. A subdirectory is just a regular directory, using that word usually implies that it's inside some specific other directory. |
| | * In your terminal create a '''red''' subdirectory under lab3-cmd and then '''cd''' into it:<syntaxhighlight lang="bash"> |
| | mkdir lab3-cmd/red |
| | cd lab3-cmd/red |
| | pwd |
| | </syntaxhighlight> |
| | * In the graphical file manager create a '''green''' directory inside the lab3 directory. |
| | * In your terminal use a relative path to create a '''green''' directory inside the lab3 directory and use '''ls''' to check that it was created:<syntaxhighlight lang="bash"> |
| | mkdir ../green |
| | pwd |
| | ls |
| | ls .. |
| | </syntaxhighlight> |
| | * In the graphical file manager create a '''blue''' directory inside the lab3 directory. |
| | * In your terminal use an absolute path to create a blue subdirectory of the lab3 directory. Check that it got created using an '''ls''' command and a '''relative-to-home''' path:<syntaxhighlight lang="bash"> |
| | mkdir /home/youruserid/lab3-cmd/blue |
| | ls ~/lab3-cmd |
| | </syntaxhighlight> |
| | *In the graphical file manager try to create another '''blue''' directory inside the lab3 directory. You'll get an error message saying that it can't be done because it already exists. |
| | *In your terminal '''cd''' to the lab3-cmd directory using an absolute path, then try to create another '''blue''' directory there. You'll get a similar error message, though not quite as easy to understand as the message in the graphical interface. |
| | This is a screenshot of me running the commands in this section: |
| | [[File:MkdirExamples.png|center]] |
|
| |
|
| <table align="right"><tr><td>[[Image:absolute-path-2.png|thumb|right|250px|The directory in red displays the FULL path from the '''root''' directory to the '''bin''' directory (i.e. the absolute pathname: '''/bin'''.]]</td><td>[[Image:absolute-path-1.png|thumb|right|250px|Directories in red display the FULL path from the '''root''' directory to the '''examples''' directory (i.e. the absolute pathname: '''/home/your-user-id/uli101/examples'''.]]</td></tr></table>
| | = Removing empty directories with rmdir and rm -r = |
| | For some reason Linux has a command which can only be used to remove empty directories: '''rmdir'''. But it's used often enough that we'll learn it now. |
|
| |
|
| An '''absolute pathname''' is a path to a file or directory always<br>'''beginning from the root directory (i.e. / )'''.
| | * In the graphical file manager create a new subdirectory of lab3/red: '''garbage'''. Inside garbage create two subdirectories called '''stuff''' and '''things'''. |
| | * Select the garbage directory and press '''Delete''' on your keyboard. It will go to your Trash (you might know it as a "recycling bin" on Windows). If you feel like it: you can restore what you deleted by clicking '''Go/Trash''' and finding your deleted files there. |
| | * In your terminal create the garbage, stuff, and things directories at the same time by giving mkdir more than one argument:<syntaxhighlight lang="bash"> |
| | cd ~/lab3-cmd/red |
| | mkdir garbage garbage/stuff garbage/things |
|
| |
|
| | </syntaxhighlight> |
| | * Try to delete the garbage directory using rmdir:<syntaxhighlight lang="bash"> |
| | rmdir garbage |
|
| |
|
| This type of pathname is referred to as '''absolute''' because the pathname always begins from the '''root directory''', regardless the location or your current directory. In other words, this type of pathname requires that you always provide the '''FULL''' pathname starting with the root directory.
| | </syntaxhighlight> |
| | * It will fail with an error saying that the directory is not empty. Delete the empty '''stuff''' directory using rmdir:<syntaxhighlight lang="bash"> |
| | rmdir garbage/stuff |
|
| |
|
| | </syntaxhighlight> |
| | * You could also delete the '''things''' directory and then the '''garbage''' directory when it's empty, but instead you can use the '''rm''' command with the '''-r''' argument:<syntaxhighlight lang="bash"> |
| | rm -r garbage |
|
| |
|
| ''Advantages of using Absolute Pathnames:'' | | </syntaxhighlight> |
| | * If you forget the '''-r''' argument: '''rm''' will refuse to delete a directory, whether it's empty or not. '''rm''' is primarily used to delete files. |
| | * Note that things deleted on the command-line are not moved to anything resembling a recycling bin. For most practical purposes they are deleted permanently and irrecoverably. |
|
| |
|
| :* Useful if you do not know your current directory location
| | = ls -R = |
| :* Understand the location of file within the filesystem.
| | The '''-R''' argument reuqests that the '''ls''' command show the contents of the PWD, and the contents of each subdirectory, and the contents of each subdirectory of each subdirectory, etc. until there are no subdirectories left. This common idea in computers is called a '''recursive''' operation. Many commands take an argument to do their operation recursively. Sometimes it's an uppercase -R and sometimes a lowecase -r. |
|
| |
|
| | * Change to your SampleFiles directory and see the difference between a regular and a recursive ls:<syntaxhighlight lang="bash"> |
| | cd ~/Downloads |
| | ls |
| | ls -R |
| | </syntaxhighlight> |
| | * With deeper and wider directory trees this can be difficult to read, but for simpler structures it's often helpful. |
|
| |
|
| ''Examples:''<br><span style="color:blue;font-family:courier;">ls /bin<br>ls /home/your-user-id/uli101/examples</span>
| | = Copying files and directories with cp and cp -r = |
| <br><br>
| |
|
| |
|
| ====Relative Pathnames==== | | * In the graphical file manager copy the '''SampleFiles.tar.xz file''' to your '''~/lab3/red''' directory. You can either use the copy and paste context menu items, or drag the file '''while holding the Ctrl key'''. |
| | * Do the same in the terminal, but copy '''SampleFiles.tar.xz''' to '''~/lab3-cmd/red''':<syntaxhighlight lang="bash"> |
| | cd ~ |
| | cp Downloads/SampleFiles.tar.xz lab3-cmd/red |
| | </syntaxhighlight> |
| | * Confirm using '''ls -l''' that the file was copied properly. |
| | * In the graphical file manager find the '''ls''' program in '''/bin''' and copy it to your '''~/lab3/green''' directory. |
| | * In your terminal copy '''ls''' from '''/bin''' to '''~/lab3-cmd/green''':<syntaxhighlight lang="bash"> |
| | cd ~/lab3-cmd/green |
| | cp /bin/ls . |
| | </syntaxhighlight> |
| | * That single dot in the end is not a typo, it's another special character. The easiest way to think of it is "present working directory", though technically it's a bit more complicated than that. |
| | * In the graphical file manager copy the '''SampleFiles''' directory from ~/Downloads to ~/lab3/blue |
| | * In your terminal attempt to copy the '''SampleFiles''' directory from ~/Downloads to ~/lab3-cmd/blue: |
| | * <syntaxhighlight lang="bash"> |
| | cd ~/Downloads |
| | cp SampleFiles ../lab3-cmd/blue |
| | </syntaxhighlight> |
| | * It dones't work because for some reason by default the '''cp''' command only copies files. If you want to copy a directory and all its contents, and all the contents of all the subdirectories you need to use the recursive ('''-r''') argument.<syntaxhighlight lang="bash"> |
| | cp -r SampleFiles ../lab3-cmd/blue |
| | </syntaxhighlight> |
| | * Confirm using '''ls -R -l''' or '''ls -R -l -h''' that the SampleFiles directory was copied over together with all its contents. |
| | * With two terminal windows open: run ls -R -l ~lab3 in one window, and ls -R -l ~/lab3-cmd in the other window. The contents of both directories should be identical at this point. If they're not: figure out what commands you can use to fill in the missing parts. |
|
| |
|
| <table align="right"><tr><td>[[Image:relative-pathname-1.png|thumb|right|250px|Directories in red display the path from the current directory location (which is '''xyz100''') to the '''bin''' directory (i.e. the relative pathname: '''../../../bin'''.]]</td><td>[[Image:relative-pathname-2.png|thumb|right|250px|Directories in red display the path from the current directory location (which is '''uli101''') to the '''examples''' directory (i.e. the relative pathname: '''uli101/examples''' or ./uli101/examples]]</td></tr></table>
| | [[File:Lab3SameAsLab3Cmd.png|center|813x813px]] |
| A '''relative pathname''' is a path to a file or directory that begins from your '''current''' directory. This is called relative because it is used to locate a specific file <u>relative</u> to your current directory.
| |
|
| |
|
| '''NOTE:''' In order to use relative pathnames, it is <u>absolutely necessary</u> that you know the '''location''' of your '''current directory'''! | | = Moving and renaming files and directories with mv = |
| | Linux doesn't have a "rename" command. It has a "move" command which also renames things: '''mv'''. If you think about it a little bit that mostly makes sense. You can move a file from one directory to anothter, and you can move it from one filename to another, or you can do both at the same time. |
|
| |
|
| | * In the graphical file manager create the ~/lab3/purple directory and drag the video and the photo from ~/lab3/blue/SampleFiles to ~/lab3/purple. Or use the cut/paste context menu items. |
| | * In the terminal create the ~/lab3-cmd/purple directory and use the mv command to move the photo and video from ~/lab3-cmd/blue/SampleFiles to ~/lab3-cmd/purple:<syntaxhighlight lang="bash"> |
| | cd ~/lab3-cmd/blue/SampleFiles |
| | mkdir ../../purple |
| | mv BigBuckBunny.mp4 ../../purple |
| | mv Snow.jpg ~/lab3-cmd/purple |
| | </syntaxhighlight> |
| | * You could also move more than one file at one time: just make sure that the last argument you give to the mv command is a directory. |
|
| |
|
| ''Relative Pathname Symbols:'' | | = Deleting files = |
| | By now you might be getting a feeling that there's some consistency between how different commands work. The command used to delete files in Linux ('''rm''') is no exception. It takes one or more arguments, and deletes those files. |
|
| |
|
| :'''<span style="font-size:1.5em;">.</span> ''' A period symbol "." represents the '''current''' directory
| | For this section don't delete files using the graphical file manager, use the terminal for both the ~/lab3/SampleFiles/ directory contents and the ~/lab3-cmd/SampleFiles/ directory contents. |
| :'''<span style="font-size:1.5em;">..</span>''' Two consecutive period symbols ".." represents the '''parent''' directory (i.e. one level up)
| |
|
| |
|
| | * In your terminal '''cd''' to ~/lab3/blue/SampleFiles. |
| | * Confirm that there are only .txt, .odt, and .pdf files in there. |
| | * Delete the four files one at a time:<syntaxhighlight lang="bash"> |
| | ls |
| | rm 1984.txt |
| | rm AnimalFarm.txt |
| | rm NotPlainText.odt |
| | rm NotPlainText.pdf |
| | ls |
| | </syntaxhighlight> |
| | * Then, still in your terminal, go to ~/lab3-cmd/blue/SampleFiles, confirm there are only four files in there, and delete them all at once:<syntaxhighlight lang="bash"> |
| | cd ~/lab3-cmd/blue/SampleFiles |
| | ls |
| | rm * |
| | ls |
| | </syntaxhighlight> |
|
| |
|
| ''Advantages of using Relative Pathnames:'' | | The star ('''*''') is another special character, a wildcard character. On its own it expands to "all not hidden files and directories". We will look into wildcards in more detail in another lab. |
|
| |
|
| :* Possible shorter pathname (less typing)
| | Again, remember that files deleted with rm cannot be undeleted. |
|
| |
|
| | = Symbolic links = |
| | One of the filesystem features Windows is lacking is symbolic links. The closest thing that Windows has is a shortcut. |
|
| |
|
| Examples: <br><span style="color:blue;font-family:courier;">ls ../../../bin</span><br><span style="color:blue;font-family:courier;">ls examples<br>ls ./examples</span>
| | A symbolic link is a pointer to another file or directory. In most cases you can use that symbolic link exactly the same way that you use the file/directory it's pointing to. |
| <br><br>
| |
| | |
| ====Relative-to-home Pathnames====
| |
| | |
| <table align="right"><tr><td>[[Image:relative-to-home-1.png|thumb|right|250px|Directories in red display the path from the home directory of the current user (which is '''userid''') to the '''examples''' directory (i.e. the relative-to-home pathname: '''~/uli101/examples'''.]]</td><td>[[Image:relative-to-home-2.png|thumb|right|250px|Directories in red display the path from another user's home directory location to their '''notes''' directory (i.e. the relative-to-home pathname: '''~jane/uli101/notes''']]</td></tr></table>
| |
| A '''relative-to-home pathname''' begins with the tilde character ( i.e. ~) to represent the user’s home directory.<br><br>
| |
| The tilde character '''~''' stores the path of the user’s home directory<br>(i.e. '''~ = /home/current-user-id''')<br><br>You can immediately place a username after the tilde to represent another user’s home directory (e.g. <span style="font-family:courier">'''~jane = /home/jane'''</span>)
| |
| | |
| | |
| Examples:<br><span style="color:blue;font-family:courier">ls ~/uli101/examples<br>ls ~murray.saul/uli101/notes</span>
| |
| | |
| | |
| '''NOTE:''' Deciding which '''type of pathname''' to use depends on many factors including: '''knowledge of current directory''', '''knowledge of directory structure''',<br>'''currently directory location''', and '''type of file management command''' that is being used.
| |
| <br><br>
| |
| | |
| ===Filename Expansion===
| |
| | |
| When issuing Linux commands, it may be '''more efficient''' (less typing) to use '''filename expansion symbols'''<br>to match files that share similar characteristics (e.g. same file extension) when issuing Linux commands.
| |
| | |
| | |
| ''Examples:''
| |
| | |
| You can use a special character to indicate to the Bash shell to match all files that end with the extension ".txt":
| |
| | |
| | |
| <span style="color:blue;font-family:courier;>'''ls *.txt'''<br>
| |
| a.txt b.txt c.txt 1.txt 2.txt 3.txt abc.txt work.txt</span>
| |
| | |
| | |
| Below are the most common Filename Expansion symbols and how they are used for filename expansion:
| |
| | |
| <table cellpadding="5"><tr><th style="border-bottom: 1px solid black;text-align:left;">Filename<br>Expansion<br>Symbol</th><th valign="bottom" style="border-bottom: 1px solid black;text-align:left;">Purpose</th></tr><tr><td>'''*'''</td><td>Asterisk (*) to represent '''0 or more characters'''</td></tr><tr><td>'''?'''</td><td>Question mark (?) to represent '''exactly one character (any character)'''</td></tr><tr><td>'''[ ]'''</td><td>Square brackets ([ ]) to represent and match for the <br>'''character enclosed within the square brackets'''.<br>It represents ONLY ONE character - it's like a<br>'''Question Mark (?)''' but with '''conditions or restrictions'''</td></tr><tr><td>'''[! ]'''</td><td>Square brackets containing an exclamation mark immediately after<br>the open square bracket ([! ]) to represent and match and<br>'''OPPOSITE''' character for the character enclosed within the square brackets.</td></tr></table>
| |
| | |
| ===Quoting Special Characters===
| |
| | |
| As discussed in the above section, there are some special characters that the shell uses to perform<br>an operation; for example, the filename expansion symbols: '''*''' or '''?'''
| |
| | |
| | |
| There are '''3 methods''' to make those special characters '''act only like text characters'''<br>when issuing Linux commands (displayed in chart below):
| |
| | |
| <table cellpadding="5"><tr><th style="border-bottom: 1px solid black;text-align:left;">Quoting Method</th><th style="border-bottom: 1px solid black;text-align:left;">Example</th></tr><tr><td>Place the character '''\''' <u>before</u> a special character</td><td><span style="font-family:courier">'''echo \*'''</span></td></tr><tr valign="top"><td>Contain special characters within '''double-quotes'''<br>'''NOTE:''' Double quotes works for most special<br>characters, but not all special characters (such as $)<br></td><td><span style="font-family:courier">'''echo "* hello *"'''</span></td></tr><tr valign="top"><td>Contain Special character within single '''quotes'''<br>(Quotes out ALL special characters)</td><td><span style="font-family:courier">'''echo '* hello *''''</span></td></tr></table>
| |
| | |
| =INVESTIGATION 1: ABSOLUTE / RELATIVE / RELATIVE-TO-HOME PATHNAMES=
| |
| | |
| <span style="color:red;">'''ATTENTION''': This online tutorial will be required to be completed by '''Friday in week 4 by midnight''' to obtain a grade of '''2%''' towards this course</span><br><br>
| |
| | |
| [[Image:dir12.png|right|350px|thumb|]]
| |
| | |
| The best way to learn about different types of file pathnames is to use them while issuing<br>Linux commands and see which pathnames (or combination of pathnames) is the '''<u>most</u> efficient'''<br>(i.e. requiring the '''least number of keystrokes''').
| |
| | |
| | |
| '''Perform the Following Steps:'''
| |
| | |
| # '''Login''' to your matrix account.<br><br>
| |
| # Issue a command to '''confirm''' you are located in your home directory.<br><br>Let's create the following directory structure under your home directory by issuing the mkdir command using only absolute pathnames.<br><br>
| |
| # Issue the following Linux command to create the directory structure displayed to the right using '''absolute pathnames''':<br><br>'''NOTE:''' Just continue typing and let the text continue of separate lines. Remeber to replace the text "youruserid" with your actual Seneca-id.<br><br><span style="color:blue;font-weight:bold;font-family:courier;">mkdir -p /home/youruserid/tutorial3/practice/commands /home/youruserid/tutorial3/practice/examples /home/youruserid/tutorial3/notes/lesson1 /home/youruserid/tutorial3/notes/lesson2</span><br><br>
| |
| # Issue the following Linux command to confirm that you properly created the directory structure:<br><span style="color:blue;font-weight:bold;font-family:courier;">tree /home/youruserid/tutorial3</span><br><br>You should notice that using absolute pathnames with this Linux command '''requires a lot of typing'''.<br>Let's '''remove''' this directory structure, and issue the same command using a ''relative-to-home'' pathname instead.<br><br>
| |
| # To remove this directory structure, issue the following Linux command (enter "'''y'''" at each prompt to remove ALL contents):<br><span style="color:blue;font-weight:bold;font-family:courier;">rm -ri /home/youruserid/tutorial3</span><br><br>
| |
| # Issue a command tree command as you did in '''step #4''' to confirm that the directory structure has been removed.<br><br>
| |
| # Issue the following Linux command to create the same directory structure using relative-to-home pathnames:<br><br>'''NOTE:''' You usually generate the ~ character by Holding down '''SHIFT''' and press the button<br>to the <u>left</u> of the number '''1''' above the text on your keyboard.<br><br><span style="color:blue;font-weight:bold;font-family:courier;">mkdir -p ~/tutorial3/practice/commands ~/tutorial3/practice/examples ~/tutorial3/notes/lesson1 ~/tutorial3/notes/lesson2</span><br><br>Did this command require less typing than the previous command using absolute pathnames?<br><br>
| |
| # Issue the '''tree''' command to confirm the directory structure was properly created.<br><br>Let's remove the '''tutorial3''' directory and its contents and issue the same command using '''relative pathnames'''.<br><br>
| |
| # Issue the same command as you did in '''step #5''' to remove the '''tutorial3''' directory and its contents safely.<br><br>
| |
| # Issue a Linux command to confirm you removed the '''tutorial3''' directory and its contents.<br><br>
| |
| # Issue the following Linux command to create the same directory structure using relative pathnames:<br><br><span style="color:blue;font-weight:bold;font-family:courier;">mkdir -p tutorial3/practice/commands tutorial3/practice/examples tutorial3/notes/lesson1 tutorial3/notes/lesson2</span><br><br>
| |
| # Issue a command to verify that the proper directory structure was created.<br><br>'''QUESTION:''' Which '''mkdir''' command (pathname type) that you performed in steps '''3''' , '''7''' , and '''11''' required the <u>LEAST</u> number of keystrokes (i.e. characters)?<br><br>
| |
| | |
| [[Image:dir13.png|right|350px|thumb|]]
| |
| :You may think that issuing Linux file management commands are better using '''relative''' or<br>'''relative-to-home''' pathnames instead of '''absolute''' pathnames, but that is not always true.
| |
| | |
| :Since the '''current''' directory location was your '''home''' directory, then it makes sense to use ''relative'' or ''relative-to-home'' pathnames. On the other hand, what if we <u>changed</u> the location to a different directory?
| |
| | |
| :When performing the next series of steps, refer to the '''tree diagram''' on the right. Learning to reference a tree diagram on a '''quiz''', '''midterm''' or '''final exam''' can help to '''prevent errors and loss of marks!'''
| |
| <br>
| |
| | |
| '''Perform the Following Steps:'''
| |
| | |
| # Make certain that your current directory is '''your home directory'''.<br><br>
| |
| # Since we will be running Linux commands depending on the directory structure that you have created, issue the following Linux command to verify you created it correctly:<br><span style="color:blue;font-weight:bold;font-family:courier;">~uli101/week3-check-1</span><br><br>
| |
| # If you encounter errors, make corrections and then re-run the checking script until you receive a congratulations message.<br><br>
| |
| # Issue a Linux command to change to the '''examples''' directory in your recently-created directory structure.<br><span style="color:blue;font-weight:bold;font-family:courier;">cd tutorial3/practice/examples</span><br><br>
| |
| # Issue a Linux command to confirm you are located in the ''examples'' directory.<br><br>
| |
| # Remembering that we are located in the '''examples''' directory, issue the following Linux command<br>using a '''relative''' pathname to display files in the /bin directory: <span style="color:blue;font-weight:bold;font-family:courier;">ls ../../../../../bin</span><br><br>
| |
| # Now issue the following Linux command using an '''absolute''' pathname: <span style="color:blue;font-weight:bold;font-family:courier;">ls /bin</span><br><br>Which type of pathname would be the best to use in this situation?<br><br>'''NOTE:''' Using the previous command using the '''relative-to-home''' pathname would work,<br> but it would look weird. Try to issue the command yourself!<br><br>
| |
| # Let's copy the file called ls which is contained in the '''/bin''' directory to your '''home''' directory by using the '''cp''' command.<br>First, issue the following Linux command to copy the ls command from the /bin directory to your home directory using absolute pathnames:<br><span style="color:blue;font-weight:bold;font-family:courier;">cp /bin/ls /home/youruserid</span><br><br>
| |
| # Now let's issue the previous command using just relative pathname (remember, our current directory location is '''examples'''):<br><span style="color:blue;font-weight:bold;font-family:courier;">cp ../../../../../bin/ls ../..</span><br><br>'''TIP:''' For relative pathnames that move up multiple parent directories such as these, it is HIGHLY RECOMMENDED<br>to view the tree diagram and check for the correct number of .. symbols. Students commonly make mistakes<br>and lose marks on these type of questions!<br><br>
| |
| # Let's issue the command using one absolute pathname and a relative pathname: <span style="color:blue;font-weight:bold;font-family:courier;">cp /bin/ls ../..</span><br>What did this command do?<br><br>
| |
| # Let's issue the same command using one absolute pathname and a relative-to-home pathname: <span style="color:blue;font-weight:bold;font-family:courier;">cp /bin/ls ~</span><br>What did this command do?<br><br>Which of the following file type combinations requires the LEAST number of keystrokes to copy the ls file to your home directory?<br><br>
| |
| # Let's copy the '''ls''' file from the '''/bin''' directory to your <u>current</u> directory (i.e. examples): <span style="color:blue;font-weight:bold;font-family:courier;">cp /bin/ls .</span><br><br>
| |
| # Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cp /bin/ls ./ls.bk</span><br><br>What does this command do?<br><br>Let's run a checking script to make certain you performed the a few of the recently-issued commands correctly.<br><br>
| |
| # Issue the following: <span style="color:blue;font-weight:bold;font-family:courier;">~uli101/week3-check-2</span><br><br>
| |
| # If you encounter errors, make corrections and then re-run the checking script until you receive a congratulations message,<br>and proceed to the next INVESTIGATION.<br><br>
| |
| | |
| =INVESTIGATION 2: FILENAME EXPANSION=
| |
| | |
| | |
| You will now get practice issuing Linux file management commands using '''filename expansion symbols'''.<br>We will be using the directory structure that was created in the previous INVESTIGATION.
| |
| | |
| A great way to practice filename expansion, use the '''touch''' command to create a lot of empty filenames,<br>write the '''ls''' Linux commands that use '''filename expansion''', predict the filenames that will be display,<br>and finally run the command to check your work.<br><br>
| |
| | |
| [[Image:dir12.png|right|350px|thumb|]]
| |
| :'''Perform the following steps:'''
| |
| | |
| # Issue a Linux command to move to the '''examples''' directory<br>(i.e. under ''practice'' directory as shown in diagram to the right).<br><br>
| |
| # Issue a Linux command to confirmed that you have moved to the '''examples''' directory.<br><br>
| |
| # Issue the '''touch''' command to create the following empty text files in the ''examples'' directory:<br>(note ''upper'' and ''lowercase'' letters)<br><br>
| |
| <div style="font-family:courier;margin-left:50px;font-weight:bold;">abc.txt<br>def.text<br>hij.TxT<br>1a4.txt<br>123.TXT<br>456.txt<br>6u9.txt<br>
| |
| ab2.html<br>1234.txt<br>abcdef.txt<br>abcde.txt<br><br></div>
| |
| <ol><li value="4">To verify that you properly created those files, issue the following:<br><span style="color:blue;font-weight:bold;font-family:courier;">~uli101/week3-check-3</span><br><br>If you encounter errors, then make corrections (eg. '''viewing directory contents''', '''check for correct filename syntax''',<br>'''case sensitivity''', '''missing files''', '''files in the wrong location''', etc.) and then re-run the checking script<br>until you receive a congratulations message, and then continue with this investigation.<br><br><span style="color:red;">'''ATTENTION''':<br>Learning to '''fix your mistakes''' by issuing Linux commands may be required if you make mistakes<br>in your online tutorial. </span><br><br><li>Issue the '''ls''' command to get a listing of files in your ''examples'' directory.<br><br>The output should look identical to the diagram displayed below.<br>You can refer to this listing to see all files so you can then predict the output from Linux commands that use filename expansion symbols.<br><br>[[Image:listing-1.png|left|650px|thumb|]]<br><br><br><br></li><li>What do you think the output will be from the following Linux command?<br><span style="color:blue;font-weight:bold;font-family:courier;">ls ???.txt</span><br>'''Write down the expected output''' on paper, then '''issue the command''' to check your answer.<br><br></li><li>What do you think the output will be from the following Linux command?<br><span style="color:blue;font-weight:bold;font-family:courier;">ls ?????.txt</span><br>'''Write down the expected output''' on paper, then '''issue the command''' to check your answer.<br><br></li><li>What do you think the output will be from the following Linux command?<br><span style="color:blue;font-weight:bold;font-family:courier;">ls ??????.txt</span><br>'''Write down the expected output''' on paper, then '''issue the command''' to check your answer.<br><br></li><li>What do you think the output will be from the following Linux command?<br><span style="color:blue;font-weight:bold;font-family:courier;">ls [0-9].txt</span><br>'''Write down the expected output''' on paper, then '''issue the command''' to check your answer.br>Did the command work?<br>What does this teach you about the character class [ ] symbol?<br><br></li><li>What do you think the output will be from the following Linux command?<br><span style="color:blue;font-weight:bold;font-family:courier;">ls [0-9][0-9][0-9].txt</span><br>'''Write down the expected output''' on paper, then '''issue the command''' to check your answer.<br><br></li><li>What do you think the output will be from the following Linux command?<br><span style="color:blue;font-weight:bold;font-family:courier;">ls [a-z][a-z][a-z].txt</span><br>'''Write down the expected output''' on paper, then '''issue the command''' to check your answer.<br><br></li><li>What do you think the output will be from the following Linux command (using character class with UPPERCASE letters)?:<br><span style="color:blue;font-weight:bold;font-family:courier;">ls [A-Z][A-Z][A-Z].txt</span><br>'''Write down the expected output''' on paper, then '''issue the command''' to check your answer.<br><br></li><li>What do you think the output will be from the following Linux command (using character class using alpha-numeric characters)?<br><span style="color:blue;font-weight:bold;font-family:courier;">ls [a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9].txt</span><br>'''Write down the expected output''' on paper, then '''issue the command''' to check your answer.<br><br></li><li>What do you think the output will be from the following Linux command?<br><span style="color:blue;font-weight:bold;font-family:courier;">ls *.txt</span><br>'''Write down the expected output''' on paper, then '''issue the command''' to check your answer. Did ALL text files get listed? Why not?<br><br></li><li>What do you think the output will be from the following Linux command?<br><span style="color:blue;font-weight:bold;font-family:courier;">ls *.[tT][xX][tT]</span><br>'''Write down the expected output''' on paper, then '''issue the command''' to check your answer. Did ALL text files get listed this time? If so, why?<br><br></li><li>'''NOTE:''' We have just been using filename expansion symbols just with the ls command.<br>Filename expansion symbols can be used for ANY Linux file management command (e.g. '''cat''', '''more''', '''less''', '''cp''', '''mv''', '''rm''', '''ls''', etc.).<br><br>Let's get some practice issuing these other Linux file management commands.<br><br></li><li>Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">file *.[tT][xX][tT]</span><br>What is the purpose of this command? Which files are contained in this output?<br><br><li>[[Image:dir12.png|right|350px|thumb|]]Change to the '''commands''' directory using an '''absolute''' pathname<br>(use the diagram on right-side for reference).<br><br></li><li>Issue a Linux command to confirm that you are now in the '''commands''' directory.<br><br></li><li>Issue the following Linux command (lowercase "l" NOT the number "1"):<br><span style="color:blue;font-weight:bold;font-family:courier;">cp /bin/l* .</span><br>View the contents of the contents directory. What did this command do?<br><br></li><li>Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">rm *</span><br><br>View the contents of the contents directory. What did this command do?<br><br></li><li>Issue the following Linux command (lowercase "l" NOT the number "1"):<br> <span style="color:blue;font-weight:bold;font-family:courier;">cp /bin/l? .</span><br>View the contents of the contents directory. What did this command do?<br><br></li><li>Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">rm l[!s]</span><br>View the contents of the contents directory. What did this command do?<br><br></li><li>Use a text editor (nano or vi) to create the file called '''ab''' in the '''commands''' directory that contains the line of text below,<br>and then save editing changes to this file:<br><span style="font-family:courier;">This is file ab</span><br><br></li><li>Use a text editor (nano or vi) to create the file called '''cd''' in the '''commands''' directory that contains the line of text below,<br>and then save editing changes to this file:<br><span style="font-family:courier;">This is file cd</span><br><br></li><li>Use a text editor (nano or vi) to create the file called '''ef''' in the '''commands''' directory that contains the line of text below,<br>and then save editing changes to this file:<br><span style="font-family:courier;">This is file ef</span><br><br></li><li>Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cat ??</span><br><br>View the contents of the contents directory. What did this command do? Why does the output look strange?<br><br>'''NOTE:''' Press the keys '''ctrl-c''' to return to the shell prompt.<br><br></li><li>Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cat [!l][!s]</span><br><br>View the contents of the contents directory. What did this command do? Does the output look better? If so, why?<br><br></li></ol>
| |
| | |
| : Proceed to the next INVESTIGATION.<br><br>
| |
| | |
| = INVESTIGATION 3: QUOTING SPECIAL CHARACTERS =
| |
| | |
| As discussed in the above investigation, there are some special characters that the shell uses to perform an operation<br>including the filename expansion symbol: '''*'''
| |
| | |
| There is a method make the shell '''ignore the purpose of special characters''' and treat as '''regular text'''.
| |
| | |
| In this investigation, you will learn '''three unique methods''' to quote special characters.
| |
| | |
| | |
| :'''Perform the Following Steps:'''
| |
| [[Image:quotation.png|right|350px|thumb|Using quotation to make the shell '''ignore the purpose of special characters''' and treat as '''regular text'''.]]
| |
| # Issue a Linux command to confirmed that you are still in the '''commands''' directory<br>(if not, change to the ''commands'' directory and confirm).<br><br>
| |
| # Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">echo hello there</span><br><br>'''NOTE:''' the '''echo''' command is used to display text onto your terminal.<br><br>
| |
| # Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">echo * hello *</span><br><br>What happened? What is shown in addition to the text "hello".<br>Why do you think those filenames are also being displayed?<br><br>
| |
| # Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">echo \* hello \*</span><br><br>What do you notice? What does the \ character do?<br><br>
| |
| # Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">echo "* hello *"</span><br><br>Is there a difference between this command and the previous command?<br><br>
| |
| # Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">echo '* hello *'</span><br><br>Is there any difference between this command and the two previous commands?<br><br>
| |
| # Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">echo $USER</span><br><br>'''NOTE''': The environment variable '''USER''' contains the current user's login name.<br>The '''$''' character immediately followed by the environment variable name causes it to<br>expand to the '''value''' that the variable contains.<br><br>
| |
| # Issue the following Linux command (using single quotes): <span style="color:blue;font-weight:bold;font-family:courier;">echo '* $USER *'</span><br><br>What happened? Why is the output display like this?<br><br>
| |
| # Issue the following Linux command (using double quotes): <span style="color:blue;font-weight:bold;font-family:courier;">echo "* $USER *"</span><br><br>What happened? Why?<br><br>
| |
| # Let's use quoting special characters for a couple of other commands. Move to the '''lesson2''' directory.<br>Confirm that you are currently located in the ''lesson2'' directory.<br><br>'''NOTE:''' Although it is NOT RECOMMENDED to create a filename using a special character (remember file naming rules?),<br>we will create an empty file called "*"<br><br>
| |
| # Issue the following Linux command (using single quotes): <span style="color:blue;font-weight:bold;font-family:courier;">touch '*'</span><br><br>
| |
| # Issue the '''ls''' command. Do you now see a file called "*" in addition to the other copied files?<br><br>
| |
| # Issue the following Linux command to remove the file called "*": <span style="color:blue;font-weight:bold;font-family:courier;">rm *</span><br><br>
| |
| # Issue the '''ls''' command to view the files in your current directory. What happened?!?<br>Why is it dangerous to use special characters when creating filenames?<br><br>
| |
| # Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cp /bin/ls .</span><br><br>
| |
| # Issue the following Linux command (using single quotes): <span style="color:blue;font-weight:bold;font-family:courier;">touch '*'</span><br><br>
| |
| # Issue the '''ls''' command to view the files in your current directory.<br><br>
| |
| # Issue the following Linux command (using single quotes): <span style="color:blue;font-weight:bold;font-family:courier;">rm '*'<br><br>
| |
| # Issue the '''ls''' command to confirm that the file called "*" has been removed in your current directory. What happened this time?<br><br>
| |
| # Complete the Review Questions sections to get additional practice.<br><br>
| |
| | |
| = LINUX PRACTICE QUESTIONS =
| |
| | |
| The purpose of this section is to obtain extra practice to help with your quizzes, your midterm, and your final exam.
| |
| | |
| Here is a link to the MS Word Document of ALL of the questions displayed below but with extra room to answer on the document to
| |
| simulate a quiz:
| |
| | |
| https://wiki.cdot.senecacollege.ca/uli101/files/uli101_week3_practice.docx
| |
| | |
| Your instructor may take-up these questions during class. It is up to the student to attend classes in order to obtain the answers to the following questions. Your instructor will NOT provide these answers in any other form (eg. e-mail, etc).
| |
| | |
| | |
| '''Review Questions:'''
| |
| | |
| | |
| When answering Linux command questions on this side or the back side of this page, refer to the following Inverted Tree diagram. The week3 directory is contained in your home directory. Assume that you just logged into your Matrix account. Directories are underlined.
| |
| | |
| | |
| [[Image:week3-practice-question.png|left|300px|thumb|]]
| |
| | |
| <br><br><br><br><br><br><br><br><br><br><br><br><br><br>
| |
| # Write a single Linux command using relative pathnames to create the directory structure displayed in the diagram above.
| |
| # Write a single Linux command to create the empty files “.answers.txt” and “questions.txt” shown in the diagram above using absolute pathnames.
| |
| # Write a Linux command to display a listing of all hidden and non-hidden filenames in the directory called “final” using a relative-to-home pathname.
| |
| # Assuming you are in your home directory, write a Linux command to view the contents of the “.answers.txt” file using a relative pathname. You can assume this text file is very large and you want to see all of the contents.
| |
| # Write a Linux command to change to the “backup “directory using an absolute pathname.<br>Write a command to verify that you changed to that directory:<br><br>
| |
| # Assuming that you remain in the “backup” directory, write a Linux command to copy the “questions.txt file” to your current directory. You are required to only use relative pathnames.
| |
| # Assuming that you remain in the “backup” directory, write a Linux command to delete the “questions.txt” file that is in your “backup” directory. Use a relative-to-home pathname.
| |
| # Assuming that you are currently located in your “backup” directory, write a Linux command to safely remove the directory “week3” and all of its contents. Use an absolute pathname.<br>Will your command you wrote in question 8 work if you run it? (yes/no). Why?<br><br>
| |
| # Assuming you are still located in the “backup” directory. Write a Linux command using a relative-to-home pathname to remove all files that end with the extension “.txt” in the “final” directory.
| |
| # Write a Linux command using an absolute pathname to list all files that consist of just 4 consecutive characters that are contained in your home directory.
| |
| # Write a Linux command using a relative pathname to list all files that begin and end with a number.
| |
| # Write a Linux command using a relative-to-home pathname to list all files that begin with a number but ends with any character other than a number.
| |
| # Assuming you are in your home directory. Write a Linux command using a relative pathname to view the contents of regular files whose file names only consist of 5 consecutive numbers.<br><br>
| |
| # Write a Linux command to display the following message:<br>*** Hello ***
| |
| # Write a Linux command to display the following message (including quotation marks):<br>“This is my message”<br><br>
| |
| | |
| | |
| _________________________________________________________________________________
| |
| | |
| Author: Murray Saul
| |
| | |
| License: LGPL version 3
| |
| Link: https://www.gnu.org/licenses/lgpl.html
| |
| | |
| _________________________________________________________________________________
| |
|
| |
|
| | For example the /bin directory you've been using on Linux Mint isn't actually a directory. The directory is /usr/bin; you've been using a symbolic link /bin which points to it. |
|
| |
|
| | * You can see this if you run '''ls -l''' on the root directory:<syntaxhighlight lang="bash"> |
| | ls -l / |
| | </syntaxhighlight>You can easily make your own symbolic links. The order of the arguments is a bit difficult to remember but the concept is simple. |
|
| |
|
| | * Inside your home directory create a symbolic link named lab3-link pointing to lab3:<syntaxhighlight lang="bash"> |
| | cd ~ |
| | ln -s lab3 lab3-link |
| | ls -l |
| | </syntaxhighlight> |
| | * In your graphical file manager and/or your terminal browse the contents of ~/lab3-link and note they are identical to what you can find in ~/lab3. |
| | * Create some extra files or directories in ~/lab3-link and see how they show up in ~/lab3. That's because when you try to create something in ~/lab3-link you actually end up creating it in ~/lab3. |
| | * A link is not a copy of the file/directory it's pointing to! |
|
| |
|
| | = Submit evidence of your work = |
| | After you finish the lab: run the following commands to submit your work:<syntaxhighlight lang="bash"> |
| | cd ~ |
| | wget http://ops345.ca/check/ops145-lab3-check.sh |
| | chmod 700 ops145-lab3-check.sh |
| | ./ops145-lab3-check.sh |
| | </syntaxhighlight>If it says "Your lab3 has been submitted": make a screenshot, and you're done. If it gives you any warnings or errors: you have to fix them and try the ./ops145-lab3-check.sh command again. |
|
| |
|
| [[Category:OPS145]] | | [[Category:OPS145]] |
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 implementation 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:
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.
- Use pwd to confirm you're in /home/youruserid
- Change your PWD to the Documents directory using a relative path, and confirm that worked:
- Now go back to your home directory (/home/youruserid, not /home):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.
- 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/
- 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:
- Here's a screenshot of the commands from this section so far:
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):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):
- 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:
- 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:
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.
Create directories with mkdir
For the rest of this lab you'll use the graphical interface to do stuff, and then do the equivalent thing in the command-line.
- Use the graphical file manager to create a lab3 directory in your home directory.
- In a terminal cd to your home directory first, then create the lab3-cmd directory:
- You can open multiple file managers and multiple terminals to convince yourself that other than the name there isn't any difference between the two directories you created.
- In the graphical file manager go into your lab3 directory and create a red subdirectory. A subdirectory is just a regular directory, using that word usually implies that it's inside some specific other directory.
- In your terminal create a red subdirectory under lab3-cmd and then cd into it:
mkdir lab3-cmd/red
cd lab3-cmd/red
pwd
- In the graphical file manager create a green directory inside the lab3 directory.
- In your terminal use a relative path to create a green directory inside the lab3 directory and use ls to check that it was created:
mkdir ../green
pwd
ls
ls ..
- In the graphical file manager create a blue directory inside the lab3 directory.
- In your terminal use an absolute path to create a blue subdirectory of the lab3 directory. Check that it got created using an ls command and a relative-to-home path:
mkdir /home/youruserid/lab3-cmd/blue
ls ~/lab3-cmd
- In the graphical file manager try to create another blue directory inside the lab3 directory. You'll get an error message saying that it can't be done because it already exists.
- In your terminal cd to the lab3-cmd directory using an absolute path, then try to create another blue directory there. You'll get a similar error message, though not quite as easy to understand as the message in the graphical interface.
This is a screenshot of me running the commands in this section:
Removing empty directories with rmdir and rm -r
For some reason Linux has a command which can only be used to remove empty directories: rmdir. But it's used often enough that we'll learn it now.
- In the graphical file manager create a new subdirectory of lab3/red: garbage. Inside garbage create two subdirectories called stuff and things.
- Select the garbage directory and press Delete on your keyboard. It will go to your Trash (you might know it as a "recycling bin" on Windows). If you feel like it: you can restore what you deleted by clicking Go/Trash and finding your deleted files there.
- In your terminal create the garbage, stuff, and things directories at the same time by giving mkdir more than one argument:
cd ~/lab3-cmd/red
mkdir garbage garbage/stuff garbage/things
- Try to delete the garbage directory using rmdir:
- It will fail with an error saying that the directory is not empty. Delete the empty stuff directory using rmdir:
- You could also delete the things directory and then the garbage directory when it's empty, but instead you can use the rm command with the -r argument:
- If you forget the -r argument: rm will refuse to delete a directory, whether it's empty or not. rm is primarily used to delete files.
- Note that things deleted on the command-line are not moved to anything resembling a recycling bin. For most practical purposes they are deleted permanently and irrecoverably.
ls -R
The -R argument reuqests that the ls command show the contents of the PWD, and the contents of each subdirectory, and the contents of each subdirectory of each subdirectory, etc. until there are no subdirectories left. This common idea in computers is called a recursive operation. Many commands take an argument to do their operation recursively. Sometimes it's an uppercase -R and sometimes a lowecase -r.
- Change to your SampleFiles directory and see the difference between a regular and a recursive ls:
- With deeper and wider directory trees this can be difficult to read, but for simpler structures it's often helpful.
Copying files and directories with cp and cp -r
- In the graphical file manager copy the SampleFiles.tar.xz file to your ~/lab3/red directory. You can either use the copy and paste context menu items, or drag the file while holding the Ctrl key.
- Do the same in the terminal, but copy SampleFiles.tar.xz to ~/lab3-cmd/red:
cd ~
cp Downloads/SampleFiles.tar.xz lab3-cmd/red
- Confirm using ls -l that the file was copied properly.
- In the graphical file manager find the ls program in /bin and copy it to your ~/lab3/green directory.
- In your terminal copy ls from /bin to ~/lab3-cmd/green:
cd ~/lab3-cmd/green
cp /bin/ls .
- That single dot in the end is not a typo, it's another special character. The easiest way to think of it is "present working directory", though technically it's a bit more complicated than that.
- In the graphical file manager copy the SampleFiles directory from ~/Downloads to ~/lab3/blue
- In your terminal attempt to copy the SampleFiles directory from ~/Downloads to ~/lab3-cmd/blue:
cd ~/Downloads
cp SampleFiles ../lab3-cmd/blue
- It dones't work because for some reason by default the cp command only copies files. If you want to copy a directory and all its contents, and all the contents of all the subdirectories you need to use the recursive (-r) argument.
cp -r SampleFiles ../lab3-cmd/blue
- Confirm using ls -R -l or ls -R -l -h that the SampleFiles directory was copied over together with all its contents.
- With two terminal windows open: run ls -R -l ~lab3 in one window, and ls -R -l ~/lab3-cmd in the other window. The contents of both directories should be identical at this point. If they're not: figure out what commands you can use to fill in the missing parts.
Moving and renaming files and directories with mv
Linux doesn't have a "rename" command. It has a "move" command which also renames things: mv. If you think about it a little bit that mostly makes sense. You can move a file from one directory to anothter, and you can move it from one filename to another, or you can do both at the same time.
- In the graphical file manager create the ~/lab3/purple directory and drag the video and the photo from ~/lab3/blue/SampleFiles to ~/lab3/purple. Or use the cut/paste context menu items.
- In the terminal create the ~/lab3-cmd/purple directory and use the mv command to move the photo and video from ~/lab3-cmd/blue/SampleFiles to ~/lab3-cmd/purple:
cd ~/lab3-cmd/blue/SampleFiles
mkdir ../../purple
mv BigBuckBunny.mp4 ../../purple
mv Snow.jpg ~/lab3-cmd/purple
- You could also move more than one file at one time: just make sure that the last argument you give to the mv command is a directory.
Deleting files
By now you might be getting a feeling that there's some consistency between how different commands work. The command used to delete files in Linux (rm) is no exception. It takes one or more arguments, and deletes those files.
For this section don't delete files using the graphical file manager, use the terminal for both the ~/lab3/SampleFiles/ directory contents and the ~/lab3-cmd/SampleFiles/ directory contents.
- In your terminal cd to ~/lab3/blue/SampleFiles.
- Confirm that there are only .txt, .odt, and .pdf files in there.
- Delete the four files one at a time:
ls
rm 1984.txt
rm AnimalFarm.txt
rm NotPlainText.odt
rm NotPlainText.pdf
ls
- Then, still in your terminal, go to ~/lab3-cmd/blue/SampleFiles, confirm there are only four files in there, and delete them all at once:
cd ~/lab3-cmd/blue/SampleFiles
ls
rm *
ls
The star (*) is another special character, a wildcard character. On its own it expands to "all not hidden files and directories". We will look into wildcards in more detail in another lab.
Again, remember that files deleted with rm cannot be undeleted.
Symbolic links
One of the filesystem features Windows is lacking is symbolic links. The closest thing that Windows has is a shortcut.
A symbolic link is a pointer to another file or directory. In most cases you can use that symbolic link exactly the same way that you use the file/directory it's pointing to.
For example the /bin directory you've been using on Linux Mint isn't actually a directory. The directory is /usr/bin; you've been using a symbolic link /bin which points to it.
- You can see this if you run ls -l on the root directory:You can easily make your own symbolic links. The order of the arguments is a bit difficult to remember but the concept is simple.
- Inside your home directory create a symbolic link named lab3-link pointing to lab3:
cd ~
ln -s lab3 lab3-link
ls -l
- In your graphical file manager and/or your terminal browse the contents of ~/lab3-link and note they are identical to what you can find in ~/lab3.
- Create some extra files or directories in ~/lab3-link and see how they show up in ~/lab3. That's because when you try to create something in ~/lab3-link you actually end up creating it in ~/lab3.
- A link is not a copy of the file/directory it's pointing to!
Submit evidence of your work
After you finish the lab: run the following commands to submit your work:
cd ~
wget http://ops345.ca/check/ops145-lab3-check.sh
chmod 700 ops145-lab3-check.sh
./ops145-lab3-check.sh
If it says "Your lab3 has been submitted": make a screenshot, and you're done. If it gives you any warnings or errors: you have to fix them and try the ./ops145-lab3-check.sh command again.