- Linux write command
- Description
- Syntax
- Options
- Examples
- Related commands
- How do I save terminal output to a file?
- 9 Answers 9
- Overview:
- linux — write commands from one terminal to another
- 7 Answers 7
- How to write the output into the file in Linux
- How do I save terminal output to a file?
- Writing the output into the file
- Appending the output or data to the file
- How to save the output of a command to a file in bash using tee command
- Examples
- I/O redirection summary for bash and POSIX shell
- Conclusion
- How to create a file in Linux from terminal window? [closed]
- 17 Answers 17
- Create an empty file
- Create a file containing a newline and nothing else
- Write text into a file
Linux write command
On Unix-like operating systems, the write command sends a message to another user.
This page covers the Linux version of write.
Description
The write utility allows you to communicate with other users by copying lines from your terminal to theirs.
When you run the write command, the user you are writing to gets a message of the format:
Any further lines you enter are copied to the specified user’s terminal. If the other user wants to reply, they must run write as well.
When you are done, type an end-of-file or interrupt character. The other user sees the message ‘EOF’ indicating that the conversation is over.
You can prevent people (other than the super-user) from writing to you with the mesg command.
If the user you want to write to is logged in on more than one terminal, you can specify which terminal to write to by specifying the terminal name as the second operand to the write command. Alternatively, you can let write select one of the terminals and it will pick the one with the shortest idle time. So, if the user is logged in at work and also dialed up from home, the message goes to the right place.
The traditional protocol for writing to someone is that the string ‘-o’, either at the end of a line or on a line by itself, means that it is the other person’s turn to talk. The string ‘oo’ indicates the person believes the conversation to be over.
Syntax
Options
user | The user to write to. |
tty | The specific terminal to write to, if the user is logged in to more than one session. |
Examples
Write a message to the user hope. After entering this command, you will be placed on a blank line, where everything you type will be sent to the other user (line by line). Typing the interrupt character (Ctrl-C, by default) returns you to the command prompt and end the write session.
Write a message to the user hope on terminal tty7.
Related commands
mesg — Control if (non-root) users can send messages to your terminal.
talk — Talk with other logged in users.
wall — Send a message to all logged-in users.
who — Report which users are logged in to the system.
Источник
How do I save terminal output to a file?
How do I save the output of a command to a file?
Is there a way without using any software? I would like to know how.
9 Answers 9
Yes it is possible, just redirect the output (AKA stdout ) to a file:
Or if you want to append data:
If you want stderr as well use this:
or this to append:
if you want to have both stderr and output displayed on the console and in a file use this:
(If you want the output only, drop the 2 above)
To write the output of a command to a file, there are basically 10 commonly used ways.
Overview:
Please note that the n.e. in the syntax column means «not existing».
There is a way, but it’s too complicated to fit into the column. You can find a helpful link in the List section about it.
The standard output stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, it gets overwritten.
The standard output stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, the new data will get appended to the end of the file.
command 2> output.txt
The standard error stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, it gets overwritten.
command 2>> output.txt
The standard error stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, the new data will get appended to the end of the file.
Both the standard output and standard error stream will be redirected to the file only, nothing will be visible in the terminal. If the file already exists, it gets overwritten.
Both the standard output and standard error stream will be redirected to the file only, nothing will be visible in the terminal. If the file already exists, the new data will get appended to the end of the file..
command | tee output.txt
The standard output stream will be copied to the file, it will still be visible in the terminal. If the file already exists, it gets overwritten.
command | tee -a output.txt
The standard output stream will be copied to the file, it will still be visible in the terminal. If the file already exists, the new data will get appended to the end of the file.
Bash has no shorthand syntax that allows piping only StdErr to a second command, which would be needed here in combination with tee again to complete the table. If you really need something like that, please look at «How to pipe stderr, and not stdout?» on Stack Overflow for some ways how this can be done e.g. by swapping streams or using process substitution.
command |& tee output.txt
Both the standard output and standard error streams will be copied to the file while still being visible in the terminal. If the file already exists, it gets overwritten.
command |& tee -a output.txt
Both the standard output and standard error streams will be copied to the file while still being visible in the terminal. If the file already exists, the new data will get appended to the end of the file.
Источник
linux — write commands from one terminal to another
I need to write commands from one terminal to another terminal.
Which just prints the ls as output and doesn’t execute.
Which don’t seem to do anything
[note that I don’t want to touch the second terminal to accomplish this. only the first one]
7 Answers 7
Is posible to show the output of a command on multiple terminals simultaneously with the following script., and it works with all console programs, including the editors. For example doing:
Open an editor and both the output and the text that we introduce will be redirected to the virtual terminal number 5. You can see your terminals:
Each virtual terminal has an associated number.
Is work with the normal terminal, konsole and xterm, just create the file execmon.bash and put this:
This is hairy. The stdin file in proc you’re trying to use is a symlink to the terminal device (probably /dev/pts/something). There are two processes that have that device open: the shell (your target) and the terminal emulator (e.g. gnome-terminal), and they use it like a socket to pass data in both directions. Presumably the latter is stealing the output and displaying it, so the shell never sees it. I don’t think this technique will work.
What are you trying to accomplish? You can’t run the process as a child using conventional tools like popen()? If it’s a GUI terminal emulator, you could try to forge keyboard input via X events or the kernel’s uinput API.
Источник
How to write the output into the file in Linux
How do I save terminal output to a file?
A command can receive input from a file and send output to a file.
Writing the output into the file
The syntax is
command > filename
For example, send output of the ls command to file named foo.txt
$ ls > foo.txt
View foo.txt using the cat command:
$ cat foo.txt
Please note that when you type ‘ls > foo.txt’, shell redirects the output of the ls command to a file named foo.txt, replacing the existing contents of the file. In other words, the contents of the file will be overwritten.
Appending the output or data to the file
The syntax is
command >> filename
For example the following will append data:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
Verify it:
cat /tmp/data.txt
How to save the output of a command to a file in bash using tee command
The tee command read from standard input and write to standard output and files. The syntax is as follows for writing data into the file:
command | tee file.txt
Want to append data? Try
command | tee -a output.txt
Examples
Display output of the date command on screen and save to the file named /tmp/output.txt. If the output.txt already exists, it gets overwritten:
$ date | tee /tmp/output.txt
$ cat /tmp/output.txt
Same as above but append to the given files, do not overwrite file:
$ pwd | tee -a /tmp/test.txt
$ echo «Today is $(date)» | tee -a /tmp/test.txt
$ hostnamectl | tee -a /tmp/test.txt
$ cat /tmp/test.txt
The above commands will append the output to the end of the file, just like the shell >> operator as explained earlier.
I/O redirection summary for bash and POSIX shell
Shell operator | Description | Overwrite existing file? |
---|---|---|
command > output.txt | Save terminal output (standard output) to a file named output.txt | Yes |
command >> output.txt | Append terminal output (standard output) to a file named output.txt | No |
command | Takes standard input from output.txt file | N/A |
command 0 | Takes standard input from output.txt file | N/A |
command 1> output.txt | Puts standard output to output.txt file | Yes |
command 1>> output.txt | Appends standard output to output.txt | No |
command 2> output.txt | Puts standard error to output.txt | Yes |
command 2>> output.txt | Appends standard error to output.txt file | No |
command &> output.txt | Puts both standard error and output to output.txt | Yes |
command > output.txt 2>&1 | <POSIX> Puts both standard error and output to file named output.txt | Yes |
command &>> output.txt | Appends both standard error and output to file named output.txt | No |
command >> output.txt 2>&1 | <POSIX> Appends both standard error and output to file called output.txt | No |
command | tee output.txt | Puts standard output to output.txt while displaying output on screen | Yes |
command | tee -a output.txt | Appends standard output to output.txt while displaying output on screen | No |
command |& tee output.txt | Puts both standard output and error to output.txt while displaying output on terminal | Yes |
command 2>&1 | tee output.txt | <POSIX> Puts both standard output and error to file named output.txt while displaying output on terminal | Yes |
command |& tee -a output.txt | Append both standard output and error to file called output.txt while displaying output on terminal | No |
command 2>&1 | tee -a output.txt | <POSIX> Append both standard output and error to file named output.txt while displaying output on terminal | No |
Conclusion
You learned how to write the output to the file in Linux or Unix-like system when using bash or POSIX shell. We have:
- /dev/stdin (standard input) — File descriptor 0 is duplicated.
- /dev/stdout (standard output) — File descriptor 1 is duplicated.
- /dev/stderr (standard error) — File descriptor 2 is duplicated.
See I/O redirection documentation for more information. We can read bash man page as follows using the man command:
man bash
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
How to create a file in Linux from terminal window? [closed]
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year .
What’s the easiest way to create a file in Linux terminal?
17 Answers 17
Depending on what you want the file to contain:
- touch /path/to/file for an empty file
somecommand > /path/to/file for a file containing the output of some command.
nano /path/to/file or vi /path/to/file (or any other editor emacs,gedit etc )
It either opens the existing one for editing or creates & opens the empty file to enter, if it doesn’t exist
Create the file using cat
Now, just type whatever you want in the file:
CTRL-D to save and exit
There are several possible solutions:
Create an empty file
The echo version will work only if your version of echo supports the -n switch to suppress newlines. This is a non-standard addition. The other examples will all work in a POSIX shell.
Create a file containing a newline and nothing else
This is a valid «text file» because it ends in a newline.
Write text into a file
These are equivalent. The $EDITOR command assumes that you have an interactive text editor defined in the EDITOR environment variable and that you interactively enter equivalent text. The cat version presumes a literal newline after the \ and after each other line. Other than that these will all work in a POSIX shell.
Of course there are many other methods of writing and creating files, too.
Источник