EE109 – Spring 2023: Introduction to Embedded Systems
Command Line Help
The command line interface (CLI) on Macs and PCs is based on the set of commands available on Unix systems since the 1970's. The Unix operating system was the original ancestor of today's Linux and Mac OS X system, and also greatly influenced the command line interface available on Windows systems. While a typical Unix system has hundreds of commands available to be used, some very complex, a user can do most of what they need to do if they are familiar with a small subset of the commands.
EE109 students need to use the command line interface on their system in order to work with the class projects and are encouraged to learn the following commands and their syntax. If you want to go beyond these, any book or web page on using the Unix "shell" will probably be an adequate reference. In the discussion below, where it mentions Unix, just assume things are the same for Mac OS X, Windows, etc. unless stated otherwise.
In the notes below, and in class discussions, the terms "folder" and "directory" are just two terms for the same thing and can be used interchangeably. People usually refer to "folders" when discussing the graphical user interface (GUI) view of the files on a computer, but use the term "directory" when working with a CLI to access files.
Starting the command line interface program
For Macs, open the Applications folder on the main hard drive and then inside there open the Utilities folder. Look for a program called "Terminal", double-click on it and a command line window will open.
For Windows, from the Start menu, search for "Command" and it should find the "Command Prompt" program in the search results. Select that and a command line window will open.
Adding a short cut to the CLI program
Since the CLI program will be used many times during the semester for editing and downloading programs, all students are encouraged to add to their computer's Desktop a short cut to the application.
For Macs, after opening the Utilities folder as described above, drag the Terminal application onto the Dock which should be at either the bottom or side of the screen. Once that's done you can start the Terminal program by clicking the icon in the Dock.
For Windows, start the Command Prompt application as described above, and then right-click on the program icon in the task bar at the bottom of the screen. Select "Pin this program to taskbar". Once that is done the icon for Command Prompt will remain in the taskbar even after the program has been exited and you can start it again by clicking the icon in the taskbar.
The prompt string
When the CLI program is waiting for a command to be entered, it usually prints out some sort of prompt character or string of characters. This can be something simple like a dollar sign, or could be a longer string containing the name of the computer and/or the folder you are working in, or other information. Here are a couple of examples:
$ <- a single dollar sign for a prompt mylaptop.usc.edu:Desktop/ee109/lab4 > <- a more elaborate prompt string
The prompt string is simply a output that tells you that the CLI program has finished the previous command and is waiting for you to type another command. It is not part of the command to be executed, and should not be typed when entering a command. In the examples below the prompt is shown as a single dollar sign, but the prompt string on your system will likely be different, and can be modified to suit your tastes.
File and folder names
Users are strongly advised to not create subdirectories or files with spaces in the name. Names with spaces are fine when working with files via a GUI, but can cause problems when using a command line interface. Rather than calling a file "My Program.c", call it "My_Program.c" or something like that.
The file system and navigating around it
To use the command line interface it's also necessary to have a basic understanding of the file system layout. The files are laid out like an up-side-down tree, with the "root" of the file system being at the top. In Windows and/or Mac, starting a Command Prompt (Windows) or Terminal (Mac) will put you in your main User directory (i.e. C:/Users/Tommy). From there you can navigate to your Documents or other folders. Directories are created at each level of the file system tree and files are placed in the various directories. Directories can have other directories inside them (subdirectories) and these can be nested as far down as you want to go.
The directory "Desktop" is a special subdirectory in your home directory that happens to contain all the files and folders that you see on the desktop when looking at the computer's screen with the graphical user interface. If you use the CLI program to add or remove a file in the Desktop subdirectory, it will also appear or disappear in the GUI view of the desktop.
The slash character "/" is used as a separator between the names of directories and files. For example the name
Desktop/ee109/lab0/test.c
refers to the file "test.c" in the "lab0" folder which is inside the "ee109" folder which is inside the "Desktop" folder.
When using a CLI you are always considered to be located at some point in the file system tree, and any commands you issue are executed relative to that point in the file system. When the CLI program is started you are in your home directory. Moving to a different point in the file system (to another directory) is done with the "cd" command (change directory). To move from the current directory down into a subdirectory just give the name of the subdirectory on the "cd" command line.
$ cd Documents
To move up a level, use the command
$ cd ..
These can be combined in various ways to move to wherever you need to go.
$ cd ../lab4 <- move up a level and then down into "lab4", a neighboring directory $ cd lab4/data <- move into directory "data" which is in directory "lab4" $ cd ../.. <- move up two levels
If you aren't sure where you are in the file system, use the "pwd" (print working directory) command to find out.
$ pwd /MyDisk/Users/weber/Desktop/ee109/sub4
The command "cd" by itself will move you back to your home directory.
Listing the files in a directory
The "ls" command is used to see a list of all the files (and subdirectories) inside the current directory.
$ ls lab4.c lab4.o main.hex main.elf Makefile $
To see the contents of a subdirectory without moving into it, put the name of the directory on the line after the "ls".
$ ls lab7 lab7.c Makefile data.txt
If you want more detailed information on the files, use the command "ls -l" and it will show the size of the files, modification dates, etc.
You can use the * as a wildcard to see all files that meet a certain pattern
$ ls *.c <- shows all files that end in ".c" $ ls lab6.* <- shows all files that start with "lab6"
Viewing text files
Most of the files you will work with in programming assignments are text files whose contents can be viewed on your computer. These can be viewed using a text editor like emacs, vi or vim, but can also be viewed (but not edited) with the "more" command.
$ more lab7.c
This will cause the contents of the file listed on the command line to be shown on the screen, one screenfull at a time. Typing a space will cause the next screenfull to be display and you can continue this to the end of the file. If you want to quit seeing the file at any point, just type a 'q' (for 'quit').
Creating new subdirectories
To create a new subdirectory inside the directory you are currently in use the "mkdir" command.
$ cd <- move up to your main user directory $ cd Documents <- move down into "Documents" directory $ mkdir lab0 <- creates a directory "lab0" underneath <- your current location (i.e. Documents)
You have now essentially created a directory "Documents/lab0" underneath your main user directory. Note that creating the subdirectory doesn't move you inside it. In this case you'll have to do "cd lab0" to get into that directory.
Copying files
The "cp" command copies files. The syntax is
$ cp source destination
If the destination name doesn't exist, a copy of the source file will be created with that name.
If the destination is the name of a directory, a copy of the file (with the same name) will be created in that directory.
$ cp lab6.c lab7.c <- creates a copy of lab6.c called lab7.c in the current directory $ cp Makefile lab0 <- creates a copy of Makefile in directory "lab0" <- which must already be created $ cp Makefile ../lab1/ <- copies Makefile from the current directory up and over into <- the neighboring "lab1" directory $ cp -r lab6 lab7 <- if lab6 is a directory underneath your current location, <- this will copy the entire folder and its contents to <- (the -r = recursive copy, copying all files underneath <- the source folder) <- a new directory, lab7 $ cp *.c ../lab7 <- copies all files ending in ".c" from your current directory <- to the lab7 folder defined in the parent directory
Renaming or moving files
The same command "mv" is to rename a file and to move it to a different location. The syntax is the same as with the "cp" command.
$ mv source destination
If the destination name doesn't exist, the file's name will be changed to be that name.
If the destination is the name of a directory, the file will be moved from its current location to that directory keeping the same name.
$ mv test5.c test5b.c <- change the name of test5.c to be test5b.c $ mv hw3.txt ../oldfiles <- move hw3.txt to be in the directory "oldfiles"
Deleting files and subdirectories
To delete files, use the "rm" command.
$ rm test4.c test4.o <- deletes the files test4.c and test4.o
To delete a subdirectory use the "rmdir" command.
$ rmdir oldlab3
This command will only work if the subdirectory is empty (has no files in it) so if you want to trash a directory full of files, you have to use the "rm" command first to clean it out ("rm *" will remove all the files in the current directory, but be careful you are in the right directory before executing that command so that you don't accidentally delete something you need), then use the "rmdir" to get rid of the directory.
Summary
- cd - Change position in the file system
- pwd - Print out current location in the file system
- ls - List files in a directory
- more - View the contents of a text file
- mkdir - Create a new subdirectory
- cp - Copy files
- mv - Rename or move files
- rm - Delete files
- rmdir - Delete a subdirectory