Skip to main content
deleted 5 characters in body
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 265

I have a scenario where I am looping through all directories and subdirectories in a given pathpath; if a file with specific extension (.txt) is found storing, store the name of the directories and subdirectories in an array later which. Later, I read and execute commandcommands on those directories.

Here is what I am performing:

!/bin/bash
x=( $(find . -name "*.txt") ); echo "${x[@]}"
for item in "${x[@]}"; { echo "$item"; }

Currently I am getting theMy current output asis:

./dir1/file1.txt
./dir1/file2.txt
./dir2/subdir1/subdir2/file3.txt

but what I want to achieve is in the array x there should not be any duplicates even if the dir contains more than one .txt file and also. Additionally, I dontdon't want to store the file name in as a path,path; the array should contain only the directory name.

The expected output:

./dir1
./dir2/subdir1/subdir2/

I have a scenario where I am looping through all directories and subdirectories in a given path if a file with specific extension (.txt) is found storing the name of the directories and subdirectories in array later which I read and execute command on those directories.

Here is what I am performing:

!/bin/bash
x=( $(find . -name "*.txt") ); echo "${x[@]}"
for item in "${x[@]}"; { echo "$item"; }

Currently I am getting the output as

./dir1/file1.txt
./dir1/file2.txt
./dir2/subdir1/subdir2/file3.txt

but what I want to achieve is in the array x there should not be any duplicates even if the dir contains more than one .txt file and also I dont want to store the file name in as a path, array should contain only the directory name.

expected output:

./dir1
./dir2/subdir1/subdir2/

I have a scenario where I am looping through all directories and subdirectories in a given path; if a file with specific extension (.txt) is found, store the name of the directories and subdirectories in an array. Later, I read and execute commands on those directories.

Here is what I am performing:

!/bin/bash
x=( $(find . -name "*.txt") ); echo "${x[@]}"
for item in "${x[@]}"; { echo "$item"; }

My current output is:

./dir1/file1.txt
./dir1/file2.txt
./dir2/subdir1/subdir2/file3.txt

but what I want to achieve is in the array x there should not be any duplicates even if the dir contains more than one .txt file. Additionally, I don't want to store the file name in as a path; the array should contain only the directory name.

The expected output:

./dir1
./dir2/subdir1/subdir2/
Became Hot Network Question
edited tags
Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Source Link
Jenifer
  • 85
  • 2
  • 5

loop through current directory store directory and subdirectory name in a array without duplicates

I have a scenario where I am looping through all directories and subdirectories in a given path if a file with specific extension (.txt) is found storing the name of the directories and subdirectories in array later which I read and execute command on those directories.

Here is what I am performing:

!/bin/bash
x=( $(find . -name "*.txt") ); echo "${x[@]}"
for item in "${x[@]}"; { echo "$item"; }

Currently I am getting the output as

./dir1/file1.txt
./dir1/file2.txt
./dir2/subdir1/subdir2/file3.txt

but what I want to achieve is in the array x there should not be any duplicates even if the dir contains more than one .txt file and also I dont want to store the file name in as a path, array should contain only the directory name.

expected output:

./dir1
./dir2/subdir1/subdir2/