find command to find files and concatenate them
Use cat
in -exec
and redirect output of find
:
find /home/downloaded/ -type f -name '*.gz' -exec cat {} \; > output
Command to find all files in a directory and concat them for a parameter?
It sounds like you want ls
and xargs
.
Bash: find and concatenate files
find /home/DIR* -name 'file*csv' |xargs cat > output.csv
find /home/DIR* -name '*csv'
gives you the files absolute paths.
xargs cat
will iterate the files and cat
print the files content
Bash: find and concatenate filenames with two digits
You are trying to use regular expression syntax where you need to use a glob.
You just need to specify the range twice, rather than using {2}
:
find "$PWD"/RUN[0-9][0-9] -name '*csv' |xargs cat > big_cat_file.csv
(Note that [!0-9]
matches any single character except a digit.)
To accommodate any legal filename that might match *csv
, you should use the -exec
primary instead of xargs
. (Consider what would happen if a file name contains whitespace, or in the worst case, a newline.)
find "$PWD"/RUN[0-9][0-9] -name '*csv' -exec cat {} + > big_cat_file.csv
This not only works with any valid file name, but minimizes the number of calls to cat
that are required.
How do I concatenate files in a subdirectory with Unix find execute and cat into a single file?
find's -exec argument runs the command you specify once for each file it finds. Try:
$ find . -type f -exec cat {} \; > out.txt
or:
$ find . -type f | xargs cat > out.txt
xargs converts its standard input into command-line arguments for the command you specify. If you're worried about embedded spaces in filenames, try:
$ find . -type f -print0 | xargs -0 cat > out.txt
Concatenate two text files using command line (windows) in a Batch script
Mmm... As I understand it, you want to append each AEDAT_...
file to the end of the corresponding ADAT_...
one (you are not clear about what "merge two files" means). If so, this is a solution:
@echo off
for /F "tokens=1* delims=_" %%a in ('dir /B AEDAT_*.txt') do (
(echo/
type "%%a_%%b") >> "ADAT_%%b"
del "%%a_%%b"
)
bash: use list of file names to concatenate matching files across directories and save all files in new directory
You can do it like this:
mkdir -p new_dir
for f in path/to/dir*/*.txt; do
cat "$f" >> "new_dir/${f##*/}"
done
This is a common use for substring removal with parameter expansion, in order to use only the basename of the file to construct the output filename.
Or you can use a find
command to get the files and execute the command for each one:
find path/to/dir* -type f -name '*.txt' -print0 |\
xargs -0 -n1 sh -c 'cat "$0" >> new_dir/"${0##*/}"'
In the above command, the filenames out of find
are preserved with zero separation (-print0
), and xargs
also accepts a zero separated list (-0
). For each argument (-n1
) the command following is executed. We call sh -c 'command'
for convenience to use the substring removal inside there, we can access the argument provided by xargs
as $0
.
Related Topics
Bash Script to Run a Constant Number of Jobs in the Background
How to Run Command During Docker Build Which Requires a Tty
Objdump and Resolving Linkage of Local Function Calls
How to Extract One Column from Multiple Files, and Paste Those Columns into One File
Bash Scripting - Iterating Through "Variable" Variable Names for a List of Associative Arrays
How to Find Out Where Is My Code Causing Glib-Gobject-Critical
Linux Kernel Headers' Organization
How to Install a Node.Js Server at Chat.Mydomain.Com on a Hostgator Vps Hosting
Font Are Not Displayed in Idea
Where Is the 'Sdk' Command Installed for Sdkman
Curl: (2) Failed Initialization
Top Command First Iteration Always Returns the Same Result
Get Last Parameter on Shell Script
How to Kill a Process on No Output for Some Period of Time