Unconfigured Ad

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • yaximik
    Senior Member
    • Apr 2011
    • 199

    question to scripting gurus

    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.
  • dariober
    Senior Member
    • May 2010
    • 311

    #2
    Originally posted by yaximik View Post
    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.
    Let's see if this puts you in the right direction...

    To uncompress all the gzip files in the current directory this may suffice:
    Code:
    gunzip *.gz ## Use gunzip -r to descent in subdirectories
    To do something with each gzipped file, e.g. print out the first 15 lines and write them to a file:

    Code:
    for gz in `find mydir/ -name '*.gz'`    
    do
    gunzip -c $gz | head -n 15 > ${gz}.head
    done
    Dario

    Comment

    • GenoMax
      Senior Member
      • Feb 2008
      • 7142

      #3
      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

      • sisch
        Member
        • Jun 2011
        • 29

        #4
        My first impression was, You are talking about the 'find' command.
        The find command has much more functionality than a simple bash loop. However, for simplicity I use bash loops more often.

        Here is the manpage of find

        Best,
        Simon

        Comment

        • EGrassi
          Member
          • Oct 2010
          • 66

          #5
          or ls | xargs ...

          Comment

          • syfo
            Just a member
            • Nov 2012
            • 103

            #6
            Originally posted by dariober View Post
            To uncompress all the gzip files in the current directory this may suffice:
            Code:
            gunzip *.gz ## Use gunzip -r to descent in subdirectories
            To do something with each gzipped file, e.g. print out the first 15 lines and write them to a file:

            Code:
            for gz in `find mydir/ -name '*.gz'`    
            do
            gunzip -c $gz | head -n 15 > ${gz}.head
            done
            Dario
            Yep.
            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

            • yaximik
              Senior Member
              • Apr 2011
              • 199

              #7
              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:

              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
              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.

              Comment

              • sisch
                Member
                • Jun 2011
                • 29

                #8
                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:

                Code:
                for f in *.fa; do grep -c ">"; done
                If you want to parallel unzip a bunch of files you could fork the loop like so:
                Code:
                for f in *.gz; do gunzip ${f} & done; wait
                This runs all gunzip calls in parallel and 'wait's for them to be finished (optional)

                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
                Last edited by sisch; 01-17-2013, 01:14 AM. Reason: for loop code corrected

                Comment

                • mbayer
                  Member
                  • Mar 2009
                  • 31

                  #9
                  Hi yaximik,

                  I use this in my bash scripts:

                  Code:
                  #extract all the filenames to an array
                  files=`ls -l *.gz | awk '{ print $9 }'`
                  
                  #iterate over these
                  for i in $files
                  do
                  	echo "gunzipping file $i"
                  	gunzip $i 
                  done
                  cheers

                  Micha

                  Comment

                  • yaximik
                    Senior Member
                    • Apr 2011
                    • 199

                    #10
                    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

                    • GATTACAT
                      Reply to Nine Things a Sample Prep Scientist Thinks About Before Sequencing
                      by GATTACAT
                      Love 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.
                      07-01-2026, 11:43 AM
                    • SEQadmin2
                      Nine Things a Sample Prep Scientist Thinks About Before Sequencing
                      by SEQadmin2


                      I’m not a sequencing expert. I’m a purification scientist who uses NGS to evaluate workflows my group develops. With this perspective, we think about the sample first and the NGS workflow second. The sequencer is an exceptionally honest reporter, but it can only report on what you give it, so whether you get clean, interpretable data from an NGS workflow is largely determined before you begin.

                      Here are nine questions we think about, in roughly the order they matter, before...
                      06-18-2026, 07:11 AM

                    ad_right_rmr

                    Collapse

                    News

                    Collapse

                    Topics Statistics Last Post
                    Started by SEQadmin2, Today, 11:05 AM
                    0 responses
                    6 views
                    0 reactions
                    Last Post SEQadmin2  
                    Started by SEQadmin2, 07-02-2026, 11:08 AM
                    0 responses
                    27 views
                    0 reactions
                    Last Post SEQadmin2  
                    Started by SEQadmin2, 06-30-2026, 05:37 AM
                    0 responses
                    25 views
                    0 reactions
                    Last Post SEQadmin2  
                    Started by SEQadmin2, 06-26-2026, 11:10 AM
                    0 responses
                    25 views
                    0 reactions
                    Last Post SEQadmin2  
                    Working...