Quantcast
Channel: Debian User Forums
Viewing all articles
Browse latest Browse all 3567

Graphical Environments & Desktops • GNOME TERMINAL output save to file

$
0
0
is there a way to have bash write all content to a file as well as the screen while the bash feature is running?
What is this "the bash feature"? Do you mean bash, as in the Bourne Again SHell? If so that's your primary command shell and a turing-complete programming environment unto itself, not a "feature" of or dependent in any way on some terminal emulator like konsole or GNOME whatever.

From your image, you appear to be trying to capture the output of 'ls'... I don't know why you think you need a GUI terminal emulator, copy-paste, menus or a mouse to get output from CLI programs into a file (or anywhere else for that matter), redirecting text streams (and the composability that enables) is fundamental to all unix shells.

Getting the STDOUT of 'ls' into a file for example is as simple as

Code:

ls > output.txt
Redirecting to a file while also echoing to the terminal is easiest done by piping to 'tee', e.g.

Code:

ls | tee output.txt
Want only the first 5 lines?

Code:

ls | head -n5 | tee output.txt
Only lines containing the string "foo"?

Code:

ls | grep "foo" | tee output.txt
As above, but replacing "foo" with "bar"?

Code:

ls | grep "foo" | sed 's/foo/bar/g' | tee output.txt
Send output to a file, and also upload it to a pastebin for sharing? (install pastebinit first OFC)

Code:

ls | tee output.txt | pastebinit
Echo to the screen and upload to a pastebin (using process substitution in place of tee's file argument, because we can)?

Code:

ls | tee >(pastebinit)
Send output to a file on a different machine?

Code:

ls | ssh user@remotehost -T "cat > /tmp/output.txt"
Bonus example, send everything to a file:

Code:

exec > >(tee -a "output.txt") 2>&1
Bonus question for the reader, how does this^ work, and what pitfalls does it have?


These concepts (and a lot more) are in

Code:

man bash
on the end of a websearch regarding [insert favoured shell here] 'pipes' 'redirection' and 'file descriptors', or in pretty much any generic unix "intro to CLI" type text.
Similarly, manuals are available for the other commands involved, e.g. 'man tee' or 'man grep'.

Almost all CLI programs can be connected with pipes and redirection this way to build more complex functionality. Welcome to unix, where we can do most things 10x faster and more efficiently without a rodent in sight. :P


If what you really want is a full record of everything you do in a shell session (i.e. both your input and command output), try ruwolf's suggestion and use script. It's a good habit to get into anyway, particularly when upgrading a system or doing anything you may want to replay or reverse.
As for scrollback/screen buffers, those aren't part of the shell, they're a feature of the terminal and its graphics output stack... And not particularly reliable, accessible or portable at the best of times.

Statistics: Posted by steve_v — 2024-06-27 07:32 — Replies 2 — Views 70



Viewing all articles
Browse latest Browse all 3567

Trending Articles