- How to copy complete content on terminal to a text file using commands ?(Not for one output but everything on terminal)
- 4 Answers 4
- Copying text from a terminal
- General approach
- Terminals without CLIPBOARD selection
- Xorg
- Wayland
- Intercepting commands output
- Accessing Linux terminal backlog
- Comparison of common emulators
- Special cases
- putty
- urxvt
- xterm
- mlterm
- The Linux cp Command – How to Copy Files in Linux
- What is the cp command?
- How to copy files with the cp command
- How to copy directories with the cp command
- How to copy files that match a glob pattern
- How to copy files via terminal?
How to copy complete content on terminal to a text file using commands ?(Not for one output but everything on terminal)
I unserstand you want to copy the output of previous commands right? Not the output of yet to be run commands.
4 Answers 4
screendump - dump the contents of a virtual console to stdout
You’ll need root privileges to run screendump, so use sudo.
You can use script. It will basically save everything printed on the terminal in that script session.
script makes a typescript of everything printed on your terminal. It is useful for students who need a hardcopy record of an interactive session as proof of an assignment, as the typescript file can be printed out later with lpr(1). You can start a script session by just typing script in the terminal, all the subsequent commands and their outputs will all be saved in a file named typescript in the current directory. You can save the result to a different file too by just starting script like:
script output.txt To logout of the script session (stop saving the contents), just type exit.
$ script output.txt Script started, file is output.txt $ ls output.txt testfile.txt foo.txt $ exit exit Script done, file is output.txt
$ cat output.txt Script started on 2020-07-23 09:57:16+05:30 $ ls output.txt testfile.txt foo.txt $ exit exit Script done on 2020-07-23 09:57:34+05:30
Copying text from a terminal
Most mature terminal emulators permit users to copy or save their contents.
General approach
In graphical terminal emulators, contents are typically selectable by mouse, and can then be copied using the context menu, Edit menu or a key combination such as Ctrl+Shift+c .
Terminals without CLIPBOARD selection
Xorg
Some emulators do not support the CLIPBOARD selection natively, and copy data to the PRIMARY selection. For them xclip may be used:
$ xclip -o | xclip -selection clipboard -i
The above command reads data from the PRIMARY selection and writes it to CLIPBOARD selection.
Other clipboard managers such as autocutsel provide automatic synchronization between selection buffers.
Wayland
Utilities like wl-clipboard and clipboard AUR can copy data to the Wayland clipboard:
Intercepting commands output
Use tee to intercept the output of a command.
$ command 2>&1 | tee output-file
After the command is executed, output-file will contain its output, while having displayed the output at the same time.
Accessing Linux terminal backlog
The backlog of a native terminal named /dev/ttyN may be accessed via /dev/vcsN . Hence, if one is working in /dev/tty1 , the following snippet will let store the backlog in a file output-file :
Comparison of common emulators
The factual accuracy of this article or section is disputed.
Unless the «Key combination» column states otherwise, the key combination is Ctrl+Shift+c .
Emulator | Select to PRIMARY | CLIPBOARD | ||||
---|---|---|---|---|---|---|
Key combination | Context menu | Window menu | Select | |||
Alacritty | Yes | Yes | No | No | No | |
aterm AUR | Yes | No | No | No | No | |
eterm AUR | Yes | No | No | No | No | |
foot | Yes | Yes | No | No | No | |
germinal AUR | Yes | Yes | Yes | No | No | |
Guake | Yes | Yes | Yes | No | No | |
Konsole | Yes | Yes | Yes | Yes | Optional | |
lilyterm-git AUR | Yes | Yes Ctrl+Delete | Yes | No | No | |
lxterminal | Yes | Yes | Yes | Yes | No | |
mate-terminal | Yes | Yes | Yes | Yes | No | |
mlterm AUR | Yes | Yes | No | No | Yes | |
pantheon-terminal | Yes | Yes | Yes | No | No | |
PuTTY | Yes | No | No | No | No | |
qterminal | Yes | Yes | Yes | Yes | No | |
roxterm AUR | Yes | Yes | Yes | Yes | No | |
rxvt AUR | Yes | No | No | No | No | |
sakura AUR | Yes | Yes | Yes | Yes | No | |
st | Yes | Yes | No | No | No | |
Terminator | Yes | Yes | Yes | No | No | |
terminology | Yes | Yes | Yes | No | No | |
Termite | Yes | Yes | No | No | No | |
Tilda | Yes | Yes | Yes | No | No | |
urxvt | Yes | Yes Ctrl+Alt+c | No | No | Optional | |
xfce4-terminal | Yes | Yes | Yes | Yes | No | |
xterm | Yes | Optional[1] | No | No | Yes | |
Yakuake | Yes | Yes | Yes | No | Optional |
Special cases
putty
The xclip approach works for putty: one just has to remember that the xclip invocation should be done on the local computer (in another terminal), not on the remote machine to which putty is connected.
urxvt
Selecting text to CLIPBOARD requires the selection-to-clipboard perl extension. See rxvt-unicode#Cut and paste for details.
xterm
Access to the CLIPBOARD selection in xterm requires additional steps.
mlterm
In addition to Ctrl+Shift+c , you can use Ctrl+Insert if you do not want to kill processes accidentally.
The Linux cp Command – How to Copy Files in Linux
Dillion Megida
There are a couple different ways to copy and paste content when you’re working on your computer.
If you spend more time in the user interface of your device, you’ll probably use your mouse to do this. You can copy files by right-clicking on the file and selecting «Copy», then going to a different directory and selecting «Paste».
For my terminal friends, you can also perform file copy-paste operations without leaving the terminal. In a Linux-based terminal, you do this using the cp command.
In this article, I’ll explain what the cp command is and show you how to copy and paste files and directories in Linux using the terminal.
What is the cp command?
You use the cp command for copying files from one location to another. This command can also copy directories (folders).
The syntax of this command is:
cp [. file/directory-sources] [destination]
[file/directory-sources] specifies the sources of the files or directories you want to copy. And the [destination] argument specifies the location you want to copy the file to.
To understand the rest of this article, I will use this folder structure example. Let’s say a directory called DirectoryA has two directories in it: DirectoryA_1 and DirectoryA_2. These subdirectories have many files and sub directories in them.
I’ll also assume you’re currently in the DirectoryA location in the terminal, so if you aren’t, make sure you are:
How to copy files with the cp command
If you want to copy a file, say README.txt from DirectoryA_1 to DirectoryA_2, you will use the cp command like this:
cp ./DirectoryA_1/README.txt ./DirectoryA_2 # ./DirectoryA_1/README.txt is the source file # ./DirectoryA_2 is the destination
If you want to copy more than a file from DirectoryA_1 to DirectoryA_2, you will use the cp command like this:
cp ./DirectoryA_1/README.txt ./DirectoryA_1/ANOTHER_FILE.txt ./DirectoryA_2
As you can see, you will put all the source files first, and the last argument will be the destination.
How to copy directories with the cp command
By default, the cp command works with files. So if you attempt to copy a directory like this:
cp ./DirectoryA_1/Folder/ ./DirectoryA_2
You will get an error stating:
./DirectoryA_1/Folder/ is a directory
To copy directories, you have to pass the -r flag. This flag informs the cp command to recursively copy a directory and its contents (which could be files or other sub directories). So for the previous command, you can add the flag before the directory sources like this:
cp -r ./DirectoryA_1/Folder/ ./DirectoryA_2
This command will recursively copy the Folder directory in ./DirectoryA_1/ as well as all files and directories in the Folder directory.
How to copy files that match a glob pattern
A glob pattern is similar to Regex, which allows you to match multiple files with names that match a specific pattern. Learn more about the difference here: Regex vs Glob patterns.
For example, if you want to copy all files in DirectoryA_1 with the .txt extension, you can execute this command:
cp ./DirectoryA_1/*.txt ./DirectoryA_2
./DirectoryA_1/*.txt matches files with the .txt extension in their names, and the cp command can copy all those files to the destination.
You can check out the glob documentation to learn more about globbing patterns and characters you can use.
Now you know how to copy files (and directories) right from the command line. Thanks for reading!
Dillion Megida
Developer Advocate and Content Creator passionate about sharing my knowledge on Tech. I simplify JavaScript / ReactJS / NodeJS / Frameworks / TypeScript / et al My YT channel: youtube.com/c/deeecode
If you read this far, tweet to the author to show them you care. Tweet a thanks
Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started
freeCodeCamp is a donor-supported tax-exempt 501(c)(3) charity organization (United States Federal Tax Identification Number: 82-0779546)
Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons — all freely available to the public. We also have thousands of freeCodeCamp study groups around the world.
Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.
How to copy files via terminal?
1) By using -i for interactive you will be asked if you would like to replace the file:
cp -i /home/levan/kdenlive/untitelds.mpg /media/sda3/SkyDrive/
or you can use -b to create a backup of your file:
cp -b /home/levan/kdenlive/untitelds.mpg /media/sda3/SkyDrive
2) Same as the above:
cp (-i or -b) /media/sda3/SkyDrive/untitelds.mpg /home/levan/kdenlive
3) Use -R for recursive and -i for interactive:
4) This last one can be done via the mv command, move is like cutting:
mv -i ~/MyFile ~/OtherFolder/MyFile
if you want to move a directory, use:
mv -Ri ~/MyDirectory ~/OtherDirectory/
@WarriorIng64 you can use four spaces identation for code blocks, or you can surround small pieces of code with `s.
@WarriorIng64 That is triggered by the bash tag in the question. highlighting will be enabled by default by the system. If you answer a question with bash tag, the hightlighting will follow bash style and so on.
@Anwar I was just partway through making a Meta post on this when I saw your comment. Of course, I credited you in the answer. 🙂
When ~/Dropbox/RECENT/ is your current directory:
And I want to copy input.txt with another name in my current directory.
Again with ~/Dropbox/RECENT/ as current directory:
Existing filenames can be auto-completed using TAB .
Long version of the same copy command (when you are not in ~/Dropbox/RECENT/ ):
cp /home/$USER/Dropbox/RECENT/input.txt /home/$USER/Dropbox/RECENT/SORT/
I put a / behind every directory. If SORT does NOT exist a cp will also create a file named SORT making you think something went wrong. Adding the / will have cp error out and not copy the file.
Copying a file something.txt to file folder : use cp something.txt folder/
Copying a file something.txt to the current directory as something2.txt : use cp something.txt something2.txt
ubuntu@ubuntu-T100TA:~/TestFolder$ ls -l total 8 drwxrwxr-x 2 ubuntu ubuntu 4096 Mar 12 21:53 Folder1 -rw-rw-r-- 1 ubuntu ubuntu 14 Mar 12 21:52 something.txt ubuntu@ubuntu-T100TA:~/TestFolder$ ls -l Folder1/ total 4 -rw-rw-r-- 1 ubuntu ubuntu 14 Mar 12 21:53 something.txt ubuntu@ubuntu-T100TA:~/TestFolder$ ls -l total 8 drwxrwxr-x 2 ubuntu ubuntu 4096 Mar 12 21:54 folder -rw-rw-r-- 1 ubuntu ubuntu 14 Mar 12 21:52 something.txt ubuntu@ubuntu-T100TA:~/TestFolder$ ls -l folder/ total 0 ubuntu@ubuntu-T100TA:~/TestFolder$ cp something.txt folder/ ubuntu@ubuntu-T100TA:~/TestFolder$ ls -l folder/ total 4 -rw-rw-r-- 1 ubuntu ubuntu 14 Mar 12 21:55 something.txt ubuntu@ubuntu-T100TA:~/TestFolder$ cp something.txt something2.txt ubuntu@ubuntu-T100TA:~/TestFolder$ ls -l total 12 drwxrwxr-x 2 ubuntu ubuntu 4096 Mar 12 21:55 folder -rw-rw-r-- 1 ubuntu ubuntu 14 Mar 12 21:55 something2.txt -rw-rw-r-- 1 ubuntu ubuntu 14 Mar 12 21:52 something.txt