I recently gave a workshop on using the command line (a.k.a., terminal, command prompt, shell, etc.) at the Graduate Center as part of my Digital Fellow responsibilities. For the sake of sharing, I’m reposting the tutorial/walkthrough that guided the workshop. It’s really basic and pretty sparse, but the resources linked at the bottom provide lots of additional content to help you or your students get started using the *NIX/BSD/CygWin/busybox command line. Note: everything listed should work with most shells, although iirc, the “apropos” command isn’t included in busybox and “man” probably won’t have much useful documentation.
Command Line Walkthrough
./setup.sh cd ~
Structure of a command
grep -r capital etext/*
*(asterisk) is a wildcard, meaning match everything in the etext directory.
/slashes are used to separate directories:
/home/keith/projects/etextmight be the full path (location) of the etext folder, which is within the projects folder, which itself is in the keith folder within the home folder located at the root of my drive (filesystem).
White spaces in commands
White spaces separate arguments and options, use quotes to include spaces in things like filenames
grep -r "they were" etext/*
The file system, files, folders, user folder
pwd ls ls -l cd mydir cd ~ mkdir mynewdir mkdir -p mynewdir/mysubdir/mysubsub/subx3 touch goodbye.txt rmdir mynewdir/mysubdir/mysubsub/subx3 rm goodbye.txt rm -r mynewdir cp hello.txt mydir/hello_copy.txt mv mydir/hello_copy.txt ./ mv hello_copy.txt hello_goodbye.txt
mvto rename a file (move a file to a new file with a different name)
Working with files: cat, less
cat hello.txt cat aesopa10.txt less aesopa10.txt
Pipes and redirection
|pipes take the output from one command and sends it as an argument of the second one
>>(append) sends output as the input to the file/stream
echo "hello world." > hello.txt # output "hello world." into a new text file, hello.txt cat aesopa10.txt | less # read the aesopa10.txt file and send the output to the less program grep -r they etext/* > them.txt # search for 'they' and output results to them.txt cat >> them.txt # enter text to be appended to the end of them.txt
Use ctrl-d to finish entering text
Finding stuff: grep, find
find . -name "*.txt" -print
.(dot) indicates the current directory.
..(two dots) indicate the directory above the current directory (use
pwdif you forget what directory you’re in).
grep -r race ./* grep -r " race" ./*
grep -ir german ./*
grep -ir "fr.nc[eh]" ./*
Getting help: man, apropos, –help
man grep apropos find grep --help
Autocompletion, bash history
cat ae<tab> # --> [cat aesopa10.txt] <up> # --> scroll through previous commands
- ctrl-c: kill the currently running command/program
- ctrl-d: exit the current program
- ctrl-z: pause and return to the command line (
%nto resume the nth paused command, e.g.,
%1for the first paused command, etc.)
- ctrl-a: jump to the beginning of a line
- ctrl-e: jump to the end of a line
- ctrl-b: move back one character
- ctrl-f: move forward one character
- ctrl-w: delete a word backwards
- up/down/ctrl-p/ctrl-n: previous/next command
- tab: autocomplete
- ctrl-l: clear the display
Installing command line programs:
Other topics to look into: users, groups, permissions, linking, processes, shell scripts, loops…
Google is your friend!!!