Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Omitting few folder from a folder

Status
Not open for further replies.

Dev Rishi

Programmer
Jun 16, 2017
3
US
Hi All,

I have a list of directories like below

Folder Path is
$GLOG_HOME/web/htdocs/js
Folders are

rateservice
ratepreference
ratefactor
dev123
I want to omit all folders and select only dev123 folder,

I have tried

#!/bin/bash

. $HOME/.profile
cd $GLOG_HOME/web/htdocs/js
touch custom_js_files.txt

ls -p grep -v 'rateservice'| grep-v 'ratepreference'| grep-v 'ratefactor'> custom_js_files.txt

I have tried grep -R for directories also. Please suggest on it.

Thanks,
Dev Rishi


 
grep -v inverts the results so you get lines that do not match the pattern.


If you already know the folder name the simplest way of course is just to specify only that folder in the path.

grep [pattern] $GLOG_HOME/web/htdocs/js/dev123/* ... ...

Is there some compelling reason that cannot be done?

I have tried grep -R for directories also.
-R is to parse all subdirectories including symbolic links.

--exclude-dir foldername(s) will 'exclude' named directories

$ man grep on the CLI will tell you this.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind this jesus character, stars had to die for me to live.
 
Thanks Chris,

I know only folders which i want to omit. The folder I wan to capture is dynamic and it can change.

So I need a script to omit all folders I know and list down which are not known or can change.

Thanks,
Dev
 
In that case, --exclude-dir is the option to use

and from your example structure;
Bash:
grep -r [pattern] [top-level-folder/filespec] --exclude-dir 'ra*'
for example will exclude physical directories where the name begin with ra and if you have more to exclude just concatenate additional --exclude-dir options

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind this jesus character, stars had to die for me to live.
 
The Korn shell has a pattern for matching 'NOT' something...

Code:
ls -lad !(rate*)

That will match everything that does NOT start with "rate".

 
Hi Chris,

What should I give in pattern. I do not have any specific pattern.

Thanks,
Dev
 
What should I give in pattern

Whatever the text is you are grepping the files for.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind this jesus character, stars had to die for me to live.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top