入门 Linux 和 Bash
This tutorial will help those new to Linux to get started installing and updating packages using the Ubuntu distribution of Linux that is installed by default using WSL, as well as using some basic commands with the Bash command line.
Installing and Updating Software
You can install and update software programs directly from the command line using the preferred package manager for the distribution you are running.
In Ubuntu, for example, first update the list of software available by running ‘sudo apt update’. Then, you can directly get software by using the ‘sudo apt-get install’ command followed by the name of the program you wish to install:
sudo apt-get install <app_name>
To update programs that have already been installed, you can run:
sudo apt update && sudo apt upgrade
[!TIP] Different distributions of Linux often have different package managers and will require using an install command specific to the associated package manager. For example, the main package manager for Arch Linux is called pacman and the install command would be
sudo pacman -S <app_name>
. The main package manager for OpenSuse is called Zypper and the install command would besudo zypper install <app_name>
. The main package manager for Alpine is called apk and the install command would besudo apk add <app_name>
. The two main package managers for Red Hat distributions, like CentOS, are YUM and RPM and an install command could besudo yum install <app_name>
orsudo rpo -i <app_name>
. Refer to the documentation of the distribution you are working with to find out what tools are available for you to install and update software.
Working with files and directories
To view the path of the directory you are currently in, use the ‘pwd’ command:
pwd
To create a new directory, use the ‘mkdir’ command followed by the name of the directory you want to create:
mkdir hello_world
To change directories, use the ‘cd’ command followed by the name of the directory you want to navigate to:
cd hello_world
To see the contents within the directory you are currently in, type ‘ls’ into the command line:
ls
By default, the ‘ls’ command will print the name of all the files and directories only. To get additional information such as the last time a file was modified or file permissions, use the flag “-l”:
ls -l
You can create a new file via the ‘touch’ command followed by the name of the file you would like to create:
touch hello_world.txt
You can edit files using any downloaded graphical text-editor or the VS Code Remote – WSL extension. You can learn more about getting started with VS Code here
If you prefer to edit a file directly from the command-line, you’ll need to use a command-line editor such as vim, emacs, or nano. Many distributions come with one or more of these programs installed, but you can always install these programs by following the installation instructions outlined in the guide from above.
To edit your file with your preferred method of editing, simply run the program name followed by the name of the file you’d like to edit:
code hello_world.txt
notepad.exe hello_world.txt
To see the contents of a file in the command line, use the ‘cat’ command followed by the file you’d like to read:
cat hello_world.txt
Using Pipes and Redirect Operators
A pipe ‘|’ redirects the output from one command as input into another command. For example, lhscmd | rhscmd would direct the output from lhscmd to rhscmd. Pipes can be used in a variety of ways to quickly accomplish tasks through the command line. Below are just a few simple examples of how pipes can be used.
Imagine you want to quickly sort the contents of a file. Take the fruits.txt example below:
cat fruits.txt
Orange
Banana
Apple
Pear
Plum
Kiwi
Strawberry
Peach
You can quickly sort this list by using a pipe:
$ cat fruits.txt | sort
Apple
Banana
Kiwi
Orange
Peach
Pear
Plum
Strawberry
By default, the output of the ‘cat’ command is sent to standard output; however, the ‘|’ allows us to instead redirect the output as the input to another command, ‘sort’.
Another use case is searching. You can use ‘grep’ which is a helpful command that searches input for a particular search string.
cat fruits.txt | grep P
Pear
Plum
Peach
You can also use redirect operators like ‘>’ to pass the output to a file or stream. For example, if you wanted to create a new .txt file with the sorted contents of fruit.txt:
cat fruits.txt | sort > sorted_fruit.txt
$ cat sorted_fruit.txt
Apple
Banana
Kiwi
Orange
Peach
Pear
Plum
Strawberry
By default, the output of the sort command is sent to standard output; however, the ‘>’ operator allows us to instead redirect the output into a new file named sorted_fruits.txt.
You can use pipes and redirect operators in many interesting ways to more efficiently complete tasks directly from the command line.