Bash directory loop, but only choose those folders with specific word in it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash directory loop, but only choose those folders with specific word in it
# 1  
Old 11-24-2016
Bash directory loop, but only choose those folders with specific word in it

Hello, how in bash i can get directory loop, but only choose those folders with specific word in it, so it will only echo those with specific word

Code:
#!/bin/bash
for filename in /home/test/*
do
if [ -d "${filename}" ]; then
echo $filename;
fi

thx!
# 2  
Old 11-24-2016
Try
Code:
for filename in /home/test/*word*

This User Gave Thanks to RudiC For This Post:
# 3  
Old 11-25-2016
Depending on what you are doing with $filename after determining that it names a directory, you might also be able to reduce the iterations through the loop by just looking for directories to start with:
Code:
for directoryname in /home/test/*word*/

This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 11-25-2016
A trailing / ensures it is a directory
Code:
for filename in /home/test/*word*/

With your original loop, you can go for a case-esac, or, if there is only one pattern, go for a [[ == ]]
Code:
if [[ $filename == *word* ]] && [ -d "$filename" ]; then
  echo "$filename"
fi

This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 11-25-2016
If you want to search deeper that just within the current directory (i.e. searching within subdirectories) you can use the find command:-
Code:
find /path/to/source -type d -name "*word*"

Be aware that this does not sort the output and will try to follow symbolic links and descend into sub-mounted filesystems. If these are a problem they can be turned off.


Does this help, or am I going the wrong way by using too big a tool for a simple job?



Robin
This User Gave Thanks to rbatte1 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to list all folders in a specific directory

The below bash is trying to list the folders in a specific directory. It seems close but adds the path to the filename, which basename could strip off I think, but not sure why it writes the text file created? This list of folders in the directory will be used later, but needs to only be the... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

Bash to add folder to exsisting folders in directory

I am trying to create subdirectories in each folder in /home/cmccabe/Desktop/NGS/test/*. When I use echo I can see each folder in the directory, but I can not seem to add the commented out portion in bold. These are the sub-directories and sub-folders I am trying to add to each folder in... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Shell Programming and Scripting

Bash to move specific files from folders in find file

I have a directory /home/cmccabe/nfs/exportedReports that contains multiple folders in it. The find writes the name of each folder to out.txt. A new directory is then created in a new location /home/cmccabe/Desktop/NGS/API, named with the date. What I am trying to do, unsuccessfully at the moment,... (7 Replies)
Discussion started by: cmccabe
7 Replies

4. Shell Programming and Scripting

Move specific folders and subfolders in a directory

I am trying to move specific folders and subfolders within a directory using the below. I can see the folders to move and they are at the location, but I am getting an error. Thank you :). mv -v /home/cmccabe/Desktop/NGS/API/6-10-2016{bam/{validation,coverage},bedtools /media/cmccabe/"My... (6 Replies)
Discussion started by: cmccabe
6 Replies

5. UNIX for Beginners Questions & Answers

How to loop command over folders in a directory?

Hello all, I'm fairly new to scripting so please bear with me. I will try to explain as best as I can but if there's anything that is not clear, please let me know. I have a directory (see below) with numerous folders and within each of those folders are various files. I would like to run a... (11 Replies)
Discussion started by: azurite
11 Replies

6. Shell Programming and Scripting

How to delete all the files and folders inside all the directories except some specific directory?

hi, i have a requirement to delete all the files from all the directories except some specific directories like archive and log. for example: there are following directories such as A B C D Archive E Log F which contains some sub directories and files. The requirement is to delete all the... (7 Replies)
Discussion started by: Little
7 Replies

7. Shell Programming and Scripting

Bash to download specific files and save in two folders

I am trying to download all files from a user authentication, password protected https site, with a particular extension (.bam). The files are ~20GB each and I am not sure if the below is the best way to do it. I am also not sure how to direct the downloaded files to a folder as well as external... (7 Replies)
Discussion started by: cmccabe
7 Replies

8. UNIX for Dummies Questions & Answers

How to print line starts with specific word and contains specific word using sed?

Hi, I have gone through may posts and dint find exact solution for my requirement. I have file which consists below data and same file have lot of other data. <MAPPING DESCRIPTION ='' ISVALID ='YES' NAME='m_TASK_UPDATE' OBJECTVERSION ='1'> <MAPPING DESCRIPTION ='' ISVALID ='NO'... (11 Replies)
Discussion started by: tmalik79
11 Replies

9. Shell Programming and Scripting

Bash take word after specific point and till next space?

Hello, I have an output like Interface Chipset Driver wlan0 Intel 4965/5xxx iwlagn - and I want to take only the 'wlan0' string. This can be done by a="Interface Chipset Driver wlan0 Intel 4965/5xxx iwlagn - " b=${a:25:6} echo $bThe thing is that wlan0 can be something else, like eth0 or... (2 Replies)
Discussion started by: hakermania
2 Replies

10. UNIX for Dummies Questions & Answers

How to count the occurences of a specific word in a file in bash shell

Hello, I want to count the occurences of a specific word in a .txt file in bash shell. Can somebody help me pleaze?? Thanks!!! (2 Replies)
Discussion started by: mskart
2 Replies
Login or Register to Ask a Question