Terminal Basics: Difference between revisions
No edit summary |
No edit summary |
||
Line 201: | Line 201: | ||
hello world | hello world | ||
== [[ | == [[Pipes]] == | ||
Piping allows the user to connect different commands and the outputs of commands together. | Piping allows the user to connect different commands and the outputs of commands together. |
Revision as of 16:58, 4 February 2016
Here you'll be given a quick Terminal tutorial outlining some introduction commands and inputs to give you a better grasp on the command line.
Following the instructions below in order will give you a step-by-step instruction that should get you up to speed with the basics of using the Terminal.
Input | Command |
---|---|
Up Arrow Key | Cycle through command history. |
Tab | Autocomplete command. |
ls | List all files in directory. |
cd [directory] | Change current directory. |
pwd | Print current (working) directory. |
~/ | The user's home directory. |
./ | Current directory location. |
open [file/ directory] | Open file through default application. |
open ./ | Open current directory with Finder. |
clear | Wipe current Terminal screen. |
ctrl+C | Cancel out of the current command. |
Navigating through the Terminal is very similar to navigating through your Finder. Think of it as a less visual version of your Finder window.
Keyboard commands
The Terminal is radically different from the standard OS GUI in that there's no mouse controls. Everything will be entirely operated from keyboard commands.
Here's a quick list of some common commands:
- Enter - Execute the currently written command.
- Left & Right Arrow keys - Scroll through characters in the current command string.
- Up & Down Arrow keys - Cycle through previous commands history. Use the up arrow to cycle back through history, down to cycle forward.
- Tab - Autocomplete a string entry you're currently typing.
- Ctrl+C - Used to kill a process, intercepted by a program so it can clean its self up before exiting, or not exit at all. Simply put, it'll quit from a process if you're currently within one.
pwd
Just like in your Finder window, your Terminal is always currently "residing" within a folder. Folders are known as directories within command line. To see what directory you currently are within on the command line, you can enter the command pwd (Print Working Directory).
$ pwd /Users/admin/Desktop
ls
Similar to the Finder, you can also see all the current files and folders within your current directory without influencing them. To see a list of files within your current directory, you can use the command ls .
$ ls planets earth sun.jpg galaxy.txt
cd
To navigate around the Terminal, you won't want to reside within the same directory forever. To change to another a directory, you can do so with the command cd.
$ cd planets $ pwd /Users/admin/Desktop/planets
$ cd ../ $ pwd /Users/admin/Desktop
clear
Every now and then the Terminal will get rather messy. Once in a while it's simply best to clean the board and start anew. To do so, simply type clear as below:
$ clear
Manipulating files
Once you have a better grasp of how to navigate around the command line, it's time to start manipulating and editing files.
Input | Command |
---|---|
touch | Create empty file within current directory. |
mkdir | Make directory. |
cat | Print current contents of file. |
rm | Delete file. |
rm -r | Delete directory. |
mv | Move file or directory. |
wc | Get word count from file. |
grep | Search a file for a patterns. |
touch
touch, in it's default state where we'll be using it, is the equivalent of creating an empty file.
$ ls file.txt $ touch hello.txt $ ls file.txt hello.txt
mkdir
mkdir is used to Make Directories.
$ ls file.txt hello.txt $ mkdir folder1 $ ls file.txt hello.txt folder1
cat
cat is used to inspect the contents of file.
$ cat file.txt hello world
mv
mv is used to move a file between directories.
$ ls file.txt hello.txt folder1 $ mv hello.txt folder1 $ ls file.txt folder1
rm
rm is used to delete or remove a file.
$ ls file.txt folder1 badfile.txt $ rm badfile.txt $ ls file.txt folder1
Be careful when using this command as it instantly deletes a file, with no means of recovery. The file is not sent to your trashbin.
rm -r
rm is used to delete or remove a file.
$ ls file.txt folder1 $ rm folder1.txt $ ls file.txt
Be careful when using this command as it instantly deletes a file, with no means of recovery. The file is not sent to your trashbin.
wc
wc can be used to gather the word count from a file.
$ cat file.txt hello world $ wc file.txt 0 2 11 file.txt
grep
grep, short for "Globally search a Regular Expression and Print", can be used to search for a pattern within a string and print the corresponding line of code.
$ grep hello file.txt hello world
Pipes
Piping allows the user to connect different commands and the outputs of commands together.
Input | Command |
---|---|
| | Pass the output of one command directly to another. |
> | Write the output of one command to a document. |
>> | Append the output of one command to a document. |
|
Used as the default pipe structure, allowing you to string the output from one command into another.
$ cat file.txt | grep hello.txt
>
Used to write the output from one command into another file.
Note: This will replace any current file contents without warning, be careful when using.
$ cat hello.txt hello_world1 hello_world2 $ cat file.txt single line of text $ grep world hello.txt >file.txt $ cat file.txt hello_world1 hello_world2
>>
Used to append the output from one command into another file.
$ cat hello.txt hello_world1 hello_world2 $ cat file.txt single line of text $ grep world hello.txt >>file.txt $ cat file.txt single line of text hello_world1 hello_world2
Fun Commands
curl
Pull data from a URL (Client URL).
Outputted markup in example has been slightly altered for display purposes.
$ curl www.google.com302 Moved 302 Moved The document has moved HREF="http://www.google.nl/?gfe_rd=cr&ei=9GirVontD5O-oQeUvaugCA">here