Looping through all folders in a folder.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Looping through all folders in a folder.
# 8  
Old 03-03-2018
Yes, the two variables for file name and folder are created, but no, not with regexes but bash "Parameter Expansion". man bash:

Quote:
${parameter#word}
${parameter##word}

Remove matching prefix pattern.
.
.
.
${parameter%word}
${parameter%%word}

Remove matching suffix pattern.
This User Gave Thanks to RudiC For This Post:
# 9  
Old 03-03-2018
It lists all the directories from the root, even though I've changed directories to where I want the work to be done. It's okay @rdtx1's solution worked. I just need to build on that now.

---------- Post updated at 11:07 AM ---------- Previous update was at 10:58 AM ----------

What's the difference between 2 %'s and one %?
# 10  
Old 03-03-2018
man bash:
Quote:
. . . the result of the expansion is the expanded value of parameter with the shortest matching pattern (the ``%'' case) or the longest matching pattern (the ``%%'' case) deleted.
This User Gave Thanks to RudiC For This Post:
# 11  
Old 03-03-2018
My directory: /folder1/folder2/file.fna.gz

Now following on from renaming the files of interest (fna.gz) I now need to loop through folder1.
As I loop through folder1 I am trying to first of all capture the first word in the file before the first underscore, for example if the folder name is /Long_John_Silver/ then I am trying to capture only "Long".
Then I want to check whether there is a folder with the same name "Long" in a distant directory /another/directory/ and if there is then a move onto the next folder in folder1, but if there isn't then I create a new folder with the captured name i.e. "Long".

Here's my work so far, I am struggling with the capturing of "Long" part.

Code:
 
for fldr in /folder1/folder2/.fna.gz ; do

## Trying to regex capture here but I'm unclear on the syntax, there's expansion, grep  etc but don't know which to use. 
    new_fldr = ${fldr} 

## Then the test for existence.
    if [-f /another/directory/$new_fldr ]; then

        echo "Folder exists already, moving on..." ;

    else [ ! -f /another/directory/$new_fldr ];

        echo "Folder not present, creating folder..." ;

        cd /another/directory/ ;

        mkdir $new_fldr ;

Any help is much appreciated.
# 12  
Old 03-04-2018
Quote:
Originally Posted by Mr_Keystrokes
My directory: /folder1/folder2/file.fna.gz

Now following on from renaming the files of interest (fna.gz) I now need to loop through folder1.
As I loop through folder1 I am trying to first of all capture the first word in the file before the first underscore, for example if the folder name is /Long_John_Silver/ then I am trying to capture only "Long".
Then I want to check whether there is a folder with the same name "Long" in a distant directory /another/directory/ and if there is then a move onto the next folder in folder1, but if there isn't then I create a new folder with the captured name i.e. "Long".

Here's my work so far, I am struggling with the capturing of "Long" part.

Code:
 
for fldr in /folder1/folder2/.fna.gz ; do

## Trying to regex capture here but I'm unclear on the syntax, there's expansion, grep  etc but don't know which to use. 
    new_fldr = ${fldr} 

## Then the test for existence.
    if [-f /another/directory/$new_fldr ]; then

        echo "Folder exists already, moving on..." ;

    else [ ! -f /another/directory/$new_fldr ];

        echo "Folder not present, creating folder..." ;

        cd /another/directory/ ;

        mkdir $new_fldr ;

Any help is much appreciated.
Without a done matching the do in:
Code:
for fldr in /folder1/folder2/.fna.gz ; do

you don't have a loop; you have a syntax error. If you fix the syntax error, you then have a loop that executes once with fldr having been set to the string /folder1/folder2/.fna.gz. There is no chance of finding the string Long, the string Long_John_Silver, or even just an <underscore> character in the value assigned to fldr! If you want to loop through a bunch of directories, you need to either provide a bunch of directories as separate operands on the for statement or use an operand to that for statement that provides a filename matching pattern with wildcard characters that will match a bunch of existing directory pathnames.

An if statement such as:
Code:
if [-f /another/directory/$new_fldr ]; then

) without a matching fi is not an if statement; it is another syntax error. If you add the missing fi in an appropriate place, you're then left with your condition in that if statement ([-f /another/directory/$new_fldr ] which has both syntax and semantic errors. There have to be breaks between the utility name ([ in this case) and the operands you want to pass to that utility. You do not have a break between the [ and the -f. If you add a <space> between those to get the needed break you are then left with a semantic error. The -f test primary tests whether or not the operand is a regular file, but your comments say that you're looking for a directory. The test primary to look for a directory is -d; not -f.

The command new_fldr = ${fldr} is not an assignment statement in the bash shell command language. An assignment statement doesn't have any breaks between the variable name, the <equals-sign> character, and the value to be assigned to that variable. The command you have given is an attempt to execute a command named new_fldr with a 1st operand = and a 2nd operand /folder1/folder2/.fna.gz.

If we knew what pathnames you were trying to match, we might be able to help you come up with a pathname matching pattern or a find statement that could be used to pipe pathnames (or only directory names) into a loop that would get what you want. Since you haven't given us any indication where these directories are located in the file hierarchy rooted in /folder1/folder2, there is no way that we can suggest code that might fix your problems.
# 13  
Old 03-05-2018
Updated script

My script works now.
I now need to add another step to the process and I'll no doubt be back if I run into a stumblingblock again.

Last edited by Mr_Keystrokes; 03-05-2018 at 10:27 AM..
# 14  
Old 03-05-2018
Unless you REALLY need the logging echoes, and exploiting mkdir's -p option (that creates missing directories but ignores existing ones) and deploying bash's "parameter expansion" (as shown before), that can be reduced to
Code:
for fldr in /home/p995824/scripts/playground/genomes_2/*; do TMP="${fldr##*/}"; mkdir -p /home/p995824/programs/kmerid-master/ref/"${TMP%%_*}"; done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Moving files and folders to another folder

I recently bought Synology server and realised it can run scripts. I would need fairly simple script which moves all files and folders from ARCHIVE folder to WORKING folder. I would also need to maintain folder structure as each of the folders may contain subfolders. How would I go about it as I am... (1 Reply)
Discussion started by: ###
1 Replies

2. UNIX for Dummies Questions & Answers

Creating a sub-folder in multiple folders

Hi I've been trying to find an answer to this question and was hoping someone would be able to help me. I want to add a sub-folder to to an existing structure: for example /toys/toy_1/new /toys/toy_2/new /toys/toy_3/new There are humdreds of theses folders - what i want to do is add a... (2 Replies)
Discussion started by: LouSan
2 Replies

3. Shell Programming and Scripting

How to delete other's folders which are in our own folder?

Hi All, I need a solution for the following scenario. I am the owner for the particular folder and I have given 777 permissions for some specific reasons. So others can able to create folders and files. But when I am done with the work, I need to delete the folders which are created by... (4 Replies)
Discussion started by: manoj_thorali
4 Replies

4. UNIX for Dummies Questions & Answers

looping through files, doing something, and rename in new folder

Hi, I'm really new at this but have several hundred files that i need to do something with and save with a new name. The files are 26 columns wide. I want to select some for renaming. I've figured out what to do to each file ("file-1.CSV"| grep -v "=" | cut -f 1-4,9,14,15,18,19,20,21,22,24,26... (2 Replies)
Discussion started by: bob101
2 Replies

5. UNIX for Dummies Questions & Answers

Tar-ing folders in a folder

How do I create individual tars of a all the directories in a directory? I have a directory called 'patients', each patient has a directory in the patients directory. I want to create tars such that each patient has their own tar file. Thanks! (5 Replies)
Discussion started by: HappyPhysicist
5 Replies

6. Shell Programming and Scripting

moving files from one folder to many folders

I have a more than 10 K files in a folder. They are accumulated in a period of more than an year (Say from 13th July 2010 to 4th June 2011). I need to perform housekeeping on this. The requirement is to create a folder like 13Jul2010,14July2010,......3June2011,4June2010 and then from the main... (2 Replies)
Discussion started by: realspirituals
2 Replies

7. Shell Programming and Scripting

Remove all folders within another folder except the lastest 1, 2 or 3.

I am using this command to remove all files and folders older than 24 hours; find /databackup/dbs/* -maxdepth 0 -type d -mtime +0 -exec rm -rf {} \; However, if/when backups to this folder fail and are not updated, it will completely destroy any backups we have in that folder. What can I add... (14 Replies)
Discussion started by: damang111
14 Replies

8. Shell Programming and Scripting

Move all files but not folders to a new folder

Hi, I have a sub directory with a number of files and folders. What i want is a subdirectory with just folders and not files for cleanliness sake. So I want to move the files into the new folder but keep the folders in the same place. Move all files (but not folders) to new folder. I am... (4 Replies)
Discussion started by: Hopper_no1
4 Replies

9. UNIX for Dummies Questions & Answers

Zip a folder including its sub-folders.

Hi, I have a folder that contains a few sub-folders. I would like to zip that folder and KEEP the subfolders. What it does at the moment is taking all the files within the subfolders and zipping them into one big file (zip -r ...). Does anyone know the UNIX command to keep the subfolders in the... (3 Replies)
Discussion started by: gdog
3 Replies

10. Shell Programming and Scripting

cygwin, looping through folders

Hello I am trying to write a script. This is the thing, I want to loop through all folders in a certain folder and send all files in these folders to a java program. The files are somewhere in the neighbourhood of 170.000. the setup is (hope you get the picture): _______________________Home... (1 Reply)
Discussion started by: smanna
1 Replies
Login or Register to Ask a Question