Reputation: 955
I am iterating through folders and finding the folders that has specific file extension. Based on the find result, I am getting the directory path as -
e.g.
./Config/SysMapping.txt
./Config/exclusion_list_old.txt
I want to simply get the name:
Config
and remove
./
before
Config
and everything after Config, i.e.
/SysMapping.txt
Upvotes: 0
Views: 121
Reputation: 1809
You can use the globstar
setting:
shopt -s globstar nullglob
files=(**/*.txt)
# if there cannot be newlines in filenames
printf '%s\n' "${files[@]%/*}" | sort -u
# if there can be newlines in filenames
printf '%s\0' "${files[@]%/*}" | sort -zu | tr '\0' '\n'
The tr
is there just to print the dirnames nicely at the end. You can choose to process the null delimited dirnames differently, with a while
loop for example.
Upvotes: 0
Reputation: 16692
This will extract the first path element after ./
and delete duplicates:
echo ./Config/SysMapping.txt | awk -F/ '{print $2}' | sort -u
In bash, you can do:
P=./Config/SysMapping.txt
P=${P%/*} # --> ./Config
P=${P#./} # --> Config
echo "$P"
Upvotes: 3
Reputation: 486
dirname <path>
would remove the file at the end of your path. In your example you'd end up with ./Config
. You can then pipe this to awk '{ gsub(/^\.\//, ""); print }'
to remove the ./
.
Upvotes: 1