July 28, 2016

Howdy, Ubuntu on Windows! Ubuntu Commands Every Windows User Should Learn

linux-on-windows-dustin-kirkland-firefly.jpg

Ubuntu on Windows
Canonical developer Dustin Kirkland is a space cowboy on the great Ubuntu-on-Windows frontier.

Some Windows desktop users will certainly be new to Bash and the new shell, Ubuntu on Windows.  In Part 1, I introduced the topic, and in Part 2, I showed how to get started by installing Windows. In this part of our series, I’ll describe a handful of essential commands to help get started.

1. <Tab>

Learn to love your <Tab> key!  Start typing any command and press <Tab> to complete the rest of your command.  When multiple matching options exist, you can press <Tab><Tab> to list all available options.  Watch a real Linux command line wizard at work, and you’ll marvel at how efficient, and accurate she is, deftly using the <Tab> key accomplish the most complex tasks in a minimum number of keystrokes.

2. man

Documentation!  Most commands in your Ubuntu shell will have excellent “manuals” or “manpages.”  Simply type man <command> and you’ll enter a page viewer, where you can learn all about whatever you’re trying to better understand.  All Ubuntu manpages are also rendered to HTML, and you can search and read them at manpages.ubuntu.com.  Heck, let’s read the manpage for man, itself!

  • man man

3. sudo

You’ll find that some commands refuse to work, unless you type sudo.  sudo stands for superuser do.  It’s exactly like Windows’ “Run as Administrator” action.  sudo is how you execute privileged instructions, like editing system configuration files, installing additional software, or making permanent changes to the system.  Read more about it in the manpage here, and try the following:

  • whoami

  • sudo whoami

4. apt

apt is the packaging system for Ubuntu.  It’s the command-line tool that enables you to add and remove software from your Ubuntu shell.  apt talks to archive.ubuntu.com,  which is much like the Windows Store or Apple iTunes store, except ALL of the software is free!  Moreover, the vast of majority of it is completely open source.  You can search for software, install packages, remove packages, update, and upgrade your system.  Read more about apt in the manpage here, and try the following:

  • sudo apt update

  • sudo apt upgrade

  • apt search tree

  • apt-cache search tree

  • sudo apt install tree

  • tree /etc

  • sudo apt purge tree

5. Pipes

While not strictly a “command”, perhaps the most fundamental concept to understand in Bash is that you can always “pipe” the output (stdout, or “standard out”) of one command, to another command as its input (stdin, or “standard in”).  In this manner, you can “pipeline” many different UNIX/Linux utilities together, with each one processing the output of the other.  To use a pipe, you simply add the | character to the end of your command, and then start typing your next command.  In the rest of the examples below, you will “build” more and more advanced processing on the command line, through the use of pipes.  You can learn more about pipes here.

6. grep

grep will quickly become your best friend, in your Ubuntu environment on Windows!  grep stands for “get regular expression,” which means that you can do some super fancy searching, if you speak in regexes.  You’ll want to learn that eventually, but let’s start simply.  Here’s a couple of examples to try, and make sure you read the manpage.

  • apt-cache search revision control

  • apt-cache search revision control | grep git

7. sed

sed is a simple “inline editor.”  Like piping text to grep, you can pipe data directly to sed, and using regular expressions, you can replace text.  This is super handy for automating lots of tasks, updating files, and countless other things.  Here’s a simple example -- we’re going to print (cat) a file, and then replace all of the colons (:) with three whitespaces.  Note that we’re not going to actually edit the file (though we could).  We’re just going to print the output to the terminal.  Try this, and make sure you read the manpage!

  • cat /etc/passwd

  • cat /etc/passwd | sed “s/:/   /g”

8. awk

awk is amazing!  It is its own programming language and can do some pretty spectacular things, like advanced mathematics and text formatting with merely a couple of characters.  You can spend the next year of your life becoming an awk expert.  But let’s just look at a super simple example, that’s always handy!  Let’s say you want to split some text into a column, and only get the first column.  We’ll build on our previous example, to get a list of users on the local system.  Try this, and then read the manpage!

  • cat /etc/passwd

  • cat /etc/passwd | sed “s/:/   /g”

  • cat /etc/passwd | sed "s/:/   /g" | awk '{print $1}'

9. xargs

If you like to automate tasks, you definitely want to get to know xargs.  xargs provides, effectively, a “loop” that you can use on the command line.  Basically, you can run the same command over and over, against a variable that changes.  Let’s say that we wanted to say hello to each of those users we just listed.  Try this, and then read the manpage!

  • cat /etc/passwd

  • cat /etc/passwd | sed “s/:/   /g”

  • cat /etc/passwd | sed "s/:/   /g" | awk '{print $1}'

  • cat /etc/passwd | sed "s/:/   /g" | awk '{print $1}' | xargs -i echo "Howdy, {}!"

10. less

Some commands produce very little or no output.  Others produce a lot more output than can fit on the screen.  In these cases, you should use the command less.  less will “page” the information, allowing you to scroll down, up, and even search in the text.  Let’s look at our system log, and then try piping it through less, moving up and down with the arrow keys and PgUp and PgDn keys.  Type q to quit, and then read the manpage!

  • cat /var/log/syslog

  • cat /var/log/syslog | less

11. ssh

ssh is how we get from one Linux machine, to another.  Ubuntu on Windows, of course, comes with the ssh client, enabling you to ssh directly to any other Linux system that has an ssh server (openssh-server, in Ubuntu) installed.  You’ll definitely want to read up on ssh in the manpage!

  • ssh example.com

12. rsync

rsync is the brilliant tool in Linux that we use to securely copy and synchronize directories from one place to another -- perhaps on the local system, or even over an encrypted connection across the network.  More than a decade before Dropbox, OneDrive, or iCloud, Linux users have been efficiently backing up their data “to the cloud” using rsync.  Check out the manpage, and try this:

  • mkdir /tmp/test

  • rsync -aP /usr/share/doc/ /tmp/test/

  • ls /usr/share/doc

  • ls /tmp/test/

  • rm -rf /tmp/test

13. find / locate

Windows users are certainly familiar with the File Explorer’s file search mechanisms.  Let’s look at two ways to search your Linux filesystem, for files and directories.  We can use the find command, which has a ton of interesting options.  You should read more about find in its manpage and locate in its manpage, and try these commands:

  • find /etc/

  • find /etc/ -type d

  • find /etc/ -type f

  • find /etc/apt -name sources*

  • find /mnt/c/Program\ Files

  • sudo updatedb

  • locate bash.exe

This is merely the tip of the iceberg!  There are hundreds of commands.  List the ones installed in /usr/bin, and count them using:

  • ls /usr/bin

  • ls /usr/bin | wc -l

There, you just learned two more commands (ls and wc)!  And I’m sure, by now, you know to check their manpages :-)

Cheers,

Dustin

Read the next article in this series: Howdy, Ubuntu on Windows! Write and Execute Your First Program

Learn more about Running Linux Workloads on Microsoft Azure in this on-demand webinar with guest speaker Ian Philpot of Microsoft. Watch Now >> 

 
Click Here!