0

I'm searching a folder in s3 bucket using this command

aws s3 ls s3://bucketname/dir1/dir2/dir3 --recursive | grep -i 'dir3'

It's getting results like

dir1/dir2/dir3/1/aaa.txt 
dir1/dir2/dir3/1/bbb.txt
dir1/dir2/dir3/1/ccc.txt

However, I need only path of that file like

dir1/dir2/dir3

I can able remove unnecessary text to get directory path by this

aws s3 ls s3://bucketname/dir1/dir2/dir3 --recursive | grep -i 'dir2' | head -n 1 | sed 's/1.*//'

But this is not working with multiple string search in grep

aws s3 ls s3://bucketname/dir1/dir2/dir3 --recursive | grep -i 'dir3\|folder3'

I need output like this

dir1/dir2/dir3
folder1/folder2/folder3
0

You can do something like:

for i in `aws s3 ls s3://bucketname/dir1/dir2/dir3 --recursive | grep -i 'dir3' `
do
dirname $i >>/tmp/file
done
sort -u /tmp/file >/tmp/file1

And on the end you will have the list of subdirectories in file /tmp/file1

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.