wiki:cli:creating_things
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| wiki:cli:creating_things [2014/08/03 09:58] – Separate output input jensv | wiki:cli:creating_things [2022/07/21 06:59] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====Objectives==== | ||
| + | *Create a directory hierarchy that matches a given diagram. | ||
| + | *Create files in that hierarchy using an editor or by copying and renaming existing files. | ||
| + | *Display the contents of a directory using the command line. | ||
| + | *Delete specified files and/or directories. | ||
| + | We now know how to explore files and directories, | ||
| + | but how do we create them in the first place? | ||
| + | Let's go back to our home directory, | ||
| + | ''/ | ||
| + | and use '' | ||
| + | |||
| + | $ pwd | ||
| + | |||
| + | / | ||
| + | |||
| + | $ ls -F | ||
| + | |||
| + | desktop.examples | ||
| + | |||
| + | |||
| + | Let's create a new directory called '' | ||
| + | (which has no output): | ||
| + | |||
| + | |||
| + | $ mkdir thesis | ||
| + | |||
| + | As you might (or might not) guess from its name, | ||
| + | '' | ||
| + | Since '' | ||
| + | (i.e., doesn' | ||
| + | the new directory is made below the current working directory: | ||
| + | |||
| + | $ ls -F | ||
| + | |||
| + | desktop.examples | ||
| + | |||
| + | However, there' | ||
| + | |||
| + | $ ls -F thesis | ||
| + | |||
| + | |||
| + | Let's change our working directory to '' | ||
| + | then run a text editor called Nano to create a file called '' | ||
| + | |||
| + | $ cd thesis | ||
| + | $ nano draft.txt | ||
| + | |||
| + | > Which Editor? | ||
| + | > | ||
| + | > When we say, "'' | ||
| + | > only work with plain character data, not tables, images, or any other | ||
| + | > human-friendly media. We use it in examples because almost anyone can | ||
| + | > drive it anywhere without training, but please use something more | ||
| + | > powerful for real work. On Unix systems (such as Linux and Mac OS X), | ||
| + | > many programmers use [Emacs](http:// | ||
| + | > [Vim](http:// | ||
| + | > even by Unix standards), or a graphical editor such as | ||
| + | > [Gedit](http:// | ||
| + | > use [Notepad++](http:// | ||
| + | > | ||
| + | > 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' | ||
| + | > documents directory instead. You can change this by navigating to | ||
| + | > another directory the first time you "Save As..." | ||
| + | |||
| + | Let's type in a few lines of text, | ||
| + | then use Control-O to write our data to disk: | ||
| + | |||
| + | 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 '' | ||
| + | '' | ||
| + | but '' | ||
| + | |||
| + | $ ls | ||
| + | |||
| + | draft.txt | ||
| + | |||
| + | Let's tidy up by running '' | ||
| + | |||
| + | $ rm draft.txt | ||
| + | |||
| + | This command removes files (" | ||
| + | If we run '' | ||
| + | its output is empty once more, | ||
| + | which tells us that our file is gone: | ||
| + | |||
| + | $ ls | ||
| + | |||
| + | > Deleting Is Forever | ||
| + | > | ||
| + | > Unix doesn' | ||
| + | > 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' | ||
| + | > computer may recycle the file's disk space right away. | ||
| + | |||
| + | Let's re-create that file | ||
| + | and then move up one directory to ''/ | ||
| + | |||
| + | $ pwd | ||
| + | |||
| + | / | ||
| + | | ||
| + | $ nano draft.txt | ||
| + | $ ls | ||
| + | |||
| + | draft.txt | ||
| + | |||
| + | $ cd .. | ||
| + | |||
| + | If we try to remove the entire '' | ||
| + | we get an error message: | ||
| + | |||
| + | $ rm thesis | ||
| + | |||
| + | rm: cannot remove thesis: Is a directory | ||
| + | |||
| + | This happens because '' | ||
| + | The right command is '' | ||
| + | which is short for " | ||
| + | It doesn' | ||
| + | because the directory we're trying to remove isn't empty: | ||
| + | |||
| + | $ rmdir thesis | ||
| + | |||
| + | rmdir: failed to remove ' | ||
| + | |||
| + | |||
| + | This little safety feature can save you a lot of grief, | ||
| + | particularly if you are a bad typist. | ||
| + | To really get rid of '' | ||
| + | |||
| + | $ rm thesis/ | ||
| + | |||
| + | |||
| + | The directory is now empty, so '' | ||
| + | |||
| + | |||
| + | $ rmdir thesis | ||
| + | |||
| + | |||
| + | > With Great Power Comes Great Responsibility | ||
| + | > | ||
| + | > Removing the files in a directory just so that we can remove the | ||
| + | > directory quickly becomes tedious. Instead, we can use '' | ||
| + | > '' | ||
| + | > | ||
| + | > | ||
| + | > '' | ||
| + | > | ||
| + | > | ||
| + | > This removes everything in the directory, then the directory itself. If | ||
| + | > the directory contains sub-directories, | ||
| + | > them, and so on. It's very handy, but can do a lot of damage if used | ||
| + | > without care. | ||
| + | |||
| + | Let's create that directory and file one more time. | ||
| + | (Note that this time we're running '' | ||
| + | rather than going into the '' | ||
| + | |||
| + | $ pwd | ||
| + | |||
| + | / | ||
| + | |||
| + | $ mkdir thesis | ||
| + | |||
| + | $ nano thesis/ | ||
| + | $ ls thesis | ||
| + | |||
| + | draft.txt | ||
| + | |||
| + | |||
| + | '' | ||
| + | so let's change the file's name using '' | ||
| + | which is short for " | ||
| + | |||
| + | $ mv thesis/ | ||
| + | |||
| + | The first parameter tells '' | ||
| + | while the second is where it's to go. | ||
| + | In this case, | ||
| + | we're moving '' | ||
| + | which has the same effect as renaming the file. | ||
| + | Sure enough, | ||
| + | '' | ||
| + | |||
| + | $ ls thesis | ||
| + | |||
| + | quotes.txt | ||
| + | |||
| + | Just for the sake of inconsistency, | ||
| + | '' | ||
| + | |||
| + | Let's move '' | ||
| + | We use '' | ||
| + | but this time we'll just use the name of a directory as the second parameter | ||
| + | to tell '' | ||
| + | but put the file somewhere new. | ||
| + | (This is why the command is called " | ||
| + | In this case, | ||
| + | the directory name we use is the special directory name '' | ||
| + | |||
| + | $ mv thesis/ | ||
| + | |||
| + | The effect is to move the file from the directory it was in to the current working directory. | ||
| + | '' | ||
| + | |||
| + | $ ls thesis | ||
| + | |||
| + | Further, | ||
| + | '' | ||
| + | We can use this to see that '' | ||
| + | |||
| + | $ ls quotes.txt | ||
| + | |||
| + | quotes.txt | ||
| + | |||
| + | The '' | ||
| + | except it copies a file instead of moving it. | ||
| + | We can check that it did the right thing using '' | ||
| + | with two paths as parameters; like most Unix commands, | ||
| + | '' | ||
| + | |||
| + | $ cp quotes.txt thesis/ | ||
| + | $ ls quotes.txt thesis/ | ||
| + | |||
| + | quotes.txt | ||
| + | |||
| + | To prove that we made a copy, | ||
| + | let's delete the '' | ||
| + | and then run that same '' | ||
| + | This time it tells us that it can't find '' | ||
| + | but it does find the copy in '' | ||
| + | |||
| + | $ ls quotes.txt thesis/ | ||
| + | |||
| + | ls: cannot access quotes.txt: No such file or directory | ||
| + | thesis/ | ||
| + | |||
| + | > Another Useful Abbreviation | ||
| + | > | ||
| + | > The shell interprets the character '' | ||
| + | > mean "the current user's home directory" | ||
| + | > directory is ''/ | ||
| + | > ''/ | ||
| + | > path: '' | ||
| + | |||
| + | ====Key Points==== | ||
| + | *Unix documentation uses ' | ||
| + | *The shell does not have a trash bin: once something is deleted, it's really gone. | ||
| + | *Nano is a very simple text editor; please use something else for real work. | ||