Common Terminal Commands

Navigation

  • cd directory Change directory.
  • cd .. Move up one directory level.
  • cd ~ Navigate to the home directory.
  • ls List files and directories.
    ls -al List all files and directories in long format, including hidden files and directories. The output includes details such as permissions, owner, group, size, and modification time.
  • pwd Print the current working directory.
  • mkdir directory_name Create a new directory.
  • rmdir directory_name Remove an empty directory.
  • rm -r directory_name Remove a directory and its contents recursively.
  • touch file_name Create a new file.

File Operations

  • cp source_file destination Copy a file.
  • mv source destination Move or rename a file.
  • rm file_name Remove a file.
  • cat file_name Display the contents of a file.
  • head file_name Display the first few lines of a file.
  • tail file_name Display the last few lines of a file.
  • grep pattern file_name Search for a pattern in a file.

File Permissions

  • chmod permissions file_name Change file permissions.
  • chown user:group file_name Change file ownership.

System Information

  • uname -a Print system information.
  • top Display dynamic real-time information about running processes.
  • df -h Display disk space usage.
  • free -h Display memory usage.

Process Management:

  • ps List running processes.
  • kill process_id Terminate a process.
  • killall process_name Terminate all instances of a process.

Compression and Archiving:

  • tar -czvf archive_name.tar.gz directory Create a compressed tarball.
  • tar -xzvf archive_name.tar.gz Extract a tarball.
  • zip -r archive_name.zip directory Create a zip archive.
  • unzip archive_name.zip Extract a zip archive.

Networking

  • ping host Send ICMP echo requests to a host.
  • ifconfig or ip addr Display network interface information.
  • netstat -tuln List open ports and associated processes.
  • ssh user@host Connect to a remote host using SSH.
    For instance, ssh -T git@github.com
    -T: This flag disables pseudo-terminal allocation. It is useful for when you just want to run a command on the remote server without opening an interactive session.

In addition to these commands, users often leverage the Nano text editor for quick file edits directly from the terminal. To open a file with Nano, simply type nano file_name.

Furthermore, when administrative privileges are required for certain commands or file edits, users can use the sudo prefix followed by the desired command. For example, sudo nano /etc/nginx/nginx.conf would open the Nano text editor with administrative privileges to edit the nginx configuration file.

Understanding Git Status and Pull Commands in Multi-Project Environments

Scenario 1: Inside a Git Repository

If you are inside a directory that is part of a Git repository, git status will show you the current status of the repository, including staged changes, unstaged changes, and untracked files.

cd /path/to/your/git/repository
git status

Output:

On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   example_file.txt

Untracked files:
  (use "git add" <file>..." to include in what will be committed)
    new_file.txt

no changes added to commit (use "git add" and/or "git commit -a")

Scenario 2: Outside a Git Repository

If you are in a directory that is not part of a Git repository and you run git status, you will receive an error message indicating that the current directory is not a Git repository.

cd /path/to/non-git/directory
git status

Output:

fatal: not a git repository (or any of the parent directories): .git

Checking the Current Directory

To determine if the current directory is part of a Git repository, you can use the following command before running git status:

git rev-parse --is-inside-work-tree
  • If it returns true, you are in a Git repository and can safely run git status.
  • If it returns an error, you are not in a Git repository.

Example Workflow

  1. Navigate to Your Project Directory:
    cd /path/to/your/project
  2. Check if It's a Git Repository:
    git rev-parse --is-inside-work-tree
  3. If the Above Command Returns true, Check the Status:
    git status

Using git pull to Update Projects

When you run git pull inside a specific project's directory, Git knows to update that particular project because it looks for the .git directory within the current working directory. Each Git project repository has its own .git directory that contains all the information about the repository, such as its configuration, branches, and history.

Example Directory Structure:

Assume you have the following directory structure:

author_name_projects/
├── project1/
│   └── .git/
├── project2/
│   └── .git/
└── project3/
    └── .git/

Navigating to a Project Directory and Running git pull:

  1. Navigate to project1 Directory:
    cd author_name_projects/project1
  2. Run git pull:
    git pull

Git uses the .git directory in the current working directory (author_name_projects/project1/.git in this case). When you run git pull, Git reads the repository configuration from .git/config and fetches and merges updates from the remote repository configured for project1.

Example Commands for Multiple Projects:

  1. Clone Projects:
    mkdir author_name_projects
    cd author_name_projects
    git clone https://github.com/author_name/project1.git
    git clone https://github.com/author_name/project2.git
    git clone https://github.com/author_name/project3.git
  2. Update project1:
    cd project1
    git pull

    This command will update project1 by fetching and merging changes from the remote repository.

  3. Update project2:
    cd ../project2
    git pull

    This command will update project2 by fetching and merging changes from the remote repository.

  4. Update project3:
    cd ../project3
    git pull

    This command will update project3 by fetching and merging changes from the remote repository.

By following these steps, you ensure that each project is updated individually. Git uses the .git directory within the current directory to determine which project to update, giving you full control over your repositories.

Note: To prevent Git from automatically rebasing your local changes when pulling updates from the remote repository, you can set pull.rebase to false globally or for specific repositories. This can be achieved by running:

git config pull.rebase false

This command ensures that Git will merge changes rather than rebasing them by default, which can be useful if you prefer to keep a linear commit history or if you want to avoid potential conflicts caused by rebasing.

Copying Files to Another Computer

When you copy the cloned repository to another computer, you have two options:

  1. Copying the Entire Directory (Including the .git Directory):
    • If you copy the entire repository directory, including the .git directory, the new computer will have a fully functional Git repository.
    • The .git directory contains all the necessary information about the repository, including history, branches, and configuration.
    • Example:
    scp -r /path/to/local/repo user@remote:/path/to/destination/
  2. Copying Only the Files (Excluding the .git Directory):
    • If you copy only the files and exclude the .git directory, the new computer will not have a Git repository.
    • The files will be present, but Git operations like git status, git pull, and git commit will not work.
    • You can initialize a new repository on the new computer using git init, but it will be a new repository without any history.
    • Example:
    cp -r /path/to/local/repo/* /path/to/destination/ rm -rf /path/to/destination/.git

Moving Files Within the Same Computer

Moving files within the same computer can also affect the repository, depending on whether the .git directory is moved along with the files:

  1. Moving the Entire Directory (Including the .git Directory):
    • If you move the entire repository directory, including the .git directory, the repository remains intact, and all Git functionality is preserved.
    • Example:
    mv /path/to/local/repo /new/path/to/repo
  2. Moving Only the Files (Excluding the .git Directory):
    • If you move only the files and exclude the .git directory, the new location will not have a Git repository.
    • You will lose the ability to perform Git operations in the new directory.
    • Example:
    mkdir /new/path/to/repo mv /path/to/local/repo/* /new/path/to/repo/ rm -rf /new/path/to/repo/.git

Effects on the Original Computer

If the .git Directory is Not Moved:

  • The original location will still have the .git directory, and the repository will remain functional in its original state.
  • You can continue to perform Git operations in the original directory.

If the .git Directory is Moved:

  • If you move the .git directory along with the files, the original location will no longer have a Git repository.
  • Git operations in the original directory will fail because the .git directory, which contains the repository's metadata, has been moved.

Copying to External Drive

You can copy Git repositories to an external drive such as a hard drive or USB flash drive using the cp command in a similar manner as copying to a local destination. The command structure remains the same:

cp -r /path/to/local/repo/* /path/to/external/drive/ 
rm -rf /path/to/external/drive/.git

Replace /path/to/local/repo/ with the path to your local repository, and /path/to/external/drive/ with the path to your external drive.

Using GUI to Copy and Paste

If you use a graphical user interface (GUI) to copy and paste files, whether the .git directory is included depends on the specific behavior of the GUI tool you're using.

In general:

  • Copying the Entire Directory: Some file managers copy the entire directory along with all its contents, including hidden files and directories like the .git directory.
  • Copying Only Files: Other file managers may only copy the visible files within the directory and exclude hidden files and directories like .git.

On macOS: In Finder, when you right-click and select "Copy" on a directory, it typically copies the entire directory along with all its contents, including hidden files and directories like .git.

On Windows: In File Explorer, right-clicking and selecting "Copy" on a directory usually behaves similarly to macOS, copying the entire directory along with hidden files and directories.

On Linux: The behavior can vary depending on the file manager being used. In many Linux file managers, right-clicking and selecting "Copy" on a directory may only copy the visible files within the directory and exclude hidden files and directories like .git.

To ensure consistent behavior across different operating systems and file managers, it's advisable to check the contents of the copied directory afterward to confirm that the .git directory was copied if needed. If necessary, you can manually copy the .git directory separately to ensure it's included in the copy operation.

Using the Tree Command

The tree command is a powerful tool to visualize the directory structure of a project, displaying directories and subdirectories in a tree-like format. To use it, open the Terminal on your MacBook Pro and navigate to the project's root directory with the cd command. Once there, simply run tree to display the entire structure, or use tree -L 2 to limit the depth to two levels. This command is particularly useful for quickly understanding the layout of complex projects.

macOS and Linux

Navigate to the Project Directory

Use the cd command to change to the directory you want to visualize. For example:

cd /path/to/your/project

Run the tree Command

Simply type tree to display the directory structure:

tree

To display hidden files and directories, add the -a flag:

tree -a

To limit the depth of the directory tree, you can use the -L option followed by the level number. For example, to display up to two levels deep:

tree -L 2

Example Output

Running tree in the deep_learning_project directory:

.
├── data
│   ├── raw
│   └── processed
├── notebooks
├── src
│   ├── data
│   ├── models
│   ├── training
│   └── evaluation
├── scripts
├── results
│   ├── models
│   ├── logs
│   └── figures
├── tests
├── requirements.txt
├── README.md
└── .gitignore

Similarly, running tree -L 2 limits the depth of the directory tree:

.
├── data
│   ├── processed
│   └── raw
├── notebooks
├── results
│   ├── figures
│   ├── logs
│   └── models
├── scripts
├── src
│   ├── data
│   ├── evaluation
│   ├── models
│   └── training
├── tests
├── .gitignore
├── README.md
└── requirements.txt

Installing tree

macOS: Use Homebrew

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install tree

Linux: Use your package manager

Debian-based (e.g., Ubuntu):

sudo apt install tree

Red Hat-based (e.g., Fedora):

sudo yum install tree

Windows

Using Windows Subsystem for Linux (WSL)

  1. Install WSL and a Linux distribution from the Microsoft Store.
  2. Open the WSL terminal and install tree as you would on Linux:
sudo apt install tree

Using GnuWin32 Utilities

  1. Download and install the tree utility from GnuWin32.
  2. Add the directory containing tree.exe to your system's PATH.

Example Commands on Windows (WSL)

Open WSL Terminal

Navigate to the project directory:

cd /mnt/c/path/to/your/project

Run tree Command

Display the directory structure:

tree

Limit the depth to two levels:

tree -L 2

See also


Comments

Popular posts from this blog

Plug-ins vs Extensions: Understanding the Difference

Neat-Flappy Bird (Second Model)

Programming Paradigms: Procedural, Object-Oriented, and Functional