Linux command result to file. Linux: Write terminal command results to file

I found one useful command on the Internet to write everything that happens in the terminal to a file. You make a complete log of all the commands you enter in the terminal and what is displayed on the screen.


To start recording, you need to run the command script. If you call the command script without parameters, then the recording will be made to a file with the name typescript.


$ script
The script is running, the file is typescript

The file will be created exactly in the directory where you are. You can display the name of the current directory with the pwd command. If the file already exists, it will be cleared.


As a parameter to the script command, you can specify the name of the file in which you want to record all activity in the terminal. Call example:


script terminal.log

After entering the command, a file will be created, but the data will be written to it only after you enter the exit command or press the key combination Ctrl + D. Example:


$ script
The script is running, the file is typescript
$pwd
/home/user
$exit
exit
The script is executed, the file is typescript
$

We have executed the command script. The file was automatically created typescript(if it already existed, it will be cleared). From this point on, the system begins to remember all input and output in the terminal. At the end, we enter the exit command and into a file named typescript records everything that has happened in the terminal since the command was entered script.


If you want to write to an already existing file, then you need to add the -a option. In this case, the file will not be cleared, but new data will be added to the current content. Example:


$ script my.log <--- Первый вызов: создается пустой файл my.log

$ifconfig
eth0 Link encap:Ethernet HWaddr bb:aa:cc:dd:aa:bb
..
$exit <--- Данные записываются в файл my.log
exit

$ script my.log -a <--- Второй вызов с опцией -a. Файл не очищается.
The script is running, the file is my.log
$pwd
/home/user
$exit
exit

Script completed, file - my.log <--- Данные добавляются к содержимому my.log

Another useful option worth mentioning -c or --command. This option allows you to specify a command to be executed, and the output of this command to write a file. Example:


$ script my.log -c ls
The script is running, the file is my.log
my.logtemp Ubuntu OneImages
DownloadsDesktop
The script is executed, the file is my.log

In this example, we call the command script with option -c and specify the command as an option parameter ls to be executed, and the output of the command must be written to a file my.log. Team ls lists the files in the current directory. So in the file my.log a list of files will appear.


Get help on a command script(for all its options) you can by running in the terminal:

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!

System administration of Linux systems, and indeed administration of any UNIX-like system. It is inextricably linked with work in command shells. For experienced system administrators, this way of interacting with the system is much more convenient and faster than using graphical shells and interfaces. Of course, this will immediately seem like an exaggeration to many, but it is an indisputable fact. Constantly confirmed in practice. And this situation is possible thanks to some tricks and tools used when working with the command console. And one of these tools is the use of information channels and redirection of data flows for processes.

A bit of theory

You should start with the fact that for each process the system provides at least three information channels for “use”:

  • STDIN - standard input;
  • STDOUT - standard output;
  • STDERR is standard error.

There is no strict ownership of these channels for each process. The processes themselves, having initially appeared in the system, “do not know” about who and how the channels belong to. They (channels) are a system-wide resource. Although they are installed by the kernel on behalf of the process. Pipes can link files, pipes link other processes, network connections, and so on.

On UNIX-like systems, according to the I/O model. Each of the information channels is assigned an integer value as a number. However, for secure access. to the above channels. Behind them are reserved permanent numbers - 0, 1 and 2 for STDIN, STDOUT and STDERR, respectively. During the execution of the processes, the input data is read using STDIN. Through the keyboard, from the output of another process, file, etc.. And the output data (via STDOUT), as well as error data (via STDERR). They are output to the screen, to a file, to the input (already through the STDIN of another process) to another program.

To change the direction of information channels and their connection with files, there are special instructions in the form of symbols<, >and >>. So, for example, the instruction< можно заставить направить процессу по STDIN содержимое файла. А с помощью инструкции >the process on STDOUT transfers the output data to a file, while replacing all the contents existing file entirely. If not, it will be created. The >> instruction does the same thing as >, but does not overwrite the file, but appends the output to the end of it. To combine streams (that is, to send them to the same receiver) STDOUT and STDERR, you need to use the >& construct. To direct one of the streams, for example STDERR to a separate place, there is a 2> instruction.

To link two channels together. For example, when you need to send the output of one command to the input of another. The instruction (symbol) "|" should be used for this. Which means the logical operation "or". However, in the context of thread binding, this interpretation does not matter, which is often confusing for beginners. Thanks to these features, you can compose entire command pipelines. What makes working in shells very efficient.

Output to file

Directing the command output and writing this output to the /tmp/somemessage file:

$ echo "This is a test message" > /tmp/somemessage

In this case, as a command from the output of which (via STDOUT) data is redirected in the form of the text " Thisisatestmassage' is the echo utility. As a result, the file /tmp/somemessage will be created, in which there will be an entry "This is a test message". If the file has not been created, then it will be created, if it is created, then all the data in it will be overwritten. If you want to append the output to the end of the file, you need to use the ">>" operator.

$ echo “This is a test message” >> /tmp/somemessage $ cat /tmp/somemessage This is a test message This is a test message

As you can see from the example, the second command added a line to the end of the file.

Getting data from a file

The following example demonstrates login redirection:

$ mail -s "Mail test" john< /tmp/somemessage

On the right side of the command (after the symbol<) находится файл-источник /tmp/somemesage, содержимое которого перенаправляется в утилиту mail (через STDIN), которая в свою очередь, имеет в качестве собственных параметров заголовок письма и адресата (john).

Other examples

The following example shows why it is sometimes very useful to separate streams from STDIN and STDERR channels:

$ find / -name core 2> /dev/null

The fact is that the find / -name core command will “scatter” error messages, sending them by default to the same place as the search results. That is, in the command console terminal, which will significantly complicate the perception of information by the user. Because the desired results will be lost among the numerous error messages associated with the access mode. The 2>/dev/null construct causes the find utility to send error messages (following the STDERR pipe with the reserved number 2) to the dummy device /dev/null, leaving only the search results in the output.

$ find / -name core > /tmp/corefiles 2> /dev/null

Here, the construction > /tmp/corefiles redirects the output of the find utility (on the STDOUT pipe) to the file /tmp/corefiles. Error messages are filtered out to /dev/null, not getting into the shell terminal output.

To link different channels to each other for different commands:

$ fsck --help | drep M

M do not check mounted filesystems

This command will output a line (or lines) containing the character "M" from the quick help page for the utility. This is very convenient when you need to see only the information of interest. In this case, the grep utility gets the output (using the | instruction) from the fsck --help command. And then according to the pattern "M" discards everything superfluous.

If you want the next command in the pipeline to be executed only after the previous command has completed and successfully completed, then you should use the && instruction for this, for example:

$ lpr /tmp/t2 && rm /tmp/page1

This command will delete the /tmp/page1 file only when its content has been sent from the print queue. To achieve the opposite effect, i.e., when you need to execute the next command in the pipeline only after the previous one fails (completes with an error with a non-zero code), you should use the || construct.

When a line of code that includes a too long command pipeline is hard to read, you can break it into logical components line by line using the backslash character "\":

$ cp --preserve --recursive /etc/* /spare/backup \ || echo "Make backup error"

Separate commands that should be executed one after another can be combined into one line, separating them with a colon character ";":

If you find an error, please highlight a piece of text and click Ctrl+Enter.

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 numbers 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 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 filesystem. 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 terminates.

On some systems, these files have different names, but they are always located 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.

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 redirection variant 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 one cannot, for example, by typing at 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 simply redirecting standard streams, there is also the possibility of not just redirecting a stream to a particular channel, but making a copy of the contents of a 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 prefer) with the symbol vertical bar- "|". 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, the result of which is 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.

exit status ( boolean returned after the program exits) 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

Looking in the input file or data from the standard input string, containing the specified pattern, and print 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 writes 3 columns to stdout: one contains lines that only occur in file 1, the second contains lines that only occur in file 2: 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 by using regular expressions, allowing you to organize, for example, a search for 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
A computer