skip to Main Content

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

2

Answers


  1. 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

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search