In this appendix, we give a short introduction to the Linux filesystem for Windows (and Mac nonterminal) users. You will need this information because your application will be hosted on a Linux machine (Red Hat Enterprise Linux, in particular). 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.
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 differently 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 ./myScript.sh. Hopefully you now see how the . and .. are expanded in those two commands.
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.
In Linux, you can carry out various operations on files and directories from the command line.
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 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.
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 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/. Did we mention you should use this carefully?
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.
Directories are created with the mkdir command: mkdir myNewDir.
The safe way to remove a directory is to:
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.
There are plenty of websites that can teach you the basic Linux commands, and there is a collection of tutorials at the Linux.com tutorial site. We would also recommend the following books:
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.