Bash: loop on files in folder

If you have a lot of files to process via bash in a folder (I had to delete the first row from 432000 txt files into a folder) and you digit the basic command:

[email protected] documents $ rm *.txt

you will obtain an error like this:

[email protected] documents $ rm *.txt
-bash: /usr/bin/rm: Argument list too long

Thus, the only chance to remove your txt files is delete the files one by one…BUT NOT BY HAND!!!

You can simply create a for-loop on each file and process it, like in the example below:

[email protected] documents $ for i in *.txt
> do
> tail -n +2 < $i > cut/$i
> done

where I read every txt file and I store it in the variable $i, that I use in tail command to read (<$i) and save the cutted file into a different directory (>cut/$i).

About Francesco Serafin

I am a PhD student at the Department of Civil, Environmental and Mechanical Engineering at the University of Trento. My two greatest loves: Computer Science and Water (take three with my Lenovo!:D). As a result, the aim of my life is to make both passions coexist. My gpg public key available at https://pgp.mit.edu

Leave a Reply

Your email address will not be published.

*