Quick Start Guide to Linux Operations: Transitioning Your Windows Habits to Linux

In the previous two articles, I introduced how to purchase a cloud server for just 99 yuan and set up your own website, and explained how to gradually master user management operations on the cloud server ECS.

In this article, we will gradually get started with Linux operations, focusing on command line usage. It can be said that there are numerous books and articles online about getting started with Linux, providing a wealth of resources.

Therefore, this article will take a different approach: using our common needs and operational habits in the Windows system as a starting point to compare and learn relevant operations in the Linux command line. Similarly, we set a goal (flag): through learning, establish a sense of control over the Linux system, truly achieving “calm in the face of challenges”.

Before we officially dive into the topic, I will paste the purchase link for the cloud server ECS again:

https://www.aliyun.com/minisite/goods?userCode=08mhjxsk
https://www.aliyun.com/daily-act/ecs/99program?userCode=08mhjxsk

The first link is for new users of Alibaba Cloud to receive a coupon, and the second link leads directly to the purchase page for the cloud server “99 package”.

Quick Start Guide to Linux Operations: Transitioning Your Windows Habits to Linux

Let’s first review the basic configuration and usage of the current cloud server ECS:

  • • We have an Alibaba Cloud cloud server (ECS) with specifications of 2 cores, 2GB RAM, 3M bandwidth, and 40GB storage, with a usage period of 1 year.
  • • The operating system is the Alibaba Cloud Linux server operating system distribution (Alibaba Cloud Linux 3.2104 LTS 64-bit).
  • • The server has a dedicated public IP address.
  • • A WordPress type website has been deployed and can be accessed directly via the public IP.
  • • In addition to the root user, two user accounts have been created: <span>ecs-user</span> (with administrator privileges) and <span>moweng</span> (a regular user who can gain root privileges via <span>sudo</span>).
  • • Password login has been disabled, and daily logins use SSH key authentication to enhance security.
  • • Direct login for the root user has also been disabled to further ensure system security.

So, what operations do we need to master to truly have a sense of “control” over the Linux system? Here are some basic yet crucial capabilities:

  • • Ability to log into the system via SSH;
  • • View basic system information, such as operating system version, CPU, memory capacity, etc.;
  • • Ability to reboot the system;
  • • Clearly understand the current folder path;
  • • Master basic file operations, including creating, copying, moving, deleting, and renaming files;
  • • Ability to edit file content and save it;
  • • View total disk space and remaining space;
  • • View currently running processes, similar to the task manager in Windows;
  • • Install and update software packages;
  • • ……

Of course, I personally believe that what truly provides peace of mind is that any operation that causes changes to the system should have a corresponding rollback mechanism. Therefore, this article will revolve around this idea to help you establish control over the system in practice.

Let’s get started!

1. Login, Basic Interface, Reboot

Log in as <span>moweng</span> user through the Alibaba Cloud console Workbench

The detailed steps have been introduced in the previous article, so I won’t repeat them here.

Quick Start Guide to Linux Operations: Transitioning Your Windows Habits to Linux

Log in using remote login software on your local computer

At the end of the previous article, I provided instructions on how to use the Git Bash software on your local computer to connect to ECS via SSH:

ssh -i ~/.ssh/moweng_rsa_key moweng@<IP Address>

Where:

  • <span>ssh</span>: the command used to connect to a remote server via SSH protocol.
  • <span>-i ~/.ssh/moweng_rsa_key</span>: specifies the private key file used for authentication.
  • <span>moweng@<IP Address></span>: specifies the username and the IP address of the remote server.<span><IP Address></span> should be replaced with your ECS’s public IP address.

Enter the command line interface

Quick Start Guide to Linux Operations: Transitioning Your Windows Habits to Linux

This is the command line interface, similar to opening CMD or PowerShell in Windows. The meaning of <span>[moweng@iZbp1ihv7x1xv46u2cgq9xZ ~]$</span> is as follows:

  • <span>moweng</span>: the currently logged-in username
  • <span>iZbp1ihv7x1xv46u2cgq9xZ</span>: the name of this computer, more formally known as the “hostname”
  • <span>~</span>: indicates that the current directory is the “home directory”
  • <span>$</span>: indicates that the current user is a regular user (if it is <span>#</span>, it indicates the administrator root)

View the current logged-in username

If the current logged-in username <span>moweng</span> does not display, the command to check is as follows:

whoami
Quick Start Guide to Linux Operations: Transitioning Your Windows Habits to Linux

Clearly, the whoami command means “who am I”.

View the hostname

hostname
Quick Start Guide to Linux Operations: Transitioning Your Windows Habits to Linux

View the current directory path

So, what does <span>~</span> represent as the “home directory”? You can check it with the following command:

pwd
Quick Start Guide to Linux Operations: Transitioning Your Windows Habits to Linux

As you can see, <span>~</span> represents the “home directory”, which is the current directory, namely <span>/home/moweng</span>.

View the current time

In Windows, to check the current time, we generally glance at the taskbar in the lower right corner. In Linux, the command is as follows:

date

Log out and log back in

The rollback operation for logging in is, of course, logging out. The command is simple:

exit
Quick Start Guide to Linux Operations: Transitioning Your Windows Habits to Linux

Clicking “Reconnect” will log in again as the current user; clicking “Return to Login” will return to the login screen, allowing you to choose another user to log in.

Elevate to <span>root</span> user

Some operations require elevation to the <span>root</span> user, we can add <span>sudo</span> before the command, or switch directly to the <span>root</span> user:

sudo -i
Quick Start Guide to Linux Operations: Transitioning Your Windows Habits to Linux

As you can see, the <span>$</span> has changed to <span>#</span>, and the username has changed to the administrator <span>root</span>.

Return to <span>moweng</span> user

The rollback operation is also <span>exit</span>

exit
Quick Start Guide to Linux Operations: Transitioning Your Windows Habits to Linux

Reboot

In Windows, we often reboot to resolve issues, right? In Linux, rebooting is not necessary frequently, but as a corresponding operation to increase our sense of control, we can also familiarize ourselves with it:

sudo reboot
Quick Start Guide to Linux Operations: Transitioning Your Windows Habits to Linux

Or

sudo shutdown -r now
Quick Start Guide to Linux Operations: Transitioning Your Windows Habits to Linux

You can also perform a reboot operation through the Alibaba Cloud ECS console:

Quick Start Guide to Linux Operations: Transitioning Your Windows Habits to Linux

Rebooting does not require a corresponding rollback operation.

2. Folder and File Operations

In Windows, the objects we most commonly interact with are undoubtedly “folders” and “files”. In Linux, we are accustomed to referring to “folders” as directories.

In fact, the essence of “folders” and “directories” is the same; they just differ in form: the former is the term used in a graphical interface, while the latter is the term used in a command line interface.

Enter a directory

In Windows, we access the contents of a folder by “opening the folder”. In the Linux command line, the corresponding common operation is called entering a directory or changing directories, using the command <span>cd</span> (change directory). Common usages are as follows:

cd /home/moweng                                        # Enter the /home/moweng directory
cd music                                                    # Enter the music directory under the current directory
cd ..                                                            # Return to the parent directory
cd ~                                                            # Return to the home directory
cd /                                                            # Enter the root directory

View directory contents

In Windows, when you open a folder, the files and subdirectories within it are automatically displayed. However, in Linux, entering a directory does not automatically list the contents. We need to use the <span>ls</span> command to manually view:

ls                                                             # View visible files/folders in the current directory
ls /home/moweng/folder1                        # View visible files/folders in the /home/moweng/folder1 directory
ls -l                                                            # View visible files/folders in the current directory with detailed information (permissions, size, modification time, etc.)
ls -a                                                         # View all files/folders in the current directory, including hidden ones
ls -al                                                         # Combination of -l and -a
ls -lh                                                         # Human-readable file size display (KB, MB)

Note: <span>ls -l</span> and <span>ls -lh</span> differ in that the former displays file sizes in bytes, while the latter automatically converts to a more readable KB or MB format.

Quick Start Guide to Linux Operations: Transitioning Your Windows Habits to Linux

Create a directory

In Windows, we typically create a new folder by right-clicking → New → Folder. In Linux, we use the <span>mkdir</span> command to create a directory:

mkdir folder1                                            # Create folder1 directory in the current directory
mkdir /home/moweng/folder2                # Create /home/moweng/folder2 directory, provided that /home/moweng exists
mkdir -p /home/moweng/folder3            # Create multiple levels of directories; if any intermediate directory does not exist, it will be created automatically

Delete a directory

The reverse operation of creating a directory is deleting a directory. Linux provides two main commands: <span>rmdir</span> and <span>rm</span>.

rmdir folder1                                                    # Delete the empty folder1 directory in the current directory
rmdir /home/moweng/folder2                        # Delete the empty /home/moweng/folder2 directory
rmdir -p /home/moweng/folder3/folder4    # Recursively delete multiple empty directories, starting from the innermost folder4 outwards until encountering a non-empty directory

Note:

  • • The <span>rmdir</span> command can only delete empty directories.
  • • The <span>rmdir</span> command with the <span>-p</span> option recursively deletes multiple empty directories, starting from the innermost empty directory until encountering a non-empty directory, at which point it exits with an error.

If you want to delete a non-empty folder, the standard practice is to use the <span>rm</span> command with the <span>-r</span> (recursive) option, for example:

rm -r folder1     # Deletion will prompt for confirmation one by one (interactive)
rm -rf folder2    # Forcefully delete recursively without any prompts (dangerous! Use with caution)

Note:

  • <span>-r</span> (–recursive): recursively processes, files and subdirectories within the folder will be deleted layer by layer;
  • <span>-f</span> (–force): forcefully deletes, skips non-existent files, suppresses errors, and can easily lead to accidental deletions; be sure to confirm the path.

Special reminder: <span>rm -rf</span> is extremely dangerous (for example, mistakenly executing <span>rm -rf /</span> could clear the system), so it is recommended to confirm the target folder path and contents with <span>ls</span> before operating, and important data should be backed up first.

Recover deleted directories

The rollback operation corresponding to deleting a folder is recovering the deleted folder. However, a significant difference from Windows systems is that Linux systems do not have a “recycle bin” feature; to implement a similar recycle bin mechanism, additional tools (such as Trash-cli) need to be installed. For the sake of this introductory article, we will not delve into this topic to avoid distracting from the main narrative and adding unnecessary burden.

Create a file

Sometimes we need to create a blank file. In Windows, we can create a blank file with a .txt extension by right-clicking → New → Text Document. In Linux, we use the <span>touch</span> command to quickly create an empty file.

touch note.txt                                         # Create an empty file named note.txt in the current directory
touch /home/moweng/folder1/note.txt    # Create an empty file named note.txt in the /home/moweng/folder1 directory
Quick Start Guide to Linux Operations: Transitioning Your Windows Habits to Linux

This file is currently empty, but we will learn how to edit it later.

Delete a file

The reverse operation of creating a file is deleting a file. In Linux, we also use the <span>rm</span> command, with common scenarios as follows:

rm note.txt                                             # Delete the note.txt file in the current directory
rm /home/moweng/folder1/note.txt     # Delete the file at the absolute path
rm note1.txt note2.txt note3.txt     # Delete multiple files in the current directory
rm *.txt                                                     # Use wildcard to delete all .txt files in the current directory
rm -i note.txt                                         # Interactive deletion, will prompt for confirmation before each deletion
rm -f note.txt                                         # Forcefully delete the note.txt file in the current directory

Copy a file

The command to copy a file is <span>cp</span>, with common operations as follows:

cp note.txt note1.txt                                                 # Copy note.txt in the current directory and name it note1.txt
cp note.txt /home/moweng/folder1/                         # Copy note.txt in the current directory to the specified directory
cp note.txt /home/moweng/folder1/note2.txt         # Copy note.txt in the current directory to the specified directory and name it note2.txt

After copying, if you need to undo the operation, you can use <span>rm</span> to delete the target file.

Copy an entire directory

What if copying a single file is too inefficient? Add the <span>-r</span> option to the <span>cp</span> command to copy an entire directory:

cp -r source_dir/ target_dir/    # Copy source_dir to target_dir

Similarly, the corresponding rollback operation is the delete directory command <span>rm -r</span>.

Move a file

Moving a file is similar to the “cut + paste” in Windows, and the command is <span>mv</span>, with usage similar to <span>cp</span>:

mv note.txt note1.txt                                                 # Rename note.txt in the current directory to note1.txt
mv note.txt /home/moweng/folder1/                         # Move note.txt in the current directory to the specified directory
mv note.txt /home/moweng/folder1/note2.txt         # Move note.txt in the current directory to the specified directory and name it note2.txt
mv folder1/ folder2/                                                     # Move the folder1 directory in the current directory to the folder2 directory in the current directory

The rollback operation for moving a file is to move the file back to its original location using the <span>mv</span> command.

Note: Unlike <span>cp</span>, moving a directory does not require adding the <span>-r</span> option, as it inherently supports recursive moving.

Find a file

In Windows, using the search function in the file explorer is also a common operation. In Linux, you can use the <span>find</span> command to find files:

find ~/ -name "note.txt"                             # Accurately find the note.txt file in the home directory and its subdirectories
find ~/ -name "*.txt"                                 # Find files with a .txt extension in the home directory and its subdirectories

3. File Editing

In Windows, we typically use Notepad or Word to open and edit text files. In Linux, there are also several common text editing methods suitable for different usage scenarios.

View file content with <span>cat</span> command

Before formally editing a file, we usually check its content first. The simplest way is to use the <span>cat</span> command. For example, we can view the WordPress configuration file <span>wp-config.php</span>:

cd /usr/share/nginx/html/wordpress                 # Directory where WordPress website files are stored
cat wp-config.php

Note: This command will output the entire content of the file to the terminal at once, suitable for quick browsing when the file content is small.

Append content to a file using <span>cat</span> command

Although <span>cat</span> is not an editor itself, it can easily append content to the end of a file when combined with the redirection symbol.

cat >> note.txt

After executing this command, the cursor will move to a new line, allowing you to input the content you want to add. Press <span>Ctrl + D</span> to end the input.

We can also define an input end symbol. A common practice is to use <span>EOF</span> (End Of File) as the end marker.

Suppose you want to add the following lines to the <span>note.txt</span> file at once:

This is the first line
This is the second line
This is the third line

Using <span>cat</span> with a custom end symbol (like <span>EOF</span>) to complete the writing is as follows:

cat >> note.txt <<EOF
This is the first line
This is the second line
This is the third line
EOF

Overwrite content in a file using <span>cat</span> command

The previous example used <span>>></span>, which indicates appending. If you want to clear the original content of the file and rewrite new content, you can use <span>></span>:

# Input content, overwrite note.txt, press Ctrl + D to end input
cat > note.txt

# Overwrite note.txt, using EOF as the end symbol
cat > note.txt <<EOF
This is the first line
This is the second line
This is the third line
EOF

Use <span>echo</span> command for single line overwrite/append

If you only need to write a line of text, you can use the <span>echo</span> command, which is more concise and efficient.

# Append a line
echo 'Append a line of text' >> note.txt

# Overwrite a line
echo 'Overwrite a line of text' > note.txt

When you need to quickly modify an option in a configuration file or simply log a message, <span>echo</span> is a very practical tool.

Edit text using <span>vi</span> editor

Most Linux systems come with the <span>vi</span> editor pre-installed. It is not as intuitive as Notepad in Windows, but it is a basic skill that every Linux user should learn.

To start the <span>vi</span> editor, use the following command:

vi note.txt

Once you enter the editing interface, you will find that you cannot directly input text; this is because <span>vi</span> is a modal editor, which has multiple operation modes.

<span>vi</span>‘s basic operation flow is as follows:

  • • Normal mode
    • • You are in this mode when you first open the file.
    • • Press <span>i</span> to enter “insert mode”.
  • • Insert mode
    • You can input or modify content like in Notepad.
  • • Exit insert mode
    • Press <span>Esc</span> to return to normal mode.
  • • Save and exit
    • Type <span>:wq</span> (write & quit), then press Enter to save and exit.

Note:

  • • If you accidentally press other keys, you can press <span>Esc</span> and then type <span>:q!</span> to abandon changes and exit.

Use <span>vim</span> command

<span>vim</span> is an enhanced version of <span>vi</span>, with more powerful features and a friendlier interface, which many developers prefer. Therefore, <span>vim</span> may be more commonly found in technical articles than <span>vi</span>.

In most Linux systems, <span>vim</span> is already pre-installed. The operating system of Alibaba Cloud ECS instances usually also comes with <span>vim</span>, so no additional installation is required.

For beginners, the basic operations of <span>vim</span> are almost identical to those of <span>vi</span>, so mastering the usage of <span>vi</span> is equivalent to mastering the core operations of <span>vim</span>.

Summary

This concludes the introduction of the first part; take some time to digest it! I believe that after a little practice, you have gained a perceptual understanding of Linux. Below is a summary of the commands in this article:

Windows Operations Linux Commands Function Description
View Task Manager – Users <span>whoami</span> View the current logged-in username
View Computer Name <span>hostname</span> View the hostname
View Address Bar <span>pwd</span> View the current directory path
View Taskbar Time <span>date</span> View the current time
Log Out <span>exit</span> Log out / Exit current user
Switch to System Account <span>sudo -i</span> Switch to <span>root</span> user
Reboot <span>sudo reboot,</span><code><span>sudo shutdown -r now</span> Reboot immediately
Enter Folder <span>cd</span> Change directory
Open File Explorer <span>ls</span> View directory contents
Create New Folder <span>mkdir</span> Create directory
Delete Folder <span>rmdir</span> Delete directory
Create Text File <span>touch</span> Create empty file
Delete File <span>rm</span> Delete file/directory
Copy, Paste <span>cp</span> Copy file/directory
Cut, Paste, Rename <span>mv</span> Move file/directory
Search for File <span>find</span> Find file
Open File <span>cat</span> View file content
Edit File <span>cat, </span><code><span>echo</span>, <span>vi</span>,<span>vim</span> Edit file content

Leave a Comment