Previous Lecture lect02 Next Lecture

lect02, Thu 04/04

Hello World! + unix, vim

Topics

Programming in the unix environment

Basic Unix Commands

In the displayed commands, don’t include brackets. E.g., mkdir [name] will look like mkdir tmp, if you want to create a directory called tmp.

pwd print (the) working directory - displays the full (absolute) path to the directory you are currently in

User’s home directory has a shortcut/alias denoted by a tilde symbol: ~

~ refers to the home directory

. refers to the current directory

.. refers to the directory one level above the current one

ls [dir] lists the contents of a directory dir

ls -l lists the contents of a directory, and provides additional information about the file/directory; distinguishes between files and directories (if the line starts with the letter ‘d’, then it corresponds to a directory, if not, then it corresponds to a file)

mv [src] [dest] move a file/directory called src to a file/directory called dest (if you are moving a file (file1) to another filename (file2), then you are effectively renaming file1)

mv [src1] [src2] [dest] move the specified files to a directory called dest

rm [file] deletes/removes a file (use with caution because it deletes files irrecoverably)

rm -r [dir] recursively deletes the contents of a directory ; if you use only rm for a directory, the operating system will complain and display the error message: cannot remove [dir]: Is a directory

mkdir [name] make a new directory called name

mkdir -p [level1/level2/...] allows you to create a series of nested directories by specifying a path using -p

cd used to change the directory you are currently located in

cd .. goes to the directory above the current one

cd ~ goes to the home directory

cd - goes to the previous directory

cat [name] displays the specified file in the terminal

./[name] used to execute a compiled program

clear tmpties text in the terminal

Directory Navigation in Linux

Relative Paths

Absolute Paths

Vim Commands

The link below is a very useful guide to learn fundamental “basic eight” commands in Vim: https://ucsb-cs16.github.io/topics/vim_basic_eight

Here are some additional helpful commands in Vim (these are all done in the Command Mode)

Writing, compiling and running a C++ program (hello world) program

// hello.cpp
#include <iostream>

using namespace std;

int main() {
	cout << "Hello CS 16!" << endl;
	return 0;
}
$ g++ -o hello hello.cpp
$ ./hello
Hello CS 16!
$

Breaking down the Hello World Program

// hello.cpp
#include <iostream>

using namespace std;

int main() {
	cout << "Hello CS 16!" << endl;
	return 0;
}
#include <iostream>
using namespace std;
int main() { ... }
cout << "Hello CS 16!" << endl;
return 0;

Comments

Practice Questions

  1. Use what you know about Unix commands to accomplish the following, in order:
    • Print your current working directory
    • Create a file called sample.txt in your current directory
    • While in your current directory, create a folder in your home directory called helloWorld
    • Copy the file you just created, sample.txt into the home directory
    • Navigate to the folder you just created from the home directory from part 3
    • Move (not copy) sample.txt that’s currently in the home directory from part 4
  2. Assuming that the file quadratic.cpp exists, do the following
    • Write a g++ command where the executable a.out is produced
    • Write a g++ command where the executable quadratic is produced
  3. What is another function of the Vim command dd, apart from deleting a single (or multiple) lines?