0

How can we copy all the contents of all the files in a given directory into a file so that there are two empty lines between contents of each files?

Need not to mention, I am new to bash scripting, and I know this is not an extra complicated code!

Any help will be greatly appreciated.

Related links are following:
* How do I compare the contents of all the files in a directory against another directory?
* Append contents of one file into another
* BASH: Copy all files and directories into another directory in the same parent directory

After reading comments, my initial attempt is this:

cat * > newfile.txt  

But this does not create two empty lines between contents of each new files.

Community
  • 1
  • 1
BhishanPoudel
  • 15,974
  • 21
  • 108
  • 169

3 Answers3

1

Try this.

awk 'FNR==1 && NR>1 { printf("\n\n") }1' * >newfile.txt

The variable FNR is the line number within the current file and NR is the line number overall.

tripleee
  • 175,061
  • 34
  • 275
  • 318
1

One way:

(
    files=(*)
    cat "${files[0]}"
    for (( i = 1; i < "${#files[@]}" ; ++i )) ; do
        echo
        echo
        cat "${files[i]}"
    done
) > newfile.txt
ruakh
  • 175,680
  • 26
  • 273
  • 307
0

Example of file organization:

I have a directory ~/Pictures/Temp

If I wanted to move PNG's from that directory to another directory I would first want to set a variable for my file names:

# This could be other file types as well
file=$(find ~/Pictures/Temp/*.png)

Of course there are many ways to view this check out:

$ man find
$ man ls

Then I would want to set a directory variable (especially if this directory is going to be something like a date

dir=$(some defining command here perhaps an awk of an ls -lt)
# Then we want to check for that directories existence and make it if
# it doesn't exist
[[ -e $dir ]] || mkdir "$dir"
# [[ -d $dir ]] will work here as well

You could write a for loop:

# t is time with the sleep this will be in seconds
# super useful with an every minute crontab
for ((t=1; t<59; t++));
do
    # see above
    file=$(blah blah)
    # do nothing if there is no file to move
    [[ -z $file ]] || mv "$file" "$dir/$file"
    sleep 1
done

Please Google this if any of it seems unclear here are some useful links: https://www.gnu.org/software/gawk/manual/html_node/For-Statement.html http://www.gnu.org/software/gawk/manual/gawk.html

Best link on this page is below:

http://mywiki.wooledge.org/BashFAQ/031

Edit:

Anyhow where I was going with that whole answer is that you could easily write a script that will organize certain files on your system for 60 sec and write a crontab to automatically do your organizing for you:

crontab -e 

Here is an example

$ crontab -l
* * * * * ~/Applications/Startup/Desktop-Cleanup.sh
# where ~/Applications/Startup/Desktop-Cleanup.sh is a custom application that I wrote 
Robert J
  • 300
  • 1
  • 3
  • 15
  • Uh, this seems tangential at best. – tripleee Feb 16 '16 at 04:41
  • I have ADHD... my bad – Robert J Feb 16 '16 at 04:45
  • Perhaps you'll want to create a new question where this would actually be topical as an answer. I'm afraid it's likely to be deleted from here. – tripleee Feb 16 '16 at 04:48
  • How so? Bhishan Poudel was talking about file organization with a bash scripting. I gave him an example of how this could be done without scripting this out for him. – Robert J Feb 16 '16 at 04:53
  • 1
    @tripleee I see what you are saying in all fairness I misread the first iteration of the question. You should look at rev 1. It seems like an entirely different question. – Robert J Feb 16 '16 at 05:07