Need help accessing multiple files' content at the same time

149 views
Skip to first unread message

Howard

unread,
Apr 30, 2021, 8:20:27 AM4/30/21
to BBEdit Talk
I have a folder on my Mac with multiple text files. I need to run the same Unix command line, which I have, on all of them. I would like to do all of them at the same time.

Can I do either of the following using BBEdit (vers. 13.5.6)?

1. Combine all the files into one text file.
2. From BBEdit, access all the files in their folder at the same time?

If either can be done, I would appreciate directions on doing it.

Thanks,
Howard

Patrick Woolsey

unread,
Apr 30, 2021, 10:05:27 AM4/30/21
to bbe...@googlegroups.com
Per item 2. this sounds like a job for either the Text -> Apply Text Transform command or a _text factory_ with a suitable 'Run Unix Filter' action.

Per item 1. you can catenate any selectable set of files into an existing document via the Edit -> Insert -> File Contents... command



Regards,

Patrick Woolsey
==
Bare Bones Software, Inc. <https://www.barebones.com/>

face

unread,
Apr 30, 2021, 2:17:38 PM4/30/21
to BBEdit Talk
hmm - in a shell like bash you would do something like
for FILENAME in ls * do
 unixcommandline $FILENAME ;
done

It can get messier, but if all the files (and only the files) are in a directory this would be simplest.

Howard

unread,
May 2, 2021, 11:36:26 AM5/2/21
to BBEdit Talk
Thanks Patrick. By catenating the files, it was easy to produce the results I needed.

Howard

unread,
May 6, 2021, 11:06:27 AM5/6/21
to BBEdit Talk
Patrick, your way of catenating individual files works well.

I have a folder that contains both folders whose contents I would like to include in the catenation as well as separate individual files. Is there a way to do that in BBEdit?

Howard

Patrick Woolsey

unread,
May 6, 2021, 11:43:24 AM5/6/21
to bbe...@googlegroups.com
On 5/6/21 at 11:06 AM, leadwi...@gmail.com (Howard) wrote:

>Patrick, your way of catenating individual files works well.
>
>I have a folder that contains both folders whose contents I
>would like to include in the catenation as well as separate
>individual files. Is there a way to do that in BBEdit?
>

I'm happy that was helpful and to your current question:

Since the Insert -> File Contents command does not presently
support selecting folders directly, you would instead need to
manually expand all the desired folders (and subfolders if
relevant) and individually select their contents.

From curiosity :-) may I ask how approximately how many items
the desired set of folders & files contains?


Regards

Howard

unread,
May 6, 2021, 12:06:28 PM5/6/21
to BBEdit Talk
It currently has 22 items, 20 of which are split between two folders.

Patrick Woolsey

unread,
May 6, 2021, 12:23:08 PM5/6/21
to bbe...@googlegroups.com
OK, thanks and manual expansion & selection should suffice in
this case, though probably not the general. :-)

(PS: I suggest you switch the selection dialog to "List" mode
rather than "Icon" or "Columns".)


Regards

Patrick Woolsey
==
Bare Bones Software, Inc. <https://www.barebones.com/>




Howard

unread,
May 6, 2021, 12:52:32 PM5/6/21
to BBEdit Talk

Patrick, thanks for your help.

Christopher Stone

unread,
May 7, 2021, 7:32:59 AM5/7/21
to BBEdit-Talk
On 05/06/2021, at 10:06, Howard <leadwi...@gmail.com> wrote:
Patrick, your way of concatenating individual files works well.

I have a folder that contains both folders whose contents I would like to include in the concatenation as well as separate individual files. Is there a way to do that in BBEdit?


Hey Howard,

If you're going to do this regularly you might want to systematize the process.

Place the following script in:

~/Library/Application Support/BBEdit/Scripts/



#!/usr/bin/env bash
# -------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2021/05/07 06:07
# dMod: 2021/05/07 06:07 
# Task: Recursively Concatenate Files in the Front Finder Window.
# Tags: @ccstone, @Shell, @Script, @Concatenate, @Files, @Finder, @BBEdit
# -------------------------------------------------------------------------

# Acquire the path to the Front Finder Window. (Finder Insertion Location.)
read -r -d '' asCmdStr <<'EOF'
   tell application "Finder"
      set targetFolder to insertion location as alias
      return POSIX path of targetFolder
   end tell
EOF
targetFolderPath=$(osascript -e "$asCmdStr")

# Change the working directory to the Finder Insertion Location.
cd "$targetFolderPath"

# Find files, concatenate their contents, send result to BBEdit.
find -E . -type f \( ! -name ".*" \) -exec sed -n '1,$p' {} \; | bbedit



Give it a memorable name.

If desired give it a keyboard shortcut in BBEdit's Menus & Shortcuts Preferences.



REQUIREMENTS:

BBEdit's command line tools must be installed.



USAGE:

1) Open the desired target directory in the Finder, so its window is FRONTMOST.

2) Run the script from BBEdit's Script menu or via a keyboard shortcut.



RESULT:

A new window will be opened in BBEdit containing the concatenated contents of every file in the target directory (recursive).



--
Best Regards,
Chris

@lbutlr

unread,
May 7, 2021, 6:10:39 PM5/7/21
to BBEdit Talk
On 07 May 2021, at 05:32, Christopher Stone <listmei...@gmail.com> wrote:
> # Find files, concatenate their contents, send result to BBEdit.
> find -E . -type f \( ! -name ".*" \) -exec sed -n '1,$p' {} \; | bbedit

OK, two questions.

1) why -E?
2) '1,$p'?

Where/what is $p?

--
When men talk to their friends, they insult each other. They don't
really mean it. When women talk to their friends, they compliment
each other. They don't really mean it.

Christopher Stone

unread,
May 8, 2021, 7:08:35 AM5/8/21
to BBEdit-Talk
On 05/07/2021, at 17:10, @lbutlr <kre...@kreme.com> wrote:
On 07 May 2021, at 05:32, Christopher Stone <listmei...@gmail.com> wrote:
# Find files, concatenate their contents, send result to BBEdit.
find -E . -type f \( ! -name ".*" \) -exec sed -n '1,$p' {} \; | bbedit

OK, two questions.

 1) why -E?
 2) '1,$p'?

Where/what is $p?


Hey Lewis,

Well, -E isn't really necessary in this use-case.  I have it in my template for find, because I often use a regular expression and always want to use the Extended regular expression set.

sed -n '1,$p'

This is just `sed` printing from line 1 through the last line of the given file.

And actually that can be reduced to just p for print:

sed -n 'p'

So this works perfectly well:

find -E . -type f \( ! -name ".*" \) -exec sed -n 'p' {} \; | bbedit

I was going to use `cat` for this task, but `sed` will add an ending line break if there isn't one already in the printed file and `cat` won't.

Sed:

file01contents
file02contents
file02contents

Cat:

file01contentsfile02contentsfile02contents


--
Take Care,
Chris

Greg Raven

unread,
May 9, 2021, 9:53:50 AM5/9/21
to BBEdit Talk
Great responses, but depending on the command-line processing that needs to be done, my approach would be to set up these files in a project. The files themselves would go into the Includes folder, and then the include commands would go into a single file in the build folder. This way, the contents of the concatenated file could be updated instantly after changing any of the source / include files.

It would also allow you to pick and chose which of the files in the include folder to concatenate.

Should any of the command-line processes throw off the arrangement of the concatenated file (via a sort, for example), I would make a copy of just the bbinclude commands and save it to the Scratchpad, from whence it could easily be retrieved for restoring the single file for re-concatenation.

Reply all
Reply to author
Forward
0 new messages