Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unix command to copy last line of file to another file

Tags:

file

command

unix

Please provide a command to append an existing file by taking a last line of another file.

like image 352
Mohamed Saligh Avatar asked Dec 03 '10 12:12

Mohamed Saligh


People also ask

How do I copy a line from one file to another in Linux?

You can use grep to search for a regular expression in details. txt and redirect the result to the new file. If not you will have to search for each line you want to copy, still using grep, and append them to new. txt using >> in stead of > .

How do I print the last line of a file in Unix?

tail [OPTION]... [ Tail is a command which prints the last few number of lines (10 lines by default) of a certain file, then terminates. Example 1: By default “tail” prints the last 10 lines of a file, then exits.

How do I copy a previous line in Linux?

To copy a line requires two commands: yy or Y ("yank") and either p ("put below") or P ("put above"). Note that Y does the same thing as yy .

How do you copy the contents of a file in Unix?

To copy files and directories use the cp command under a Linux, UNIX-like, and BSD like operating systems. cp is the command entered in a Unix and Linux shell to copy a file from one place to another, possibly on a different filesystem.


2 Answers

tail -n 1 $file1 >> $file2

Though most systems also support

tail -1 $file1 >> $file2
like image 184
Fred Foo Avatar answered Jan 02 '23 08:01

Fred Foo


tail $file1 -n 1 >> $file2

Or,

tail -1 $file1 >> $file2

If you want it to work with sudo:

tail -1 $file1 | tee -a $file2
like image 29
Attila O. Avatar answered Jan 02 '23 08:01

Attila O.