0

I have a directory mnt/d/LIVI.

Inside the directory, LIVI, I have sub-directories:

mnt/d/LIVI/ak
mnt/d/LIVI/ag
mnt/d/LIVI/few
mnt/d/LIVI/ww4
mnt/d/LIVI/ks5

I wanted to copy a file named tt.txt from mnt/d/LIVI/ak/tt.txt and paste to all the sub directories of LIVI, using Ubuntu terminal. How do i do it using a shell script file?

I tried the following one, but it didn't work.

I created a text file named mnt/d/LIVI/FOLDERS.txt, This listed all the sub directories names.

And saved a script file in mnt/d/LIVI directory. The following is the script

#!/bin/sh
# -*- coding: utf-8-unix -*-
ROOTDIR=$(dirname $0 | xargs readlink -f)
for SIMDIR in cat FOLDERS.txt | xargs readlink -f ; do
    cp mnt/d/LIVI/ak/tt.txt $SIMDIR
done
#cd ..
date
tripleee
  • 175,061
  • 34
  • 275
  • 318

2 Answers2

0

You may try this bash script

#!/bin/bash

cd "${0%/*}" || exit
for dir in */; do
    if [[ $dir != 'ak/' ]]; then
        cp ak/tt.txt "$dir"
    fi
done

The script must reside under the diectory mnt/d/LIVI

M. Nejat Aydin
  • 9,597
  • 1
  • 7
  • 17
  • This just loops over all the subdirectories, not the ones in `FOLDERS.txt` – tripleee Aug 18 '22 at 12:52
  • @tripleee Yes. I thought that the file `FOLDERS.txt` wasn't needed because the OP wrote _"This listed all the sub directories names."_. Perhaps the OP should clarify if he needs all subdirectories or the ones listed in the file only. – M. Nejat Aydin Aug 18 '22 at 12:57
0

Don't read lines with for. (If you really wanted to, the syntax for that would look like

for dir in $(cat FOLDERS.txt); do
    ...

but really, don't. The link above explains why in more detail.)

I don't see why you want to run readlink on the directories at all?

cd "$(dirname "$0")"
while read -r dir; do
    cp ak/tt.txt "$dir"
done <FOLDERS.txt

Note also Correct Bash and shell script variable capitalization

tripleee
  • 175,061
  • 34
  • 275
  • 318