Fab Files

Objectives

Developing the Information Technology and Programming and Development learning strands, specifically:

  • Uses a variety of software to manipulate and present digital content: data and information.
  • Executes, checks and changes programs. Understands that programs execute by following precise instructions.
  • Uses technology with increasing independence to purposefully organise digital content.

Introduction

Learn It

  • Let's get to grips with understanding how files are organised on UNIX/Linux/MacOS systems.
  • The entire system starts in a root drectory, which has several directories inside it…

4_1.png

  • /bin: The user's binaries (programs).
  • /sbin: Programs that help the computer system run
  • /home: The user's files (the same as 'My Documents' on Windows)
  • /lost+found: When the system disk is checked, any stray file fragments are stowed in here.
  • /root: root is a special admin user of the system, who can do anything to the system. Their files live in here. Hackers will often try to hack the root password, as this would give them unlimited access to every file on the system.
  • /tmp: Any temporary files the system makes are stored in here, and (ideally) deleted once they're done with.
  • /boot: These are files that the system needs when it's first powered up.
  • /media: Any devices plugged into the computer (e.g. CD-ROMs, DVDs, USB Memory sticks) appear here.
  • /mnt: Unlike Windows (where different drives appear as letters of the alphabet (e.g. C:\, G:\), on UNIX-type systems you mount different devices as folders on your computer when you access them.
  • /var: Variable files. This is where the system writes data while its working. Web servers often store their data in the /var/www directory, for instance.
  • /dev: This directory shows a list of devices attached to the computer (e.g. printers, hard disks, monitors…)
  • There are several others too. opt, proc, lib and others all have important roles in the system. We'll tend to do all our work in /home.
  • Incidentally, you can use ~ as a shortcut for your own home drive at any time. You could type cd ~ to jump straight to your home directory, for instance.

Try It

  • Today, we'll learn to move files around on the system and how to pick up files from the web when we need new things.
  • There a few commands we'll learn today…
Command Usage Description
mkdir mkdir newFolder Makes a new directory
mv move source destination Moves a file from location a to b
cp copy source destination Copies files/directories
touch touch filename.txt Creates a new empty file
rm rm filename.txt Removes (deletes) a file
  • Once you can do all the things we've done in this unit, you're in pretty good shape to do useful work on a computer.
  • We'll have a quick go at manipulating some files, then you'll do some work by yourself to collect the badge. We'll use the files we picked up from the web in lesson 2.
Click here for pop-up window version
  • Open a 'pop up' version of the Linux machine using the link above, then type:
cd /home
cp -R * /tmp
cd /tmp/moveIt

Copy It

  • Let's start by listing what's in this directory, by typing ls -l

4_2a.png

  • You'll hopefully spot that there are two directories (secretDir and otherDir) and a text file called fileA.txt.
  • To avoid any problems, let's start by making a copy of the text file.
  • Type: cp fileA.txt fileB.txt
  • If you do ls again, you'll see you've now got a new file.
  • We could put a copy in another folder if we'd wanted…
  • Type: cp fileA.txt ./otherDir/anotherOne.txt
  • If you cd into the otherDir, and do ls you'll see a copy of the file in there too.

Move It

  • Sometimes, you'll want to move a file (or some files) rather than copy them.
  • If you're not already theren navigate into the otherDir directory.
  • Type mv anotherOne.txt ../secretDir/
  • If you do ls, you'll see that your file is now gone. Where did it go? What do you think the .. did in the destination file path (../secretDir/) that you wrote?
  • You can rename files by moving them too. You could do mv kittenpic.jpg dogpic.jpg for instance, which would rename kittenpic.jpg to dogpic.jpg.

Remove It

  • If you can copy and move files, the last thing to do is be able to delete the ones you don't want.
  • Navigate into the ../secretDir directory.
  • Type rm anotherOne.txt and hit enter.
  • If you now do ls, you'll see that your file is gone. Linux tends to work on the assumption you know what you're doing, so won't ask if you're sure when you try and delete a file.

Going Wild

  • If we need to, we can delete (or move or copy) lots of things at once by using wildcards.
  • Let's try. Type: cd into the moreDocs directory and ls the contents.

4_2.png

  • We're going to delete the a_hamster.txt and a_horse.txt files. They both start with a_h.
  • Type: rm a_h* and then do another ls -l
  • That last command told the OS to remove all files in the current directory that start with a_h and then have any number of characters after it in the filename.
  • This only describes the hamster and horse files, which is why they're the only ones to be removed.
  • Task: Delete the a_antagonistic.txt and a_antarctica.txt
  • You can sandwich wildcards in the middle of commands too. Consider: cp a_b*r* ../
  • This copies all the files in the current directory that…
    • Start with a_b then…
    • has any number of letters and numbers after it, followed by…
    • a single letter r and finally…
    • any number of letters/numbers
    • and copies these files to the directory below the current one.
  • Clever, eh?
  • Task: Delete all the files that have filenames starting with a_be in the moreDocs directory you're currently in.

Touchy Subject

  • Sometimes, you'll need to make new empty files ready to put things into.
  • To do this, simply use the touch command, like this.
    • touch newFile.txt
    • touch /tmp/moveIt/topsecretfile.txt
  • You can make new directories with mkdir
    • mkdir cheatMode
    • mkdir /tmp/moveIt/otherDir/1D_posters

Badge It

  • Hopefully, you'll now feel confident with manipulating files. If not, the video tutorial below goes through everything covered in the notes above.
  • Browse into the /tmp/moveIt/otherDir/badge directory and attempt the following…
  • Silver: Complete the following…
    • Make three new files called badge1, badge2 and myThirdBadge.
    • Make a directory called myBadgeDir.
    • Move all three files into the new folder.
    • Navigate into that directory, do a ls, screenshot the Terminal window and upload the image.
  • Gold: Write commands which would do the following…
    • Move all the .txt files in the /tmp/moveIt/otherDir/badge/ directory to the myBadgeDir directory.
    • Remove all the .jpg files in the current directory.
    • Move into the myBadgeDir directory that you made for the silver badge task, and remove all the files that start a_anta
    • Rename the a_beatle.txt file to BEETLE.txt (Tip: You use mv to achieve this)
    • Do a pwd, then a ls, screenshot the Terminal window and upload the image.
  • Platinum: When you type commands, you can run them through other commands to help when there are thousands (or billions if you work at Facebook) of files to work with. One of them is grep.
  • Read ahead into the online notes to find out how to use pipes. Investigate how how to list the contents of a directory, showing only files that contain nt (like a_antler.txt).
  • Put the command you use in a file called platAnswer.txt, save it and upload for marking.