23

I am a new user, who has just been told to run / execute a command. The answer has some text with a grey background like this text here or

sometimes this text here.

However, I don't know what "run" or "command" means in relation to Ubuntu.

How do I do this?


This is intended for use as a Q&A you can link to when you tell someone to run a command.

Tim
  • 32,861
  • 27
  • 118
  • 178
  • 2
    Hmm downvote because we don't need this? Or newbie questions are discouraged? Or this is a duplicate? – Tim Feb 28 '16 at 19:17
  • 7
  • The run dialog is pretty limited - for a lot of commands, you do need a terminal to be opened. – muru Feb 28 '16 at 19:56
  • 3
    Eh.. How do you run a command? A: Use a terminal. Then: What is a terminal, etc.? It follows trivially. – muru Feb 28 '16 at 19:57
  • It's so related that, for me, it's a dupe. My vote stands. – muru Feb 28 '16 at 19:58
  • -1: because the run dialog is only useful if a) you don't need the output, and b) you don't need to provide input. – muru Feb 28 '16 at 19:59
  • You're going to say that to a new user who's so unfamiliar with these concepts that they don't even understand what a command is? Good luck explaining input, output, authorization for sudo commands, etc. to them. This is one question I certainly won't be linking to. – muru Feb 28 '16 at 20:01
  • 6
    @muru no. This is a question that is simply saying how to run a command... I thought that we were here to help people new to Ubuntu too, no? Why the angst towards people who don't know things? I think the answer clearly explains that it's good for launching a GUI program... – Tim Feb 28 '16 at 20:02
  • So I, who am not posting misleading things, am the angsty one? All right then. As I said, my vote stands. Good day. – muru Feb 28 '16 at 20:03
  • As I said before: The run dialog is only useful if you don't need input or output. And since you claim so boldly that you don't need a terminal, thus implying you don't need either of these, you are misleading new users. Good luck getting a user to run sudo apt-get update from the run dialog. That's all I have to say. – muru Feb 28 '16 at 20:05
  • 1
    Excellent! Favourited, bookmarked and bounty added!!! ;-) – Fabby Mar 03 '16 at 12:31
  • 1
    Using it as well! http://askubuntu.com/a/746248/344926 – Fabby Mar 16 '16 at 14:56

3 Answers3

42

What is a command?

Commands, and the command line, are another way to tell the computer what to do. For example, you can click the google chrome icon, or you can run the command google-chrome.

This may seem a silly thing to do, but when you click the icon, that's what the computer does! It also is much easier to do some tasks with the command line.

For example, installing programs can be quicker and easier - just use sudo apt-get install program, rather than navigating through the software center.

And if my Bluetooth stops working, I run a command to fix it - that's the only way!

Anyway, how do you run a command?

This is also simple, although there are many ways to do it.

Simply, you open a Terminal and type (or copy / paste) the command in, then press Enter. In the image, I've run the command ls Desktop/ which will list all files on my desktop. Note that to paste you press Ctrl + Shift + V in the terminal.

By default you open the terminal by pressing Ctrl + Alt + T, or opening the Dash and searching for "Terminal".

What is a tty?

Sometimes, you may be told to run a command in the tty - often followed by a number, for example tty2. The GUI runs on tty7. (Update for 2021: the GUI now often runs on tty1 or tty2).

To access this, you press Ctrl + Alt + FX - where FX is replaced with the Function key that corresponds to the number. For example, to access tty2 you would press Ctrl + Alt + F2.

You have to login to this, but some things need the tty to work.

Getting back to the GUI will depend on your Ubuntu version, but trying each tty in turn should work until you find it. If you can’t find it on any tyy 1-8, you may need to restart - but ask the person who is helping you by commenting below the answer!

What about GUI (Graphical User Interface) programs?

The terminal can also launch GUI programs. This can have the advantage of viewing the output the program gives - for example running software-centre looks like this:

You can also provide extra "arguments" - also known as "parameters" and "options" to the command.

For example, if I run google-chrome --app=http://youtube.com it opens a window without the URL bar or the tabs:

Bear in mind, if you close the terminal, you also lose the Google Chrome window...

One final method

This is only good for GUI programs, like Chrome, Firefox and Gedit. However, it also means you don't have a terminal lying around - which you could accidentally close. It's no good for sudo apt-get install program or similar, because you can't type your password after running it.

Press Alt + F2, and a "Run Dialouge" appears - a bit like the run window on Windows:

Ubuntu:                                                                                    Windows:

You can type any command into here, and it will run it!

Tim
  • 32,861
  • 27
  • 118
  • 178
  • 4
    FN is easy to confuse with laptop key Fn. I'd rather provide a concrete example instead, like F2 for tty2. Also, I may have overseen, but I've not found where you mention how to go back to X from the VT. If a user is really a newbie as you suppose in the question, then they must learn how to go back to "warm and safe" GUI. – Ruslan Feb 28 '16 at 19:07
  • 4
    @Tim I would suggest you to put a warning on your What is a tty? paragraph explaining how to come back to the GUI before explaining how to access ttx: some people (e.g. younger I) do not read explanations until the end of the paragraph but rather directly try commands... and stays blocked on command-line mode! – ebosi Feb 29 '16 at 08:37
  • This answer misses the point of the question. Too lengthy. – mbiber Mar 05 '16 at 02:00
  • 1
    @mbiber This answered my question pretty well. If you have any specific suggestions as to where it could be cut down, please say. I think I managed to condense a lot of information pretty well - but if it could be better I'm happy to listen :) – Tim Mar 05 '16 at 12:36
  • @Tim If someone wanted to "read up" then first result in Google (first 10 results for that matter) are hefty peaces of text. For example https://help.ubuntu.com/community/UsingTheTerminal. I am very satisfied with this answer, that's why I gave it +1, but I expect complete noobs would require an answer for this question to be short and to the point. Too much information is often of-putting to non technical users! I'll add an answer I would have expected when I was a complete noob with Ubuntu. – mbiber Mar 05 '16 at 16:57
  • @mbiber that seems sensible, I doubt I can cut down the answer without taking away quite a lot for it to still make sense. Another answer could be much better. – Tim Mar 05 '16 at 16:58
  • In recent Ubuntu versions, GUI no more runs on tty7, but on lower numbered ttys. Usually the login screen is on tty1, and the actual GNOME session on tty2. Ttys from 3 up are free to use. – raj Sep 14 '21 at 12:18
  • @raj yes, thanks for the reminder. Updated. – Tim Sep 14 '21 at 12:21
  • @Tim? Thank you very much. For the last point, the one that uses Alt + F2, most of commands are unknown to it. I keep getting Command not found. For example none of my aliases are there. – aghArdeshir May 05 '22 at 06:58
3

Types of commands vary somewhat. They can be single line, like

 echo 'HelloAskubuntu' # Print 'HelloAskubuntu' 

single line with redirection to other commands

echo 'Hello World' | wc # count number of lines, words, and chars in 'Hello World'

multiline with redirection

$> bash << END                                                                 
> echo one
> echo two
> echo three
> END
one
two
three

Or multiline in form of scripts (which in terms of Windows language is batch files). Scripts are just files with lists of commands in them, that are executed sequentially. These are effectively custom build programs/software, analogous to building your own electronics out of existing spare parts.

Regardless of the type of command, you will need access to a command line, commonly known as Terminal. There is either TTY terminals on Ctrl+Alt+F[1-6] keyboard shortcuts (exit those with Alt+F7), or you can use GUI app by pressing Ctrl + Alt + T.

Terminals typically run software known as shell. The shell interprets the commands and executes them. Some shells are compatible with each other for the most part like bash, ksh, mksh, zsh , while tcsh and csh are quite different. They all have different features, and often are used for different tasks.

Either a script or a command have to be executed in shell. Some commands are physically present executable files on disks, like /bin/ls. Other commands are aliases or functions that are lists of existing commands. They exist only in the settings of the shell. For example, for bash it would be .bashrc settings file. Real executables can be run with the name or full path, like ls or /bin/ls respectivelly.

Scripts can be run with full path, or if they are stored somewhere that your shell knows about ( and it knows about only things in the PATH variable, which you can read with echo $PATH ). For example, I can run my script by going to my bin directory and running it with ./ operator, like so

 cd $HOME/bin; ./helloworld.sh

Or give terminal full path

 /home/Serg/bin/helloworld.sh
Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
3

To run a command you need to open a terminal window. You can find the terminal among your applications like you would any other. Copy and paste the grayed text into the terminal window after opening it and then press Enter key to execute. You will probably see some text output during and after execution. This is feedback from the command you just ran.

Command is an instruction you give to a computer - you tell it to do something. You are already familiar with some commands, you use them while using your computer all the time. For example clicking on buttons in your GUI (graphical user interface), like web browsers, is the same thing - telling your computer to do something.

If you require more information you can find it at What is a terminal and how do I open and use it? and https://help.ubuntu.com/community/UsingTheTerminal

mbiber
  • 840
  • 5
  • 12