Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append new line in all files of the folder

Tags:

linux

grep

sed

I have 300 files in a folder. I have to append one new line at the end of all files in a folder.

How can i achieve it using grep.

I tried the following command but its not working

sed 's/$/\n/' /Path/filename.txt
like image 575
Mangoski Avatar asked Jun 25 '15 14:06

Mangoski


People also ask

How do you add a line to every file in a directory?

Just say echo "" >> file . This will append a new line at the end of file .

How do I add a new line to a file?

Sometimes we need to work with a file for programming purposes, and the new line requires to add at the end of the file. This appending task can be done by using 'echo' and 'tee' commands. Using '>>' with 'echo' command appends a line to a file.

How do I add a new line to an existing file in Linux?

Append Text Using >> Operator Alternatively, you can use the printf command (do not forget to use \n character to add the next line). You can also use the cat command to concatenate text from one or more files and append it to another file.

How do you add a new line at the end of a file in Unix using SED?

The code works because sed by default appends a newline to its output if it is not already there. The code "$a\" just says "match the last line of the file, and add nothing to it." But implicitly, sed adds the newline to every line it processes (such as this $ line) if it is not already there.


Video Answer


1 Answers

Just say echo "" >> file. This will append a new line at the end of file.

To do it in all the files in the folder:

for file in *
do
    echo "" >> "$file"
done

From the comments, in your case you have to say:

for file in /path/*.txt
do
    echo "" >> "$file"
done
like image 172
fedorqui 'SO stop harming' Avatar answered Nov 09 '22 04:11

fedorqui 'SO stop harming'