Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-27-2012
Registered User
 
Join Date: Oct 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Read all files in a directory for a unix command

Hello,

Below, I have a unix command, which can be executable for single file.

Code:
cat input.txt | sort -k3,3 > output.txt

I have 100 input files in a directory. It is hectic and time taking to run the above command for all the 100 files for 100 times.

Now, I want to execute the above unix command to all the files in a directory. And the output should be written in an output directory with same input file name.

Please help me in this.

Thanks in advance.


Moderator's Comments:
Please view this code tag video for how to use code tags when posting code and data.

Last edited by vbe; 06-27-2012 at 12:07 PM..
Sponsored Links
    #2  
Old 06-27-2012
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Location: Switzerland - GE
Posts: 4,636
Thanks: 118
Thanked 257 Times in 246 Posts
Why use cat ?
Doesnt sort -k3,3 input.txt > output.txt work?

For your directory you will need to use a loop and since your output is in another directory you can keep the same name as the one used as variable in the loop...

Code:
for FILE in ...
do
   sort -k3,3 $FILE >~newdirectory/$FILE
.
.
done

Sponsored Links
    #3  
Old 06-27-2012
methyl methyl is offline Forum Staff  
Moderator
 
Join Date: Mar 2008
Posts: 6,388
Thanks: 286
Thanked 668 Times in 640 Posts
Similarly as it turned out:


Code:
ls -1d * | while read filename
do
       if [ -f "${filename}" ]
       then
            sort -k3,3 "${filename}" > "/your_path/your_directory/${filename}"
       fi
done

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to read and append certain files form directory lio123 Shell Programming and Scripting 2 02-09-2011 01:33 PM
Using Awk within awk to read all files in directory flevongo UNIX for Dummies Questions & Answers 6 09-19-2009 04:47 PM
read files from directory Satyak Shell Programming and Scripting 2 10-17-2008 05:20 AM
Read Files from a Directory kiran_418 UNIX for Dummies Questions & Answers 2 04-18-2008 01:23 PM
unix command/s to find files older than 2 hours in a directory Presanna Shell Programming and Scripting 6 11-20-2007 08:22 AM



All times are GMT -4. The time now is 05:03 PM.