I came across a simple one line command allowing take files in a folder one by one and pipe them into another command, but I cannot find this thread. Can someone help? Say, decompress hundreds of gziped files in a folder after downloading a database, or pipe to another process.
Unconfigured Ad
Collapse
X
-
Let's see if this puts you in the right direction...Originally posted by yaximik View PostI came across a simple one line command allowing take files in a folder one by one and pipe them into another command, but I cannot find this thread. Can someone help? Say, decompress hundreds of gziped files in a folder after downloading a database, or pipe to another process.
To uncompress all the gzip files in the current directory this may suffice:
To do something with each gzipped file, e.g. print out the first 15 lines and write them to a file:Code:gunzip *.gz ## Use gunzip -r to descent in subdirectories
DarioCode:for gz in `find mydir/ -name '*.gz'` do gunzip -c $gz | head -n 15 > ${gz}.head done
-
-
This should do it: http://www.cyberciti.biz/faq/bash-loop-over-file/
There will be analogous commands for tcsh, if that is your favorite.
Comment
-
-
Yep.Originally posted by dariober View PostTo uncompress all the gzip files in the current directory this may suffice:
To do something with each gzipped file, e.g. print out the first 15 lines and write them to a file:Code:gunzip *.gz ## Use gunzip -r to descent in subdirectories
DarioCode:for gz in `find mydir/ -name '*.gz'` do gunzip -c $gz | head -n 15 > ${gz}.head done
And to get the results in the same file, you can do
Code:for gz in *.gz ; do less $gz | head -15 ; done > outfile
Comment
-
-
Thanks everyone. I realized I did not express the question clearly. Gzip is just one of applications, i rather asked about general looping over files:
The beauty of what I saw was that it fit just in one line and was pretty general, so it can be applied to other commands that do not take '*' for multiple files.Code:while/for/if [I]something is in the folder[/I] do [I]something to each file[/I] until [I]all are processed[/I] done
Comment
-
-
In fact, you can also do for loop one-liners with ";" or even with & to fork them to background
For example looping over Fasta files (*.fa) and counting the headers (i.e. number of sequences) could be done on one line like so:
If you want to parallel unzip a bunch of files you could fork the loop like so:Code:for f in *.fa; do grep -c ">"; done
This runs all gunzip calls in parallel and 'wait's for them to be finished (optional)Code:for f in *.gz; do gunzip ${f} & done; wait
I learned the hard way: While those one-liners are nice and quick for scripting on a terminal, never use them in longer bash-scripts because a) you will at some point have a typo and then will have a very hard time finding it and b) there is essentially no need in saving space/shortening out commands in a bash script.
Best
Simon
Comment
-
-
Thanks a lot, everyone who provided inputs. That was very educational in general as I get a better idea how I can use simple loops. I wonder, would it be useful to have something like WiKiBits (or a better name) as a collection of little ingenious solutions?
Comment
-
Latest Articles
Collapse
-
by SEQadmin2
Genomics studies in neuroscience face a special challenge due to the brain’s complexity and scarcity of samples. Mapping changes in cell type and state using conventional next-generation sequencing methods remains challenging. Advances in technologies like single-cell sequencing, spatial transcriptomics, and long-read sequencing have opened the door to deeper studies of the brain and diseases like Alzheimer’s, amyotrophic lateral sclerosis (ALS), and schizophrenia.
...-
Channel: Articles
07-09-2026, 11:10 AM -
-
by SEQadmin2
Cancer survival rates have significantly increased in the last few decades in the United States, reaching a combined 70% 5-year survival rate by 2021. Behind this number, there are years of research to find new therapies, drug targets, and early detection methods. But there is one core challenge that keeps slowing down these advances, and it’s about drug resistance.
There is no single reason why many patients don’t respond to treatment as expected. Cancer is...-
Channel: Articles
07-08-2026, 05:17 AM -
-
by GATTACATLove this - good data definitely starts from good input, and poor input can only give relatively poor data. I particularly like the mention of Nanodrop/absorbance based methods for quantification. It's such a toss up if you'll get an accurate reading or what amounts to a randomly generated number, and a lot of library/sequencing related issues can be traced back to poor quant.
-
Channel: Articles
07-01-2026, 11:43 AM -
ad_right_rmr
Collapse
News
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by SEQadmin2, 07-13-2026, 10:26 AM
|
0 responses
20 views
0 reactions
|
Last Post
by SEQadmin2
07-13-2026, 10:26 AM
|
||
|
Started by SEQadmin2, 07-09-2026, 10:04 AM
|
0 responses
30 views
0 reactions
|
Last Post
by SEQadmin2
07-09-2026, 10:04 AM
|
||
|
Started by SEQadmin2, 07-08-2026, 10:08 AM
|
0 responses
18 views
0 reactions
|
Last Post
by SEQadmin2
07-08-2026, 10:08 AM
|
||
|
Started by SEQadmin2, 07-07-2026, 11:05 AM
|
0 responses
34 views
0 reactions
|
Last Post
by SEQadmin2
07-07-2026, 11:05 AM
|
Comment