Lesson 2: More UNIX practice - joslynnlee/CHEM-454 GitHub Wiki
Make a new directory
Open up one of the programs on your computer: GitBash (Windows) or Terminal (Mac).
Check your current directory with print working directory command: pwd
:
pwd
If you are in your M:Drive, you can stay in this directory. From the Canvas website, Computational Exercise 1 page, there was a file download called "shell-lesson-data.zip". You will need to download that into your M:Drive and unzip it.
On a Windows PC: Unzipping this file can be done by right-clicking on the shell-lesson-data.zip
file name and choosing 'Extract all'. A window will pop-up to request the location to extract files. You will need to erase the text shell-lesson-data
from the line to only read M:/
. If you leave in shell-lesson-data
, you will have a folder within a folder.
On a Mac: You can double-click the file and it will unzip in the current directory.
If you get stuck here or need help, please let me know.
Type in ls
to see if your new directory shell-lesson data
is now available.
ls
If you see shell-lesson-data
you can proceed forward. If you do not see it, let's try to think about where you unzipped the file. Working in the M:Drive is a good place for us at the moment to keep these files/folders.
Let's create a new directory.
mkdir thesis
As you might guess from its name, mkdir
means “make directory”. Since thesis is a relative path (i.e., doesn’t have a leading slash), the new directory is created in the current working directory, type ls -F
:
ls -F
Make a new directory called thesis-2
using mkdir
but with the flag -v
. What happens?
Good names for files and directories
Complicated names of files and directories can make your life painful when working on the command line. Here we provide a few useful tips for the names of your files.
Don’t use whitespaces.
EXAMPLE:
JL water sample ID list copy12.txt
Whitespaces can make a name more meaningful but since whitespace is used to break arguments on the command line is better to avoid them in names of files and directories. You can use
-
(dash) or_
(underscore) instead of whitespace.EXAMPLE:
JL-water-sample-ID-list-copy12.txt
Don’t begin the name with
-
(dash). Commands treat names starting with-
as options. Stick with letters, numbers,.
(period),-
(dash) and_
(underscore).Many other characters have special meanings on the command line. We will learn about some of these during this lesson. There are special characters that can cause your command to not work as expected and can even result in data loss.
If you need to refer to names of files or directories that have whitespace or another non-alphanumeric character, you should surround the name in quotes ("").
Move into the thesis
directory using cd
:
cd thesis
Now, let's use an editor called nano
.
nano draft.txt
When we say, ‘nano
is a text editor’ we really do mean ‘text’: it can only work with plain character data, not tables, images, or any other human-friendly media. We use it in examples because it is one of the least complex text editors. However, because of this trait, it may not be powerful enough or flexible enough for the work you need to do after this workshop. On Unix systems (such as Linux and macOS), many programmers use Emacs or Vim (both of which require more time to learn), or a graphical editor such as Gedit
. On Windows, you may wish to use Notepad++
. Windows also has a built-in editor called notepad
that can be run from the command line in the same way as nano
for the purposes of this lesson.
No matter what editor you use, you will need to know where it searches for and saves files. If you start it from the shell, it will (probably) use your current working directory as its default location. If you use your computer’s start menu, it may want to save files in your desktop or documents directory instead. You can change this by navigating to another directory the first time you ‘Save As…’
You will not be able to use the mouse, only the keyboard. The control button is indicated by ^
in nano
.
To exit nano
, type Control-X
.
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
:
ls
draft.txt
isn’t a particularly informative name, so let’s change the file’s name using mv
, which is short for “move”. To understand how the mv
command works, type mv man
to learn how to use the command.
mv man
The usage of mv
is:
mv source-to-move target-directory
-f
overwrite destination file
-v
verbose, print source and destination files
Type:
mv -v draft.txt quotes.txt
The first argument tells mv what we’re “moving”, while the second is where it’s to go. In this case, we’re moving thesis/draft.txt
to thesis/quotes.txt
, which has the same effect as renaming the file. Sure enough, ls
shows us that thesis now contains one file called quotes.txt
:
ls
Sure enough, ls
shows us one file called quotes.txt
.
Let’s move quotes.txt
into the directory above shell-lesson-data
. We use mv
once again, but this time we’ll just use the name of a directory as the second argument to tell mv
that we want to keep the filename, but put the file somewhere new. (This is why the command is called “move”.) In this case, the directory name we use is the special directory name .
that we mentioned earlier.
Type:
mv quotes.txt .
Type ls
to view the what is in your current directory.
ls
The quotes.txt
file should not be in there. To see if its is in the directory above, type in the change directory command:
cd ..
PRACTICE: After running the following commands, Jamie realizes that she put the files sucrose.dat
and maltose.dat
into the wrong directory:
$ ls -F
analyzed/ raw/
$ ls -F analyzed
fructose.dat glucose.dat maltose.dat sucrose.dat
$ cd raw/
Fill in the blanks to move these files to the current directory (i.e., the one she is currently in):
$ mv ___/sucrose.dat ___/maltose.dat ___
Note: Draw this out again. Check with the person next to you. Think about how this working on a graphical user interface (GUI).
ANSWER:
- The first command lists
ls -F
in the current working directory the two available directories:analyzed/
andraw/
. The-F
flag lists all directories with the/
. - The second command lists the contents in the
analyzed/
directory. The output lists four files, including the twosucrose.dat
andmaltose.dat
. - The third command
cd raw/
changes to the directoryraw/
. - To move the files from
analyzed
toraw
(current directory), Jamie will need to use themv
command. The..
is one directory above the current directory and.
refers to the current directory. Thus:
$ mv ../analyzed/sucrose.dat ../analyzed/maltose.dat .
Copying files and directories
The cp
command works very much like mv
, except it copies a file instead of moving it. We can check that it did the right thing using ls with two paths as arguments — like most Unix commands, ls can be given multiple paths at once:
cp quotes.txt thesis/quotations.txt
Check if your thesis
directory contains the quotations.txt
ls thesis/quotations.txt
We can also copy a directory and all its contents by using the recursive option -r, e.g. to back up a directory:
cp -r thesis thesis_backup
We can check the result by listing the contents of both the thesis
and thesis_backup
directory:
ls thesis thesis_backup
Take a photo of your GitBash shell to show you've made directories.
Deleting is Forever
The Unix shell doesn’t have a trash bin that we can recover deleted files from (though most graphical interfaces to Unix do). Instead, when we delete files, they are unhooked from the file system so that their storage space on disk can be recycled. Tools for finding and recovering deleted files do exist, but there’s no guarantee they’ll work in any particular situation, since the computer may recycle the file’s disk space right away.
Type in ls
:
ls
If you see the thesis
directory, we can remove the file using the rm
command.
rm thesis
This happens because rm
by default only works on files, not directories.
To really get rid of the directory thesis
we must also delete the file draft.txt
. We can do this with the recursive option -r
for rm
:
rm -r thesis
Check if the directory is removed from the current directory by using ls
:
ls
Note: Removing the files in a directory recursively can be a very dangerous operation.
We will come back to the M:Drive to work with files and learn about Shell Scripting on Friday!