29

When I first migrated from Windows to Ubuntu, by far the most daunting thing I had to do was use the command line.

Typing commands is an alien experience when you've only ever been used to pointing and clicking.

When I talk to new Ubuntu users, they are often uneasy with the idea of talking directly to their computer.

Is there a simple and friendly guide to help new users get acquainted with the command line?

Do you have any tips to make the experience easier or more fun?

1
  • 4
    For what it's worth, Ubuntu tries hard (at least, harder than any other Linux distribution) not to make you use the command line if you don't want to. But there will always be things that are much easier/quicker to do on the command line than in a GUI. So it's a good idea to learn about it (and a good question).
    – David Z
    Jul 31, 2010 at 16:58

13 Answers 13

17

If you are looking for a good guide to learn the command line, my favorite is LinuxCommand.org

The guide will show you the basics of the command line, and will even guide you into writing useful shell scripts.

That said, most user will not need to use the command line for most day to day operations. I do not think that the command line should discourage users from migrating to Ubuntu. But once you learn the power of the command line, you won't be able to live without it!

15

Here are some common commands for manipulating the filesystem:

  • cp [src] [dest] - copies src to dest
  • mv [src] [dest] - moves src to dest (also used for renaming)
  • cd [dir] - changes current directory to dir
  • pwd - prints the current directory
  • cat [file] - prints the contents of file to the screen
  • rm [file] - removes a file1
  • rmdir [dir] - removes an empty directory

Prefixing any of the commands with sudo causes the command to be executed as the root user.

1 - don't type sudo rm -rf / as it will erase the filesystem

2
  • 5
    Heh, +1 for the sudo rm warning :) Jul 31, 2010 at 20:31
  • 2
    Don't forget a note about the infamous forkbomb ":(){ :|:& };:" that essentially forks an infinite number of new processes and gradually slowing the system until the it runs out of resources requiring a system restart. For more info see en.wikipedia.org/wiki/Fork_bomb. Sep 10, 2010 at 7:11
12

1) Tab completion:

A giant time saver. If you are typing a command, you need only type enough of the command to provide an initial segment that can only be extended in a single way and then can press TAB once to expand your initial segment to the entire command. So, for instance, on my system umo TAB expands to umount. (On my system as what initial segments are extendable only in one way is a function of what you have installed, etc.) If you do not type enough to make the completion unambiguous, TAB will not expand, but a second TAB will display a list of possible completions. So, on my system, um TAB TAB yields:

umask       umax_pp     umount      umount.hal

Tab completion also works on paths: cd /home/me/docs/reallylo TAB will, if unique, expand to cd /home/me/docs/reallylongdirname and, if not unique, offer a list of candidate continuations as with um above.

2) man some-command or some-command --help or some-command -h:

If you cannot recall how a command works, you can get documentation right there in the shell. man usually provides the most detail. Usually one or both of the --help and -h arguments to a command provides a short summary.

3) head:

man some-command takes over the terminal and prevents you from entering commands while the man text is displayed. man some-command | head will display the first 10 lines. man some-command | head -n will display the first n lines. In both cases, you get your prompt back, so that you can have the man text on screen as you enter your command.

5
  • When I use man some-command, man allows me to scroll up and down through the page. Is that not usual? Jul 31, 2010 at 16:28
  • @mac9416: Indeed. I'd never tried that before :-[ Modified the answer to take this into account. Thanks!
    – vanden
    Jul 31, 2010 at 16:34
  • Yes, man uses a pager (usually less). If you want to examine a file in a similar way, type less somefile. If you do get used to vim, you can use jk keys to scroll.
    – Umang
    Aug 1, 2010 at 5:06
  • In all fairness, tab completion is not available in all shells. Aug 4, 2010 at 19:19
  • @George Edison: True enough. But, if it is a new user at issue (as it is in the question), they will not be very likely to have changed the shell from the default and thus will have tab completion.
    – vanden
    Aug 7, 2010 at 19:43
7

Switch to zsh!

While it is very much like bash, it has a lot of nice additional features out of the box (like for example typo correction, even in a preceding path component or a useful widget to call help for the current command (via run-help; I press ESC-h after e.g. having typed mplayer, and it opens the man page. After closing it I'm back at the old line)).

I recommend the following book, which covers zsh, bash and some other shells:
From Bash to Z Shell: Conquering the Command Line. While it is a few years old already, I'm glad this had not turned me away from buying it. This recommendation also holds if you do not want to switch to zsh.

I have been using the command line a lot since a few years (locally and via SSH), I've only recently made the switch to zsh myself (mostly because of my custom bash prompt, which is not compatible). Here is my zsh config (integrated in my dotfiles repository).

You can use chsh -s /bin/zsh to switch your shell (via /etc/passwd), or just call it from your current shell, i.e. type zsh in your bash prompt (you likely have to install it first though (sudo apt-get install zsh).

5

"apropos" (or it's equivalent: "man -k") to find a command to do something.

$ apropos [my query]

For instance, to find the command to copy files:

$ apropos copy

will list a bunch of commands, of which

cp (1) - copy files and directories

is one.

"cp" is the command and "1" is the section from the manuals where it appears. Section 1 is general user commands (other sections include things like library calls, which you won't be interested in). To restrict the search to just section 1 use:

$ apropos -s1 [my query]

To then find out more about the command use "man". e.g.

$ man cp
1
  • +1 for being the first one to mention man. Bravo!
    – jathanism
    Aug 4, 2010 at 7:38
4

Try using fish

fish is a user friendly command line shell for UNIX-like operating systems such as Linux.

Amongst other things it features more advanced tab completion than bash which can be very helpful while learning.

alt text

https://www.pablumfication.co.uk/2010/02/26/fish/

http://fishshell.org/index.php

2
  • 1
    I love the history search feature. Just type anything, hit the up arrow key and you are searching in your history for those same words. Great time-saver. Aug 2, 2010 at 16:47
  • the link to fishshell.org is outdated. fishshell.com is the new one.
    – NES
    Jan 11, 2011 at 22:10
2

history | grep SOMETHING — finds command you used before that contains SOMETHING.

fortune ­— :-)

1
  • 2
    For this, I use Ctrl+R SOMETHING (with the difference that this gives the last occurrence). Repeat Ctrl+R to find more occurrences of SOMETHING in the command history. Aug 2, 2010 at 19:17
2

To learn how to use a command add a space and then "--help" to the end of it - this tells you how to use it and gives a list of options.

e.g.

cp --help
1
  • Warning, this doesn't work with all commands!
    – SilverWolf
    Feb 12, 2018 at 2:15
2

The Ubuntu Pocket Guide and Reference features a chapter on using the command line. It'll quickly get you up and running with the command line.

Also it is free to download or read online.

2

I recommand to use CLI Companion:

CLI Companion is a tool to store and run Terminal commands from a GUI. People unfamiliar with the Terminal will find CLI Companion a useful way to become acquainted with the Terminal and unlock its potential. Experienced users can use CLI Companion to store their extensive list of commands in a searchable list.

enter image description here

You get it by running:

sudo add-apt-repository ppa:clicompanion-devs/clicompanion-nightlies
sudo apt-get update
sudo apt-get install clicompanion

If you don't want to add the PPA try this file for 12.04 (latest version 1.1-6, released on 2012-04-14 - check this page for newer versions.) - or remove the PPA after installing with sudo add-apt-repository -r ppa:clicompanion-devs/clicompanion-nightlies .

1

Find an Ubuntu book with good command line index, zerox it and place it near the computer. Force yourself to use it. A good resource is the book "Ubuntu Linux Toolbox 1000+ commands", covers all you need to know (http://www.amazon.com/Ubuntu-Linux-Toolbox-Commands-Debian/dp/0470082933)

However, if you don't run a server, in Ubuntu desktop almost everything is available with the GUI.

0

I learned a ton about using the command line and getting comfortable with working within it from reading The Bash Cookbook from O'Reilly and Associates. It's a book about Bash scripting, but the bite sized chunks of the cookbook format make it very accessible. As a side benefit, if you think "Gee, I'd sure like to do X, but I don't know how," you can use the table of contents to look up X (and Y and Z for that matter) and get a good idea on how to do it (and a decent explanation of how it works with pointers to other recipes and resources that can further expand your understanding).

0

I have been reading Official Ubuntu Server Book, The (2nd Edition) to learn system administration, and not only have I become more adept at using the command line, but I've also begun to learn the inner workings of the OS itself.

Using the Ubuntu Server Edition helps me to learn the command line easier, because I don't have the GUI to fallback on.

Not the answer you're looking for? Browse other questions tagged .