Recursively cat all the files into single file
find data/ -name '*.json' -exec cat {} \; > uber.json
a short explanation:
find <where> \
-name <file_name_pattern> \
-exec <run_cmd_on_every_hit> {} \; \
> <where_to_store>
Recursive cat with file names
Of course! Instead of just cat
ing the file, you just chain actions to print the filename, cat the file, then add a line feed:
find . -name 'foo.txt' \
-print \
-exec cat {} \; \
-printf "\n"
How to read each file in a folder using cat (recursively) and store it in a file?
Instead of using cat
use cp
it's more efficient.
find . -name \*.pdf -exec cp {} $(basename {}).txt \;
Concatenate all files recursively, ignoring one file extension
find . -type f -not -name "*.XYZ" -exec cat {} \; > /tmp/alldata.txt
More recent versions of gnu find
include -not
which negates the next argument. In this case, you can combine that with the -name
argument to get what you want without invoking grep -v
.
Cat several files into one file with the file name before the data
Using awk
awk 'FNR==1{sub(/[.][^.]*$/, "", FILENAME); print FILENAME} 1' file*.log >all.log
FNR
is the file record number. It is one at the beginning of each file. Thus, the test FNR==1
tells us if we are at the beginning of a file. If we are, then we remove the extension from the filename using sub(/[.][^.]*$/, "", FILENAME)
and then we print it.
The final 1
in the program is awk's cryptic way of saying print-this-line.
The redirection >all.log
saves all the output in file all.log
.
Using shell
for f in file*.log; do echo "${f%.*}"; cat "$f"; done >all.log
Or:
for f in file*.log
do
echo "${f%.*}"
cat "$f"
done >all.log
In shell, for f in file*.log; do
starts a loop over all files matching the glob file*.log
. The statement echo "${f%.*}"
prints the file name minus the extension. ${f%.*}
is an example of suffix removal. cat "$f"
prints the contents of the file. done >all.log
terminates the loop and saves all the output in all.log
.
This loop will work correctly even if file names contain spaces, tabs, newlines, or other difficult characters.
Cat files into a new files in multiple sub directories
Using a Bash for-Loop, this should work:
for sub_dir in $(find ./ -type d -not -name '.'); do
cat $sub_dir/*.txt > $sub_dir/all.txt;
done
Use all the subdirs found by find (excluding the . directory) and issue a cat on each one of them.
Save all files content from a directory and sub directory into a single file with Windows cmd/Powershell
Use Get-ChildItem
to recursively discover any files in the folder hierarchy, then pipe those to Get-Content
(for reading) and finally pipe the output from those calls to Set-Content
(for writing the whole thing back to disk).
Get-ChildItem path\to\root\folder -Recurse -File |Get-Content |Set-Content path\to\save.txt
Note: Unlike cat
, Get-Content
reads the files as text/strings by default, so this will work well for text-encoded files (like js, php and html source files), but won't work on binary files
Related Topics
Mixing Static Libraries and Shared Libraries
Replace All Lines That Do Not Contain Matched String
How to Remove Folders with a Certain Name
How to Check If There Are Symbolic Links Pointing to a Directory
What Would Be the Equivalent of Win32 API in Linux
Print Previous Line If Condition Is Met
Change Gnome Terminal Theme Programmatically
Shell Prompt That Is Based on Location in Filesystem
How to Increase the Scrollback Buffer in a Running Screen Session
Where Are All My Inodes Being Used
Expression After Last Specific Character
How to Identify the Particular Linux Flavor via Command Line
Linux Bash: Setting Iptables Rules to Allow Both Active and Passive Ftp