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.
# 1  
Old 03-02-2018
Looping through all folders in a folder.

I am trying to write a script that loops through all the folders within a given folder.
My intention is to access each folder and rename each file ending with fna.gz with the name of the folder it resides in.

Code:
#!/bin/bash

cd /p/w/d/

for f in /p/w/d/*; do
   echo $f

done

I'm going to take this step by step because first of all at this point if I print "f" it prints the whole pwd including the folders i.e. p/w/d/folder1. Instead of printing just folder1. So firstly how can I get it to print just folder1?

Moderator's Comments:
Mod Comment Please use CODE (not ICODE) tags as required by forum rules!

Last edited by RudiC; 03-02-2018 at 08:24 PM.. Reason: Added CODE tags.
# 2  
Old 03-02-2018
Code:
find . -name *fna.gz | while read f ; do fl=${f%/*}; fld=${fl##*/} ; echo mv "$f" "$fl/${fld}_${f##*/}" ; done

Test first. To perform rename, update as needed then remove echo
This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 03-02-2018
Quote:
Originally Posted by Mr_Keystrokes
I am trying to write a script that loops through all the folders within a given folder.
My intention is to access each folder and rename each file ending with fna.gz with the name of the folder it resides in.

Code:
#!/bin/bash

cd /p/w/d/

for f in /p/w/d/*; do
   echo $f
done

I'm going to take this step by step because first of all at this point if I print "f" it prints the whole pwd including the folders i.e. p/w/d/folder1. Instead of printing just folder1. So firstly how can I get it to print just folder1?

If you remove the full path from the for loop, what does it give you?
Code:
#!/bin/bash
cd /p/w/d/
for f in *; do
   echo $f
done

# 4  
Old 03-02-2018
Code:
for a in `find /p/w/d -type f -name '*.fna.gz'`
{
 d=${a%/*}
 mv $a ${a%.*}.${d##*/}
}

# 5  
Old 03-03-2018
Quote:
Originally Posted by Aia
If you remove the full path from the for loop, what does it give you?
Code:
#!/bin/bash
cd /p/w/d/
for f in *; do
   echo $f
done

It returns unwanted files and folders.

Last edited by RudiC; 03-03-2018 at 11:37 AM.. Reason: Moved the /QUOTE tag.
# 6  
Old 03-03-2018
Quote:
Originally Posted by Mr_Keystrokes
I am trying to write a script that loops through all the folders within a given folder.
My intention is to access each folder . . .
Quote:
Originally Posted by Mr_Keystrokes
It returns unwanted files and folders.
What were "unwanted folders" in the context of your post#1?
This User Gave Thanks to RudiC For This Post:
# 7  
Old 03-03-2018
Thanks, your solution works. I'm just trying to decode what you've done.

So I see here that the find function looks for files of a given type i.e. .fna.gz.
And then the pipe ( | ) passes each file to be read.
Code:
 find . -name *fna.gz | while read f ; do

Now as the files are being read something is going on that I can't make out.
You seem to be creating 2 variables, the first of which captures the name of the folder and then the captures the name of the file in that folder. Is this correct?
Is this a regex? What do the values {}, % and # mean?
Code:
fl=${f%/*}; fld=${fl##*/} ;

And then here you rename the files.
Code:
mv "$f" "$fl/${fld}_${f##*/}" ;

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