Bash: Picking up filenames then moving to next directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash: Picking up filenames then moving to next directory
# 1  
Old 02-13-2018
Bash: Picking up filenames then moving to next directory

I have a collection of directories, for example as below

I want to create a loop that goes in the first directory and picks up the *hhz*.sac.pzs
filename in a variable, and the other files matching *hhz*.sac in another variable (however I do not want to pick the *hhz*.sac.pzs). This is because I want to pass the file names to another program called sac.

Then I do my file processing in sac, then move to the next disectory and do the same things so I get the file name in the new directory.

Code:
.
├── epoz
│   ├── 2018.30.0.16.EPOZ.LHZ.SAC
│   ├── iv.epoz..hhe_2013.074.14.40.46.0000_2599.365.23.59.59.99999.sac.pzs
│   ├── iv.epoz..hhe.d.2018.028.000000.sac
│   ├── iv.epoz..hhe.d.2018.029.000000.sac
│   ├── iv.epoz..hhe.d.2018.030.000000.sac
│   ├── iv.epoz..hhe.d.2018.031.000000.sac
│   ├── iv.epoz..hhn_2013.074.14.40.46.0000_2599.365.23.59.59.99999.sac.pzs
│   ├── iv.epoz..hhn.d.2018.028.000000.sac
│   ├── iv.epoz..hhn.d.2018.029.000000.sac
│   ├── iv.epoz..hhn.d.2018.030.000000.sac
│   ├── iv.epoz..hhn.d.2018.031.000000.sac
│   ├── iv.epoz..hhz_2013.074.14.40.46.0000_2599.365.23.59.59.99999.sac.pzs
│   ├── iv.epoz..hhz.d.2018.028.000000.sac
│   ├── iv.epoz..hhz.d.2018.029.000000.sac
│   ├── iv.epoz..hhz.d.2018.030.000000.sac
│   ├── iv.epoz..hhz.d.2018.031.000000.sac
│   ├── test2.m
│   ├── test2.m~
│   ├── test.m
│   └── test.m~
├── haga
│   ├── iv.haga..hhe_2006.111.00.00.00.0000_2599.365.23.59.59.99999.sac.pzs
│   ├── iv.haga..hhe.d.2018.028.000000.sac
│   ├── iv.haga..hhe.d.2018.029.000000.sac
│   ├── iv.haga..hhe.d.2018.030.000000.sac
│   ├── iv.haga..hhe.d.2018.031.000000.sac
│   ├── iv.haga..hhn_2006.111.00.00.00.0000_2599.365.23.59.59.99999.sac.pzs
│   ├── iv.haga..hhn.d.2018.028.000000.sac
│   ├── iv.haga..hhn.d.2018.029.000000.sac
│   ├── iv.haga..hhn.d.2018.030.000000.sac
│   ├── iv.haga..hhn.d.2018.031.000000.sac
│   ├── iv.haga..hhz_2006.111.00.00.00.0000_2599.365.23.59.59.99999.sac.pzs
│   ├── iv.haga..hhz.d.2018.028.000000.sac
│   ├── iv.haga..hhz.d.2018.028.000000.sac.vel
│   ├── iv.haga..hhz.d.2018.029.000000.sac
│   ├── iv.haga..hhz.d.2018.029.000000.sac.vel
│   ├── iv.haga..hhz.d.2018.030.000000.sac
│   ├── iv.haga..hhz.d.2018.030.000000.sac.vel
│   ├── iv.haga..hhz.d.2018.031.000000.sac
│   └── iv.haga..hhz.d.2018.031.000000.sac.vel
├── hpac
│   ├── iv.hpac..hhe_2012.326.14.06.31.0000_2599.365.23.59.59.99999.sac.pzs
│   ├── iv.hpac..hhe.d.2018.028.000000.sac
│   ├── iv.hpac..hhe.d.2018.029.000000.sac
│   ├── iv.hpac..hhe.d.2018.030.000000.sac
│   ├── iv.hpac..hhe.d.2018.031.000000.sac
│   ├── iv.hpac..hhn_2012.326.14.06.31.0000_2599.365.23.59.59.99999.sac.pzs
│   ├── iv.hpac..hhn.d.2018.028.000000.sac
│   ├── iv.hpac..hhn.d.2018.029.000000.sac
│   ├── iv.hpac..hhn.d.2018.030.000000.sac
│   ├── iv.hpac..hhn.d.2018.031.000000.sac
│   ├── iv.hpac..hhz_2012.326.14.06.31.0000_2599.365.23.59.59.99999.sac.pzs
│   ├── iv.hpac..hhz.d.2018.028.000000.sac
│   ├── iv.hpac..hhz.d.2018.029.000000.sac
│   ├── iv.hpac..hhz.d.2018.030.000000.sac
│   └── iv.hpac..hhz.d.2018.031.000000.sac
├── hvzn
│   ├── iv.hvzn..hhe_2013.024.12.00.00.0000_2599.365.23.59.59.99999.sac.pzs
│   ├── iv.hvzn..hhe.d.2018.028.000000.sac
│   ├── iv.hvzn..hhe.d.2018.029.000000.sac
│   ├── iv.hvzn..hhe.d.2018.030.000000.sac
│   ├── iv.hvzn..hhe.d.2018.031.000000.sac
│   ├── iv.hvzn..hhn_2013.024.12.00.00.0000_2599.365.23.59.59.99999.sac.pzs
│   ├── iv.hvzn..hhn.d.2018.028.000000.sac
│   ├── iv.hvzn..hhn.d.2018.029.000000.sac
│   ├── iv.hvzn..hhn.d.2018.030.000000.sac
│   ├── iv.hvzn..hhn.d.2018.031.000000.sac
│   └── iv.hvzn..hhz_2013.024.12.00.00.0000_2599.365.23.59.59.99999.sac.pzs
├── mfnl
│   ├── iv.mfnl..hhe_2017.299.00.00.00.0000_2599.365.23.59.59.99999.sac.pzs
│   ├── iv.mfnl..hhn_2017.299.00.00.00.0000_2599.365.23.59.59.99999.sac.pzs
│   ├── iv.mfnl..hhn.d.2018.027.235959.sac
│   ├── iv.mfnl..hhn.d.2018.028.002538.sac
│   ├── iv.mfnl..hhn.d.2018.029.002707.sac
│   ├── iv.mfnl..hhn.d.2018.030.001756.sac
│   ├── iv.mfnl..hhn.d.2018.030.001928.sac
│   ├── iv.mfnl..hhn.d.2018.030.002323.sac
│   ├── iv.mfnl..hhn.d.2018.031.001312.sac
│   ├── iv.mfnl..hhn.d.2018.031.001946.sac
│   ├── iv.mfnl..hhz_2017.299.00.00.00.0000_2599.365.23.59.59.99999.sac.pzs
│   ├── iv.mfnl..hhz.d.2018.027.235959.sac
│   ├── iv.mfnl..hhz.d.2018.028.002538.sac
│   ├── iv.mfnl..hhz.d.2018.029.002707.sac
│   ├── iv.mfnl..hhz.d.2018.030.001756.sac
│   ├── iv.mfnl..hhz.d.2018.030.001928.sac
│   ├── iv.mfnl..hhz.d.2018.030.002323.sac
│   ├── iv.mfnl..hhz.d.2018.031.001312.sac
│   └── iv.mfnl..hhz.d.2018.031.001946.sac
├── msfr
│   ├── iv.msfr..hhe_2011.033.00.00.00.0000_2599.365.23.59.59.99999.sac.pzs
│   ├── iv.msfr..hhe.d.2018.028.000000.sac
│   ├── iv.msfr..hhe.d.2018.029.000000.sac
│   ├── iv.msfr..hhe.d.2018.030.000000.sac
│   ├── iv.msfr..hhe.d.2018.031.000000.sac
│   ├── iv.msfr..hhn_2011.033.00.00.00.0000_2599.365.23.59.59.99999.sac.pzs
│   ├── iv.msfr..hhn.d.2018.028.000000.sac
│   ├── iv.msfr..hhn.d.2018.029.000000.sac
│   ├── iv.msfr..hhn.d.2018.030.000000.sac
│   ├── iv.msfr..hhn.d.2018.031.000000.sac
│   ├── iv.msfr..hhz_2011.033.00.00.00.0000_2599.365.23.59.59.99999.sac.pzs
│   ├── iv.msfr..hhz.d.2018.028.000000.sac
│   ├── iv.msfr..hhz.d.2018.029.000000.sac
│   ├── iv.msfr..hhz.d.2018.030.000000.sac
│   └── iv.msfr..hhz.d.2018.031.000000.sac
├── msru
│   ├── iv.msru..hhe_2004.194.10.00.00.0000_2599.365.23.59.59.99999.sac.pzs
│   ├── iv.msru..hhe.d.2018.028.000000.sac
│   ├── iv.msru..hhe.d.2018.029.000000.sac
│   ├── iv.msru..hhe.d.2018.030.000000.sac
│   ├── iv.msru..hhe.d.2018.031.000000.sac
│   ├── iv.msru..hhn_2004.194.10.00.00.0000_2599.365.23.59.59.99999.sac.pzs
│   ├── iv.msru..hhn.d.2018.028.000000.sac
│   ├── iv.msru..hhn.d.2018.029.000000.sac
│   ├── iv.msru..hhn.d.2018.030.000000.sac
│   ├── iv.msru..hhn.d.2018.031.000000.sac
│   ├── iv.msru..hhz_2004.194.10.00.00.0000_2599.365.23.59.59.99999.sac.pzs
│   ├── iv.msru..hhz.d.2018.028.000000.sac
│   ├── iv.msru..hhz.d.2018.028.000000.sac.vel
│   ├── iv.msru..hhz.d.2018.029.000000.sac
│   ├── iv.msru..hhz.d.2018.029.000000.sac.vel
│   ├── iv.msru..hhz.d.2018.030.000000.sac
│   ├── iv.msru..hhz.d.2018.030.000000.sac.vel
│   ├── iv.msru..hhz.d.2018.031.000000.sac
│   └── iv.msru..hhz.d.2018.031.000000.sac.vel
├── nov
│   ├── iv.nov..hhe_2009.345.15.00.00.0000_2599.365.23.59.59.99999.sac.pzs
│   ├── iv.nov..hhe.d.2018.028.000000.sac
│   ├── iv.nov..hhe.d.2018.029.000000.sac
│   ├── iv.nov..hhe.d.2018.030.000000.sac
│   ├── iv.nov..hhe.d.2018.031.000000.sac
│   ├── iv.nov..hhn_2009.345.15.00.00.0000_2599.365.23.59.59.99999.sac.pzs
│   ├── iv.nov..hhn.d.2018.028.000000.sac
│   ├── iv.nov..hhn.d.2018.029.000000.sac
│   ├── iv.nov..hhn.d.2018.030.000000.sac
│   ├── iv.nov..hhn.d.2018.031.000000.sac
│   ├── iv.nov..hhz_2009.345.15.00.00.0000_2599.365.23.59.59.99999.sac.pzs
│   ├── iv.nov..hhz.d.2018.028.000000.sac
│   ├── iv.nov..hhz.d.2018.028.000000.sac.txt
│   ├── iv.nov..hhz.d.2018.029.000000.sac
│   ├── iv.nov..hhz.d.2018.030.000000.sac
│   └── iv.nov..hhz.d.2018.031.000000.sac
└── solun
    ├── iv.solun..hhe_2017.080.18.00.00.0000_2599.365.23.59.59.99999.sac.pzs
    ├── iv.solun..hhn_2017.080.18.00.00.0000_2599.365.23.59.59.99999.sac.pzs
    ├── iv.solun..hhz_2017.080.18.00.00.0000_2599.365.23.59.59.99999.sac.pzs
    └── iv.solun..hhz.d.2018.027.235957.sac

---------- Post updated at 01:51 PM ---------- Previous update was at 01:31 PM ----------

Currently I can pick the files using

Code:
pzfl=`find ./haga/ -name '*hhz*.sac.pzs' -type f`
sacfl=`find ./haga/ -name '*hhz*.sac' -type f`

# 2  
Old 02-13-2018
Perhaps you do not need variables?
Code:
hpat=hhz
for dir in */
do
  echo "Run sac with" "$dir"*$hpat*.sac.psz "and" "$dir"*$hpat*.sac
done

The shell expands the unquoted patterns with the matching file names.
# 3  
Old 02-13-2018
Ok, currently I use this code and call it using

There seems to be a problem as the sac program is picking only the first
in the list .sac. I need a space between them, but I think find is making a new line

Code:
cd ~/chaos/resip/ictja/amvib-t108/datafiles/sac/stnsort
../../../../siculet-prerelease/amvib/instru-correct.sh .

cat instru-correct.sh

Code:
stnm=`find $1 -type d`
#!/bin/bash
echo "stnm: $stnm"

for st in $stnm; do

  if [[ $st != "." ]]; then
    pzfl=`find $st -name '*hhz*.sac.pzs' -type f`
    sacfl=`find $st -name '*hhz*.sac' -type f`
    echo "st: $st"
    echo "pzfl: $pzfl"
    echo "sacfl: $sacfl"
sac << ops  
  r $sacfl
  rmean
  rtrend
  transfer from polezero subtype $pzfl to vel freq 0.001 0.004 1 2 
  w append .ic
  quit
ops
    echo "--------------------------------------------"
  fi
done


Last edited by kristinu; 02-13-2018 at 06:06 PM..
# 4  
Old 02-13-2018
Why not
Code:
for st in $(find $1 -type d ! -name .)
  do    sac <<-ops
                r $(ls -xw0 $st/*hhz*.sac)
                rmean
                rtrend
                transfer from polezero subtype $(ls $st/*hhz*.sac.pzs)  to vel freq 0.001 0.004 1 2 
                w append .ic
                quit
        ops
    done

# 5  
Old 02-13-2018
ls -xw0 $st/*hhz*.sac is saying

Code:
ls: invalid line width: 0

I can solve it using ls -xw800 $st/*hhz*.sac but I do not know how long the filenames
will be

Quote:
Originally Posted by RudiC
Why not
Code:
for st in $(find $1 -type d ! -name .)
  do    sac <<-ops
                r $(ls -xw0 $st/*hhz*.sac)
                rmean
                rtrend
                transfer from polezero subtype $(ls $st/*hhz*.sac.pzs)  to vel freq 0.001 0.004 1 2 
                w append .ic
                quit
        ops
    done


Last edited by kristinu; 02-13-2018 at 07:01 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Moving Files one directory to another directory shell script

Hi, Could you please assist how to move the gz files which are older than the 90 days from one folder to another folder ,before that it need to check the file system named "nfs" if size is less than 90 or not. If size is above 90 then it shouldn't perform file move and exit the script throwing... (4 Replies)
Discussion started by: venkat918
4 Replies

2. Shell Programming and Scripting

Bash shell script not working-picking segment patterns from a file

Hi All, I have to pick particular segments from a file and I have prepared below shell script.But its not working and I am not able to find out whats the issue.could you guys pls help? Sample file: TS3*1451575*12*20151231*4*482.44 NM1*QC*1*CUTLER*BETTY DTM*472*20150808... (4 Replies)
Discussion started by: Venkata Prasad
4 Replies

3. UNIX for Dummies Questions & Answers

Moving all files in a directory to another directory and archiving them

Hi All, i need to move all files in a directory to some other directory and need to archive them,,, Ex.. Source_Path/my_directory/ files in it are... acw.csv 123.txt bge.dat etc ..and we dont know how many files does my_directory contains and all are with different extensions ..so i need... (6 Replies)
Discussion started by: dssyadav
6 Replies

4. Shell Programming and Scripting

Grepping file names, comparing them to a directory of files, and moving them into a new directory

got it figured out :) (1 Reply)
Discussion started by: sHockz
1 Replies

5. Shell Programming and Scripting

Use filenames to create directory.

I have many files similar to this one: AC41_AC85_86_AC128_129_MC171_173_SF_207_FMV.pdf. I want a directory named AC41 and to put the file AC41_AC85_86_AC128_129_MC171_173_SF_207_FMV.pdf into the directory. Next, a directory named AC85 and put the file into it. Also, continue to cycle through... (1 Reply)
Discussion started by: ndnkyd
1 Replies

6. Shell Programming and Scripting

Picking high and low variables in a bash script - possible?

Is it possible to have a bash script pick the highest and lowest values of four variables? I've been googling for this but haven't come up with anything. I have a script that assigns variables ($c0, $c1, $c2, and $c3) based on the coretemps from grep/sed statements of sensors. I'd like to also... (5 Replies)
Discussion started by: graysky
5 Replies

7. Shell Programming and Scripting

Moving filenames containing spaces

I want to ftp all the sh files in the directory. Also if any of the file name contains spaces in them, it should be converted to underscores before it is ftped. I wrote the following code below: FILESSH=$(ls /mysh/*.sh) --- FILESH being used here for some other task --- echo "$FILESSH" |... (3 Replies)
Discussion started by: amicon007
3 Replies

8. Shell Programming and Scripting

Creating date directory and moving files into that directory

I have list of files named file_username_051208_025233.log. Here 051208 is the date and 025233 is the time.I have to run thousands of files daily.I want to put all the files depending on the date of running into a date directory.Suppose if we run files today they should put into 05:Dec:08... (3 Replies)
Discussion started by: ravi030
3 Replies

9. Shell Programming and Scripting

looping thru filenames in a directory

Hi, i am very new to UNIX, i am trying to loop thru the files in a directory. I got the filenames into a variable using $files=`ls` Here $files will contain <filename1> <filename2> <filename3> I want to get one filename at a time and append it to some some text. forexample, ... (1 Reply)
Discussion started by: silas.john
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
Login or Register to Ask a Question