Archive for the ‘command line’ Category

Linux Screen [session management utility]

Thursday, February 7th, 2013

terminal

Screen is a unix/linux application that allows you to run multiple processes from an interactive shell, such as Apple’s Terminal application. This nifty utility also allows you keep processes running when you close out of an interactive shell session (if you have experience working with interactive shells, you know that when you close a shell the processes running in that environment will stop running).

Screen is extremely useful for working with the Raspberry Pi, or any other remote computer to which you connect using ssh. Here are two scenarios that illustrate why I love screen. If you are intrigued, read on to learn how to use screen.

  • You are logged into the Raspberry Pi, using ssh, running an http server that publishes data from a sensor connected to the RPi. All of a sudden you get a “Connection Closed” message. Your session is abruptly closed and your http server gets shutdown because it was linked directly to your session. If you were running your http server using screen, then it would have continued running even when your session ended.
  • Let’s say that you want to run two different node servers on a remote computer. You could open two separate ssh connections to the remote computer, and start-up a node server in each one. In order to keep the servers running you will need to keep both sessions open indefinitely. With screen, you can connect to the remote server and create two separate screens, each one running its own node server. Best of all, you can log out of your session and the screens will continue to run the node servers.

Now that I’ve given you my description of what screen does, and why it rocks, here is a the official description from the man page: “Screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells).” Check out the Screen User’s Manual for more details.

Quick Start Guide to Using Screen
I am not a screen (or Unix/Linux) expert. So here is my short cheat sheet with all the screen commands that you will need to get started using this powerful utility.

Find Out What Screens are Running: When you run the command below you will get a list of all screens that are currently running. The name of each screen will be preceded by a process number and followed by the screen’s status. When the status is “attached” that means that a session is currently logged into that screen; “detached” means that the no one is logged into the screen.

$ screen -ls

Create New Screen: In order to create a new screen just run the command below, replacing the screen_name with an alphanumeric identifier. You can create two screens with the same name, though I don’t recommend it. If you do so, you can use the unique process id to differentiate between them.

$ screen -S screen_name

Connect to Existing Screen: To connect (or attach) to an existing screen you need to know it’s name or process id number. Then just run the command below using the appropriate identifier.

$ screen -r screen_name

Detach from Screen: To detach from a screen just use the key stroke combinations described below. Note that to “detach” means to disconnect from a screen without closing it. After detaching from a screen I always confirm that it is still running by using the list screens command described previously.

'ctrl-a' followed by 'd'

Closing a Screen: To close a screen you need to use the key stroke combinations described below. Remember that when you close a screen it will shut down any processes that were running in the screen.

'ctrl-a' followed by ':quit' then 'enter' key

Using Terminal as FTP Client

Saturday, May 21st, 2011

As I’ve become more comfortable using the terminal over the past year I’ve started to do more and more using this handy command line application. My newest trick is using the terminal app to connect to my web server via ftp. This is an easy and effective approach to upload and download content to your web site.

Starting the FTP Application
1. Open the Terminal app.
2. Type “ftp” into the command line to enter the ftp application.
3. You will know that the ftp app is running when you see the prompt “>ftp”.
4. Then use the command “open <ip address>” to connect to an ftp server.
5. Enter your user name and password when prompted.
6. Use the commands below to manage your files

FTP Commands
ls: directory listing on remote server equivalent to ls.
ls -l: long directory listing, featuring more details.
pwd: display name of active directory on remote server.
cd: change active directory on remote server.
lcd: change the active directory on local computer.
get: download file from the remote server.
put: transfer file and save on the remote server.
mget: download multiple files from the remote server.
mput: transfer multiple files to the remote server.
delete: delete a file on remote server.
status: display how the current remote session is configured.
mkdir: create directory on remote server.
quit/close/bye/disconnect: disconnect from the remote server
prompt: turn on/off interactive mode.
binary: turn on binary mode.
ascii: turn on ascii mode.


Installing Git on your Mac

Saturday, April 23rd, 2011

Github rocks. I started using it about a year ago and it has already saved my ass, I mean my code, from two computer crashes. Github is a service that lets you back-up and control versions of all documents associated to a project.

Git was created to support the Linux project. It is extremely powerful, it was built to enable collaboration between multiple users and even lets users create more than one concurrent version (or branch in git lingo) of the project. I use only a small part of the functionality offered by git,

I am currently working to set-up Github on my computer so that I can restore the code I lost during my most recent computer crash. Since I am having to re-learn the not-so-simple process of installing Github, I decided to create this simple tutorial to document my learnings.

My motives are only partially altruistic, I know at some point in the near future I will either need to restore my own code again and I’ve already had a few people ask for help to set-up GitHub on their computers. Here are the official installation instructions from GitHub.

Time Requirements: the answer to this question depends on what software you have loaded on your computer. If you have none of the required software it can take up to 5 hours (downloading Xcode can takes several hours). If you have all of the required software it can take you as little as 30 minutes.

Software Requirements: before you can install Github you need to have MacPorts loaded on your computer, but before you can do that you need to install Xcode. So start off by downloading both. You can download MacPorts from the link below. You will need to purchase Xcode from the app store on your mac. I recommend that you download the latest version of both to ensure that they will work together.

link to Xcode website |  link to MacPorts website

Note: Xcode takes several hours to download because it is huge (over 4GB). You can also get Xcode from your Mac install disc, though it probably won’t be the latest version and this could potentially cause issues.

Installing Software: once you have downloaded Xcode and MacPorts you are ready to get started. Github is installed by running a Unix command on the Terminal utility (more about the Terminal utility below the installation instructions). If you have Xcode and MacPorts installed in your machine skip to step 2.

  1. Install Xcode and then MacPorts to set-up your computer
  2. Open the Terminal utility, which can be found in the directory “Applications/Utilities”
  3. Type “sudo port install git-core” into the Terminal and press enter
  4. When prompted enter your password and press return (password will not show up)
  5. Wait for the installation process to end, which can take more than an hour depending on your internet connection

Terminal is an application that is included on all Macs. It provides users with access to the Unix kernel, so that they can run Unix commands and scripts. In order to use GitHub you will have to familiarize yourself with some basic Terminal commands. You will need to keep the Terminal utility open for the rest of the setup. Here is a link to one of my recent post with an overview of basic Unix concepts and commands.

Setting Up Github: once you have installed git you need to set your name and email address. This information will be used to sign files when you save them (or commit in git lingo) to a git repository on your computer, or upload them (or push in git lingo) to a git repository online. To set your name and email use the commands below on the Terminal.

git config –global user.name “your name”
git config –global user.email “your_email@email_server.com”

Downloading from a Repository: To download code from an existing repository you first need to get the project’s git URL. This URL can begin with “ssh://”, “http://”, “git://” or just a Github username. Then type the command below into Terminal utility and press enter. Git will create a new directory with the name of the project, and download all of the project files into that directory.

git clone http://www.sample_url.com/sample_folder/sample.git

Creating a New Local Repository: To create a repository from an existing project you need to navigate that project’s directory using terminal.

In the Terminal utility you can navigate through directories (or folders) using the “cd” command. Typing “cd folder_name” takes you to a folder called folder_name that exists within the current folder; typing “cd ..” takes you to the folder that contains the current one. You can also use the “ls” command to get a list of all the folders that are contained in your current directory. Once you have reached to the appropriate directory just type:

git init

Git will reply with “Initialized empty Git repository in .git/” and you will notice that a new hidden folder labelled .git has been added to your project’s directory.

Backing up to Local Git Repository: Once you have a git directory set-up it is easy to back-up your project, and restore it when needed. Here are the basic overview of the process and commands that you will need to use.

First you need to add your files to the staging area using the “git add” command. You can add a single file to the staging area using “git add filename”, or you can add the entire project to the staging area using “git add .”

Once you have added files to the staging area you will need to use the “git commit” command to add the files to your local repository. This command will move all files on the staging area to the repository. When typing this command you can use the -m modifier to add a comment about the updates that have been made to the project. Here is an example:

git commit -m “this area can feature information about updates to the code”

Online Repositories and Advanced Functionality: With this basic knowledge you can create local repositories for version control. This is all I am going to cover today. However, to take advantage of the full benefits provided by git you will need to learn how to restore your code to a previous version and how get set-up to use an online repository. Here are some helpful links for you take these next steps:


Unix on Mac: Command Line Basics

Saturday, April 23rd, 2011

I started using Unix on my Mac about a year ago. I am still a novice at using the command line but I have come to realize how powerful and useful this interface can be. To learn how to use the command line I watched two videos from the PeepCode screencast website – videos number 34, command line basics, and 37, advanced command line. Below is just a short overview of the most important concepts and commands.

Unix
Unix is multitasking and multiuser operating system that is based on a hierarchical data structure. Now a days, most computers can handle a large number of tasks and users; however, when Unix was originally developed this was not the case. One interesting thing about the design of Unix is that the system handles everything as though it were a file. This means that folders and executable files can be copied, read, moved, deleted and processed like a text file. Mac’s version of Unix is called Darwin (many different versions of Unix exist).

The Terminal Utility
To access Unix on OS X there is an application called Terminal that comes standard on all Macs. This command line app provides access to Unix via a text based interface. The benefit of using Terminal is that you have access to all of the power of Unix; the downside is that the interactions are all text-based and there is little feedback provided when commands are executed.

So now let’s dive into the terminal interface and go over many common commands. Here is a screenshot of my Terminal window. The first thing worth noticing is that the command prompt contains three important pieces of information: (1) the system name followed by a colon; the (2) the current directory followed by a space; last, (3) the current user name followed by a dollar sign. Here is an example from the screenshot bellow: “Julio-Terras-MacBook-Pro:Farm-Bridge julioterra$”. As you can imagine, the information contained in these command prompts is quite useful.

Unix Commands
It is useful to think of Unix commands as scripts or applications. The structure of most unix commands is as follows: [command] [modifier] [source] [destination]. The first two parts of this structure hold true for all commands. Modifiers are always preceded by a dash (-). The third and fourth elements are only used for relevant commands. Here is a list of the most common commands along with relevant modifiers – all of these commands are case sensitive (Here is a link to a useful wikipedia list of Unix commands).

One important characteristic of Unix systems is that when a command is successfully executed then the system does not return any information (they usually only return error messages unless the user requests a ‘verbose’ output, more on this later).

Now here is a list of common Unix commands:

  • “help” – the help command returns a list of the most common commands available.
  • “pwd” – returns the full path of the current directory
  • “cd” [path] – changes the current directory to the specified path. There are some special characters that can be used to navigate directories: “~” always refers to the root directory; “.” refers to the current directory; “..” refers to the parent directory.
  • “ls” [modifier] [path] – returns a list of the files in a given directory. The path argument is optional, if no path is provided then the command returns the files from the current directory. Common modifiers include: “l” returns more detailed information about each file; “a” returns all files in a directory, including the hidden ones; “h” when combined with “l” returns the same info as “l” by itself but it also provides more details regarding file sizes.
  • “chmod” [modifier] [source] – changes the access privileges associated to a file or directory. To understand the modifiers for this command you need to understand how access privileges are managed in Unix. There are three separate sets of access privileges that are available for any given file or directory: read (“r”), write (“w”), and execute (“e”). These privileges can be set for the user (“u”), group (“g”), others (“o”) or all (“a”). The character “+” is used to add privileges, while “-” is used to remove privileges.
  • “cat” [source] – known as the concatenate command it reads a file and outputs the content to the terminal window.
  • “cp” [modifier] [source] [destination] – copies the file or directory identified as the source to a location and name identified as the destination. It is important to note that if another file exists with the same name at the destination then that file will be overwritten. List of useful modifiers: “i” to prompt user before system overwrites another file at destination; “r” means recursive and is used to copy a directory along with all of its contents.
  • “mv” [modifier] [source] [destination] – command to move a file or a directory from the source to the destination. This command is also used to change the name of a file. Useful modifiers include: “i” to prompt user before system overwrites another file at destination; “v”, which stands for verbose, to inform user of the activities that system is doing in carrying out this task; “n” to ensure that the system does not overwrite any files in the process.
  • “rm” [modifiers] [source] – command removes files from the source location. Useful modifiers include: “i” to prompt user before system removes any files specified; “r” to instruct the system to remove all files within a directory (otherwise, the “rm” command is only able to delete empty directories).
  • “mkdir” and “rmdir” [path] – these commands can be used to create and delete a directory respectively.

To get help with a specific command you can often use one of the following modifiers: “–help”, “-help”, or “-h”. Unfortunately, the help feature is implemented in an inconsistent manner across commands so you may need to try all three options, and sometimes none of them will work.

The last important item that I want to cover is the concept of “pipe”. Pipe is a functionality in Unix that enables a user to pass the output of one command to be processed by another command. The “|” character is used to identify a pipe linkage. One command that is often used with a pipe is the “more” command. This command makes it so that a the system only outputs on screen’s worth of content at a time (the user needs to press return or the space bar to request that the system display more content). Here is how a pipe could work: “cat filename.txt | more”

That’s all for today.