This document serves as a simple introduction to Linux
Andrew L. Mackey
This will provide you with a brief introduction to the following:
ls
List the contents of a directorypwd
Print the current working directorycd
Change the current directoryrm
Remove a file/directorycat
Display the contents of a filemkdir
Make a directorycp
Copy a filejpico
A simple command-line text editorjavac
Java compilerjava
Java runtimechmod
Change permissions of files or directorieschown
Change owner of files or directorieschgrp
Change group owner of files or directoriestar
Tape Archives
If you are connecting to a remote system, you will need to know the account information, server name and port number.
ssh -l yourusername -p theserverportnumber the.server.address
ssh -l nlpprof -p 12345 server.uafs.edu
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.
If you need to copy a file from one location to another, you can use the cp
command:
cp /path/to/file /path/to/new/location
After you execute 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
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
.
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. This will allow any class with a main()
method to be executed:
java Test
At this point, you should be able to see "Hello Lions" displayed on the terminal.
.java
files.Alternatively, if I know that my home directory is located in /home/amackey
, I can simply type cd /home/amackey
.
In Linux and Unix operating systems, permissions are assigned for 1) the file owner, 2) the group owner, and 3) all others. We can assign read, write, and execute privileges for any of these three set of owners. To view the permissions assigned to a file, we can use ls -l
.
code:~ # ls -l -rw-r--r-- 1 amackey root 15 Dec 30 20:46 file.txt
On the left side of the output, you will see -rw-r--r--
in the output. This represents the permissions assigned to the file named file.txt
. There are a total of 10 characters that represent the permissions and they are listed as follows.
From the listing above, we can see that the listing is not a directory, the owner of the file is amackey, and the group that owns the file is root. The owner (amackey) has read and write privileges. All members in the root group can read the file. All other members also have read privileges. Privileges can easily be modified. Let's assume that we are the owner of the file. To change the permissions, you first need to know the following:
These permissions are additive. Let's assume that we wanted to assign read, write, and execute permissions; we would assign a value of 7. If we only wanted to assign read and execute privileges, we would use a value of 5 (4 for read + 1 for execute). If we wanted to revoke permissions, we can use the value 0.
Modifying privileges can be accomplished by using the chmod
command. Remember, the ordering is user, group, and then all others. If we wanted modify the permissions of the file.txt
by assigning read, write, and execute privileges to the owner; read and execute privileges to the group; and no privileges to all others, we would use the combination 750
as follows:
chmod 750 file.txt
We can review the new permissions by using the ls -l
command.
code:~ # ls -l -rwxr-x--- 1 amackey root 15 Dec 30 20:46 file.txt
If we wish to change the owner, we can use the chown
command. Similarly, we can use the chgrp
to change the group owner. Both of the following syntax:
chown newowner somefilename chgrp newgroup somefilename
We can change the owner to mackey
and the group to users
for file.txt
.
code:~ # chown mackey file.txt code:~ # chgrp users file.txt code:~ # ls -l -rwxr-x--- 1 mackey users 15 Dec 30 20:46 file.txt
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.
#!/bin/bash
instructs the operating system to use the /bin/bash
scripting language.set -e -x
will stop further processing if an error is generated (-e
) and prints every command to the console before it is executed (-x
).javac Test.java
compiles the Test.java
files within the same directory as this script.java Test
runs the Test.class
output from the previous command.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
When we want to send a collection of files, .zip
files are a commonly used to compress multiple files into a single file that is easy to transmit. Another type of file is known as a "tarball". Tarballs get their name as a result of the TAR command (TAR = TApe Archive) and represent a collection of files that are combined into a single file using this command. This produces an uncompressed file with a .tar
extension. To compress the file, we commonly use another utility known as gzip
. This will allow us to compress files into something with smaller. Together, both of these commands produce a file with a .tar.gz
extension (or .tgz
). There are other compression utilities that we can use, but we will save that for a later discussion.
To create a tarball and use the gzip utility for compression, we can easily accomplish this using the z
attribute with the tar
command:
tar -zcvf myfiles.tar.gz Test.java build.sh
Here is a quick breakdown of the attributes used with this command:
z
- Use the gzip
utility for compressionc
- Create an archivev
- Verbose (which means to show everything that is happening while it is happening)f somefilename
- use this to specify the filename of the archiveFor our example, we created a file named myfiles.tar.gz
and stored files named Test.java
and build.sh
within this archive.
Extracting files from an archive file is just as easy as creating them. We can specify the x
attribute to extract files from an archive. The following example will extract the contents of the archive file in the current directory where we are located.
tar -xvf myfiles.tar.gz
If we wish to extract the contents of the archive file into a specific directory, we can use the -C somepath
attribute.
tar -xvf myfiles.tar.gz -C /uafs/my/new/path/
We can also list the contents of an archive without extracting them by using the t
attribute.
tar -tvf myfiles.tar.gz