LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-28-2005, 11:30 PM   #1
shinni
LQ Newbie
 
Registered: Jan 2005
Posts: 5

Rep: Reputation: 0
Bash script to access all files in a directory


Hi .. I am a new bie in writing scripts, so this doubt may be a simple one.

I am trying to apply a parsing program to a set of input files in a directoy.
How can i access all the files from a source directory , one by one and to write the
output files with another name to some directory ? i.e
my exe file is a.out
I have input files 1.txt,2.txt .. in one directory.
I need this cmd line ./a.out <inputfilename> > <outputfilename> , on all input files.

can i write a loop to get all files like this ? right now i am writing the same line
again and again to apply to all files..

Plz post ur suggestions.

Thank u

Shinni
 
Old 01-29-2005, 12:00 AM   #2
hk_linux
Member
 
Registered: Nov 2004
Location: India
Distribution: RedHat, PCQLinux, Fedora
Posts: 95

Rep: Reputation: 15
Hi,
You can use a loop as follows. The problem is with naming the output file. I have made the output filename as inpufilename.out

#!/bin/bash
cd <input_file_directory>
for file in `dir -d *` ; do
<exeFile with full path> "$file" <output_file_path/"$file".out>
done

HTH.
 
Old 01-31-2005, 05:06 AM   #3
shinni
LQ Newbie
 
Registered: Jan 2005
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks a lot .. It is working..

I have another doubt , as a follow up to the same script .. If i need to change this input directory and output directory to test different data sets, what should i do ? Now, i hav specified SourceDir and DestDir within the program. Can i pass command line arguements or do something similar to assign some path to variables representing the directories , each time when i run the script?

Thank u

Shinni
 
Old 01-31-2005, 06:19 AM   #4
hk_linux
Member
 
Registered: Nov 2004
Location: India
Distribution: RedHat, PCQLinux, Fedora
Posts: 95

Rep: Reputation: 15
Pass the input and output path as argument to the script.

#!/bin/bash
input_file-directory=$1
output_file_path=$2
cd <input_file_directory>

for file in `dir -d *` ; do
<exeFile with full path> "$file" <output_file_path/"$file".out>
done

I also recomend you to go through any shell script tutorials. I find one by "Mendel cooper" to be interesting.

HTH.
 
Old 01-31-2005, 07:41 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,288

Rep: Reputation: 174Reputation: 174
This could be a job for make.
You don't want to regenerate every time if the
data hasn't changed. (do you?)


Code:
# this will do COMMAND
# on a list on all .txt files in
# the directory outputing to a .lis
# file
#
# it genrates the list of txt files so you
# don't have to worry about them

include depend.mk

COMMAND = echo
COMMAND = cp


# change the suffixes if you don't like
# .txt and .lis
# ==============

.SUFFIXES: .txt .lis
OUTFILES = $(DATA:txt=lis)

.txt.lis:
	$(COMMAND) $< $@


all:$(OUTFILES)
	rm depend.mk

# automagically generate list of txt files
# ========================================
depend.mk:
	@echo DATA = *.txt > $@
the indents are TABs.
copy to file called Makefile
then type make.

or maybe not?

Last edited by bigearsbilly; 01-31-2005 at 07:47 AM.
 
Old 04-24-2009, 03:46 PM   #6
louisgag
LQ Newbie
 
Registered: Feb 2005
Posts: 15

Rep: Reputation: 2
Quote:
Originally Posted by hk_linux View Post
Hi,
You can use a loop as follows. The problem is with naming the output file. I have made the output filename as inpufilename.out

#!/bin/bash
cd <input_file_directory>
for file in `dir -d *` ; do
<exeFile with full path> "$file" <output_file_path/"$file".out>
done

HTH.
thanks. works perfectly
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Bash, Find script files in a directory or subdirectories within... ray5_83 Programming 4 10-10-2008 07:42 PM
cd keeps snapping back to current directory when called from bash script ghrellin Linux - General 5 08-28-2005 12:50 PM
Bash, how do I test for *.txt files in a directory with IF? severian23 Programming 7 11-04-2004 09:47 AM
triggering bash script if directory modified cbb Linux - Software 1 10-23-2004 07:52 AM
Specifying target directory for command in bash shell script? spectrescape Programming 1 07-22-2004 05:37 PM


All times are GMT -5. The time now is 01:32 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration