12

Recently, I'd copied some text from browser and I was too lazy too go and paste it in file by using file explorer. I was wandering if I can paste it directly to the file by just using terminal and some commands. Any suggestions?

Ashu
  • 131
  • 1
    You can paste it onto the command line and use an echo command line to write it into a file like the following, echo 'this text is pasted here' > file.txt or if you want to append to the end of a file, echo 'this text is pasted here' >> file.txt – sudodus Feb 20 '18 at 18:23

2 Answers2

12

You can use cat, with a here document e.g.

cat > somefile

hit Enter then paste from the default buffer with a middle click or standard terminal emulator shortcut Ctrl+Shift+V, and terminate the input with Ctrl+D.

Ex.

$ cat > somefile
foo
bar
baz
^D

Use >> in place of > if you want to append to somefile instead of overwrite it.

dessert
  • 39,982
steeldriver
  • 136,215
  • 21
  • 243
  • 336
  • I had some problems with gnome-terminal in ubuntu, when I pasted a large number of lines, the pasting was truncated. I do not know if this has been fixed, but be warned. – Gibezynu Nu Sep 08 '23 at 14:11
9

You can use xclip (sudo apt install xclip) for that:

xclip -se c -o       # print the clipboard's content to stdout
xclip -se c -o >out  # print the clipboard's content to file named “out” 
  • -se c – use the clipboard selection, leave out or change to -se p to use the default buffer instead (the default buffer holds the last thing you selected, e.g. by double-clicking a word)
  • -o – print to stdout
  • >out – redirect stdout to file named out overwriting it, change to >>out to append to the file's content

See man xclip (How can I get help on terminal commands?) for more.

dessert
  • 39,982