Common Linux Commands (for SSH)

Here is a short introduction to some Linux commands you might use when you SSH into your OpenShift Application. One shortcut you should know right away is that the tilde character (~) is an alias for your home directory. The operating system will expand that symbol to the path to your home directory.

Users do not have root privileges on their gears.
The secure shell access is quite powerful and it is possible for you to accidentally damage your application. Proceed with care and make sure you have a current backup snapshot of your application.

Listing Directory Contents

To list the contents of a folder, you can execute the ls command. This will show the contents of the directory, but will not display any information about permissions or which items are directories. To view the permissions for items, use the command ls -l. Use ls -lh for human-readable file sizes, or ls -lha to list all files, including hidden files. The output should look something like this:

[me@localhost tmp]$ ls -lha
total 92K
drwxrwxr-x.  3 me me 4.0K Jun 28 06:28 .
drwx------. 58 me me 4.0K Jun 28 03:14 ..
drwxrwxr-x.  2 me me 4.0K Jun 28 06:28 a_directory
-rw-rw-r--.  1 me me  78K Jun 28 06:28 example.txt

Reading from left to right, the first 11 characters represent the file type and permissions. If the first character is a d, it means the line refers to a directory. The next three characters are the permissions for the user, the following three are the permissions for the group, and the next three are the permissions for the "world." Each character represents a different capability: r stands for read, w stands for write, and x stands for execute. For example, the file example.txt has read and write permissions for the owner and group but only read permissions for the world. If there were a shell script in this file you would not be able to execute the script because the execute permission is not set. The final . in the 11 characters indicates that extended attributes are enabled; in this case it is SELinux permissions.

The next field, always a number, gives the number of directories and files that are linked from that resource. For example, a_directory contains two "things" inside it: the first is a reference to the directory above it and the second is the directory itself.

The next item is the owner of the resource, in this case the user me, followed by the Linux group for that resource, which in this case is also me. They do not have to be the same value.

Following that is the size of the resource and then the date and time of last modification. Finally, we have the name of the resource. By default, anything that starts with a dot (.) is not output when you use the ls or ls -l commands. For example .myHiddenDirectory would not show up. Again, to get ls to show the hidden files you need to use the -a flag.

There are two special resources that will always show up when executing ls -la: the . and .. resources. The single dot (.) refers to the current directory, which is good to know from a permissions perspective, and the double dot (..) is the directory above.

To change to the parent directory, you can use the command cd ... If you want to execute a file in a certain directory you have to first make sure the execute bit is set (which we will show you how to change soon) and second, preface the command with a ./. So, for example, the way to execute a script would be ./ Hopefully you now see how the . and .. are expanded in those two commands.

Changing Permissions

To change permissions on a file or directory, you use the chmod command. As with most Linux commands, you can use chmod --help to get a brief help output and use man chmod to get a more detailed explanation. We are going to show you the basic syntax, which looks like chmod who action permission resource where:

  • who is:

    • u = user

    • g = group

    • o = world

    • a = all three of these groups (ugo)

  • action is:

    • + = add the permission

    • - = remove the permission

    • = = whatever permission specified; overwrites the previous permissions

  • permission is (we are only listing the most common):

    • r = read

    • w = write

    • x = execute

For example, if we wanted to change the execute settings on example.txt for the owner we would do this at the command prompt:

$ chmod u+x example.txt

If we wanted to change the permissions on all the contents of a_directory and any subdirectories to maximum permissiveness, we would do

$ chmod -R a+rwx a_directory

The -R option is to apply the permissions recursively. Be careful with using -R - with great power comes great responsibility.

Working with Files and Directories

In Linux, you can carry out various operations on files and directories from the command line.

Creating Files

You can create files by opening the new filename in your text editor. For example, Steve likes Nano as a text editor (nice and simple), so he would just do nano myNewFile.txt. Katie prefers Vim, so she would use the command vim myNewFile.txt. If you wanted to create an empty file, you could use touch. If you execute the command touch myNewFile.txt, it will create the new empty file. You can also use touch to update the last modified time of an existing file to the time when you execute the command.

Moving Files and Directories

Moving files is achieved with the command mv. The syntax is pretty simple:

$ mv old/dir/filename.txt new/dir/newfilename.txt

This command is also commonly used to rename files by moving the file to a new name in the same directory:

$ mv oldFileName.txt newFileName.txt

Copying Files and Directories

The command for copying files and directories is cp. As with the chmod commands, you can also use it recursively with the -R flag. Here is the syntax for the command:

$cp file.txt directory/toCopyTo/

Deleting Files

Deleting a file is also very simple. It is accomplished with the command rm. To remove a file filename.txt, you could execute the command:

$ rm filename.txt

As with changing permissions, you can execute deletion recursively; however, you should use this with extreme caution as there is no undo button. For example, to remove all text files from the current directory downward, you could do:

$ rm -r *.txt

You can also use this command to remove a directory and all its contents at the same time:

$ rm -r myDirectory/

Again, use this with extreme caution!

If you are prompted to confirm each deletion and you feel confident you are correct, you can use the -f flag to tell rm to force the removal.

Creating Directories

Directories are created with the mkdir command:

$ mkdir myNewDir

Deleting Directories

The safe way to remove a directory is to:

  1. Remove all the files in the directory.

  2. Remove the directory using the rmdir command:

    $ rmdir myEmptyDir

As mentioned before, you can use rm to do the same thing in one fell swoop, but you should do so cautiously.

Where To Learn More

There are plenty of websites that can teach you the basic Linux commands, and there is a collection of tutorials at the tutorial site. We would also recommend the following books:

  1. Linux Pocket Guide, 2nd Edition by Daniel J. Barrett (O’Reilly)

  2. The Linux Command Line: A Complete Introduction by William E. Shotts Jr. (No Starch Press)

If you want to have a system to practice these commands with we recommend installing the Fedora or CentOS operating system, either as a dual boot on your machine or in a virtual machine. We suggest Fedora or CentOS because they have the closest syntax to the shell on Red Hat Enterprise Linux, which is the OS underneath OpenShift Online.