Ubuntu – How to run a command and copy its output

command line

I've been asked to run a command and copy and paste the output but I have no idea where to start.

What do I do?

Best Answer

Hopefully you will have access to a graphical desktop. That's what the first part of this answer deals with. If you do not, skip the first three steps and read the bottom of the answer.

Step 1: Load a terminal...

The first step is opening a terminal. There are a multitude of different terminal options out there but most Ubuntu users will have gnome-terminal, KDE users tend to have konsole, and XFCE users should have xfce4-terminal.

Once you have identified which command you want to run, it's just a case of running it.

  • Press Ctrl+Alt+T.
  • Unity and Gnome Shell users can search for Terminal.
  • KDE users can find Konsole under Applications → System.
  • All desktops can press Alt+F2 and type in the terminal binary name (as above) manually.

Step 2: Running your command

This is the simple bit. You should be looking at something like the following:

enter image description here

You can either type the command, or you can copy it (select the text, right click, "Copy", then right click the terminal, "Paste"). Copy & Paste is recommended for long commands. We want to avoid errors in transcription!

When you have typed or pasted the command into this window, press Return. Output should now appear on the screen as below:

enter image description here

Step 3: Copying the content

Copying is similar to most other applications apart from most consoles only know how to copy "blocks". To copy, simply use your mouse to drag around the output until it is highlighted, like so:

enter image description here

Right click on the terminal (to go to the Edit menu) and select "Copy". Then you can go back to your browser or editor and paste. We're done.

Step 3.5: Alternative for copying:

Seeing as you're getting the hang of the command line now, you can also use a command called xclip to directly insert things into X's clipboard. This will only work if a GUI is running.

lspci -nnk | grep VGA -A1 | xclip -selection clip

Note: you can omit -selection clip but the data will be in your middle-click clipboard - yes, there are two clipboards... A story for another day, perhaps :)


Addendum: Extracting command line data without Copy & Paste

If you need to get the output from a computer that, for example, the display settings are so broken you can't get into a usable desktop, we have to find another method for extracting the output. You could do it by hand but often (in case of logs) this is impractical.

At the very least we need to get into a terminal which you can do by pressing: Control+Alt+F1.

You may need to log in.

From here we can run our command but the output will only show on the display. Now we have two options:

I have an internet connection

If you have access to the internet, you can simply "pipe" the output into a command called pastebinit. This uploads the content to http://paste.ubuntu.com and returns a short, easy to transcribe URL that you can access from another computer or just give to people.

pastebinit is not installed by default so the first thing we run is:

sudo apt-get install pastebinit

Then we run our command, followed by | pastebinit:

lspci -nnk | grep VGA -A1 | pastebinit

And you'll see something like the following (though obviously not in a window):

enter image description here

Write down the URL and you can share that with whomever you like.

I don't have an internet connection

This is more problematic but not unsurpassable. We will need some form of external storage. A USB stick is a popular example. Plug it in. In a non-graphical setting, USB drives are not automatically mounted. As I said, problematic.

Run lsblk and you should see something like this:

NAME              MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
sda                 8:16   0 111,8G  0 disk  
├─sda1              8:17   0   100M  0 part  /boot
├─sda2              8:18   0     1G  0 part  /
├─sda3              8:19   0    20G  0 part  /usr
├─sda4              8:20   0     1K  0 part  
├─sda5              8:21   0  58,7G  0 part  
└─sda6              8:22   0    32G  0 part  [SWAP]
sdb                 8:96   1  14,5G  0 disk  
└─sdb1              8:97   1  14,4G  0 part
sr0                11:0    1   4,4G  0 rom   
sr1                11:1    1  1024M  0 rom   

In my case, the new disk I've plugged in is that second one (sdb) and it has one partition (sdb1). We need to mount this so we can write to it. Simply:

sudo mount -o umask=0000 /dev/sdb1 /mnt

Then we can funnel our commands into new files on the external disk:

lspci -nnk | grep VGA -A1 > /mnt/lspci
cat /var/log/Xorg.0.log >/mnt/xorg.log

That will create two new files on the external drive. You can also copy things to it with cp. When you're done you want to unmount the drive:

cd /  # just in case you cd'd into /mnt
sudo umount /mnt

Then you can unplug your drive and take it to another computer and upload the information as requested.