Difference between revisions of "CSC231 Bash Lab 1"

From dftwiki3
Jump to: navigation, search
(A view from the Home Directory)
(Creating Subdirectories)
 
(5 intermediate revisions by the same user not shown)
Line 11: Line 11:
 
<br />
 
<br />
 
=Switching to the Bash Shell=
 
=Switching to the Bash Shell=
 +
<br />
 +
Skip this part if you have already switched to the Bash shell...
 
<br />
 
<br />
 
By default, your student account using the shell called '''tcsh'''.  We will be using another shell that is more friendly: '''bash'''.
 
By default, your student account using the shell called '''tcsh'''.  We will be using another shell that is more friendly: '''bash'''.
 
<br />
 
<br />
Open a terminal or console window and connect to '''aurora.smith.edu''' with your class account.  Here we assume that your class account is of the form ''231b-xx'' where ''xx are two unique letters associated with your personal account.
+
Open a terminal or console window and connect to '''aurora.smith.edu''' with your class account.  Here we assume that your class account is of the form ''cs231a-xx'' where ''xx are two unique letters associated with your personal account.
 
<br />
 
<br />
Run these commands at the Linux prompt:
+
Run this command at the Linux prompt:
 
<br />
 
<br />
  cd
+
  setup231.sh
cp  .login  .login.bak
 
cp  .bashrc  .bashrc.bak
 
cp  ~231b/.login  .login
 
cp  ~231b/.bashrc  .bashrc
 
 
<br />
 
<br />
What this does is to copy the a file from your instructors account (~231b) into your account (~231b-xx).  This file is read automatically every time you login to your account and will set the shell to bash.
+
What this does is to copy the a file from your instructors account (~cs231a) into your account (~cs231a-xx).  This file is read automatically every time you login to your account and will set the shell to bash.
 
<br />
 
<br />
 
* logout of your account
 
* logout of your account
 
* log back in.  You should see something like this:
 
* log back in.  You should see something like this:
 
<br />
 
<br />
  231b-xx@aurora.smith.edu's password:  
+
  cs231a-xx@aurora.smith.edu's password:  
 
  Welcome to Linux Mint 17 Qiana (GNU/Linux 3.13.0-24-generic x86_64)
 
  Welcome to Linux Mint 17 Qiana (GNU/Linux 3.13.0-24-generic x86_64)
 
   
 
   
Line 36: Line 34:
 
  Last login: Thu Feb  2 16:59:49 2017 from 131.229.199.140
 
  Last login: Thu Feb  2 16:59:49 2017 from 131.229.199.140
 
  bash: setenv: command not found
 
  bash: setenv: command not found
  <font color="lightgreen">231b-xx@aurora</font> <font color="blue">~ $ </font>
+
  <font color="lightgreen">cs231a-xx@aurora</font> <font color="blue">~ $ </font>
 
   
 
   
 
<br />
 
<br />
 +
 
=Backing up your files=
 
=Backing up your files=
 
<br />
 
<br />
Line 52: Line 51:
 
=Creating Subdirectories=
 
=Creating Subdirectories=
 
<br />
 
<br />
We are going to create the following ''tree'' of directories shown below.  Note that '''you do not need to create the 231b-xx folder''', as it is by default already there: it's your ''' ''home'' ''' directory.
+
We are going to create the following ''tree'' of directories shown below.  Note that '''you do not need to create the cs231a-xx folder''', as it is by default already there: it's your ''' ''home'' ''' directory.
 
<br />
 
<br />
 
[[Image:treeOfFolders231.jpg|center|500px]]
 
[[Image:treeOfFolders231.jpg|center|500px]]
Line 58: Line 57:
 
Type in the following commands to create the tree and put files in the different folders.  Note: you will use the '''getcopy''' command that is not a Linux command, but rather a program created for the CS department accounts.
 
Type in the following commands to create the tree and put files in the different folders.  Note: you will use the '''getcopy''' command that is not a Linux command, but rather a program created for the CS department accounts.
 
<br />
 
<br />
+
  rm lab1
 
   mkdir lab1
 
   mkdir lab1
 
   mkdir hw1
 
   mkdir hw1
Line 78: Line 77:
  
 
<br />
 
<br />
 +
 
=Navigating the Subdirectories=
 
=Navigating the Subdirectories=
 
<br />
 
<br />
Line 128: Line 128:
 
  ls misc
 
  ls misc
 
  ls misc/demo
 
  ls misc/demo
  ls misc/pictures/demo
+
  ls misc/pictures  
 
   
 
   
 
You should have seen all 4 prog*.asm files.  Yes?
 
You should have seen all 4 prog*.asm files.  Yes?
Line 191: Line 191:
 
  ./HWorks/hw3/prog3.asm
 
  ./HWorks/hw3/prog3.asm
  
We are now going to use '''cp''' (for copy) to copy prog2.asm, prog3.asm, and prog4.asm to the '''HWorks''' directory.
+
We are now going to use '''cp''' (for copy) prog2.asm, prog3.asm, and prog4.asm to the '''HWorks''' directory.
 
   
 
   
 
  cd  
 
  cd  
Line 203: Line 203:
  
 
<br />
 
<br />
 +
 
=Moving Files=
 
=Moving Files=
 
<br />
 
<br />

Latest revision as of 10:39, 15 September 2017

--D. Thiebaut (talk) 17:04, 2 February 2017 (EST)



This tutorial introduces the basic navigation commands, and will show you how to create directories (folders) and move in and out of them. I have used and adapted much of the information that is presented in several excellent tutorials:



Switching to the Bash Shell


Skip this part if you have already switched to the Bash shell...
By default, your student account using the shell called tcsh. We will be using another shell that is more friendly: bash.
Open a terminal or console window and connect to aurora.smith.edu with your class account. Here we assume that your class account is of the form cs231a-xx where xx are two unique letters associated with your personal account.
Run this command at the Linux prompt:

setup231.sh


What this does is to copy the a file from your instructors account (~cs231a) into your account (~cs231a-xx). This file is read automatically every time you login to your account and will set the shell to bash.

  • logout of your account
  • log back in. You should see something like this:


cs231a-xx@aurora.smith.edu's password: 
Welcome to Linux Mint 17 Qiana (GNU/Linux 3.13.0-24-generic x86_64)

Welcome to Linux Mint
* Documentation:  http://www.linuxmint.com
Last login: Thu Feb  2 16:59:49 2017 from 131.229.199.140
bash: setenv: command not found
cs231a-xx@aurora ~ $ 


Backing up your files


Just in case you mess up and erase files in your account that you'd like to keep, you will make an archive of all your files and save it in your instructor's account:

cd
tar -czvf backup.tgz *
rsubmit backup backup.tgz

That's it! Your instructor should now have a copy of all the files you may have created in your home account, and in case you delete some by mistake, they should be recoverable.


Creating Subdirectories


We are going to create the following tree of directories shown below. Note that you do not need to create the cs231a-xx folder, as it is by default already there: it's your home directory.

TreeOfFolders231.jpg


Type in the following commands to create the tree and put files in the different folders. Note: you will use the getcopy command that is not a Linux command, but rather a program created for the CS department accounts.

  rm lab1
  mkdir lab1
  mkdir hw1
  mkdir misc
  ls

You should see the 3 directories in your account.

Let's go in the misc folder:

  cd misc
  ls

Let's create 2 subdirectories there: demo and pictures:

  mkdir demo
  mkdir pictures
  ls


Navigating the Subdirectories


Now, let's go "up" one level and look at what we have created and what they contain (the subdirectories should all be empty at this point):

  cd ..
  ls
  ls hw1
  ls lab1
  ls misc
  ls misc/demo

Let's go into lab1 and put the file prog1.asm there. We'll get prog1.asm from the instructor's handout directory:

  cd lab1
  getcopy prog1.asm
  ls

Now let's go into hw1, which is at the same level as lab1 and put prog2.asm into it:

  cd ../hw1
  getcopy prog2.asm
  ls
  cat prog2.asm 

Same thing with misc and prog3.asm

  cd ../misc
  cd demo
  getcopy prog3.asm

and finally we'll get prog4.asm and put it into pictures.

  cd ../pictures
  getcopy prog4.asm
 


A view from the Home Directory


Let's go back "home" and see if we can see the different prog*.asm files without actually "cd"-ing to the folders:

To go "home" we simply type:

cd

And we can look at the contents of the different sub- and sub-sub-directories:

ls lab1
ls hw1
ls misc
ls misc/demo
ls misc/pictures 

You should have seen all 4 prog*.asm files. Yes?


Challenge #1: A New Directory Tree

QuestionMark1.jpg


Let's remove all the new files and subdirectories you have just created. In Linux, "removing" means "deleting." The "rm" command deletes a file, while "rmdir" removes an empty directory. You have to delete all the files and subdirectories inside a directory before deleting it.

 cd
 rm hw1/prog2.asm 
 rm lab1/prog1.asm 
 rm misc/demo/prog3.asm 
 rm misc/pictures/prog4.asm 
 rmdir misc/pictures/
 rmdir misc/demo/
 rmdir misc
 rmdir lab1
 rmdir hw1

Now, using similar commands as the ones you used in the previous section, create the following directory tree:

TreeOfFolders231b.jpg


Challenge 1: Verification


You can check wether you have created the correct tree by typing the following command (too evolve for us to try to understand it today) which will look for all the file with a ".asm" extension in your account:

find . -name "*.asm" -print

If you have created your tree correctly, you should get

./hws/hw1/prog1.asm
./hws/hw1/prog2.asm
./misc/demo/pictures/prog3.asm
./misc/demo/pictures/prog4.asm

(You may have other files that you had created before today listed. That's fine, as long as the 4 lines above appear in the output.)

Copying Files


cp is the copy command. It simply makes a duplicate of a file somewhere in the tree.
Run the command makeLab1Tree.

makeLab1Tree

It will create a new subdirectory tree in your account, with the HWorks as the top subdirectory. Explore it with the cd command and go up and down the tree of subdirectories. Verify that it contains 4 different subdirectories and 4 assembly programs:

./HWorks/hw1/prog1.asm
./HWorks/hw2/prog2.asm
./HWorks/hw4/prog4.asm
./HWorks/hw3/prog3.asm

We are now going to use cp (for copy) prog2.asm, prog3.asm, and prog4.asm to the HWorks directory.

cd 
cd HWorks
cp hw1/prog1.asm  .
cp hw2/prog2.asm  .
cp hw3/prog3.asm  .
cp hw4/prog4.asm  .

Take a look at your current directory with ls, and verify that you now have your 4 programs in one place.


Moving Files


Assume that you didn't want copies of the files but you wanted to move them from the hw1, hw2, hw3, and hw4 directories to HWorks. You need to use mv (for move) rather than cp.

Let's remove the asm files currently in HWorks:

cd 
cd HWorks
rm prog*.asm 

The * character is called a wild-char and matches any string. So prog*.asm will match prog1.asm, prog2.asm, prog3.asm and prog4.asm. So one command will remove all 4.

Now move the files from the hw1, hw2, hw3, and hw4 directories to HWorks:

mv hw1/prog1.asm  .
mv hw2/prog2.asm  .
mv hw3/prog3.asm  .
mv hw4/prog4.asm  .

Verify that all 4 assembly programs are now in HWorks, and have disappeared from hw1, hw2, hw3, and hw4.


Challenge #2: Secret Message

QuestionMark2.jpg


Run the program makeLab1Tree2. It will create a new tree of subdirectories in your home.

Explore the tree and see if you find a mystery file. Its contents has important information for you!