Information system paper
Command Line Interface
Maureen P. Kinkela
1
Learning Objectives
A student who successfully completes this section will be able to:
Define Command Line Interface
Describe its purposes.
Access Command Line Interface for the current operating system
Understand the purpose of a batch file
Create and run a simple batch file.
Above is the list of objectives for this lecture. Command line Interface is an enormous topic and we will only be able to scratch its surface in the time we have, but it is really useful to understand a little about what is going on underneath the user interfaces that we use for convenience.
2
Week 9 – Command Line Interface
What is Command Line Interface?
Techopedia.com defines Command Line Interface in the following way:
Command line interface (CLI) is a text-based interface that is used to operate software and operating systems while allowing the user to respond to visual prompts by typing single commands into the interface and receiving a reply in the same way.
CLI is quite different from the graphical user interface (GUI) that is presently being used in the latest operating systems.
CLI is an older method for interacting with applications and operating systems and is used to perform specific tasks required by users. CLI is a text-based interface, unlike the GUI, which uses graphical options that enable the user to interact with the operating system and applications.
3
Week 9 – Command Line Interface
What is Command Line Interface?
Command Line Interface is used in various ways:
To manipulate Operating Systems (Windows, Linux, OSx)
To run applications in interactive mode (git)
To operate programming languages in interactive mode (Python)
Our purpose in this lecture is to understand the various ways in which command line interfaces are used.
4
Week 9 – Command Line Interface
DOS
We will spend most of our time with DOS, doing some exercises to help us understand the different aspects of command line processing.
5
https://www.lifewire.com/dos-commands-4070427
http://edu4java.com/en/concepts/ms-dos-commands-modifiers-parameters.html
Week 9 – Command Line Interface
Syntax for DOS Commands
Here are some links to DOS command listings. DOS stands for “Disk Operating System.” It was originally created by Seattle Computer Products for the 8086 computer. It was first licensed and then later purchased by Microsoft, who presented it to IBM for their IBM PC.
Microsoft and IBM together debugged and enhanced the operating system into its current form. It has been known variously as 86-DOS, PC-DOS and MS-DOS.
6
Week 9 – Command Line Interface
Accessing in MS-DOS
To access the DOS shell, you click on Windows System and Command Prompt.
7
Week 9 – Command Line Interface – MS-DOS Commands
To begin, let’s change the color of the interface to something a little easier to read. Type this command in at the prompt. We will talk more about colors shortly.
8
Week 9 – Command Line Interface – MS-DOS Commands
There is no substitute for practice in learning how to do anything new with computers. Let’s start with some really simple exercises. At the prompt you start out at, type the following commands.
9
Week 9 – Command Line Interface – MS-DOS Commands
The command “cd\” means “Take me to the root directory”. “cd” stands for “change directory.” “cd” with a backslash followed by no directory name, takes you to the root directory. You can also add a directory name to the command to change to a specific directory, or Windows folder. An example is “cd\Temp”. This command takes me to a directory where I place all kinds of files in transit from one place to another.
10
Week 9 – Command Line Interface – MS-DOS Commands
Now type the command “exit” at the prompt. This will take you out of Command Line mode. Go into Learn and copy the files I have provided for you into your new “Temp” directory. Then for an exercise, go back into Command Line mode, and get yourself back into the Temp directory.
11
Week 9 – Command Line Interface – MS-DOS Commands
The command “dir” with no argument means “Give me a listing of the files on the current directory”. If you add a directory name to the “dir” command, it will give you a listing of the files in that particular directory. So, the command “dir c:\Temp” will give me a listing of the files not in the root directory, but in my directory named “Temp”. Once I viewed a list of files on the current directory, I chose “Temp” and indicated “cd\temp” to reset the current directory to the “Temp” directory. Note that DOS is case-insensitive.
12
Week 9 – Command Line Interface – MS-DOS Commands
In any given directory, I can create a new directory with the “md directoryName” command, and I can remove it with the “rd directoryName” command. “Md” stands for “make directory” and “rd” stands for “remove directory.” I can also clear out a directory with the “del *.*” command. Be careful with this command! There is no simple “Undo” for this type of destructive command in DOS, as there is in Windows.
13
Week 9 – Command Line Interface – MS-DOS Commands
What I am doing here is having you make a couple of identical directories (or folders) for our exercises. Follow the steps exactly.
14
Week 9 – Command Line Interface – MS-DOS Commands
The two-part “color” command changes the Command Line interface. The command takes two hexadecimal arguments. The first one sets the background color and the second one sets the text color.
15
Week 9 – Command Line Interface – MS-DOS Commands
The command “color” with no arguments, sets the screen color back to the default.
16
Week 9 – Command Line Interface – MS-DOS Commands
The command “color 0E” reset the interface to yellow on black. The “cls” command will clear the screen.
17
Week 9 – Command Line Interface – MS-DOS Commands
In this screen, I use a command line command to call a Python program. The command is as simple as naming the program file at the DOS prompt.
18
Week 9 – Command Line Interface – Batch Files
Operating system shells are full-fledged, very complex programming languages with logic, looping and hundreds of ways to customize the functionality. So, it isn’t surprising that there would be a mechanism in DOS for combining commands into files to be run as batches. Batch files are often used to perform file maintenance activities. Companies often run them at night for routine maintenance, so that the performance of the maintenance doesn’t impact the users of the system.
The three batch files that follow use different commands to back files up. The purpose of these exercises is to examine some of the most commonly used file manipulation commands.
Create each of these batch files in a text file with a “.bat” file extension. You can run the batch files by simply typing their name at the DOS prompt. Note in this example that I included the complete hierarchical path to the file in the command. I have to do this if the batch file isn’t in the current directory, or the computer would have no way of knowing where the file I am calling is.
19
Week 9 – Command Line Interface – Batch Files
Let’s try it. Open your editor, and type the following three lines into the blank file. Save the file as “C:\temp\Hello.bat”.
20
Week 9 – Command Line Interface – Batch Files
To run the batch file, simply type its name at the prompt. Note that you do not need to add the “.bat” extension.
21
Week 9 – Command Line Interface – Batch Files
Conditionals
In these batch files, I am creating sub-folders of the Temp folder to put the backup files in. The command “md WordDocs” creates a sub-directory of the current directory called “WordDocs”. (On the other hand, the command “md \WordDocs” would create a subdirectory or folder off the root directory.) Open a new blank file and type these commands into it. Name the batch file “C:\temp\Hello1.bat”. Note that by specifying the location in the command to save the file, you also indicate where you want the file to reside. In DOS, the path “C:\temp\” is actually part of the file name. The full file name is “C:\temp\Hello1.bat”.
22
Week 9 – Command Line Interface – Batch Files
Conditionals
In Temp, call hello1. Observe the messages that scroll down the screen as the batch file does its job. Then type “dir”. Do you see what the batch file did?
23
Week 9 – Command Line Interface – Batch Files
Conditionals
Type “dir ExcelDocs.” Here you will find the backup files in their own directory.
24
Week 9 – Command Line Interface – Batch Files
Conditionals
You can actually combine the “copy *.docx WordDocs” and “del *.docx” commands into one command: “move”. Open a new blank file and copy the commands above into it. Then save it as “c:\temp2\Hello2.bat.”
25
Week 9 – Command Line Interface – Batch Files
Let’s get a little fancier. In DOS, make an additional “Temp4” exercise folder.
26
Week 9 – Command Line Interface – Batch Files
Conditionals
Let’s assume you want to do several things with your code:
Leave the original file in place.
Make a backup copy of each file backed up, with a “.BKU” extension.
Only create a new folder if the backup folder doesn’t already exist.
Output messages describing what the program is doing.
Output an error message if there are no files to back up.
In order to do all of this, open a new blank file and type this code into it.
27
Week 9 – Command Line Interface – Batch Files
Conditionals
Save it as “c:\temp3\hello3.bat”
28
Week 9 – Command Line Interface – Batch Files
Conditionals
If all goes well, this should be the result when you run it. Note that the program started with the command “echo off” and ended with the command “echo on”. You can also type “echo off” at the DOS prompt before running a program. This is because if you don’t, the system will talk to itself as it runs, repeating each line of code on the screen that it is executing. Type “echo on” after the program has completed to get your DOS prompt back.
29
Week 9 – Command Line Interface – Batch Files
Looping
It isn’t surprising that DOS batch files would have looping – after all, repeating the same action many times without error is what computers were designed to do. Let’s try a simple loop – known as a “for loop.” Type these three lines of code into a new text file and save it as “C:\temp4\Looping1.bat.
30
Week 9 – Command Line Interface – Batch Files
Looping
Is this the result you expected? What happens in the loop is that the loop control variable “x” gets initialized to 1, and then printed out and incremented by 1 until it is equal to 100, at which time the loop stops.
31
Week 9 – Command Line Interface – Batch Files
Looping
You can use looping in our backup program to list the backup files that have been created. Add code to the program as indicated in the next two slides:
32
Week 9 – Command Line Interface – Batch Files
Looping
Save the file to “c:\temp4\hello4.bat”. Run it from the Temp4 folder.
33
Week 9 – Command Line Interface – Batch Files
Looping
If all goes well, you should get a result similar to this one.
34
Week 9 – Command Line Interface – MS-DOS Switches
Displays a list of files and subdirectories in a directory.
DIR [drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N]
[/O[[:]sortorder]] [/P] [/Q] [/R] [/S] [/T[[:]timefield]] [/W] [/X] [/4]
[drive:][path][filename]
Specifies drive, directory, and/or files to list.
/A Displays files with specified attributes.
attributes D Directories R Read-only files
H Hidden files A Files ready for archiving
S System files I Not content indexed files
L Reparse Points - Prefix meaning not
/B Uses bare format (no heading information or summary).
/C Display the thousand separator in file sizes. This is the
default. Use /-C to disable display of separator.
/D Same as wide but files are list sorted by column.
/L Uses lowercase.
/N New long list format where filenames are on the far right.
The complexity of the activities performed by operating system commands is apparent in the set of what we call “switches” that apply to them. The switches follow the command in the command line and customize the functionality for our particular purpose.
35
Week 9 – Command Line Interface – MS-DOS Switches
/O List by files in sorted order.
sortorder N By name (alphabetic) S By size (smallest first)
E By extension (alphabetic) D By date/time (oldest first)
G Group directories first - Prefix to reverse order
/P Pauses after each screenful of information.
/Q Display the owner of the file.
/R Display alternate data streams of the file.
/S Displays files in specified directory and all subdirectories.
/T Controls which time field displayed or used for sorting
timefield C Creation
A Last Access
W Last Written
/W Uses wide list format.
/X This displays the short names generated for non-8dot3 file
names. The format is that of /N with the short name
inserted before the long name. If no short name is present, blanks are displayed in its place.
/4 Displays four-digit years
Let’s play with some of these switches.
36
Week 9 – Command Line Interface – MS-DOS Redirection
| Command | Description | Example |
| command > filename | Redirect command output to a file | dir c:\temp2 > OutputFile.txt |
| command >> filename | APPEND into a file | dir c:\temp2 >> OutputFile.txt |
| command < filename | Type a text file and pass the text to command | sort < OutputFile.txt |
| commandA | commandB | Pipe the output from commandA into commandB | dir c:\Windows | more |
| commandA & commandB | Run commandA and then run commandB | dir c:\temp2 *.docx & dir c:\temp2 *.xlsx |
| commandA && commandB | Run commandA, if it succeeds then run commandB | dir OutputFile.txt && dir c:\temp2\ > OutputFile2.txt |
| commandA || commandB | Run commandA, if it fails then run commandB | dir OutputFile.txt || dir c:\temp2 > OutputFile.txt |
| commandA && commandB || commandC | If commandA succeeds run commandB, if it fails commandC | dir OutputFile.txt && dir c:\temp > OutputFile2.txt || dir c:\temp > OutputFile.txt |
Redirection is another customization of command running. All commands generate some sort of output. Sometimes the output is the purpose of the command, and sometimes it is just a code that represents whether the command succeeded or not. If the output of the command is to be redirected into a file, the command uses angle brackets “>” to indicate the direction that the information flows. The vertical line in some of these redirections indicates that the results of one operation will flow into another operation in some way. This is called a “pipe.”
37
Week 9 – Command Line Interface
Linux
There are hundreds of DOS commands, switches and redirections in thousands of possible combinations. These commands can be combined into very complex batch files. Understanding it all is probably not something you would expect from many people. You use the commands that you need for the work that you do.
Moving along to Linux, we will consider a particular flavor of Linux known as “Ubuntu.” Last week we looked at the Ubuntu GUI.
38
Week 9 – Command Line Interface
Accessing in Ubuntu
If you press “CTRL” “ALT” and “T” together while in the Ubuntu GUI, it puts you in terminal mode, the command line mode.
39
https://www.pendrivelinux.com/ubuntu-linux-shell-commands-quick-reference/
Week 9 – Command Line Interface
Syntax for Linux Commands
Here are some links to Linux commands. There is also a free Linux manual available, and I am attaching a link to it in a couple of slides from now.
40
Week 9 – Command Line Interface
Linux Command Line Concepts: Unix Shells
The Bourne Shell is the original UNIX shell, or command interpreter, that was developed at AT&T. Named for its developer, Stephen Bourne, the Bourne shell is also known by its program name, sh. The shell prompt (character displayed to indicate readiness for input) used is the $ symbol. The Bourne shell family includes the Bourne, Korn shell, bash, and zsh shells.
This is a picture of Stephen Bourne.
41
Week 9 – Command Line Interface
Linux Command Line Concepts: Unix Shells
https://www.gnu.org/software/bash/manual/bash.pdf
Here is a picture of Brian Fox. He continued on as the primary maintainer of Bash until 1993. The link above will take you to the reference manual that he wrote for Bash.
42
Week 9 – Command Line Interface
Linux Command Line Concepts: Unix Shells
Bash is the GNU Project's shell. It was first released in 1989. It was written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell, hence the name Bash (for Bourne Again SHell.) Bash is an sh-compatible shell that incorporates useful features from the Korn shell (ksh) and C shell (csh). It offers functional improvements over sh for both programming and interactive use.
The improvements offered by Bash include:
Command line editing
Unlimited size command history
Job Control
Shell Functions and Aliases
Indexed arrays of unlimited size
Integer arithmetic in any base from two to sixty-four
You can install the Bash shell on any 64-bit edition of Windows 10, including Windows 10 Home. For our exercises, we will use the Bash shell that exists on our Linux-on-a-stick virtual environment.
43
Week 9 – Command Line Interface
Linux Command Line Concepts:
Sudo: To run administrative tasks in Linux, you must have root (also known as superuser) access. Logging in as root is very dangerous if you don’t know what you are doing and Ubuntu disables root by default, which prevents users from making mistakes and keeps the system safe from intruders. To run commands that require root access, use sudo.
Using sudo is better (safer) than opening a session as root for a number of reasons, including:
Nobody needs to know the root password (sudo prompts for the current user's password). Extra privileges can be granted to individual users temporarily, and then taken away without the need for a password change.
It's easy to run only the commands that require special privileges via sudo; the rest of the time, you work as an unprivileged user, which reduces the damage that mistakes can cause.
Sudo is a prefix that is used to get temporary root access to commands that need it to run properly. The access is on for the command only and requires only the user’s password, which keeps the root password secure.
44
Week 9 – Command Line Interface
Linux Command Line - man
MAN: Bash has very good help facilities which are available at all times when you are working in the command prompt. Just type “man” (for manual) as a preface to any command to access this help.
45
Week 9 – Command Line Interface
Linux Command Line - man
Here is the help available for the copy command, “CP”.
46
Week 9 – Command Line Interface
Linux Command Line
To access files on your computer using the Linux shell, you need to figure out the volume names. You can see these names if you float your mouse over the selected volumes in the Ubuntu shell. This slide shows the volume name for the hard disk on my Ubuntu computer.
47
Week 9 – Command Line Interface
Linux Command Line
This slide shows the volume name for my flash disk. You won’t see these volumes when you run Linux-on-a-stick since this is a virtual environment. You can save things to the desktop in this environment however.
48
Week 9 – Command Line Interface
Linux Command Line
This slide shows the command to get a directory of the hard disk
49
Week 9 – Command Line Interface
Linux Command Line
In this slide you can see the directory of my flash drive.
50
Week 9 – Command Line Interface
Linux Command Line
Here is a tiny “Hello World” bash script that I want to run through the command line. I saved it as a “Unix script file” in Notepad ++
51
Week 9 – Command Line Interface
Linux Command Line
This slide shows the commands you run to prepare the bash file and run it from the command prompt. The “chmod” command is used to modify or remodify the permissions for a file. It has many switches, like all Linux commands. The “+x” switch stands for “execute”, and this command gives the user permission to execute the file. “sh” calls the interpreter that executes commands read from the specified bash file. Note that the echo command outputs the text to the terminal.
52
Week 9 – Command Line Interface
Git Command Line
I would like to briefly discuss a couple of other types of command line scenarios. Those of you who took the CI 100 series of classes are familiar with the Bitbucket server. We used a client named SourceTree to push our files onto Bitbucket. Git is a command line alternative to the type of client that SourceTree is. Its purpose is version control. It has no front end, and the commands for cloning, staging, pushing, branching, and so forth are entered at the command line. The next slides show the process of pushing a file into a repository using the git Command Line. Git was created by Linus Torvalds and other kernel developers in 2005 for development of the Linux kernel, and it is distributed under the terms of the GNU General Public License version 2. The term “git” has been described as an acronym for various terms. The polite one is “global information tracker.”
53
Week 9 – Command Line Interface
Git Command Line
54
Week 9 – Command Line Interface
Git Command Line
Here are the results in Bitbucket of my git commands.
55
Week 9 – Command Line Interface
Python Command Line
Another use of command line is in using a programming language in interactive mode. This is a tiny Python program which prints out a “Hello” message, It is the program we ran from the command prompt as we were going through the DOS commands. The lines of code are saved in a file and execute one after another when the program is called.
56
Week 9 – Command Line Interface
Python Command Line
Python also has a shell which you can use to enter commands interactively. Each command is executed as soon as it is typed in and the “Enter” key is pressed.
57
Week 9 – Command Line Interface –
MAC OSx
58
Week 9 – Command Line Interface – Accessing in MAC OSx
Here are the instructions for accessing Terminal Mode in Mac OS.
59
Week 9 – Command Line Interface – Command Comparison
| Description | DOS Command | Linux Command | Mac OSX Command |
| Change Directory | cd | cd | cd |
| Clear the screen | cls | clear | clear |
| Copy a file | copy | cp | cp |
| Display text on screen | echo | echo | echo |
| Find a file | if exist | locate, grep, find | locate, grep, find |
| Leave the shell | exit | exit | exit |
| List files | dir | ls, dir | ls |
| Create new directory | md | mkdir | mkdir |
| Move a file | move | mv | mv |
I thought you would be interested to see a comparison of some of the commands I used in my examples. It isn’t surprising that the Linux commands and the Mac OSx commands are the same, since they are both based on Unix.
60
https://www.pendrivelinux.com/ubuntu-linux-shell-commands-quick-reference/
Week 9 – Command Line Interface
Syntax for OSX Commands
Here is a link to OSx commands. We could easily, really easily spend the entire quarter working with any of these shell languages. I hope that this small introduction to some of the uses of these languages will whet your appetite to play with whatever OS you have on your computer and explore more of the richness of complexity that they offer. Command line is so interesting to me – when I started working with computers, it was the only way you could work with them! Then it pretty much went away altogether as Windows dominated the business landscape for more than a decade. Now it is back with a vengeance, and I find it really useful to try to understand it better.
When you play with your OS and work with Command line, be sure to remember that you are closer than usual to your hard disk, and things can go wrong as you have fewer protections from errors in this mode. Before you press the “Enter” key, always re-read the command and confirm that you are telling the computer to do what you want it to do. Errors working with Command line commands can be costly.
But keeping this in mind, have fun with it and have a great week!
61
References
https://www.techopedia.com/definition/3337/command-line-interface-cli
http://blog.teamtreehouse.com/introduction-to-the-mac-os-x-command-line#
https://ss64.com/nt/syntax-redirection.html
https://www.lifewire.com/dos-commands-4070427
http://edu4java.com/en/concepts/ms-dos-commands-modifiers-parameters.html
https://www.pendrivelinux.com/ubuntu-linux-shell-commands-quick-reference/
http://searchdatacenter.techtarget.com/definition/Bourne-shell
https://www.gnu.org/software/bash/
https://upload.wikimedia.org/wikipedia/commons/thumb/3/35/BrianJFox.png/220px-BrianJFox.png
62