[root@localhost companyshare]# tree
.
├── CompanyShare
│ ├── Purchasing
│ │ ├── 01 - January
│ │ │ ├── Balance Sheet.xlsx
│ │ │ └── Invoice.pdf
│ │ ├── 02 - February
│ │ │ ├── additional-table.xlsx
│ │ │ ├── important-business-figures.xlsx
│ │ │ ├── important-invoice.pdf
│ │ │ └── not-important.mp4
│ │ └── 03 - March
│ │ └── numbers-from-march.xlsx
│ └── Sales
│ ├── 01 - January
│ │ └── Sales-January.xlsx
│ ├── 02 - February
│ │ └── Balance-Sheet-February.pdf
│ └── 03 - March
│ ├── Additional-Invoice-March.pdf
│ └── Sales-March.xlsx
└── __MACOSX
└── CompanyShare
├── Purchasing
│ ├── 01 - January
│ ├── 02 - February
│ └── 03 - March
└── Sales
├── 01 - January
├── 02 - February
└── 03 - March
19 directories, 11 files
How do I do this using globbing. I am attempting this and will put my valid attempts at the thread.
@Ihattaren , show your attempts, you know this forum is a collaboration, we're not here to do your work for you without any input from you.
1 Like
Try multiple wildcards like
echo */*/*/02" - "*/*.xls*
In bash you can use brace expansion, like
{*.xls*,*.pdf}
1 Like
I'm sure you're aware that an asterix expands to match anything, however you can match specific things using brace expansion eg
$ echo Hello\ {Skrynesaver,Ihattaren}
Hello Skrynesaver Hello Ihattaren
You should be able to extrapolate a suitable shell expansion for that to meet your needs.
For more details on the MANY ways you can modify values with Bash expansions, take a look at the Bash Reference Manual
[root@localhost companyshare]# echo CompanyShare/*/*/*.pdf
CompanyShare/Purchasing/01 - January/Invoice.pdf CompanyShare/Purchasing/02 - February/important-invoice.pdf CompanyShare/Sales/02 - February/Balance-Sheet-February.pdf CompanyShare/Sales/03 - March/Additional-Invoice-March.pdf
I got upto here.
what is that term
{*.xls*,*.pdf}
called in bash? Brace expansion? That helped me get the solution here's it.
echo CompanyShare/*/*/{*.xls*,*.pdf}
Plus I needed to enable
shopt -s globstar
May I ask why does * instead of companyshare doesn't work?
echo */*/{*.xls*,*.pdf}
Alas, this wont match what you say you're after (.pdf,.xls*) files from January and February
if you posted the accompanying output from the command you would see that.
Ihattaren:
echo */*/{*.xls*,*.pdf}
echo */*/*/{*.xls*,*.pdf}
Each *
expands to one directory level.
Option globstar
? It expands **
over directories like
echo **/{*.xls*,*.pdf}
where the **
is the whole directory tree.
In any case a shopt -s nullglob
is useful, because it does not show an empty match as the wildcard.
In the thread title you have a further requirement January+February.
echo */*/{01,02}*/{*.xls*,*.pdf}
More efficient should be a [ ]
character set:
echo */*/0[12]*/{*.xls*,*.pdf}