Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When in the 'lower level terminal' (not sure how this is called), the one opened by pressing Ctrl + Alt + F1, how can I copy the content of the command I used there, since there is no mouse and I can not move my cursor out for the command line?

share|improve this question
    
@Beniamin paste it here so i can get some support : ) – silkAdmin Oct 25 '11 at 12:27
    
Do you want to copy the command, or its output? – Kris Harper Oct 25 '11 at 12:28
    
The output, in this instance i am trying to run X -config and i am getting a couple of errors – silkAdmin Oct 25 '11 at 12:29
    
you could just redirect the output to a file with > or &> – RobotHumans Oct 25 '11 at 12:31
    
@aking1012 thanks, that was it, can you please post that as an answers so i can vote it ? – silkAdmin Oct 25 '11 at 12:41
up vote 9 down vote accepted

You could just redirect the output to a file with > or &>... I was asked to move it from comments to answers, so here it is.

By the way, a couple notes:

Putting > in between a command and a filename will overwrite the current contents of the file with standard out.

Putting >> appends to a file.

Putting a & before the redirector will also catch standard error (in Bash > 4).

share|improve this answer

To send the output of a command to a file, you can use redirection.

For example, you could send your dmesg command to a file with

dmesg > ~/Documents/output.txt

Then the output will write to that file instead of the screen. When you come back to the GUI, you can find the file in your Documents folder and access the contents.

share|improve this answer
    
Thank you root45. I voted up but i ll kuddo Aking as he was the first to suggest this answer in the comments. – silkAdmin Oct 25 '11 at 13:35

The last used command history is usually stored in your history file (.bash_history). You can open this file with the editor-of-your-choice and copy contents.

As for manipulating the command-line of the console window, you can rely on the command line editing functions of the bash shell. They are discussed in the READLINE section of the Bash manual page (man bash). But they will not allow you to paste the copied part into an editor.

Another way to work around this is to use the screen utility. There are several introductory tutorials on the web. Screen has a copy and paste function:

  1. Press CTRL+A then ESC to get into copy mode.
  2. By moving around the cursor keys you can position to the first character of your selected region, then press Space. You now have set the first mark.
  3. Move the cursor to the end of the text you'd like to select and press Space. You have now copied the selected region into the buffer.
  4. To paste the buffer press CTRL+A and then ].
share|improve this answer
    
Thanks a lot, very instructive answer! – silkAdmin Oct 25 '11 at 13:36

There's several ways to go around this. The simplest is with xclip and I've saved for the last.

hbdgaf's answer presents one solution - redirection. Here's two variations on the theme - redirection with named pipe and byobu. First requires no additional software installation, while byobu has to be installed.

Option 1: named pipe

When you run a command and use pipeline | to redirect its output to another command, that is called an anonymous pipe - it exists for a set moment of time. Named pipe , on the other hand, is a special sort of file, where data goes in, and then goes out.

Here's the steps to make it work:

  1. create the named pipe with mkfifo /tmp/myPipe

  2. run the command you want and redirect the output to /tmp/myPipe . For instance, sudo apt-get install byobu > /tmp/myPipe. Notice, that the command will wait until you release the output from myPipe. Use & to make it wait in the background, and let you continue using the terminal, like so sudo apt-get install byobu > /tmp/myPipe & . Notice also that stdout will go into the pipe, but stderr will go to the screen. If you want both stderr and stdout in the pipe , use &> instead of >

    1. come back to the GUI, and release the output from that pipe with cat /tmp/myPipe. Now you can copy the output with mouse and keyboard.

Option 2: shared session in byobu

As for byobu, the basic idea is that it creates a shared session. You launch byobu in TTY and GUI terminal, and you see the same output there and there. I'd suggest launching TTY first, because that way it's cleaner. So let's say I launch byobu in TTY2 run df;free , go to GUI, launch gnome-terminal then call byobu, and voila - I see the same output that I saw in TTY !

Option 3: xclip, the easiest way

Suppose I wanted to copy output of df command. All I'd have to do with xclip is this:

$ export DISPLAY=:0 ; df | xclip -sel clip

And it will be already in your GUI clipboard !

There's a small catch however - xclip is not a native app, and has to be installed via sudo apt-get install xclip

Of course typing all that in can get repetitive, so you can create a .bashrc function to do the job. Simply copy and paste the function bellow into your .bashrc file, save the file, and now each time you log in , you have a simple command to copy output to clipboard

function toClipboard
{
  export DISPLAY=:0
  "$@" | xclip -sel clip

}

And you'd run it as $ toClipboard df -h , so notice that you can run command with parameters.

Option 4: script

There exists a command called script which has exactly that purpose - record what you see on command line into a file. You can either run it first, and then type output in it's shell, and then exit; or you can call screen with --command flag, like so script --command "df -h"

However, there's a drawback - the output file may contain special characters. Reffer here for info on how to clean the output up

Option 5: /dev/vcsX

As shown by Giles from U&L site, one can actually dump contents of tty onto another terminal's stdout. Suppose we want to dump tty2. The command for that is

 cat /dev/vcs1

Draw back of this method is that if your terminal is set to certain level of scrollback lines, you can get only partial output.

share|improve this answer
sudo apt-get install gpm

GPM is just what you need & very useful. While in the console (ctrl+alt+f1), you can copy by dragging the left-hand mouse button, and then paste by clicking the right-hand mouse button.

share|improve this answer
    
Why? what does it do? You might want to edit your answer and explain for the benefit of the OP and other users – stephenmyall Mar 12 '13 at 11:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.