Setup | Shells | Markdown and IDEs | Git |Virtual Environments | Task Management | Advanced Shells
A shell is a computing environment where commands can be interpreted, evaluated, and its output displayed (i.e., an instance of a read–eval–print loop (REPL)). A good shell provides access to a rich set of commands and allows simple programming of commands, which can be used to create powerful scripts and tools.
ls -R | grep ":$" | sed -e "s/:$//" -e "s/[^-][^\/]*\//--/g" -e "s/^/ /" -e "s/-/|/"
But with great power comes great bullshittery. Commands and their options can be terse, inconsistent, and difficult to learn. A steep learning curve often prevents novices from enjoying the eventual payoff. If you've hardly used a command line environment before, you might want to go review this more thorough tutorial: software carpentry: shell-novice---this page is more of a discussion of common tasks and mistakes, advanced topics, and resources.
You may also want to reference the online book, the Unix Workbench.
99% of the reason you use shells is to run useful commands.
ls: list content of a directory.cd: change directories to a new path.mkdir: make a new directory.pwd: output current directorycp: copy filesrm: rm filestouch: make a new file/update status**cat: output the contents of a file.head: output the first lines of a file.tail: output the last lines of a file.grep: search files for a key phrase.wget: retrieve file from the web.cut: extract output of a file (columns)awkandsed: Magic commands for extracting, searching, and transforming content.
Command can run sequentially or conditionally:
command1 ; command2
(command1 ; command2) # in a sub-shell
command1 || command2 # do command2 only if command1 fails
command1 && command2 # do command2 only if command1 succeedsNote: In Windows, ; does not work in Cmd, but does in Powershell. Use && for the most portable operation.
Try running this command that combines these shell commands.
echo "Hello World" > shells-test.txt && cat shells-test.txt
Now, try using the || operator.
cat shells-test.txt || echo "backup plan"
See what happens in this case.
cat filedoesnotexist.txt || echo "backup plan"
The UNIX shell commands push data from sources through filters along pipes. Normally, each command runs as a process and reads and writes data the following way:
- Standard input (stdin): get information from keyboard.
- Standard output (stdout): write information as output to console.
- Standard error (stderr): write error information as output to console.
Pipes and redirects change stdin and stdout from default sources. For example, we can change the stdin of a process to be piped from the output of another process. Or rather than printing to the console, we can get a process to write to a file.
command # default standard in and standard out
command < inputFile # redirect of inputFile contents to command as standard in
command > outputFile # redirect command output to outputFile as standard out
command1 | command2 # pipes output of command1 as standard in to command2Neat trick: Copy the value of a file into your clipboard!
Windows: clip < file.txt Mac: pbcopy < file.txt
pbcopy < ~/.ssh/id_rsa
clip < %HOME%/.ssh/id_rsa
Click the following to start the exercise.
You might find Julia Evan's zine useful: Bite Size Command Line!.
Here is an example for the lsof command. More examples can be found here.
A list of command line examples for interesting tasks:
http://www.commandlinefu.com/commands/browse
Create a graphical directory tree from your current directory.
ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'
What does tar -zxvf ph.tar.gz do?
