---
# System prepended metadata

title: Useful CMD Commands
tags: [lecture, 112-1]

---

# Useful CMD Commands

### Check Current Directory
```bash
# Linux, Mac
pwd

# Windows
chdir
```

### List Directory
```bash
# Linux, Mac
ls
ls -l    # show detailed stats

# Windows
dir
```

### Change Directory
```bash
# move yourself to your directory 
cd [your_directory]
# eg. move to /home/user/test
cd /home/user/test
# take advantage of "relative path"

# move to parent directory
cd ..
# Linux: move to root
cd /
# Linux: move to home
cd ~
```

### Copy File 
```bash
# single file
cp [your_filename] [your_folder/your_filename]
# folder
cp -r [your_foldername] [your_another_folder/]

# Windows
copy [your_file] [your_folder/]
```

### Move(Rename) File 
```bash
mv [your_filename] [your_folder/your_filename]
# rename file
mv [your_old_name] [your_new_name]

# Windows
move [your_file] [your_folder/]
# rename file
ren [your_old_name] [your_new_name]
```

### Create File
```bash
touch [new_file]

# Windows: not trivial
type nul > filename.txt
```

### Create Directory

```bash
mkdir [new_directory]
```

### Remove Directory

```bash
# For empty directory
rm [old_directory]
rmdir [old_directory]

# For nonempty directory (usually)
rm -r [old_directory]

# Windows
del [old_directory]
rd /s /q [old_directory]
```

### SuperUser(Linux, Mac)

```bash
sudo [your_command]
# For example installing packages
sudo apt install gcc
```

### Clear Terminal screen

```bash
clear

# Windows
cls
```
### Exit Terminal
```bash
exit
```

### Open VSCode Editor
```bash
cd [your_work_directory]
code .

# alternative
code [your_work_directory]
```
---
## Git
```bash
# clone a repo
git clone [repo_url]

# check status
git status

# stage add changed file
git add [changed_file]

# commit changes
git commit -m "[your comment]"

# push commit to github branch
git push origin [your_branch]

# check branches
git branch

# create a new branch
git checkout -b [new_branch_name]

# switch to existing branch
git checkout [branch_name]

# delete branch
git branch -D [your_branch]

# discard changes
git checkout -- [your_file]

# restore stage add files
git restore --staged [your_file]

# check commit lineup
git log --oneline

# reset pushed commits
git reset [OPTION] [COMMIT_NAME]
  [OPTION]
    --mixed   # default, files in the deleted commit will be unstaged
    --soft    # files in the deleted commit will stay staged
    --hard    # files and commit will be discarded
  [COMMIT_NAME]
    HEAD      # the latest commit
    HEAD~1    # the second latest commit
    
# 不要亂用 reset!! 除非你已經對github commit的機制很熟悉
```

### Notes
