The Command Line Unix
Total Page:16
File Type:pdf, Size:1020Kb
The Command Line Unix • Function over form (as opposed to Windows form over function) • In Unix, “a word is worth a thousand mouse clicks.” • More efficiency and quicker (once you get some experience) • Can automate many tasks • Examples: OS X and Ubuntu Learning Resources • http://matt.might.net/articles/basic-unix/ • http://matt.might.net/articles/settling-into-unix/ • https://unix.stackexchange.com/ • https://www.udacity.com/course/linux-command-line-basics--ud595 Copying Remote Files • rsync is my preferred command • But scp is available with Git-Bash • scp local_file aclark@grok:~/ • scp aclark@grok:~/remote_file . Basic Commands cd : change to a different directory (., .., ../..) ls : list files and directories in the current working directory pwd : print the current working directory cat : easy way to print a file (does quite a bit more though) less : read through a longer file (press q to quit) vim : pretty powerful command-line text editor (or nano or emacs) man : a command for accessing the manual of different commands grep : search for text inside files (ripgrep is better) find : search for files by name (fd is better) ssh : secure shell to remotely control a different computer File Permissions Every file and directory has: • An owner, • A group, and • A set of permissions chmod : change file modes (permissions) chown : change the owner of a file chgrp : change the group of a file Pipes, Redirection, and Flags/Options • Most commands take input from STDIN • Most commands write output to STDOUT • Most commands write errors to STDERR • Input and output act as if you were interacting with a Python program • You can redirect STDIN and STDOUT using < and > • You can use a pipe one program’s STDOUT to another’s STDIN • Many commands take flags to change the output • For example: ls -lah Glob Matching • You can use a * to match any number of unknown characters (*.py) • You can use a ? To match exactly one unknown character (*.c??) • You can use […] to match a set of characters (*.c[px][px]) Environment Variables and PATH • Run env to see all environment variables • Available to all programs run in the shell • PATH is a list of directories in which the shell will look for programs Terminals and Consoles (or Konsole) • tty: teletype, dating back to the late 1800s • Interfacing with computers (a terminal or a physical console) • These are terms for things you use to interact with a computer • Examples: iterm, iterm2, konsole, xterm, etc. • Now, when you hear terminal it is an emulated input/output device • It is more or less just a GUI (or a windowed program) that acts like the old physical terminals/consoles Shell • A shell is the software that a user interacts with • It interprets user commands and starts/runs/monitors other programs • Examples: bash, fish, zsh • Users can also write scripts in a language provided by the shell Terminal or Shell? Terminal Shell • Handles key and turns them into • Handles control sequences control sequences • Command history, tab- • Scroll-back history completion, etc. • Acts on display commands • Sets display info (color, text, etc.) • Manages the prompt Options on Windows 1. PuTTY: is a free implementation of SSH and Telnet for Windows and Unix platforms, along with an xterm terminal emulator. 2. Linux on Windows: enabled by Windows Subsystem for Linux 3. Git for Windows: Git for Windows provides a BASH emulation used to run Git from the command line. It is essentially a Cygwin-derived shell using a terminal called mintty. 4. Cygwin: a set of Unix tools that have been patched and compiled for Windows. Cygwin vs WSL • You can think of Cygwin as a set of libraries that help you port code from POSIX to the Win API • The WSL, on the other hand, let’s you run ELF binaries directly on windows without porting • It does this by effectively translating POSIX system calls to WIN system calls on the fly • Wine is the opposite of Cygwin For Reference: Customizing Bash ~/.bash_profile should be super-simple and just load .profile and .bashrc (in that order) ~/.profile has the stuff NOT specifically related to bash, such as environment variables (PATH and friends) ~/.bashrc has anything you'd want at an interactive command line. Command prompt, EDITOR variable, bash aliases for my use A few other notes: • Anything that should be available to graphical applications OR to sh (or bash invoked as sh) MUST be in ~/.profile • ~/.bashrc should not output anything • Anything that should be available only to login shells should go in ~/.profile • Ensure that ~/.bash_login does not exist. https://blog.flowblok.id.au/2013-02/shell-startup-scripts.html.