1

I am attempting to name files based on their parent directory names. With a file in ./dir*/dir*/, I would like to name the file "dir*_dir*" or "dir*dir*" (plus some constant appended text and extension but that is not relevant to the question). I have something like:

for file in ./dir*/dir*/*GABA*.dat; do 
 tag=${file%/*}
 tag=${tag} #here, I believe the value of the variable will be "dir*/dir*",    
   so this is where I was thinking to remove the '/' 
 tag=$(echo "$tag" | tr '[:upper:]' '[:lower:]')
mv -- "$file" "./GABA/${tag}.dat"
done

I'm not specifically asking for evaluation of the overall code, although that would be welcome also (it runs seemingly properly in another similar context), but particularly how to update my "tag" variable to remove the "/" between dir*/dir*. Thanks!

2 Answers 2

3

Replace / with _:

tag=${tag//\//_}
3
  • 4
    That assumes ksh93, mksh, zsh or bash. Aug 30, 2016 at 15:01
  • 1
    @cuonglm, no that was introduced by ksh93. Actually you did say so in one of your own recent answers (which did have me double-check as I also had that wrong notion that it also worked in ksh88). Aug 30, 2016 at 15:37
  • @StéphaneChazelas Ops, I mis-read the answer. I remembered I checked it in Solaris 10 ksh88
    – cuonglm
    Aug 30, 2016 at 15:40
1

With zsh:

autoload zmv # best in ~/.zshrc
zmv -n '(dir*)/(dir*)/*GABA*.dat' 'GABA/${(L)1}${(L)2}.dat'

Remove -n if happy.

(L) is a parameter expansion flag to convert the expansion to lower case.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .