Output to Linux file. How to save console output to a file

If the output to the (graphical) console is not very large, you can simply select a piece with the mouse and paste it into the message by clicking the middle button. Otherwise, you can use redirecting output to a file through a "funnel", like this:

Some_command parameters > logfile.txt

To see the result of execution on the screen, and at the same time write to a file, you can use the tee command:

Some_command parameters | tee -a logfile.txt

The setterm -dump command creates a snapshot of the current virtual console buffer as a plain text file with the default name screen.dump. You can use the number of the console for which you want to dump as its argument. And adding the option -file filename will redirect this dump to a file with the specified name. The -append option will append the new dump to an existing file - the "default" screen.dump or one named with the -file option.

Those. after using command like

Setterm -dump -file /root/screenlog

respectively in the file /root/screenlog will be the content of one console page.

Found another solution for copy/pasting text in a text console without a mouse. You can also copy text from the scroll buffer (i.e. everything on the screen and above the screen). For a better understanding, read about the console-based window manager screen . It may also be useful to increase the size of the scroll buffer.

1) Run screen

2) Press Enter. All. We are in the zero console window.

3) We execute the necessary commands, the output of which must be copied.

4) Ctrl+A, Ctrl+[ - we are in copy mode. We put the cursor at the beginning of the selection, press the spacebar, then we put the cursor at the end of the selection, press the spacebar. Text copied to clipboard.

5) Ctrl+A, c - we have created a new 1st window.

6) Ctrl+A, 1 - we switched to the 1st window.

7) Open any (?) text editor (I tried in mc), and press Ctrl + A, Ctrl +] - the text is inserted. We save.

8) Ctrl+A, Ctrl+0 - go back to the zero window.

How to increase scrollback buffer?

The first solution is to increase the default (default) buffer size in the kernel sources and recompile it. Let me assume that you are as reluctant to do this as I am and look for a more flexible medium.

And there is such a tool, but it is called framebuffer console , fbcon for short. This device has a documentation file fbcon.txt; if you installed the documentation for the kernel, then you have it. Look for it somewhere around /usr/share branches (I can't give the exact path due to differences in distributions).

At this point, I apologize: we have to digress a little and talk a little about the video buffer ( framebuffer ).

The video buffer is a buffer between the display and the video adapter. Its beauty is that it can be manipulated: it allows tricks that would not work if the adapter was connected directly to the display.

One such trick is related to the scroll buffer; turns out you can "ask" the video buffer to allocate more memory to the scroll buffer. This is achieved through the boot parameters of the kernel. First you require framebuffer(video buffer); Then you request a larger scroll buffer.

The following example is for GRUB but can easily be adapted to LILO . In the GRUB configuration file - menu.lst- find the line corresponding to the kernel, and then: Remove the vga=xxx option, if present. Add the option video=vesabf or whatever matches your hardware. Add the option fbcon=scrollback:128 . After this procedure, the kernel parameter line should look something like this:

Kernel /vmlinuz root=/dev/sdb5 video=radeonfb fbcon=scrollback:128

The question is, why remove the vga=xxx option? Due to possible conflicts with the video option. On my ATI adapter, I cannot change the scroll buffer if vga=xxx is listed. Perhaps in your case this is not the case. If the above options work - good; but what if you want to increase the number of lines, or set a smaller font on the screen? You've always done this with the vga=xxx option, and that's what disappeared. Don't worry - the same can be achieved by changing the fbcon parameters as described in the file fbcon.txt(but not described in this article).

With the fbcon=scrollback:128 option, my scroll buffer increased to 17 screens (35 times Shift+PgUp half a screen). By the way, 128 is a kilobyte. The author of the article claims that more can not be established. I didn't try.

You can use script .

Script filename.log

when all required commands are executed -

Everything is written to filename.log

FreeBSD has a wonderful watch utility that allows you to monitor terminals, but as it turned out, in Linux it performs completely different functions =\ It's worth googling on this topic, there is something ...

When you work in the terminal, you naturally see all the output of commands in real time right in the terminal window. But there are times when the output needs to be saved in order to work with it later (analyze it, compare it, etc.). So, working in Bash, you have the ability to redirect the displayed information from the terminal window to a text file. Let's see how it's done.

Option 1: only redirect terminal output to a file

In this case, the entire result of the work of any command will be written to a text file, without displaying it on the screen. That is, we will literally redirect information from the screen to a file. To do this, you need to use the operators > And >> and the path to the file to write to, at the end of the command being executed.

Operator > saves the result of the command to the specified file and, if it already contains any information, overwrites it.

Operator >> will redirect the output of the command to a file, and if it also contains information, the new data will be added to the end of the file.

Let's take a look at the example command ls , which displays a list of files and folders in the specified directory. Let's write the result of her work to a text file. We need to write a command, put an operator and specify the path to the file:

Ls > /home/ruslan/example

Now let's see if everything worked. To do this, you can use any text editor that you have. You can also do this directly in the terminal using the cat command:

Cat /home/ruslan/example

Everything is working.

Remember, that " > " will overwrite all the data that was previously in the file, so if you need to add something to the file, use the operator " >> «

Let's say that after we've redirected the output of the command ls to file " example » we decided to find out the version of the system kernel and also save the output to the same file. To find out the kernel version, use the command uname and parameter -a , then we tell Bash how and where to save the result of its execution:

Uname -a >> /home/ruslan/example

Let's check the result again:

Cat /home/ruslan/example

As you can see, we have preserved the results of the work and ls , And uname .

Option 2: redirecting the output to a file and displaying it on the screen

Not everyone and not always convenient to use operators > And >> , since it's still better when the flurry of commands can be observed in real time in the terminal window. In this case, we can use the command tee , which will display the execution of commands on the screen, and save it to a file. Its syntax is this:

Team | tee /path/to/file

This option is similar to the operator > from the previous paragraph, that is, when writing to a file, all old data will be deleted. If you need to append to a file, you need to add a parameter to the construct -a :

Team | tee -a /path/to/file

One of the most interesting and useful topics for system administrators and new users who are just starting to understand working with the terminal is Linux I/O redirection. This feature of the terminal allows you to redirect command output to a file, or the contents of a file to command input, concatenate commands together, and form command pipelines.

In this article, we will look at how Linux I/O redirection works, what operators are used for this, and where all this can be applied.

All the commands we execute return three kinds of data to us:

  • The result of executing the command, usually the text data requested by the user;
  • Error messages - inform about the process of command execution and unforeseen circumstances that have arisen;
  • The return code is a number that allows you to evaluate whether the program worked correctly.

In Linux, all substances are considered files, including linux I/O streams - files. Each distribution has three main stream files that programs can use, they are defined by the shell and identified by a file descriptor number:

  • STDIN or 0- this file is associated with the keyboard and most commands get their data to work from here;
  • STDOUT or 1- this is the standard output, here the program sends all the results of its work. It is associated with the screen, or to be precise, with the terminal in which the program is executed;
  • STDERR or 2- all error messages are output to this file.

I/O redirection allows you to replace one of these files with your own. For example, you can make the program read data from a file on the file system instead of the keyboard, you can also write errors to a file instead of on the screen, and so on. All this is done using symbols "<" And ">" .

Redirect output to file

Everything is very simple. You can redirect output to a file using the > symbol. For example, let's store the output of the top command:

top -bn 5 > top.log

The -b option causes the program to run in non-interactive batch mode, and the n option repeats the operation five times to get information about all processes. Now let's see what happens with cat:

Symbol ">" overwrites information from the file if there is already something there. To add data to the end, use ">>" . For example, redirect output to a linux file for top:

top -bn 5 >> top.log

The default for redirection is to use the standard output file descriptor. But you can specify it explicitly. This command will give the same result:

top -bn 5 1>top.log

Redirect errors to a file

To redirect error output to a file, you need to explicitly specify the file descriptor you want to redirect. For errors, this is number 2. For example, when trying to access the root directory, ls will throw an error:

You can redirect standard error to a file like this:

ls -l /root/ 2> ls-error.log
$ cat ls-error.log

To append data to the end of a file, use the same symbol:

ls -l /root/ 2>>ls-error.log

Redirect standard output and errors to a file

You can also redirect all output, errors, and standard output to a single file. There are two ways to do this. The first one, the older one, is to pass both descriptors:

ls -l /root/ >ls-error.log 2>&1

First, the output of the ls command will be sent to the ls-error.log file using the first redirect character. Further all errors will be sent to the same file. The second method is easier:

ls -l /root/ &> ls-error.log

You can also use append instead of overwrite:

ls -l /root/ &>> ls-error.log

standard input from file

Most programs, except services, receive data for their work through standard input. By default, standard input expects input from the keyboard. But you can force the program to read data from a file with the statement "<" :

cat

You can also immediately redirect the output to a file too. For example, let's reorder the list:

sort sort output

Thus, we are redirecting the input output of linux in one command.

Use of tunnels

You can work not only with files, but also redirect the output of one command as input to another. This is very useful for complex operations. For example, let's print five recently modified files:

ls -lt | head -n 5

With the xargs utility, you can combine commands so that standard input is passed as parameters. For example, let's copy one file to several folders:

echo test/tmp/ | xargs -n 1 cp -v testfile.sh

Here, the -n 1 option specifies that only one parameter should be substituted for one command, and the -v option in cp allows you to print detailed information about the movements. Another command that is useful in such cases is tee. It reads data from standard input and writes to standard output or files. For example:

echo "Tee test" | tee file1

In combination with other commands, all this can be used to create complex instructions from several commands.

conclusions

In this article, we covered the basics of Linux I/O stream redirection. Now you know how to redirect output to a linux file or output from a file. It is very simple and convenient. If you have any questions, ask in the comments!

While normally, as mentioned, program I/O is bound to standard streams, there are special facilities in the shell for redirecting I/O.

5.5.1 Operators >,< и >>

The symbols " are used to indicate a redirect. > ", "< " And " >> ". The most common use is to redirect the output of a command to a file. Here is a relevant example:

$ ls -l > /home/jim/dir.txt

This command will save a list of files and subdirectories of the directory that was current at the time the command was executed in the file /home/jim/dir.txt ls; at the same time, if the specified file did not exist, it will be created; if it existed, it will be overwritten; if you want the output of the command to be appended to the end of an existing file, then instead of the symbol > use >> . In this case, the presence of spaces before or after characters > or >> is immaterial and is for the convenience of the user only.

You can direct the output not only to a file, but also to the input of another command or to a device (such as a printer). For example, to count the number of words in the /home/jim/report.txt file, you can use the following command:

$ cat /home/jim/report.txt > wc -w

and to print the file - the command:

$ cat /home/jim/report.txt > lpr

As you can see, the operator > serves to redirect the output stream. In relation to the input stream, a similar function is performed by the operator < . The above example command to count the number of words in a particular file can be rewritten as follows (note the absence of the command cat):

$ wc -w< /home/jim/report.txt

This type of redirection is often used in various scripts for commands that normally accept (or expect input from) the keyboard. In a script that automates some routine operations, you can give the command the necessary information from a file that contains what you need to enter to execute this command.

Because the symbols < , > And >> act on standard streams, they can be used not only in the usual way, as is usually done, but also in a slightly different way. So the following commands are equivalent:

$ cat > file

$cat>file

$ >file cat

$ > file cat

However, by itself (without any command for which standard streams are defined) the redirection character cannot be used, so you cannot, for example, by typing on the command line

$ file1 > file2

get a copy of some file. But this does not diminish the value of this mechanism, because standard streams are defined for any command. In this case, you can redirect not only standard input and output, but also other streams. To do this, you must specify the number of the stream to be redirected before the redirect symbol. The standard input stdin is number 0, the standard output stdout is number 1, the standard error message stream stderr is number 2. That is, the full format of the redirect command is (recall that the spaces next to > are not required):

command N > M

Where N And M— numbers of standard streams (0,1,2) or file names. The use of symbols in some cases < , > And >> without specifying the channel number or file name is possible only because instead of the missing number, 1 is substituted by default, i.e. standard output. Yes, the operator > without a number is interpreted as 1 > .

In addition to simple redirection of standard streams, there is also the possibility not just to redirect the stream to one or another channel, but to make a copy of the contents of the standard stream. There is a special character for this. & , which is placed before the number of the channel to which the stream is redirected:

command N > &M

This command means that the output of the channel with the number N is sent both to the standard output and is duplicated in the pipe with the number M. For example, in order for error messages to be duplicated on standard output, you need to issue the command 2>&1, while 1>&2 duplicates stdout to stderr. This feature is especially useful when redirecting output to a file, since we then both see the messages on the screen and save them to a file.

5.5.2 Operator |

A special variant of output redirection is the organization of a program channel (sometimes called a pipeline or pipeline). To do this, two or more commands, such that the output of the previous one serves as the input for the next, are connected (or separated, if you like it better) with a pipe character - "|". In this case, the standard output stream of the command located to the left of the symbol | , is directed to the standard input of the program located to the right of the character | . For example:

$ cat myfile | grep Linux | wc -l

This line means that the output of the command cat, i.e. the text from the file myfile, will be directed to the input of the command grep, which will select only lines containing the word "Linux". command output grep will in turn be routed to the input of the command wc -l, which will count the number of such rows.

Program pipes are used to combine several small programs, each of which performs only certain transformations on its input stream, to create a generalized command, which will result in some more complex transformation.

It should be noted that the shell simultaneously invokes all the commands included in the pipeline, running a separate instance of the shell for each of the commands, so that as soon as the first program starts to issue something to its output stream, the next command starts processing it. In the same way, each subsequent command performs its operation, waiting for data from the previous command and giving its results as input to the next one. If you want one command to complete completely before the next one starts executing, you can use it on the same line as a pipe symbol | , and a semicolon ; . Before each semicolon, the shell will stop and wait until all previous commands included in the pipeline have finished executing.

The exit status (boolean returned when the program terminates) from the pipe is the same as the exit status returned by the last command in the pipeline. Before the first command of the pipeline, you can put the symbol "!", then the exit status from the pipeline will be a logical negation of the exit status from the last command. The shell waits for all pipeline commands to complete before setting the return value.

5.5.3 Filters

The last of the above examples (with the command grep) can be used to illustrate another important concept, namely the filter program. Filters are commands (or programs) that accept an input data stream, perform some transformations on it, and output the result to standard output (from where it can be redirected somewhere else at the user's request). Filter commands include the commands already mentioned above. cat, more, less, wc, cmp, diff, as well as the following commands.

Table 5.1. Filter Commands

Team

Short description

grep, fgrep, egrep

Search the input file or standard input for lines containing the specified pattern and write them to standard output

Replaces in the input stream all occurrences of the characters listed in the specified list with the corresponding characters from the second specified list

comm

Compares two files line by line and prints 3 columns to stdout: one contains lines that occur only in 1 file, the second contains lines that occur only in 2nd file: and the third contains lines that appear in both files

Formats a text file or the contents of standard input for printing.

sed

Line editor used to perform some transformations on the input data stream (taken from a file or standard input)

The special filter is the command tee, which "doubles" the input stream, on the one hand directing it to standard output, and on the other - to a file (whose name you must specify). It is easy to see that in its action the command tee similar to the redirect operator 1>&file.

The possibilities of filters can be significantly expanded through the use of regular expressions, which allow organizing, for example, a search using various, often very complex, patterns.

A lot could be said about redirection and filters. But this material is found in most UNIX and Linux books, such as Petersen [A1.4] and Kelly-Bootle [A1.8] . Therefore, we confine ourselves to what has been said, and proceed to consider the so-called environment or environment created by the shell.

V. Kostromin (kos at rus-linux dot net) - 5.5. I/O redirection, channels and filters

I/O system in LINUX.

In the I/O system, all external devices are treated as files on which normal file operations are allowed. Of course, there are also device drivers, but the interface with them is designed for the user as a call to a special file. Special files are a means of unifying the I/O system.

Each connected device (terminal, disks, printer, etc.) has at least one special file associated with it. Most of these special files are stored in the /dev directory:
$ cd /dev
$ ls -l
onsole system control panel
dsk chunks on disk
fd0 floppy disk 1
mem memory
lp printer
lp0 parallel port 0
. . .
root portion on disk for the root filesystem
swap swap portion
syscon console alternative name
systty is another name for the system console
term directory for terminals
ttyS0 serial port 0 (COM1)
. . .

When a program writes to such a special file, the OS system intercepts them and sends them to a device, such as a printer). When reading data from this type of file, it is actually being received from a device such as a disk. The program should not take into account the peculiarities of the I / O device. For this purpose, special files (drivers) serve, which act as an interface between the components of the OS kernel and general-purpose application programs.

The system only detects the difference between a regular file and a special file after it has parsed the corresponding inode referenced by the directory entry.
The inode of the special file contains information about the device class, type, and number. The device class defines both character-by-character and block-by-block devices. An example of a device with a character-by-character exchange is a keyboard. Special files that provide communication with devices of this type are called byte-oriented. Block devices are characterized by the exchange of large blocks of information, which speeds up the exchange and makes it more efficient. All disk devices support block exchange, and the special files serving them are called block-oriented. Special files do not contain any character information, so their length is not indicated in the directory listing.

The type and number of the device are also the main characteristics of special files (the main and minor numbers of the corresponding device are placed in the length field). The first of them defines the device type, the second - identifies it among similar devices. The OS can simultaneously serve several dozens, and even hundreds of terminals. Each of them must have its own special file, so the presence of a major and minor number allows you to establish the required correspondence between the device and such a file.

You can create multiple file systems on a single disk. Some systems use one file system per disk, while others use several. A new file system can be created using the mkfs (make file system) command. For example, the expression # /sbin/mkfs /dev/dsk/fl1 512 means: create on floppy disk b: 512 blocks in size.

Optionally, you can set the size of the file system in blocks and the number of i-nodes (ie, the maximum number of files that can be stored in the file system). By default, the number of i-nodes is equal to the number of blocks divided by four. The maximum number of i-nodes in a single file system is 65,000. If for some reason you need more than 65,000 i-nodes on a disk, you must create two or more file systems on that disk.

Any file system can be attached (mounted) to a common directory tree, at any point in it. For example, the directory / is the root directory of the system, in addition, it is the base of the file system, which is always mounted. The /usr1 directory is located in the / directory, but in this case it is a separate file system from the root file system, since all the files in it are on a separate part of the disk, or even on a separate disk. The /usr1 file system is a mounted file system - rooted at the point where the /usr1 directory exists in the overall hierarchy (Figures 1 and 2).

Rice. 1. File system before
mounting /dev/dsk/os1

Rice. 2. File system after
mount /dev/dsk/os1 as /usr/

The /sbin/mount command is used to mount a file system. This command allows the given file system to be placed anywhere in the existing directory structure:
#/sbin/mount/dev/dsk/osl/usr1 mounts /dev/dsk/osl on /usr1
#/sbin/mount/dev/dsk/flt/a mounts /dev/dsk/flt on /a

If you want to mount a filesystem on disks that must be write-protected to make the system read-only, you must add the -r option to the /sbin/mount command.
The directory to which the mounted file system is attached must be currently empty, as its contents will not be accessible while the file system is being mounted.

To get information about file systems that are mounted, for example, on a LINUX system, you can use the /sbin/mount command with no arguments (Figure 3).

Rice. 3.

This command prints the directory on which the file system was mounted (eg usrl), the /dev device it resides on, and the hour and date it was mounted. The /sbin/umount command is used to unmount a filesystem, which is the opposite of the mount command. It frees the filesystem and sort of takes it out of its entire directory structure so that all its own files and directories are inaccessible:
# /sbin/umount /b
# /sbin/umount /dev/dsk/0s2

The root file system cannot be unmounted. Also, the umount command will fail if someone is using a file from the file system being unmounted (this could even be as simple as the user being in one of the directories of the file system being unmounted).

In the mount and umount commands, the user uses the physical disk device abbreviation.
In LINUX, disk drives have peculiar designations. In LINUX, the user is never faced with the problem of accurately specifying the physical device on which the information resides. In LINUX, an arbitrary number of external devices can be very large, so the user only has to deal with the name of the directory where the files he needs are located. All filesystems are mounted once, usually at system boot. File systems can also be mounted on some directories from remote computers.

For physical devices, LINUX has the dsk and rdsk directories, which contain files corresponding to disk devices. Usually the file names in these directories are the same and the only difference between them is that the rdsk directory contains disk devices with special access (raw), which some system devices use for faster disk access. One typical dsk directory contains the following devices:
$ 1s /dev/dsk
0s0 1s0 c0t0d0s0 c0tld0s0 f0 f05q f13dt fld8d
0sl 1sl c0t0d0sl c0tld0sl f03d f05qt f13h fld8dt
0s2 1s2 c0t0d0s2 c0tld0s2 f03dt f0d8d f13ht fld8t
. . .
$

In a LINUX system, disk devices are logically divided into partitions, similar to the partitions defined in MasterBoot MS DOS's Partition Table. Files 0s1, 0s2, 0s3, etc., correspond to sections one, two, three, etc. of disk number 0. Files 1s0, 1sl, 1s2, etc. correspond to sections one, two, three, etc. disk number 1. If the system has more disks, the partitions will be numbered ns0, nsl, etc. for each disk number n.

Systems with many disk drives use the following numbering system:
with controller d disk s section

where controller - disk controller number; disk - disk number; section - disk section number.
So, 0s0 is usually equivalent to c0t0d0s0, and 0sl is c0t0d0sl, and three-character partition names are just shorthand for disk controller number 0.

Files whose names start with f define different kinds of floppy disks. The rmt directory contains files on tape devices:
$ 1s /dev/rmt
c0s0 cls0 c3s0 ntape ntapel tape tapel

The files c0s0, cls0, c2s0, and c3s0 define four cassette tape storage devices. The files tape and tapel define two-reel magnetic storage devices. Files whose names start with n refer to the same devices, only the tape is not rewound after use, while using other files causes the tape to be rewound when the program using it ends.

On some systems, these files have different names, but they are always found in /dev, and the dictionary that usually comes with the system contains a detailed description of the devices and their associated files.

The extX file system uses data buffering for I/O operations. When reading a block of information, the kernel issues an I/O operation request to several adjacent blocks. Such operations greatly speed up data retrieval when sequentially reading files. When writing data to a file, the extX file system, writing a new block, places up to 8 adjacent blocks side by side in advance. This method allows you to place files in contiguous blocks, which speeds up their reading and makes it possible to achieve high system performance.

mob_info