Shell Commands for Managing Files and Directories

Creation

Lets make a directory

Return to you home directory, and we'll make a new directory to hold our bootcamp files. To do this we use mkdir short for "make directory":

    cd ~
    mkdir bootcamp

Move into this new directory with the "relative" path.

    cd bootcamp
    pwd

For practice, let's create a subdirectory called temp and move into it. Then create a file called draft.txt.

    mkdir temp
    cd temp
    nano draft.txt

Text Editors

You could create the draft.txt file with any text editor, but be careful -- a text editor is NOT a word processor. Even Apple's 'Text Edit' will add many lines of formatting specification to the top of a file. nano edits plain text, which is what we need for programs. It will get us started for a bootcamp, but for real work you're better off learning a professional level editor: On Unix systems (such as Linux and Mac OS X), many programmers use Emacs or Vim (both of which are completely unintuitive, even by Unix standards), or a graphical editor such as Gedit or Sublime Text. On Windows, you may wish to use Notepad++.

Let's type in a few lines of text, then use Control-O to write our data to disk:

Nano in Action

Once our file is saved, we can use Control-X to quit the editor and return to the shell. (Unix documentation often uses the shorthand ^A to mean "control-A".) nano doesn't leave any output on the screen after it exits, but ls now shows that we have created a file called draft.txt. If you use an editor from your computer's start menu, it may save files in your desktop or documents directory by default instead of bootcamp. You can change this by selecting "Save As...".

What does this do?

    touch draft-2014-04-14.txt
    ls -s
    cat draft.txt
    cat draft-2014-04-14.txt

Destruction

For practice, we'll remove the file :

    rm draft.txt

This command removes files ("rm" is short for "remove"). If we run ls again, its output is empty once more; our file is gone.

Deleting Is Forever

Unix doesn't have a trash bin: when we delete files, they are unhooked from the file system so that their storage space on disk can be recycled. There's no guarantee of recovery because the computer may recycle the file's disk space right away.

Let's re-create that file with nano again, and then move up to ~/bootcamp again.

    pwd
         /users/you/bootcamp/temp/
    nano draft.txt
    cd ..     
    pwd 
        /users/you/bootcamp/   

Now let's remove the subdirectory, temp.

    rm temp

We get an error because rm only works on files, not directories. Let's try rmdir:

    rmdir temp

So to really get rid of the directory we must first delete all files inside it:

   rm temp/draft.txt
   rmdir temp

The NUCLEAR OPTION

This removing to remove becomes tedious. We can use rm with the -r flag ("recursive"). It will travel down the directory tree, deleting everything in its path. When it's done, the directory is only a memory....you can't restore it.

$ rm -r temp

Of course it is very handy, but think first!

If you have time to kill at this point, experiment with the up arrow on your keyboard. Also try out the control keys ^a, ^e, and ^c. Share your results!

Moving and Copying

Let's set up another practice situation.

    mkdir thesis
    nano thesis/draft.txt
    ls thesis
        draft.txt

Notice that we can create and edit files in a different directory! On the other hand, draft.txt isn't a very informative name. We can use mv to move it, effectively renaming it. If we gave mv a different path, we could change the file's location as well. Remember . refers to the current directory... so what do you expect from:

    mv thesis/draft.txt thesis/quotes.txt
    mv thesis/quotes.txt .
    ls thesis/
    ls

We can also move directories with mv.

    mv thesis/ thesis-2014-04-14/
    cp thesis-2014-04-14 thesis-backup

The typing gets tedious, even with a nice responsive terminal window. Imagine a slow one...a queue of departmental jobs backed up for hours... your output on a few inches of computer paper amid everyone else's work. Typos would be hell.

IBM 1403 line printer

Unix was developed in the line printer age, and the need to avoid typos led to the 1-2 letter command names we're learning. It also led to techniques for getting the computer to do the typing for you.

Did you try out the up arrow out yet?

Try this:

    mv th<tab>   # hit the <tab> key, try hitting it twice

Yes, when you hit the tab key, the shell tries to match what you've already typed to a path or filename. It will fill in as much as it can and show you the options if you hit it twice. Try moving around your file system again, using cd and tab completion.

Fewer mistakes? Thank the line printer.

Challenges

  1. What is the output of the closing ls command in the sequence shown below?

        pwd
            /home/thing/data
        ls
            proteins.dat
        mkdir recombine
        mv proteins.dat recombine
        cp recombine/proteins.dat ../proteins-saved.dat
        ls
  2. Suppose that:

        ls -F
            analyzed/  fructose.dat    raw/   sucrose.dat

    What command(s) could you run so that the commands below will produce the output shown?

        ls
            analyzed   raw
        ls analyzed
            fructose.dat    sucrose.dat
  3. What does cp do when given several filenames and a directory name, as in:

        mkdir backup
        cp thesis/citations.txt thesis/quotations.txt backup

    What does cp do when given three or more filenames, as in:

        ls -F
            intro.txt    methods.txt    survey.txt
        cp intro.txt methods.txt survey.txt

    Why do you think cp's behavior is different from mv's?

  4. The command ls -R lists the contents of directories recursively, i.e., lists their sub-directories, sub-sub-directories, and so on in alphabetical order at each level. The command ls -t lists things by time of last change, with most recently changed files or directories first. In what order does ls -R -t display things?

Command Review:

mkdir
rmdir
nano <filename>
touch <filename>
rm
rm -r
mv <filename> <destination>
cp <filename> <destination>
cat <filename> ... 
# use the tab key!
In []: