Common Terminal Commands
Navigation
cd directoryChange directory.cd ..Move up one directory level.cd ~Navigate to the home directory.lsList files and directories.
ls -alList 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.pwdPrint the current working directory.mkdir directory_nameCreate a new directory.rmdir directory_nameRemove an empty directory.rm -r directory_nameRemove a directory and its contents recursively.touch file_nameCreate a new file.
File Operations
cp source_file destinationCopy a file.mv source destinationMove or rename a file.rm file_nameRemove a file.cat file_nameDisplay the contents of a file.head file_nameDisplay the first few lines of a file.tail file_nameDisplay the last few lines of a file.grep pattern file_nameSearch for a pattern in a file.
File Permissions
chmod permissions file_nameChange file permissions.chown user:group file_nameChange file ownership.
System Information
uname -aPrint system information.topDisplay dynamic real-time information about running processes.df -hDisplay disk space usage.free -hDisplay memory usage.
Process Management:
psList running processes.kill process_idTerminate a process.killall process_nameTerminate all instances of a process.
Compression and Archiving:
tar -czvf archive_name.tar.gz directoryCreate a compressed tarball.tar -xzvf archive_name.tar.gzExtract a tarball.zip -r archive_name.zip directoryCreate a zip archive.unzip archive_name.zipExtract a zip archive.
Networking
ping hostSend ICMP echo requests to a host.ifconfigorip addrDisplay network interface information.netstat -tulnList open ports and associated processes.ssh user@hostConnect 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 rungit status. - If it returns an error, you are not in a Git repository.
Example Workflow
- Navigate to Your Project Directory:
cd /path/to/your/project - Check if It's a Git Repository:
git rev-parse --is-inside-work-tree - 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:
- Navigate to
project1Directory:cd author_name_projects/project1 - 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:
- 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 - Update
project1:cd project1 git pullThis command will update
project1by fetching and merging changes from the remote repository. - Update
project2:cd ../project2 git pullThis command will update
project2by fetching and merging changes from the remote repository. - Update
project3:cd ../project3 git pullThis command will update
project3by 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:
- Copying the Entire Directory (Including the
.gitDirectory): - If you copy the entire repository directory, including the
.gitdirectory, the new computer will have a fully functional Git repository. - The
.gitdirectory contains all the necessary information about the repository, including history, branches, and configuration. - Example:
- Copying Only the Files (Excluding the
.gitDirectory): - If you copy only the files and exclude the
.gitdirectory, the new computer will not have a Git repository. - The files will be present, but Git operations like
git status,git pull, andgit commitwill 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:
scp -r /path/to/local/repo user@remote:/path/to/destination/
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:
- Moving the Entire Directory (Including the
.gitDirectory): - If you move the entire repository directory, including the
.gitdirectory, the repository remains intact, and all Git functionality is preserved. - Example:
- Moving Only the Files (Excluding the
.gitDirectory): - If you move only the files and exclude the
.gitdirectory, the new location will not have a Git repository. - You will lose the ability to perform Git operations in the new directory.
- Example:
mv /path/to/local/repo /new/path/to/repo
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
.gitdirectory, 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
.gitdirectory along with the files, the original location will no longer have a Git repository. - Git operations in the original directory will fail because the
.gitdirectory, 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)
- Install WSL and a Linux distribution from the Microsoft Store.
- Open the WSL terminal and install
treeas you would on Linux:
sudo apt install tree
Using GnuWin32 Utilities
- Download and install the
treeutility from GnuWin32. - Add the directory containing
tree.exeto 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
Post a Comment