Create New File

Writing program creates new file, we call file that has our program ‘source file’, because it is the ‘source’ of the program.

Before that though, we first need to create a directory to hold all our source files. By having a directory, it will help us find them later easier. So where do we create this directory?

Linux operating system can have multiple users, each user can have his own ‘home’ directory. A ‘home’ directory is where user can create new file. When a different user is using the computer, he’ll get to his own home directory. This way everyone can have his own stuff in a private area, so think ‘home’ directory as your private school locker.

Where is my home directory?

Open a terminal (CTRL + ALT + T), type ‘cd‘ without anything else. Now you are at your home directory.

How do we make sure we indeed are at home, in other words, how do we know where exactly we are? If we are in the middle of a huge tree,  we can easily get lost otherwise.

We’ll learn another useful command here,

  • command: pwd

‘pwd’, Print current Working Directory, tells us where we are in the directory tree.

So type ‘cd’ then ‘pwd’ tells us our home directory is ‘/home/frank’. Remember this is called directory path, the path to our home directory? Let’s explain what this path means,

The first ‘/’ denotes the root directory, this is the root of the tree, and every path starts here. To experiment, the following command will always bring us to the root of directory,

cd  /

The ‘home’ is a subdirectory under root, this is the directory that holds each user’s home directory.

The next ‘/’ indicates there’s more within current directory. Or, this is the separator of multiple sub-directories in any path.

The ‘frank’ is a subdirectory, and this is user Frank’s home directory.

We are home, let’s create a directory called ‘my-programs’, note that I used ‘-‘ instead of space between words. Space is allowed in directory name, but usually using space in directory or file name in Linux/Unix system is a very bad idea. Why? if you try “cd /home/frank/my program/fun games”, you won’t reach the target “fun games” directory, instead it will complain that ‘my’ directory is unable to be found, since anything after the space will not be treated as part of the ‘cd’ command. There’s way to work-around this issue, but we’d rather not to get to this trouble.

In order to create the directory, let’s learn another command,

  • command: mkdir

‘mkdir’, MaKe DIRectory, makes a directory.

Make sure we are at home directory (simply type ‘cd’), then type ‘mkdir my-programs‘, now we have our directory for the great programs we are going to write!

Let’s move on to create our program file …

For our program, we not only need to create it, we need to edit it, probably many times. We need a file editor, what editor shall we use?

Word? Power Point?

Those are all editors, but not the right ones for programming. There are actually many options, emacs can be a good one, however it may take you some time to learn it, although you’ll love it once you learn it. Let’s start with a much easier one, ‘gedit’,

On your terminal, while you are at ‘/home/frank/my-programs’ directory, type ‘gedit hello‘, you’ll see the editor shows up and now you can type whatever. Once you are done editing, click ‘save’, you’ll see ‘hello’ file created in ‘my-programs’ directory.

Great, you just created your first program file under your own directory. Well, this file is not yet a program, but we now are much closer …

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">