|
# Bash Cheat Sheet
|
|
# Bash Cheat Sheet
|
|
|
|
|
|
These are a list of commands that work in any bash shell.
|
|
These are a list of commands that work in any bash shell.
|
|
They will also work in any UNIX or UNIX-like environment (e.g. a OSX terminal, a Linux terminal).
|
|
They will also work in any UNIX or UNIX-like environment (e.g. a OSX terminal, a Linux terminal, Git Bash).
|
|
|
|
We also provide Windows commands as well.
|
|
|
|
|
|
## Cheat Sheet
|
|
## Cheat Sheet
|
|
|
|
|
|
| **Command** | **Description** | **Invocation** |
|
|
| **UNIX Command** | **Win Command** | **Description** | **Invocation** |
|
|
| ----------------- | --- | --- |
|
|
| --- | --- | --- | --- |
|
|
| `pwd` | Outputs your current directory. | `pwd` |
|
|
| `pwd` | `cd` | Outputs your current directory. | `pwd` |
|
|
| `cd` | Enters a directory. | `cd /path/to/dir` |
|
|
| `cd` | `cd` | Enters a directory. | `cd path/to/dir` |
|
|
| `ls` | Lists the contents of the current directory. | `ls` |
|
|
| `ls` | `dir` | Lists the contents of the current directory. | `ls` |
|
|
| | Lists the contents of a directory. | `ls /path/to/dir` |
|
|
| `rm` | `erase` | Deletes a file (permanently). | `rm path/to/file` |
|
|
| `rm` | Deletes a file. | `rm /path/to/file` |
|
|
| | | Deletes a directory (permanently). | `rm -rf path/to/directory` |
|
|
| | Deletes a directory. | `rm -rf /path/to/directory` |
|
|
| `cp` | `copy` | Copies a file. | `cp path/to/src path/to/dst` |
|
|
| `cp` | Copys a file. | `cp /path/to/src /path/to/dst` |
|
|
| | | Copies a directory. | `cp -r path/to/src path/to/dst` |
|
|
| | Copys a directory. | `cp -r /path/to/src /path/to/dst` |
|
|
| `mv` | `rename` | Moves a file or directory. | `mv path/to/src path/to/dst` |
|
|
| `mv` | Moves a file or directory. | `mv /path/to/src /path/to/dst` |
|
|
| `mkdir` | `mkdir` | Creates a directory. | `mkdir path/to/dir` |
|
|
| `mkdir` | Creates a directory. | `mkdir /path/to/dir` |
|
|
| `touch` | None | Creates a file. | `touch path/to/file` |
|
|
| `touch` | Creates a file. | `touch /path/to/file` |
|
|
|
|
|
|
|
|
## On `~`, `.`, and `..`
|
|
## On `~`, `.`, and `..`
|
|
|
|
|
... | @@ -29,6 +29,42 @@ Running `cp ~/HelloWorld.java .` will copy `HelloWorld.java` from your home dire |
... | @@ -29,6 +29,42 @@ Running `cp ~/HelloWorld.java .` will copy `HelloWorld.java` from your home dire |
|
The third is `..` which represents the directory above the current directory.
|
|
The third is `..` which represents the directory above the current directory.
|
|
To move into the directory above the current directory, we run `cd ..`.
|
|
To move into the directory above the current directory, we run `cd ..`.
|
|
|
|
|
|
|
|
## On `/` and `\`
|
|
|
|
|
|
|
|
### In UNIX Environments
|
|
|
|
|
|
|
|
**Refer to this section if you are using Git Bash, OSX, or Linux**
|
|
|
|
|
|
|
|
In a UNIX environment, `/` denotes the *root directory*.
|
|
|
|
This is the top level directory in any UNIX system and is the only directory where `..` refers to `.`.
|
|
|
|
Moreover, we use the `/` character to separate directories in a path.
|
|
|
|
E.g. `cd /home/user/` would change the directory to `/home/user/`.
|
|
|
|
In this case, `user` is a subdirectory of `home`, which is a subdirectory of `/`.
|
|
|
|
Moreover, the path `/home/user/` is an *absolute path* since it begins with the `/` character (meaning its relative to the root directory).
|
|
|
|
If we run `cd git/helloworld/` we would be in the directory `/home/user/git/helloworld`.
|
|
|
|
The path `git/helloworld/` is a *relative path* meaning that the path is relative to the current directory.
|
|
|
|
In this case, `git` is a directory within `/home/user` and `helloworld` is a directory within `git`.
|
|
|
|
|
|
|
|
Additionally, in any UNIX environment, `\` allows you to break a command into multiple lines as so.
|
|
|
|
```
|
|
|
|
git config \
|
|
|
|
--global \
|
|
|
|
user.name "Your name here"
|
|
|
|
```
|
|
|
|
|
|
|
|
### In Windows Environments
|
|
|
|
|
|
|
|
**Refer to this section if you are using Windows Command Prompt or Windors Powershell**
|
|
|
|
|
|
|
|
In a Windows environment, `\` separates the directories in a path.
|
|
|
|
An example of an *absolute path* in a Windows environment is `C:\Users\UserName\`.
|
|
|
|
In any absolute path in Windows, you start with the drive letter and a `:` (typically `C:`) followed by the name of each directory in your path (in order).
|
|
|
|
In this example, `Users` is a directory on the `C` drive and `UserName` is a subdirectory of `User`.
|
|
|
|
Thus, the command `cd C:\Users\UserName` will change the directory to the `UserName` directory.
|
|
|
|
From there, we may wish to enter the `git` directory within the `Documents` directory.
|
|
|
|
Instead of using the lengthy absolute path for the directory we can enter it by using a *relative path*, or a path relative to the current directory.
|
|
|
|
To do so, we simply run `cd Documents\git\` and our working directory will be `C:\Users\UserName\Documents\git\`.
|
|
|
|
|
|
[Return to Cheat Sheet index](./index)
|
|
[Return to Cheat Sheet index](./index)
|
|
|
|
|
|
[Return to Bash and Git index](../home) |
|
[Return to Bash and Git index](../home) |