Login/Logout and Getting the Date and Time
Create, Edit, Compile, and Run a Java Program
Standard Input/Output Redirection and Filters
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
Loggin in, logging out, and getting date and time
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”.
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.
Shortcuts: Pico Menu Commands, Other Helpful Commands
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.
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.
^O: Write Out
^G: Get Help
^K and ^U: Cut and Uncut Text
^R: Read File
^Y and ^V: Prev Page and Next Page
^C: Cur Pos
^W: Where Is
^J: Justify
^T: To Spell
^D: Deletes a character.
^G: Displays the help page which has descriptions of all the pico commands.
^^: Mark Set
^/: Allows you to replace all or some occurrences of a word or phrase.
Shortcuts: Navigating the page, Editing commands, Saving and reading files, Exiting vi
Just like pico you can use two commands to open vi:
vi
vi filename - creates a new file with filename or opens already existing file with file name.
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 .
j: move down one line
k: move up one line
o: takes you to the beginning of a line
$: takes you to the end of a line
w: takes you to the beginning of the next word
b: takes you to the beginning of the previous word.
/ or ?: Allows for you to search a word
N: allows you to move to the next occurrence of a searched word
:# : Takes you to particular line number.
:= : Tells you the number of the line you are currently on
u: undo the previous command
yy: copies the current line
# yy: copies a specified number of lines. In the terminal, a message will appear in the bottom left corner that reads “# lines yanked”.
p: Message will appear in the bottom left corner that reads “ # more lines”.
# x: deletes a specified number of characters
d # w: deletes a specified number of words
D or dd: deletes a line.
r: replaces a character at the current position. Key sequence would be r character.
R: replaces all the characters as you type. It’s almost like you are in insert mode, but you are just overwriting text.
:r<enter> filename : Loads file after current line
:w<Enter> : writes text to file name used in vi call
:w filename<Enter> : writes text to specified file
:x<Enter> : Quits vi and saves file to file named in vi call
:q : Quits vi
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”
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.
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.
How a editor will look if making a new file
How editor will look if opening existing file
By creating or editing the file you are writing the source code of the file.
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.
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.
To run the java program simply type “java MainFileName” again replacing MainFileName with the file which contains the main class of the program.
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.
Running a program with command line arguments
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.
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.