School of Economics and Management
Beihang University
http://yanfei.site

• Mac: ssh student@47.92.146.46
• Windows
• Hostname: 47.92.146.46
• User ID: student
• port: 22

## The command line

• A command line, or terminal, is a text based interface to the system.
• You are able to enter commands by typing them on the keyboard and feedback will be given to you similarly as text.

## Shell

• A program that takes commands from the keyboard and gives them to the operating system to perform.
• Bash： one kind of shell.

## Where we are?

pwd
## /Users/ykang/Library/Mobile Documents/com~apple~CloudDocs/Teaching/Big-Data-Essentials/BDE-L02-linux

## What’s in our current location?

ls
## BDE-L2-linux.Rmd
## BDE-L2-linux.html

## Paths

• The file system under linux is a hierarchical structure.
• At the very top of the structure is what’s called the root directory. It is denoted by a single slash ( / ).

## Absolute and relative paths

• Absolute paths specify a location (file or directory) in relation to the root directory. You can identify them easily as they always begin with a forward slash ( / ).
• Relative paths specify a location (file or directory) in relation to where we currently are in the system. They will not begin with a slash.
ls
## BDE-L2-linux.Rmd
## BDE-L2-linux.html
ls /Users/ykang/Teaching/Big-Data-Essentials/BDE-L02-linux
## BDE-L2-linux.Rmd
## BDE-L2-linux.html

## More on paths

• ~ (tilde) - This is a shortcut for your home directory. /home/ykang/Documents or ~/Documents.
• . (dot) - This is a reference to your current directory. ls ./.
• .. (dotdot)- This is a reference to the parent directory. ls ../.

## Let’s move around a bit

• In order to move around in the system we use a command called cd which stands for change directory.
• Use Tab Completion.

## Using manual pages

• The manual pages are a set of pages that explain every command available on your system including what they do, the specifics of how you run them and what command line arguments they accept.
• Try man ls.

## Lab

• Use the commands cd and ls to explore what directories are on your system and what’s in them. Make sure you use a variety of relative and absolute paths.
• Now go to your home directory using 4 different methods.
• Make sure you are using Tab Completion when typing out your paths too. Why do anything you can get the computer to do for you?

## File manipulation

• Making a directory: mkdir
• Removing a directory: rmdir
• Creating a blank file: touch
• Copying a file or directory: cp
• Moving a file or directory: mv
• Renaming files or directories
• Removing files or empty directories: rm
• How to removing non empty directories?
• Note: no undo options.

## Lab

• Start by creating a directory in your home directory in which to experiment.
• In that directory, create a series of files and directories (and files and directories in those directories).
• Now rename a few of those files and directories.
• Delete one of the directories that has other files and directories in them.
• Move back to your home directory and from there copy a file from one of your subdirectories into the initial directory you created.
• Now move that file back into another directory.
• Rename a few files
• Next, move a file and rename it in the process.
• Finally, have a look at the existing directories in your home directory.

• Mac: scp or rsync
• Windows： use FileZilla

## A command line editor

• There are two modes in Vim. Insert (or Input) mode and Edit mode.
• In input mode you may input or enter content into the file.
• In edit mode you can move around the file, perform actions such as deleting, copying, search and replace, saving etc.
• A common mistake is to start entering commands without first going back into edit mode or to start typing input without first going into insert mode.

## First file

• Start with vim firstfile.
• You always start off in edit mode so the first thing we are going to do is switch to insert mode by pressing i. You can tell when you are in insert mode as the bottom left corner will tell you.

## Saving and editing

• :q! - discard all changes, since the last save, and exit
• :w - save file but don’t exit
• :wq - again, save and exit

## Other ways to view files

• Try cat firstfile.
• For larger files there is a better suited command which is less.
• head, tail.

## Deleting content

• x - delete a single character
• nx - delete n characters (eg 5x deletes five characters)
• dd - delete the current line
• dn - d followed by a movement command. Delete to where the movement command would have taken you. (eg d5w means delete 5 words)

## Undoing

• u - Undo the last action (you may keep pressing u to keep undoing)
• U (Note: capital) - Undo all changes to the current line

## Lab

• Start by creating a file and putting some content into it.
• Save the file and view it in both cat and less
• Go back into the file in vi and enter some more content.
• Move around the content using at least 6 different movement commands.
• Play about with several of the delete commands, especially the ones that incorporate a movement command. Remember you may undo your changes so you don’t have to keep putting new content in.

## What are they?

• * - represents zero or more characters
• ? - represents a single character
• [] - represents a range of characters

## Examples

• ls ?i*
• ls *.???
• ls [ab]*
• ls *[0-9]*
• rm *.jpg
• ls -lh /home/*/.bash_history

## Lab

• A good directory to play with is /etc which is a directory containing config files for the system. As a normal user you may view the files but you can’t make any changes so we can’t do any harm. Do a listing of that directory to see what’s there. Then pick various subsets of files and see if you can create a pattern to select only those files.
• Do a listing of /etc with only files that contain an extension.
• What about only a 3 letter extension?
• How about files whose name contains an uppercase letter? (hint: [[:upper:]] may be useful here)
• Can you list files whose name is 4 characters long?