find specific file names and execute a command depending on file's name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find specific file names and execute a command depending on file's name
# 1  
Old 02-09-2012
find specific file names and execute a command depending on file's name

Hi,

As a newbie, I'm desperate ro make my shell script work. I'd like a script which checks all the files in a directory, check the file name, if the file name ends with "extracted", store it in a variable, if it has a suffix of ".roi" stores in another variable. I'm going to use these two variables in a new command that needs them. So I came up with:
Code:
e#!/bin/bash

for file in path/*
if [$file == *_extracted]
then
extracted_file =$file
else
if [$file== *.roi]
then 
roi_file=$file
fi
fi

done;

what am doing wrong? Any input is gratefully appreciated

Last edited by Franklin52; 02-10-2012 at 04:05 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 02-09-2012
Hello, welcome to the forum.

How about:
Code:
#!/bin/bash

for extracted_file in /path-to-dir/*extracted
do
  echo "do stuff with $extracted_file"
done

for roi_file in /path-to-dir/*.roi
do
  echo "do stuff with $roi_file"
done

*Edit*
Your version needs ksh93/bash style double straight brackets and proper spacing inside for pattern matching to work:
Code:
if [[ $file == *_extracted ]]

The same can be done with a more universal case statement inside the loop:
Code:
case $file in
  *_extracted) 
      extracted_file=$file ;;
  *.roi)
      roi_file=$file;;
esac

plus you need do..done for the for loop...
There can be no space before or after an assignment operator (=)

If you post code on this forum it would be good if you could use code tags ([code]...[/code]) and properly indent your code..

Last edited by Scrutinizer; 02-09-2012 at 04:36 PM..
# 3  
Old 02-09-2012
Hi,

Thanks for the suggestion. but the problem is that I woud need both of these two files, at the same time in an in-house command that needs two inputs like:

command file1 file2

that's why I have gone for if. The case in works fine. Thank you very much
# 4  
Old 02-09-2012
Code:
#!/bin/bash

for file in path/*_extracted
do
  extracted_file=$file
done

for file in path/*_roi
do
  roi_file=$file
done

if [ -n $extracted_file -a -n $roi_file ]
then
  yourcommand $extracted_file $roi_file
fi

In fact you do not need for loop if there is only 1 extracted file and 1 roi file
Code:
yourcommand path/*_extracted path/*_roi


Last edited by chihung; 02-09-2012 at 09:40 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

List all file names that contain two specific words. ( follow up )

Being new to the forum, I tried finding a solution to find files containing 2 words not necessarily on the same line. This thread "List all file names that contain two specific words." answered it in part, but I was looking for a more concise solution. Here's a one-line suggestion... (8 Replies)
Discussion started by: Symbo53
8 Replies

2. Shell Programming and Scripting

Find matching file in bash with variable file names but consisent prefixs

As part of a bash the below line strips off a numerical prefix from directory 1 to search for in directory 2. for file in /home/cmccabe/Desktop/comparison/missing/*.txt do file1=${file##*/} # Strip off directory getprefix=${file1%%_*.txt} ... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. Shell Programming and Scripting

Remove all files with specific file names in directory

If I have 5 files in a directory, what is the best way to remove specific files in it? For example, snps.ivg probes.ivg Desired output probes.ivg probes.txt all.txt Basically, removing those files with "snp" in the filename regardless of extension. Thank you :). (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

How to find complete file names in UNIX if i know only extention of file

Suppose I have a file which contains other file names with some extention . text file containt gdsds sd8ef g/f/temp_temp.sum yyeta t/unix.sum ghfp hrwer h/y/test.text.dat if then.... I want to get the complete file names, like for above file I should get output as temp_temp.sum... (4 Replies)
Discussion started by: panchal
4 Replies

5. Shell Programming and Scripting

How to split a data file into separate files with the file names depending upon a column's value?

Hi, I have a data file xyz.dat similar to the one given below, 2345|98|809||x|969|0 2345|98|809||y|0|537 2345|97|809||x|544|0 2345|97|809||y|0|651 9685|98|809||x|321|0 9685|98|809||y|0|357 9685|98|709||x|687|0 9685|98|709||y|0|234 2315|98|809||x|564|0 2315|98|809||y|0|537... (2 Replies)
Discussion started by: nithins007
2 Replies

6. Shell Programming and Scripting

KSH Script to Execute command from specific column in file

Hi Unix Experts,Team I have a file (say comand_file.prm), The file has a command specified in column 6 (say "./execute_script.sh"). I want to execute this command in my script. I am writing a KSH script to achieve this. Could you please assist me with this. (6 Replies)
Discussion started by: Jeevanm
6 Replies

7. Shell Programming and Scripting

List all file names that contain two specific words.

Hi, all: I would like to search all files under "./" and its subfolders recursively to find out those files contain both word "A" and word "B", and list the filenames finally. How to realize that? Cheers JIA (18 Replies)
Discussion started by: jiapei100
18 Replies

8. Shell Programming and Scripting

Execute commands to specific folder from input file

Hi, I have one text file input.txt, which has folders path as follows: /home/user/automate/abc /home/user/automate/abc/xyz /home/user/automate/test /home/user/automate/test2 /home/user/automate/test2/abc/Main In those folders i have .svn folder. 1) First i want to remove .svn like rm... (5 Replies)
Discussion started by: dragon.1431
5 Replies

9. AIX

find for specific content in file in the directory and list only file names

Hi, I am trying to find the content of file using grep and find command and list only the file names but i am getting entire file list of files in the directory find . -exec grep "test" {} \; -ls Can anyone of you correct this (2 Replies)
Discussion started by: madhu_Jagarapu
2 Replies

10. UNIX for Dummies Questions & Answers

find command not giving file names accord. to timestamps

Currently iam working on solaris environment, Iam using find command to get list of all files between any two given dates. But the find command is not listing files accord. to timestamp. I tried using -exec option as -exec ls -ltr {} \; Still the files are not listed according to timestamp..... (8 Replies)
Discussion started by: thanuman
8 Replies
Login or Register to Ask a Question