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
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
Related
I'm trying to concatenate a bunch of files into a string so I can use them for a function.
As a test script I'm trying to do this:
#!/bin/bash
for line in $(cat list.txt)
do
x=" "
A=$A$line$x
done
echo "$A"
mv "$A" ./stuff
but I'm getting the error:
mv: cannot stat ‘x.dat y.dat z.dat ’: No such file or directory
but they are most definitely there
can I get some advice please?
This solution will handle file names with spaces too.
#!/bin/bash
mapfile -t lines < list.txt
echo "${lines[#]}"
mv "${lines[#]}" ./stuff/
It reads the entire contents of the file into an array variable, displays the content of the entire array, and finally uses those values in the mv command
Change the last line to mv $A ./stuff
That should work with files that do not have space in their names.
I have folder which contains more then 100 files. I want to insert line number into each file.
nl command gives output to standard output on terminal. But I want to add line number in all files of folder.
Can you suggest me how to do this?
Following on #Gianluca's answer, and using bash instead:
for i in *.c *.h ; do ( nl $i > $i.numbered ) && mv $i.numbered $i ; done
This replaces all files ending with .c or .h in the current directory with line-numbered versions.
Using tcsh you can do something like
foreach f (`ls *`)
nl $f >> $f.out
mv $f.out $f
end
You can delete them mv command if you don't want to rename the files
(try the script on a copy ;-) )
I'm trying to copy files from one directory to another and append current date to this filename. The script look like this
#!/bin/bash
echo 'Move to homedir'
cd $HOME
echo 'Copy .txt files'
NOW=$(date +"%d%m%Y")
for FILENAME in *.txt
do
cp "${FILENAME}" "/newdir/${FILENAME}${NOW}"
done
This generates an error because date is appended after file extension, like this
file1.txt10082013
How to avoid that?
Try extracting the extension and renaming the file:
NAME="${FILENAME%.*}"
EXT="${FILENAME##*.}"
cp "${FILENAME}" "/newdir/${NAME}${NOW}.${EXT}"
I want to add the following 2 lines:
VNCSERVERS="1:root"
VNCSERVERARGS[1]="-geometry 1600x1200"
to the end of the file vncservers found at the directory /etc/sysconfig/.
How can I do this?
The easiest way is to redirect the output of the echo by >>:
echo 'VNCSERVERS="1:root"' >> /etc/sysconfig/configfile
echo 'VNCSERVERARGS[1]="-geometry 1600x1200"' >> /etc/sysconfig/configfile
I am using cat *.txt to merge multiple txt files into one, but I need each file to be on a separate line.
What is the best way to merge files with each file appearing on a new line?
just use awk
awk 'FNR==1{print ""}1' *.txt
If you have a paste that supports it,
paste --delimiter=\\n --serial *.txt
does a really great job
You can iterate through each file with a for loop:
for filename in *.txt; do
# each time through the loop, ${filename} will hold the name
# of the next *.txt file. You can then arbitrarily process
# each file
cat "${filename}"
echo
# You can add redirection after the done (which ends the
# for loop). Any output within the for loop will be sent to
# the redirection specified here
done > output_file
for file in *.txt
do
cat "$file"
echo
done > newfile
I'm assuming you want a line break between files.
for file in *.txt
do
cat "$file" >> result
echo >> result
done