Linux Tutorial 

Created by : Mikayla Buford, Xinrong Geng, Ben Holzem, and Xander Hasan

Login/Logout and Getting the Date and Time

Navigating Through Files

Text Editors

Create, Edit, Compile, and Run a Java Program

Standard Input/Output Redirection and Filters

 

Login/Logout and Getting Date and Time

To login type “login” to the command line and enter your username and password. If there are no accounts you will be asked to create an account.

To logout type “logout” and you will be logged out. If there is only one account it will say to type exit to leave the command line

To get the date and time type “date” into the command line

 

PCITURE10.JPG

Loggin in, logging out, and getting date and time

 

 

Navigating Through Files Using Linux Commands

In this section, we will introduce basic Linux commands for printing directories, returning files in current root files.

CLI: Command Line Interface

“pwd”: print working directory

It tells you which directory you are currently at (i.e. your location).

It will show you your location: e.g. “/home/(your username)”

“ls”: list contents of current directory

It tells you what files do you have in the directory you’re currently at (i.e. what do you have).

It will show you your files in the directory: e.g. “desktop  pictures  music  downloads  etc.”

“clear”: everything disappears

“cd” command is used for navigation through directories and folders^^ 

e.g. type “cd Music” you will be in the Music folder, if you want to know if you are in this folder, you can know it by then typing “pwd”, it will show “/home/(your username)/Music”, if you want to know what files are in the music folder, type “ls”.

Xinrong Geng --avigate with commands.PNG

 

Creating and Editing Files

In this section, we will introduce you to two common editors used in the Linux terminal, pico and vi. You can use editors to create new files and edit already existing files as well. We’ll start with the easier of the two, pico.

Pico Editor

Shortcuts: Pico Menu Commands, Other Helpful Commands

Opening Pico:

If you want to create a new file you can use the command,

            pico filename or pico.

To open an already existing file, you must first navigate to the file’s parent directory and then enter the command on a new line,

            pico filename.

If you don’t navigate to the file’s parent directory, then a new file will be created with the same filename.

Once pico is opened, you will see a menu at the bottom of the screen. This menu includes the key commands you will use the most while editing your file.

 Screen Shot 2017-10-28 at 10.55.30 AM.jpg

The caret sign, “^”, next to each letter means that you must hold down the control button while typing the letter key. So if you wanted to exit the pico editor, which is done using the ^X key command, you would hold the control key and press the "x" key. 

 Now let’s explain what each command does.

Pico Menu Commands:

 

Other Helpful Commands:

Vi Editor

Shortcuts: Navigating the page, Editing commands, Saving and reading files, Exiting vi

Opening Vi:

Just like pico you can use two commands to open vi:

Vi is a mode editor, meaning that it has a command mode and an insert mode. In command mode, the editor is expecting only key commands to edit text and navigate the page. Only in insert mode can you enter in text.

When vi opens it’s in command mode. In order to switch to insert mode, you must press the “I” key. To leave insert mode, press the Esc key and the editor will return to command mode.

 

We’re going to separate the types of commands into 4 groups: navigation, editing, saving and reading files, and exiting vi.

Vi was designed using a QWERTY keyboard with no arrow keys, so key commands were used to navigate. While some UNIX platforms allow for the arrow keys to move the cursor, the key commands are a still good to know .

Editing Commands:

Yanked lines.jpg

 

Saving and Reading Files:

Exiting Vi:

Creating, Editing, Compiling, and Running a Java Program

Traverse to the location of the desired file. An example of a relative path to lis location may be “programs/program1”. An absolute path could be “/mnt/c/users/username/programs/program1”

The file would be located in “program1” with a name something like “file.java”

 

Creating/Editing a Program

To create or edit a java file open or create a new file with a line editor with the desired name of the file followed by the extension “.java”.

For example if you wanted to create a new java file with the name “hello” using vi you would write the “vi hello.java” into the command line. If you had an existing file with the name “goodbye” and wanted to edit you would type“vi goodbye.java” into the command line.

 

Picture1.JPG

Command to open editor

 

Doing either of these will open the line editor with either a blank page for a new file or the file you wish to edit. From here you can create or edit the java file as you please.

 

Picture2.JPG

How a editor will look if making a new file

Picture3.JPG

How editor will look if opening existing file

 

By creating or editing the file you are writing the source code of the file.

 

Compiling a program

To compile the java file in the command line type “javac FileName.java” replacing FileName with the file you wish to compile. This will create a class file with the same name FileName.

If there are multiple files java files in the project compile them all before trying to run the program.

 

Picture4.JPG

Compiling a program

 

This step converts the source code into bytecode. Then the java virtual machine translates the bytecode into machine code to run the program.

 

Running a Program

To run the java program simply type “java MainFileName” again replacing MainFileName with the file which contains the main class of the program.

 

Picture5.JPG

Picture6.JPG

Running a program

 

To use command line arguments type the arguments after the file name with a space between them. For example if you wanted to run a program called “program1” with the arguments “arg1” and “arg2” you would type “java program1 arg1 arg2” into the command line.

 

Picture7.JPG

Picture8.JPG

Running a program with command line arguments

 

 Standard Input/Output Redirection and Filters

A Linux command takes what you type on the keyboard, or the standard input and Linux displays the appropriate standard output on the screen. An easy way to send the standard output as the input for either a file or another command is by using something called redirecting output.

To store the standard output to a file, use the “>” operator after your command, followed by the file’s name. If the file doesn’t’ yet exist, it will be created. Here is an example of this:

                                

The command cat concatenates files and outputs them all together in the standard output.                                

cat > example_file.txt

CS

367

is great!

Ctrl-d

The contents of  example_file.txt would read:

CS

367

is great!


If this command were to be done again, the user’s input the second time would overwrite the current contents of example_file.txt. To avoid overwriting, the operator “>>” is used to

This sends an email with subject “This is a test” to the address test@example.com, with the file Attached as an attachment.

ensure that any input will be added to the file without changing its current contents.

If you want to send the standard output of one program to be the input for another program, we use the “|” ,or pipe, operator. The pipe essentially has the same function as the “>” operator, but is used only for redirecting program to program output.

Alternatively, if you want to send a file to be the standard input, use the “<” operator. For example, you can send emails with file attachments from the Linux terminal using input redirection: 

CS367@example.com: ~$ mail -s “This is a test” test@example.com < Attached

This sends an email with subject “This is a test” to the address test@example.com, with the file Attached as an attachment.

Filters

In Linux, a filter is normal command that when used along with the pipe output redirector, filters the standard input, and returns a standard output. For example, the filter grep searches through a file for lines matching a certain string, and returns them as standard output. Another filter awk searches the lines in a file in a specified pattern (i.e. the 5th word on each line), and does an action on that line. For example, 

$ awk ‘{rm $3} example.txt

           Line 1

           Line 2

           Line 3

would find the 3rd element of each line, then remove that element from the file. 

Filters are very useful commands to use with pipe operators, which can sort or alter the standard output in the desired way before writing it to a file with a “>” operator.