Visit Our UNIX and Linux User Community

Read all files in a directory for a unix command

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Read all files in a directory for a unix command
# 1  
Old 06-27-2012
Read all files in a directory for a unix command


Below, I have a unix command, which can be executable for single file.
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:
Mod Comment 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 02:07 PM..
# 2  
Old 06-27-2012
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...
for FILE in ...
   sort -k3,3 $FILE >~newdirectory/$FILE

# 3  
Old 06-27-2012
Similarly as it turned out:

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


Previous Thread | Next Thread
Test Your Knowledge in Computers #431
Difficulty: Medium
JavaScript arrow functions were first introduced in 6th Edition - ECMAScript 2015.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How do i read only last 5 files records from a directory.?

Hello team, I have a directory containing huge number of files. I need to go select last 5 files and read it to do a calculation. Kindly guide, how can i achieve it. Regards, Sadique (13 Replies)
Discussion started by: sadique.manzar
13 Replies

2. Shell Programming and Scripting

How to read and append certain files form directory

Hi ,i have a question ,if I am in sertain directory and I have 4 files *.c how can I read and append this files in file backup.bac Thanks find ./ -name "*.csh" | wc -l (2 Replies)
Discussion started by: lio123
2 Replies

3. Shell Programming and Scripting

Apply 'awk' to all files in a directory or individual files from a command line

Hi All, I am using the awk command to replace ',' by '\t' (tabs) in a csv file. I would like to apply this to all .csv files in a directory and create .txt files with the tabs. How would I do this in a script? I have the following script called "csvtabs": awk 'BEGIN { FS... (4 Replies)
Discussion started by: ScKaSx
4 Replies

4. UNIX for Dummies Questions & Answers

Read directory files and count number of lines

Hello, I'm trying to create a BASH file that can read all the files in my working directory and tell me how many words and lines are in that file. I wrote the following code: FILES="*" for f in "$FILES" do echo -e `wc -l -w $f` done My issue is that my file is outputting in one... (4 Replies)
Discussion started by: jl487
4 Replies

5. Shell Programming and Scripting

read files from directory

hi i have a directory /tmp/Satya,it contains 5.FILE 6.FILE 7.FILE i need to read each file , and read its content line by line please help thanks Satya (2 Replies)
Discussion started by: Satyak
2 Replies

6. UNIX for Dummies Questions & Answers

need help on how to read command in unix

can any one please help me how to use "read" command for openig a file.i want to process the file record by record and then needs to search for a particular string at a particular position.can any one please let me know the code.i am new to the shell. below are sample sample records ... (3 Replies)
Discussion started by: raoscb
3 Replies

7. UNIX for Dummies Questions & Answers

Read Files from a Directory

Hi, I have a requirement where I have get the files from FTP server then delete the files in the FTP server and then store the file name within the file where there is "TR". i.e Filename|TR|20071231|.... Once I finish loading my file I have to archive the files. Is there any way to do it... (2 Replies)
Discussion started by: kiran_418
2 Replies

8. Shell Programming and Scripting

unix command/s to find files older than 2 hours in a directory

I need to write a script to find files older than 2 hours in set of direcotries and list them ina mail. I know find command ti list files greater/lesser than days but i need to do it for hours. Any input. (6 Replies)
Discussion started by: Presanna
6 Replies

9. Shell Programming and Scripting

Read from fileList.txt, copy files from directory tree

Hi, hopefully this is a fairly simple Q&A. I have a clean file list of approximately 180 filenames with no directory or slashes in front of the filename nor any extension or dot ".". I would like to read from this list, find these files recursively down through directory trees, copy the files... (1 Reply)
Discussion started by: fxvisions
1 Replies

10. UNIX for Dummies Questions & Answers

moving files from a unix directory to a windows directory

Any body any ideas i'm failry new to this so any help would be appreciated. Cheers Steve (2 Replies)
Discussion started by: gleads
2 Replies

Featured Tech Videos