Insert empty line after the contents of a file in a new file - linux

This is a simple problem, I'm just stuck on it. I am taking the contents of a bunch of different files and printing each file's name as a header before its contents. That much works. But I want to have an empty line separating the the contents of one file and the header for the next file's content.
I want it to look like:
File 1 header
File 1 contents
[empty space]
File 2 header
File 2 contents
I tried putting \n after "{}" in my code, but that didn't work. Any suggestions?
find . -type f -name '*_top_hits.txt' -print -exec cat {} \; > combinedresults.txt

You can just an empty echo as part of the find -exec as
find . -type f -name "*_top_hits.txt" -print -exec sh -c "cat {};echo" \; > combinedresults.txt
The echo just produces a single empty new-line after each file content. Also you don't need multiple -exec options rather use a single sub-shell.

You can try adding a second -exec:
find . -type f -name '*_top_hits.txt' -print -exec cat {} \; -exec echo \; > combinedresults.txt
One side effect of this is that a new line will be added at the end after the contents of the last file.

Related

Linux deleteing a folder's content

I can find ./ -type d -name "Debug" -exec rm -f {} + to delete the fold and content.
My question is: How to fine all "Debug" folders and ONLY delete the content and Not the folder?
#!/bin/bash
find . -type d -name "Debug" -print | xargs -I% find % -maxdepth 1 -type f -delete
the first find lists all Debug directories
xargs defines the "%" symbol as what is replaced by the received stdin. You will often see -I{} for xargs, but I chose another string since you might need {} in the find. Any char will do.
the second find, in directory "%" (so here one of the Debug directories received from the first find), deletes all files under that directory.
-maxdepth 1 is used in the second find to make sure it only deletes the files in the current Debug directory, and does not recursively deletes all files.

Copy recursive files of all the subdirectories

I want to copy all the log files from a directory which does not contain log files, but it contains other subdirectories with log files. These subdirectories also contain other subdirectories, so I need something recursive.
I tried
cp -R *.log /destination
But it doesn't work because the first directory does not contains log files. The response can be also a loop in bash.
find /path/to/logdir -type f -name "*.log" |xargs -I {} cp {} /path/to/destinationdir
Explanation:
find searches recursively
-type f tells you to search for files
-name specifies the name pattern
xargs executes commands
-I {} indicates an argument substitution symbol
Another version without xargs:
find /path/to/logdir -type f -name '* .log' -exec cp '{}' /path/to/destinationdir \;

cat files in subdirectories using linux commands

I have the following directories:
P922_101
P922_102
.
.
Each directory, for instance P922_101 has following subdirectories:
140311_AH8MHGADXX 140401_AH8CU4ADXX
Each subdirectory, for instance 140311_AH8MHGADXX has the following files:
1_140311_AH8MH_P922_101_1.fastq.gz 1_140311_AH8MH_P922_101_2.fastq.gz
2_140311_AH8MH_P922_101_1.fastq.gz 2_140311_AH8MH_P922_101_2.fastq.gz
And files in 140401_AH8CU4ADXX are:
1_140401_AH8CU_P922_101_1.fastq.gz 1_140401_AH8CU_P922_4001_2.fastq.gz
2_140401_AH8CU_P922_101_1.fastq.gz 2_140401_AH8CU_P922_4001_2.fastq.gz
I want to do 'cat' for the files in the subdirectories in the following way:
cat 1_140311_AH8MH_P922_101_1.fastq.gz 2_140311_AH8MH_P922_101_1.fastq.gz
1_140401_AH8CU_P922_101_1.fastq.gz 2_140401_AH8CU_P922_101_1.fastq.gz > P922_101_1.fastq.gz
which means that files ending with _1.fastq.gz should be concatenated into a single file and files ending with _2.fatsq.gz into another file.
It should be run for all files in subdirectories in all directories. Could someone give a linux solution to do this?
Since they're compressed, you should probably use gzip -dc (decompress and write to stdout) -
find /somePath -type f -name "*.fastq.gz" -exec gzip -dc {} \; | \
tee -a /someOutFolder/out.txt
You can use find for this:
find /top/path -mindepth 2 -type f -name "*_1.fastq.gz" -exec cat {} \; > one_file
find /top/path -mindepth 2 -type f -name "*_2.fastq.gz" -exec cat {} \; > another_file
This will look for all the files starting from /top/path and having a name matching the pattern _1.fastq.gz / _2.fastq.gz and cat them into the desired file. -mindepth 2 makes find look for files that are at least under the current directory; this way, files in /top/path won't be matched.
Note that you will probably need zcat instead of cat, for gz files.
As you keep adding details in comments, let's see what else we can do:
Say you have the list of directories in a file directories_list, each line containing one:
while read directory
do
find $directory -mindepth 2 -type f -name "*_1.fastq.gz" -exec cat {} \; > $directory/output
done < directories_list

How to find empty files and edit the header of them. by find and echo

I have a folder contains ~30000 files and some of them are empty. I want to find them and put 'NON' as a header of the empty files.
my script is:
find -type f -empty -exec echo 'NON' {} \;
my output is:
NON ./file1
NON ./file2
NON ./file3
NON ./file4
but I want the 'NON' to write as a header of the file1 , file2 , file3 and file4.
Thanks in advance.
Why not just echo NON first and then do your find, with no need for exec?
Also I am not sure how you are running find without specifying a directory to search, so I have added the current directory to search below.
echo 'NON'
find . -type f -empty
The other possible interpretation of your question is that you want to add the string NON to all zero-length files. You can do that like this.
find . -type f -empty -exec sh -c "echo NON > {}" \;

In Unix,cmd to search a file recursively and retrieve the file instead of just the path of the file

In Unix, what is the single cmd that lets me search and locate a file recursively and then retrieve the file instead of just the path of the file?
What do you mean by retrieve?
You can simply use -exec argument to find.
$ find /path/to/search -type f -name '*.txt' -exec cat {} \;
$ find /path/to/search -type f -name 'pattern' -exec cp {} /path/to/new \;
The second one should work.
cat `find /wherever/you/want/to/start/from -name name_of_file`
Note those quotes are backquotes (`).

Resources