LL4 – Files, Directories, Wildcards & Help

Required Reading

Review previous reading and LLs as necessary

The Linux Command Line by Shotts – 1.3, 1.4, 1.5

 

Assignment Submission Directions

As in the previous assignments, you will access the HVCC Acadnx environment and take periodic screenshots of your terminal as directed below  and paste the screenshots into a single LastnameFirstnameLL4 document, 1 screenshot per page with the proper heading and when done, save in a .pdf document (ex. for me I would submit LoobyJamesLL4.pdf). If you fail to follow this file naming convention your assignment will not be accepted.

Please use the Linux “clear” command where instructed.  If you fail to clear your screen where directed your assignment will not be accepted.

I expect that everyone will make mistakes from time to time and to this extent I do not need perfect, flawless, errorless screens for assignment submission – just correct your mistakes and ensure that the exercise goal is met with your screenshots.

Also, please resize/expand/enlarge your Terminal window but note that while the screenshot may not capture every command you have entered, it will demonstrate your present state and that is all that is necessary.

If you need assistance I am of course available but should you need remote assistance, please use Jing to document your difficulties as presented in LL2 and LL3 and please be sure to narrate (i.e. speak) during the Jing cast. If Jing is not working for you please include screenshots and good descriptive narrative as getting and providing help is also an educational endeavor.

Please note the above instructions will be standard for the rest of the course.

 

Intro ScreenShot

Open or launch your SSH application (e.g. Mac Terminal or Win PuTTy). Please resize your Terminal to make it larger to capture as much as possible but note you may not capture everything and this will be fine. If possible just take a screenshot/picture of your open terminal as if you include other items the text in the Terminal can be very small making it harder for me to see your commands. Paste this screenshot into your correctly named pdf document and label or title this screenshot “Intro Screenshot”.

 

Review and setup

First (as always) we need to both practice our commands from the last lab and confirm that everything is correct.  You should do this each lab or each time you login.

Upon logging in, in your home directory, please perform a listing, a long listing and a complete listing showing all files.  If you cannot do this you need to read more and review LL3.  Now you should only have your ciss100 directory in your home directory. If there are other items, please remove them.

Next change directories to the ciss100 directory and perform a listing and a complete listing.  You should see your FirstnameLastname subdirectory.  I specify the complete listing here so that you see and understand the difference between the . directory and the .. directory.  If you do not understand this please review the reading.

Next change directories back to your home directory and then change directories directly to your ciss100/FirstnameLastname directory. For me this would be $ cd ciss100/JamesLooby.  Perform a ls in your FirstnameLastname subdirectory as this should be empty.  If it is not, please remove any files or directories and then change directories back to your home directory to begin the lab.

 

4.1 Copying Files

cp (copy)

cp file1 file2 is the command which makes a copy of the source file1 in the current working directory and calls it the destination file2 also located in the current working directory. Note we can copy to and from elsewhere by providing the correct path (i.e. directory path and we can even use the single . as introduced in a previous LL). cp can be used to back up files by providing a .bak extension in the destination file. Also for completeness, to firmly establish correct terminology, cp is a Linux command whereas  file1 and file2 are arguments as the Linux creators had no idea what filenames you will have and use in your system. The cp command requires a source file and destination.

 

4.2 vim Editor

First, we need to get a file to copy and we need to get acquainted with a basic text editor for future work so let’s get started with the vim editor using the following commands in the Terminal (of course this requires that you start the Terminal by connecting via SSH).  Please recall I will use the standard pound sign or hash tag for inline comments and it is not necessary to type these comments on the command line.  In your home directory enter the following:

vim lastnamefirstnameL.txt

Note this creates a .txt file not the file you will submit ( please note the single L as this is an intentional error that we will correct later.  Therefore for me I am creating loobyjamesL.txt).

Now type i to enter vim’s insert mode

Type “Lastname Firstname” on the first line (using your last and first name without the delimiting quotes) and [Enter] to move to the next line

Type “Testing testing testing” on the second line and [Enter] to move to the next line

Type/count from 0 to 40 on subsequent lines with 1 number on each line beginning at 0 (i.e. type 0 and [Enter] moving to the next line, type 1 on the next line and [Enter] moving to the next line, 2 on next line… etc. all the way up to 40).  We do this so we can have 41 lines as we will need these 41 lines in subsequent labs.  Again this is 0-based counting as computers begin counting (and addressing) at 0 (i.e. range is 0-40 with one number on each line but the total number of numbers is 41).

Type “End testing” on the last line.

Now use the esc key to enter vim’s command mode and type :wq  to save and quit vim (Note, if you are doing this in a Virtual Machine rather than the HVCC AcadNX server, you may have to hit ctrl-esc.).  Also note you can reenter vim using the commands above, the letter i will place you in insert mode and esc followed by :wq will write the file and quit vim).

Now you’re wondering why I made you use this archaic non-graphical editor. Well… when a Unix/Linux system first comes up, you may not have a graphical editor and you may need to perform tasks to get the system up and running. Also, by default, servers may not have a GUI for both security and efficiency.  

Believe it or not, vim is an update to the vi editor  that I learned on as it was the only editor available back in my day… slightly after the Civil War during the age of slide rules.  You can imagine the excruciating pain of writing a lengthy C or Java program using vim in your 1st programming course.  Now for your reference, vim documentation is located here: http://www.vim.org/ and this applies to vi (i.e. we could have done this with vi and this is a viable option if you are having network difficulties).  Note that Unix/Linux also provides the sed and ed editors in a basic non-graphical implementation.

Now let’s perform a directory listing to see our file and note the color coding of files and directories so enter the following:

ls # recall we can get a lot more information using ls -la

As I will repeatedly recommend, perform a ls after nearly every file or directory command to verify success and directory contents. This includes when you create a file or directory or when you navigate (e.g. if you change directories, immediately perform a ls to see what’s there).

Ok, now let’s copy the file by providing a new name, i.e. lastnamefirstnameLL.txt

cp lastnamefirstnameL.txt lastnamefirstnameLL.txt

I will not repeat this again but you should perform a ls to check your work to see that the file was copied correctly.

Continuing on:

Now let’s copy this renamed file to our ciss100/FirstnameLastname subdirectory. There are two ways to do this:

1. We can cp the file to a subdirectory by providing the correct path or

2. We can navigate to the subdirectory and then copy the file to our present working directory.

We will choose the latter so first we have to navigate to ciss100/FirstnameLastname (for me this is ciss100/JamesLooby and note the home directory is implicit) so enter:

$ cd ciss100/FirstnameLastname# you can use the pwd command to verify where you are

Note that we could have changed directories in 2 separate steps, 1st cd to ciss100 and then cd to your FirstnameLastname subdirectory.

Now let’s copy the renamed lastnamefirstnameLL.txt file (for me this is loobyjamesLL.txt) so enter the following:

$ cp ~/lastnamefirstnameLL.txt .

Don’t forget the dot (.) at the end recalling the dot means the current directory and there is a space between .txt and the dot (.) but you should have known this from your readings and research. If you did not notice this or understand this, this is a clear sign that you are not researching the commands enough and this will result in problems in the future.  Also recall the ~ is our home or top level directory of our user space so this copied the file from your home directory to your present directory.

Now as I introduced above, I could also have copied the file from my home or top level directory by issuing the following command: $ cp loobyjamesLL.txt ciss100/JamesLooby. In this case my pwd was my home directory so I simply specified the source file and the destination directory.

Also note that I can rename the file whenever I copy it by providing a new file name so the command $cp loobyjamesLL.txt ciss100/JamesLooby/LL.txt would copy the loobyjamesLL.txt file to the ciss100/JamesLooby subdirectory and rename the file LL.txt

Exercise 4.2 – Navigate to your ciss100/FirstnameLastname subdirectory (for me this would of course be ciss100/JamesLooby).  Create a backup of your lastnamefirstnameLL.txt file by copying it to a file named lastnamefirstnameLL.ba and then perform a print working directory command and a long listing.  Take a screenshot and paste it into your assignment submission file with the correct heading. Note the backup extension misspelling (i.e. .ba rather than .bak) was intentional.

Please clear your screen after taking your screenshot and before continuing.

 

4.3 Moving files

mv (move)

mv file1 file2 #moves (or renames) file1 to file2

To move a file from one place to another, use the mv command. This has the effect of moving rather than copying the file, so you end up with only one file rather than two.

The mv command can also be used to rename a file, by moving the file to the same directory and giving it a different name.

Let’s use the mv command to correct our previous mistake.   Navigate to your ciss100/FirstnameLastname directory if you have changed directories and let’s correct our spelling mistake above and rename the lastnamefirstnameLL.ba file lastnamefirstnameLL.bak.

$ mv yournameLL.ba  yournameLL.bak

Excercise 4.3 – Perform a long listing, take a screenshot and paste it into your submission document with the correct heading.  Note you can now correct filename spelling mistakes and everyone makes fat finger mistakes.

Please clear your screen after taking your screenshot and before continuing.

 

4.4  Removing files and directories

Please clear your screen before continuing.

rm (remove), rmdir (remove directory)

Exercise 4.4 – Please navigate to your home directory and remove the lastnamefirstnameL.txt and lastnamefirstnameLL.txt files you originally created using the rm command. Please perform a print working directory and a complete listing and then take a screenshot and paste it into your submission file with the correct exercise heading.

Please clear your screen after taking your screenshot and before continuing.

 

4.5 Clear #already covered

clear (clear screen)

Before you start the next section, you may like to clear the terminal window of the previous commands so the output of the following commands can be clearly understood.

At the prompt, type

$ clear

This will clear all text and leave you with your prompt at the top of the window.

 

4.6 Wildcards – the characters * and ?

The character * is called a wildcard, and will match none or more character(s) in a file (or directory) name. For example, in your ciss100/FirstnameLastname directory type

$ ls P*

Did you find anything?  Do you know why?  Think about what P* will return.

Now type ls with the first letter of your lastname followed by a star.  For me, my last name is looby so this would be:

$ ls l* #it finds my loobyjamesLL.txt file

Again you should not be typing comments in.  If you do not know what a comment is you have received notice you are not reading enough or in sufficient detail.

The character ? will match exactly one character so ls ?ouse will match files like house and mouse, but not grouse.

Beware – be very careful if using the rm or rmdir command with a wildcard as rm * could be disastrous deleting your entire file system – 🙂

Exercise 4.6 – Please navigate to your home directory and take a screenshot and paste it into your document and as always, provide the Exercise 4.6 heading to the screenshot.

Please clear your screen after taking your screenshot and before continuing.

 

4.7 Filename conventions

Please clear your screen before continuing.

We should reinforce here that a directory is merely a special type of file. So the rules and conventions for naming files also apply to directories.

In naming files, characters with special meanings such as / * & $ , should be avoided. Also, avoid using spaces within names. The safest way to name a file is to use only alphanumeric characters, that is, letters and numbers, together with the _ (underscore) and . (dot).  My preference is camel case notation and you should know what this is by this point.

File names conventionally start with a lower-case letter, and may end with a dot followed by a group of letters indicating the contents of the file although Linux does not manage extensions to the same extent as Windows and Mac OS (less management improves OS efficiency). For example, all files consisting of C code may be named with the ending .c, for example, prog1.c . Then in order to list all files containing C code in your home directory, you need only type ls *.c in that directory.

Beware: some applications give the same name to all the output files they generate. For example, some compilers, unless given the appropriate option, produce compiled files named a.out. Should you forget to use that option, you are advised to rename the compiled file immediately, otherwise the next such file will overwrite it and it will be lost.

 

4.8 Getting Help

man – We already introduced this but for completeness, there are on-line manuals which give information about commands, programs, utilities and system configuration files. The manual pages tell you which options a particular command can take, and how each option modifies the behaviour of the command. Type man command to read the manual page for a particular command.  For example, to find out more about the wc (word count) command, type

$ man wc

By default the man pages are shown though the less command so we can move forward and backward through the file’s presentation.   You may search for a word using the / character followed by the word (‘n’ will search for next occurence and ‘N’ will search for previous instance).

info – the info command provides broader information than man.

whatis – gives a one-line description of the command, but omits any information about options etc.  As a result this provides a nice quick intro of the command so try this

$ whatis wc

apropos is used when you are not sure of the exact name of a command so apropos keyword will give you the commands with “keyword” in their manual page header. For example, try typing

$ apropos keyword

Now try

$ apropos copy # zowie hunh 🙂

Exercise 4.8 – Take and paste a screenshot into your submission document and no matter how big your terminal screen it is likely that the screenshot won’t capture everything but that’s ok.

Please clear your screen after taking your screenshot and before continuing.

 

4.9 Links (Please reread text  on links ~page 32)

In Linux there are 2 types of links, hard links and symbolic or soft links. Hard links are typically only used by Sys Admins so we will focus on symbolic/soft links and we will need this knowledge in the final projects. Essentially a soft link creates a pointer (hence symbolic) to a file or directory and you see these in other OSs. The symbolic link format is as follows:

ln -s filename linkfilename

This creates a soft link named linkfilename to the original filename. Another way to think about or remember this is you are creating a link from the source file (filename in this example) to a destination file (i.e. the link or linkfilename in this example).

Now most often we will create links to files throughout the file system so the filename and linkfilenames will probably include paths as follows:

ln -s /path/filename /linkpath/linkfilename

This creates a soft link named linkfilename in the linkpath to the original file /path/filename.

Continuing on: please remove the lastnamefirstnameLL.txt in your home directory if you did not do so already so: cd ~, then rm this file and verify it is gone with a ls – l.

Now let’s create a symbolic link to your ciss100/FirstnameLastname/lastnamefirstnameLL.txt file with a similar name in your home directory so please perform the following:

$ ln -s ciss100/FirstnameLastname/lastnamefirstnameLL.txt  lastnamefirstnamelink.txt

Now if this didn’t work and you received a failed to create… file exists error message, perform a listing and see if an ambiguity in the namespace exists (i.e. a file by the same name still exists in your home directory).  If this is the case you did not read the instructions above carefully enough and you are moving too fast.

When complete you should also perform a ls to see that the linkfile was created and you will note it is probably displayed in a different color font. If you see the file in a blacked out box or a red font you made a spelling error and linked to a non-existent file.  To resolve any of these errors, please reread the material and retrace your steps above to properly remove the file and create the correctly linked file.

If you need help I will need screenshots of directory listings that includes your home directory, your ciss100 subdirectory and your FirstnameLastname directory.

Exercise 4.9 – Please perform a long listing  in your home directory(noting the new entry and the symbolic links representation/coloring) and take and paste a screenshot into your submission document and no matter how big your terminal screen is you won’t capture everything but thats ok.

Please clear your screen after taking your screenshot and before continuing.

Now I get tired of changing directories and including the full path each time (as an example $ cd ciss100/JamesLooby) so let’s create a symbolic/soft link in our home directory to our ciss100/FirstnameLastname subdirectory and call this directory myStuff.  For me I would do this as follows but you will substitute your path name:

$ ln -s ciss100/JamesLooby myStuff

Now let’s test it so 1st perform a ls to see if it is present and correct (correct color).

Now let’s change directories using the linked directory and perform a listing to see the directory contents.

$ cd myStuff

$ ls

Is this the same contents as your ciss100/FirstnameLastname directory?  Please verify by navigating (changing directories) to your home directory and then navigating to your ciss100/FirstnameLastname subdirectory using the actual path (i.e. not the myStuff link).

Exercise 4.10 – (1) Please perform a long listing  in your ciss100/firstnameLastname subdirectoy to verify the contents, (2) then change directories to your home directory and perform a long listing and (3) then perform a listing of your myStuff directory from your home directory. Please take a screenshot and paste into your assignment appropriately.

 

Please submit your assignment in .pdf format.

Troubleshooting and Help

Troubleshooting your system and getting help often follow the same steps.  As an example, if you have trouble creating the softlink you should research and look at the command closely (repeated here).

$ ln -s ciss100/FirstnameLastname/lastnamefirstnameLL.txt  lastnamefirstnamelink.txt

1. If your link is blacked out or in red denoting a broken link you need to delete the link (delete it just like a file).

2. Perform a listing of your home directory to verify the ciss100 subdirectory exists.

3. Change directory to your ciss100 subdirectory and perform a listing to verify  the existance of your FirstnameLastname subdirectory.

4. Change directory to your FirstnameLastname subdirectory and perform a listing to verify the existance of your lastnamefirstnameLL.txt file.

* Of course you could have checked the existence of your directories and file from your home directory by providing the correct path as an argument to your directory listing command.  If you do not understand what I have just written => You have not read the material enough as it can take 1, 3 or even 10 readings to understand the material.

Also, to help your debugging process further (or to seek my help),  you must print out screenshots and reread the material with with the screenshots at your side for investigation.

Should you need additional help, please us Jing as introduced in previous LLs or minimally screenshots and references to the screenshots as directed above.

 

Linux Lab 4 Commands

Command/ArgumentDescription
*match 0 or more characters
?match 1 character
apropos keywordmatch commands with keyword in their man pages
clearclear the terminal screen
cp file1 file2copy file1 to file2
geditgraphical editor
man commandread the online manual page for a command
mv file1 file2move or rename file1 to file2
rmremove a file
rmdirremove a directory
vimcommand line editor
whatis commandbrief description of a command
wcword count returns number of lines, words and characters

Leave a Reply