Introduction to Linux Commands

This document serves as a simple introduction to Linux

Andrew L. Mackey

Overview

This will provide you with a brief introduction to the following:

 

Basic File System Commands

Navigating a Linux file system is relatively straightforward. First, you need to begin by knowing how to list the contents of your current directory. This can be accomplished by typing following:

ls

The output from this command will list all directories and files within the directory in which you are currently located. You may also need to know the current directory in which you are presently located. To print the working directory (pwd), type the following:

pwd

Your home directory is generally represented by the tilde ~ symbol. You generally have full access to read and modify files and directories within your home directory. If you are not presently in your home directory, we can use the cd command to change the current directory and specify the path where you want to go after it:

cd ~
or...
cd /mackey/path/to/some/dir

If you would like to delete some file, you simply need to use the rm command.

rm somefile.txt

You can remove multiple files by using a wildcard. For example, if I would like to remove any file with the .java extension within the current folder (denoted by ./), I would execute the following:

rm ./*.java

If you would like to remove a directory that contains files or subdirectories within it, you will need to specify the -rf attributes:

rm -rf ./somedir

Sometimes, you may just want to display the contents of a file to the console. You can use the cat command:

cat somefile.txt

You can create a directory by using the mkdir command:

mkdir mackeydir1

You can now type ls to see that the folder has been created and then type cd mackeydir1 to navigate into the directory.

 

Command Line Operators

After you execute most commands, the output is generally displayed to the terminal you are using. If you would like to save the output to a file, you can do so by using redirection operators. The > symbol can be used to save the output from a command to an indicated file. For example, the output from the ls command could be saved to the myoutput.txt as follows:

ls > test.txt

Consider a situation in which you were wanting to use the cat command to display the contents of a large file. However, you would like to filter the results and display only lines that contain a specific word. You can use the grep command to apply such a filter and we will send the output from the cat command by using | the pipeline operator

cat largefile.txt | grep mackey

 

Command Line Editors

There are several excellent editors for you to utilize on the command line. For this class, I will emphasize the use of jpico for simplicity. Simply type the following command to start:

jpico

This will start the command line editor. Most of the commands/keyboard shortcuts that you need to know are preceeded by the ^ character followed by some other key. This represents the Control key on your keyboard. To save a file, you need to use ^O which means press and hold the Control key and press the letter O.

When you are ready to exit the editor, you can use the shortcut ^X.

 

Compiling Java Applications

When you first begin writing Java applications, you will create a text file with the .java file extension. For example, if I wanted to create a Java class file (for now, you can consider simply acknowledge this as a program or application) with the name Mackey, then I will need to create a file named Mackey.java.

Let's begin by creating our first Java application. The class name will be called Test, so this implies that the filename will be named Test.java. To get started, you can type the following command:

jpico Test.java

This will launch the jpico editor and save the results in the Test.java file. You are also welcome to use other editors such as vi or emacs.

Be sure to save your work frequently! Next, you will automatically add the following lines of code within each Java class while replacing Test with the appropriate class name (again, this should match the filename exactly without the .java extension).

public class Test {
	
	public static void main(String[] args) {

		//insert your code here

	}

}

Let's create a very simple "Hello Lions" application (because "Hello World" is way overused). This requires that we use the function System.out.println( "some string" ); to output our message to the terminal.

public class Test {
        
        public static void main(String[] args) {

                System.out.println("Hello Lions");
        }

}

Once we save the file in jpico and exit the editor using the ^X command (this will ask you to save before you quit), we are ready to compile the class file into machine-readable code. The javac command will take the source code file that ends with the .java file extension that people can read and output a file with the same name except with the .class extension. This is referred to as bytecode and is executable by the Java virtual machine. To generate the executable machine code, we need to run the following command:

javac Test.java

Assuming that we have no errors, you should be able to type ls and see that we have a Test.java file and a Test.class file. As long as we have the appropriate file with the .class extension, we can run the program by using the java command and specifying the class name without the .class extension:

java Test

At this point, you should be able to see "Hello Lions" displayed on the terminal.

Alternatively, if I know that my home directory is located in /home/amackey, I can simply type cd /home/amackey.

 

File Commands

Alternatively, if I know that my home directory is located in /home/amackey, I can simply type cd /home/amackey.

 

Scripting

Often, we are required to execute the same commands in a sequence. A script will allow us to group these commands together and execute them. The next few steps will demonstrate how to write a script to compile and run the Java files. Create a file named build.sh using your preferred command-line text editor:

#!/bin/bash
set -e -x
javac Test.java
java Test

Let's breakdown each line so that you understand what is happening.

The next step is to ensure that the script build.sh is executable, so we need to set the appropriate permissions:

chmod 755 ./build.sh

At this point, you are now ready to run your script!

./build.sh