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!

3

Replace / with _:

tag=${tag//\//_}
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.