- Moving between directories and creating/removing files
- Working with commands and processes
- Investigating text files
- Parsing files
- Working with input and output
- Absolute and relative paths, symbolic links
- Bash variables and commands
- Text editors: nano, vim, emacs, Sublime Text, MacDown
- Execute bash scripts and dot-files (e.g.,
.bash_profile
) - Set up your Terminal settings and bash environment
pwd
-- print working directoryls
-- list contents of working directorycd DIRECTORY
orcd
-- change directory, change to home directorymkdir DIRECTORY
-- make directoryrmdir DIRECTORY
-- remove empty directorytouch FILE
-- create an empty filecat FILE
-- print contents of filecat > FILE
-- write to filecat >> FILE
-- append to filecp FILE1 FILE2
-- copy filemv FILE1 FILE2
-- move filerm FILE
-- remove fileexit
-- close the current session
man COMMAND
-- display manual page of command if it existswhich COMMAND
-- show location of commandhistory
-- display past commandstop
-- display current processes
There are several things you can do to set up your bash environment, which is what you see when you use the terminal (command line). You put these commands in a file called ~/.profile (or ~/.bashrc or ~/.bash_profile). That notation means the file is called .profile (yes, that's a period, and the file is called a dot-file), and it's in your home directory.
# customize prompt with color and pwd
PS1="\[\033[1;33m\][\u@\h:\w]$\[\033[0m\] "
# customize terminal window title to display pwd
PROMPT_COMMAND='echo -ne "\033]0; ${PWD##*/}\007"'
# autocomplete history with up arrow
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'
# grep coloring
export GREP_OPTIONS='--color=auto'
export GREP_COLOR='01;38;5;226'
# command aliases (shortcuts)
alias cd..='cd ..'
alias cd...='cd ../..'
alias cd....='cd ../../..'
alias du='du -sh'
alias excel='open -a /Applications/Microsoft\ Office\ 2011/Microsoft\ Excel.app'
alias l='ls -p'
alias la='ls -ap'
alias lal='ls -alhp'
alias ll='ls -lhp'
alias m2u="tr '\015' '\012'"
alias u2m="tr '\012' '\015'"
alias rm='rm -i'
alias taill='ls -lrt | tail'
For serious coding, Sublime Text is great. For small jobs or if you want to stay inside the terminal, there are other useful programs. Note: cat
is only useful for creating very basic files, or starting files and finishing them in a proper text editor.
nano FILE
- nano is the most basic text editor (see Appendix: The Nano Text Editor at end of this lesson)emacs FILE
- emacs is a popular full-featured text editor controlled by keystrokesvim FILE
- vim or vi is a popular competitor to emacs that loads faster
less FILE
-- view a text filehead FILE
-- first 10 lines of filetail FILE
-- last 10 lines of filewc FILE
-- count the words and characters in a fileopen FILE
-- open a file using default program (on a Mac)file FILE
-- get the file type for file(s)grep REGEX FILE
-- search a text file for a string or regular expression
cut -d "," -f 5- FILE
-- output 5th field through end using comma field delimitersed 's/FIND/REPLACE/g'
-- replace text in a fileperl --e 's/FIND/REPLACE/g'
-- run perl commands in the command line (advanced)
|
-- pipe output from one command to another>
-- redirect (write) to file<
-- get output of file (other type of redirect)`COMMAND`
-- pass output of a command (e.g., in a for loop)
.
-- current directory..
-- one directory up../..
-- two directories up/
-- root directory~
-- home directoryln -s FILE LINK
-- make a symbolic link
A=0
-- assign a variableecho VARIABLE
-- output the value of a variable or expression$PATH
-- your path variable$SHELL
-- your current shell$RANDOM
-- a random number$PPID
-- process ID$HOME
-- your home directory (another name for~
or/Users/you
)for VARIABLE in LIST; do COMMANDS; done
--for
loop in bash
bash SCRIPT.sh
-- run a bash shell scriptsource .DOTFILE
-- run a dot-file like your .profile
Any of the commands we have used from the command line (the bash prompt $
) can also be typed into a text file and executed by typing bash SCRIPT.sh
from the command line.
Basic commands
- Create a working directory for today's class.
- Within that directory, create a temporary test directory.
- Create a file using one method I showed you.
- Create a file using a different method I showed you.
- Rename one of the files.
- Copy one of the files.
- Delete one of the files.
- Delete the temporary directory.
Working with commands
- Learn more about a command you just learned using a unix command.
- Learn more about a command you just learned using Google.
- Find out where the commands
mv
andcp
are located on your computer. - Get a list of the commands you've typed already.
- See which processes are running on your computer.
- What happens when you type Tab in the middle of a command?
- What happens when you type Tab in the middle of a file name or path?
Setting up your bash environment
- Download the text editor Sublime Text.
- Customize your terminal by editing
.profile
or.bashrc
using Sublime Text. - Source your bash profile file using
source ~/.profile
. - Open a new terminal to make sure it automatically sources your bash profile file. You may have to change the preferences in the Terminal app.
More commands
- Print the first 5 lines of a text file.
- Print the last 10 commands you entered.
- View the contents of a file without printing anything to the screen.
- Open a file in its designated application.
- Determine the kind/type of a file.
- Search for a word in a file.
- Get only the third column of a tab-delimited file.1
- Using a different method, get the first field of a tab-delimited file and save it as a new file.
Paths and variables
- Navigate to root and home directories using absolute paths.
- Navigate to root and home directories using relative paths.
- Store an integer as a shell variable then print it.
- Store a file name as a shell variable.
- Print your current path variable.
- Print your home directory.
- Write a
for
loop to count to 10.
Executing bash scripts and dot-files
- Write a bash script that uses the commands
mkdir
,cat
,mv
,echo
, and afor
loop. - Execute you bash script using the terminal.
Credit: SDSU Department of Astronomy
Nano is a text editor suited to working in UNIX. It is not as powerful as PC window-based editors, as it does not rely on the mouse, but still has many useful features.
Most nano commands are invoked by holding down the Ctrl
key (that is, the control key), and pressing one of the other keys. In this text, the control key is referred to using ^
. For example, ^X
means "hold down the control key and press the x key". Most of the important commands are listed at the bottom of your screen.
^G
nano help
To edit a file called filename, type nano filename. In nano, you can insert another file:
^R
read an existing file into nano (inserted at the current cursor position)^T
opens a browser that allows you to select a file name from a list of files and directories
The usual mouse-based point-and-click method is not supported by nano. Use the arrow keys to move around the page in nano.
Other navigation commands:
^A
move to beginning of line^E
move to end of line^Y
move down a page^V
move up a page^_
move to a specific line (^_^V
moves to the top of the file,^_^Y
to the bottom)^C
find out what line the cursor is currently on^W
search for some text.
When searching, you will be prompted for the text to search for. It searches from the current cursor position, wrapping back up to the top if necessary.
Insert new text at the current cursor position just by typing the text in.
Delete commands:
^D
delete character currently under the cursorBackspace
delete character currently in front of the cursor^K
delete entire line^\
search for (and replace) a string of characters
Cut and paste:
^K
does not delete lines permanently; the most recent set of deletions are stored in a buffer. These lines may be re-inserted at the current cursor location using^U
. This may be used to simulate cut and paste:- Repeatedly use
^K
until all of the text you want to move has been deleted. - Move to the line that you want to insert the text at, and use
^U
.
- Repeatedly use
- Note that pressing
^U
more than once will cause multiple copies to be inserted. This is particularly useful if you want to copy text:- Repeatedly use
^K
until all of the text you want to copy has been deleted. - Press
^U
immediately to put a copy back in its original location. - Move to the line that you want to copy the text to, and use
^U
.
- Repeatedly use
^O
save contents without exiting (you will be prompted for a file to save to)^X
exit nano (you will be prompted to save your file if you haven't)^T
when saving a file, opens a browser that allows you to select a file name from a list of files and directories
Footnotes
-
To insert a Tab character, type Ctrl-V and then Tab. ↩