Docsity

Command Line Interfaces-Learn The Linux-Handout, Exercises of Linux skills

Toggle sidebar
bg1
Figure 1: Linux file manager
For the rest of this self-guided tutorial, try keeping the web
browser running on one workspace, and switch to another to try
the various exercises. Notice how the contents of the outlines
change, as you open and close new application windows.
Switch to the second workspace, and double-click on the
icon, labelled “cs9xyz’s Home”. This will open a file manager
application, showing a view of your peronal filestore (Figure 1).
Notice how the contents are the same as your H: drive under
Windows. Use the minimise button on the Exceed window to
switch back to the Windows desktop to check this. (But be careful
not to close the Exceed window altogether!)
The Linux file manager can be used to create, delete or rename
files and folders, and to move them from one location to another. The file manager window (along
with every other application window) also has a title bar, containing minimise, maximise and close
buttons . These can be used to manipulate the size and location of the window on the
desktop, in much the same way as under Windows. Practise moving and resizing the file manager
window, as well as moving and renaming files and folders within the file manager, until you are
comfortable with how these work. (Also try right-clicking on the title bar, to see what happens)
2 Editing text files
Use the file manager to find the file myBooks.txt and double-click on it. This will open the file in
a simple text editor called “gedit” (Figure 2a), which is the default editor for most types of text
files.
You can also invoke this directly by clicking on the “Applications” menu in the top left corner of
the desktop, expanding the “Accessories” submenu, and selecting “Text Editor” near the bottom
1But note that it only appears in the Linux task bar - it isn’t present in the Windows task bar. Windows only
sees the main Exceed application - everything else is invisible to it.
1
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Command Line Interfaces-Learn The Linux-Handout and more Exercises Linux skills in PDF only on Docsity!

Figure 1: Linux file manager

For the rest of this self-guided tutorial, try keeping the web browser running on one workspace, and switch to another to try the various exercises. Notice how the contents of the outlines change, as you open and close new application windows.

Switch to the second workspace, and double-click on the icon, labelled “cs9xyz’s Home”. This will open a file manager application, showing a view of your peronal filestore (Figure 1). Notice how the contents are the same as your H: drive under Windows. Use the minimise button on the Exceed window to switch back to the Windows desktop to check this. (But be careful not to close the Exceed window altogether!) The Linux file manager can be used to create, delete or rename files and folders, and to move them from one location to another. The file manager window (along with every other application window) also has a title bar, containing minimise, maximise and close

buttons. These can be used to manipulate the size and location of the window on the desktop, in much the same way as under Windows. Practise moving and resizing the file manager window, as well as moving and renaming files and folders within the file manager, until you are comfortable with how these work. (Also try right-clicking on the title bar, to see what happens)

2 Editing text files

Use the file manager to find the file myBooks.txt and double-click on it. This will open the file in a simple text editor called “gedit” (Figure 2a), which is the default editor for most types of text files. You can also invoke this directly by clicking on the “Applications” menu in the top left corner of the desktop, expanding the “Accessories” submenu, and selecting “Text Editor” near the bottom

(^1) But note that it only appears in the Linux task bar - it isn’t present in the Windows task bar. Windows only sees the main Exceed application - everything else is invisible to it.

Figure 2: Linux text editors

of the list (you’ll probably have to scroll down to get to this entry). The operation of this editor is similar to Notepad++, which you saw last week. Experiment with adding and deleting text from the file, both using the keyboard and via cut-and-paste.

Now use the file manager to find the file HelloWorld.java from last week, right-click on it and select “Open With > Emacs”. This opens a different editor called Emacs (Figure 2). Again, the basic operation is very similar to the other editors that you have been using. Both gedit and Emacs also use syntax highlighting to indicate the structure of your code (as you saw with jEdit last week). For any given file, there will typically be a range of applications that can sensibly be used to view or edit it. You can choose between these by right-clicking on the file, hovering over the “Open With >” menu entry, and selecting from the list that appears. You can use any suitable text editor to write your code, on either the Linux or Windows systems (but remember what you’ve been told about the use of IDEs).

3 Command line interfaces

Although Windows and Linux desktops are primarily graphical environments, they both also pro- vide a command-line interface, allowing you to type commands into a “terminal window” and see the results as text-based output. For COMP101, you will be expected to compile and run your Java programs via the command line. We’ll look at exactly how to do this shortly. But first, let’s look briefly at simple use of the command line on both systems. On Linux, you can start a shell window by opening the “Applications” menu from the tool bar, expanding the menu “System Tools”, and selecting the entry “Terminal”. Under Windows, the equivalent “Command Prompt” is found at the top of the “All Programs” list. The two terminal windows look fairly similar (Figure 3), and both initially just contain a shell prompt. This typically indicates the current directory - which folder the shell is looking at, and hence which files are visible. This prompt (which will be represented throughout this tutorial by $ or >)

mv test1 myDir/test2 move test1 myDir\test2 Move a file into another folder (and rename it) ls myDir dir myDir List the contents of a folder rmdir myDir rmdir myDir Try to delete a (non-empty) folder (which fails) mv myDir/*. move myDir*. Empty the folder (by mov- ing the contents to the cur- rent directory) rmdir myDir rmdir myDir Delete the (empty) folder rm test1 test2 del test1 test2 Delete the test files

Be careful when using rm. Linux assumes you know what you are doing. So, if you ask it to delete a file, then it will be deleted. It will not simply be moved to the “Wastebasket” folder (which is what the file manager does). So it’s typically not possible to “undelete” a file that has been deleted by mistake - when it’s gone, it’s gone. The same holds for Windows and del. Note that Linux is a case-sensitive operating system - the folder Admin is different to one called admin. Be very careful to type the names of files or folders exactly as they appear in the file manager, or in the output of ls or dir. Windows is more forgiving, and will ignore case differences. Also be aware that Windows Explorer will often hide the filename extension - the last three or four characters after the final ‘.’. When using the command line, you typically need to give the full filename, including the extension.

3.2 Wildcards

The third-from-last command above (emptying the test directory before deleting it) illustrates a new idea - the use of “wildcards”. Most of the commands above specify the name of a file or folder to work with. And as the last command shows, it’s often possible to manipulate several files at the same time, by listing them on the command line. But if there are large number of files to manipulate, typing all of them out would be both timeconsuming and subject to errors. So both Linux and Windows provide a wildcard mechanism, to match the names of multiple files (or folders) at once. Create two copies of the timetable file (using cp myBooks.txt myFile1 and cp myBooks.txt myFile2), and try the following sequence of commands. Think about the results you see, and what these patterns might mean.

$ ls myFile? $ ls * $ ls . $ ls .txt $ ls myFile $ ls *file? Also try the corresponding commands with the Windows command line (using dir instead of ls).

3.3 Redirection

Another useful technique is “redirection”, where the output of a command can be saved to a file.

$ ls myFile* > out.file $ cat out.file This can be very useful when you come to test your Java programs. But note that any errors will still be displayed

$ ls yourFile* > out2.file ls: cannot access yourFile*: No such file or directory $ cat out2.file Redirection works on both Linux and Windows command line terminals. The main problem with this approach is that it only saves the output of the program, and not anything that is typed on the keyboard. Redirecting output to a file also means that you won’t see any prompts or instructions that might be displayed by the program. In once sense, this doesn’t really matter - as long as you know what information you need to supply, you can type this “blindly” and the program should run correctly. But it would be better if you could see the output (including any prompts) and still have everything saved to a file. On Linux, this can be done using the script command:

$ script -c "command " out3.file (where the -c option specifies the command to be run and captured). Try experimenting with this, using some of the commands above. (Although it will only really become useful when you start running programs that expect input from the keyboard). Note that script is only available on the Linux systems - there isn’t an equivalent mechanism under Windows.

3.4 Pipes

A variation of output redirection is the idea of a ”pipe” - using the output of one command as the input to another. Try the following in the Linux terminal window

$ ls | sort -r $ cat myBooks.txt | tail - $ ls /lib | more (press the ’q’ key to quit) Compare the results of these pipeline commands, with the output generated by the first com- mand in each pipeline on its own. Pipes are also available under Windows, though they are much less widely used. The last com- mand would work in much the same way (dir C:\Windows\System | more). Windows doesn’t typically include the same range of “filter” command such as sort, head and tail.^3 Most (traditional) Linux commands are designed to process the contents of the specified files, or (if no files are listed) to work on “standard input” as part of just such a pipeline of commands.

3.5 Command line history

When developing a computer program, you’ll typically find yourself repeating the same sequence of commands again and again - editing the file containig the source code, compiling this file, running the resulting program, editing the file to fix any errors, compiling the corrected file, running the

(^3) Although these commands are actually available on the departmental Windows system, as part of the Cygwin

package.