Basic Command Line

Learn Unix command line and guarantee a brighter future

Unix-like systems command line

This article is intended to help you get started with UNIX "basic command line" and get you familiar with the term.

There are many different versions of UNIX and they share common similarities, the most known versions out there is Sun Solaris, FreeBSD, GNU/Linux, and MacOS X.

It is important to know that most servers relies on such systems, and getting to know the command line is a key to success and a bright career; Almost every company requires that you at least know a little bit about these type of systems.

Once you get to one of these systems you can easily find the command line by looking for a program called "Terminal" or if you are accessing remotely from other operating systems such as Windows you would normally get directly to the command line.

A common software used in Windows to access an Unix-like operating system is Putty, it is free and you can also find a portable version so you can carry around.

Note: The protocol utilised to access the server is called SSH (Secure Shell)

Manual

This is probably one of the most useful commands in UNIX, because it will provide you with all the information you need to know about the command, for instance you can see how to use the command and which parameters you can pass to get different output based on your needs.

E.g:

$ man ls

Will output something like:

NAME
       ls - list directory contents

SYNOPSIS
       ls [OPTION]... [FILE]...

DESCRIPTION
       List information about the FILEs (the current directory by default).  Sort entries alphabetically if none of -cftuvSUX nor --sort.

       Mandatory arguments to long options are mandatory for short options too.

       -a, --all
              do not ignore entries starting with .

Note: To exit from the man page just type in q

Directories:

File and directory paths in UNIX use the forward slash "/" to separate directory names in a path.

E.g:

/ "root" directory

/usr directory usr (sub-directory of / "root" directory)

/home/rodrigo directory rodrigo (sub-directory of /home "home" directory)

The current directory (.)

In UNIX, "." means the current directory, so if you enter:

$ cd .

Note: $ the dollar sign is a symbol commonly used to indicate the command line. It simply means that the rest of the line is a command, rather than a sentence. You do not type the $ sign because as previously mentioned it is just an indication.

The above command will change the current directory to the current directory, because "." means current directory. This might not seem very useful but you will see that you can save a lot of typing.

The parent directory (..)

".." means the parent of the current directory, so if you enter

$ cd ..

It will take you back one directory, for instance if the hierarchy looks like "/home/rodrigo" then the command above will take you back to "/home".

You can go back as many directories as you like by duplicating the (..) with the forward slash in between (../..), so this would take you back two directories up on the hierarchy.

$ cd ../..

Note: typing cd with no argument will always take you to your home directory. This is very useful if you are lost in the file system.

You could also use the ~ tilde sign as a parameter to go to your home directory:

E.g:

$ cd ~

you can also omit to specify your home directory when changing directory:

$ cd ~/projects

it is the same as:

$ cd /Users/rodrigo/projects

Basic Commands

When you first login into the system your current directory will be your home directory. Your home directory has the same name as your username, for instance, rodrigo, and it is where your personal files and subdirectories are saved.

The initial directory might be different in other UNIX systems such as MacOS X.

Linux:

/home/rodrigo

MacOS X:

/Users/rodrigo

pwd (return path to the working directory)

To see in which directory you are currently on, type:

$ pwd

Output:

/Users/rodrigo

ls (list directory contents)

To find out what is in your home directory, type:

$ ls

Output:

Android            Copy               Downloads          Games              Movies             Public             backup             source-install.log
Applications       Desktop            Dropbox            Google Drive       Music              VirtualBox VMs     compile-cache      tmp    Documents          Library            Pictures           Windows Images

As I have mentioned earlier, you can pass parameters to list more information, for instance we can use -a to list other directories that begins with a ".".

Hidden directories or files in UNIX are identified with the leading dot ".".

E.g

$ ls -a

Output:

.CFUserTextEncoding                                 .openvpn-connect.json                               .zshrc
.DS_Store                                           .pear                                               .zshrc.pre-oh-my-zsh
.NERDTreeBookmarks                                  .pearrc                                             Android
.Trash                                              .php_history                                        Applications
.Xauthority

as you can see all hidden directories and files was listed, for more information about which parameters you can pass, use the man command as I mentioned earlier.

mkdir (Making Directories)

Sooner or later you will feel the need to create directories to work on projects etc, you can simply create a directory by typing:

$ mkdir projects

Output:

There will be no output, because UNIX does not output anything when a command has been successfully executed. But if you use the command mentioned above "ls", you should be able to see your new directory projects.

cd (change directory)

You are now probably familiar with this command, because I have mentioned above several times; suppose we want to enter the directory projects that we created earlier on.

$ cd projects

Now if we use the command "pwd" that you've learned earlier on, you should be able to see in which directory you are currently on.

$ pwd
/Users/rodrigo/projects

A follow-up on this tutorial will be published weekly until you reach a good level of understanding about UNIX command line.

Comments

blog comments powered by Disqus