Working with folder names


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Working with folder names
# 1  
Old 01-06-2009
Working with folder names

I have the following directory structure:

/maindir
/maindir/product1/
/maindir/product1/type1
/maindir/product1/type2
/maindir/product1/type3
/maindir/product2/
/maindir/product2/type1
/maindir/product2/type2
/maindir/product2/type3
/maindir/product2/type4
...

I'm able to traverse through my directory structure and print each folder name using the following code:

Code:
#! /bin/sh
find ../maindir/subdir -type d | while read dirname # process all directories
do
  dir=`basename $dirname` 
  echo $dir
done

However, I need to interrupt the looping at each product# change so I can start a new file that lists all the product types.

Can I get some assistance with this? I'll admit I'm new to shell scripting and I've searched the other posts for similar solutions with no avail.
# 2  
Old 01-06-2009
Code:
find ../maindir/ -prune -type d -print | while read products
do
    product=`basename $products`
    find ../maindir/$product -prune -type d -print | while read types
    do
        type=`basename $type`
        echo $type >> ${product}.txt
    done
done

HTH
# 3  
Old 01-06-2009
There is similar thread. Hope it helps you

https://www.unix.com/unix-dummies-que...b-folders.html
# 4  
Old 01-06-2009
thanks pludi for the input.

How would I exclude 'html' folder names in the find statement? Or would I have to do a test for a substring instead?
# 5  
Old 01-22-2009
Pludi - the find code you provided works great, but now I've got another problem. I've added some new files and folders, and now the script doensn't process everything in alphabetical order.

I've tried using the -name "*" option to find, but this resuts in a "bad option *" error. Any suggestions?
# 6  
Old 01-23-2009
That's because find doesn't report entries in any order but that in which it reads them from the file system. Try changing the "-print | while" part to "-print | sort | while"
# 7  
Old 01-23-2009
Thank you, thank you. That worked!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to tar/rsync/rm multiple folder names

hi all, i attach a link with what im trying to do automatically via script but i have some questions i need answering please, bear in mind i am really new to bash scripting, the only thing i know how to do is commands in scripts like cd rm tar rsync cp stuff like that i have mutiple project... (48 Replies)
Discussion started by: robertkwild
48 Replies

2. Shell Programming and Scripting

Remove trailing space from file and folder names

I have a folder that contains many sub folders and files. This tree has to be backed up to an archive system. According to the tech support, one of the archives is failing to back up due to the possibility of trailing spaces on file and folder names. Therefore, I would like to have a script... (16 Replies)
Discussion started by: vipertech
16 Replies

3. Homework & Coursework Questions

Bash script for printing folder names and their sizes

1. The problem statement, all variables and given/known data: The task is to create a script that would reproduce the output of 'du' command, but in a different way: what 'du' does is: <size> <folder name>and what is needed is <folder name> <size>We need to show only 10 folders which are the... (3 Replies)
Discussion started by: UncleIS
3 Replies

4. Shell Programming and Scripting

Bash script for printing folder names and their sizes

Good day, everyone! I'm very new to bash scripting. Our teacher gave us a task to create a script that basically does the same job the 'du' command does, with the difference that 'du' command gives an output in the form of <size> <folder name>and what we need is <folder name> <size>As for... (1 Reply)
Discussion started by: UncleIS
1 Replies

5. Shell Programming and Scripting

Renaming File Names in a folder/Dir

Hi Team, I'm new to Unix shell scripting . I've the following requirement A folder contains the list of files with the following format ab.name.11.first ab.name.12.second ab.name.13.third ---------- I have to rename the above file to like below ... (6 Replies)
Discussion started by: smile689
6 Replies

6. UNIX for Dummies Questions & Answers

Working with file-names

Hello forum, I am new to shellscripting. My first goal is to write a script that verifies that for each file in one path there is an associated one in another path. The association is deviated by a distinct 'part' of the filename(s). Therefore I wanted to work with substitution. Here is... (3 Replies)
Discussion started by: danielandross
3 Replies

7. Shell Programming and Scripting

editing names of files in multiple folder

I have 1000's of directories which is named as numbers. Each directory contains multiple files. Each of these directories have a file named "att". I need to rename all the att files by adding the directory name followed by "_" then att for each of the directories. Directories 120 att... (2 Replies)
Discussion started by: Lucky Ali
2 Replies

8. Shell Programming and Scripting

Script to move files with similar names to folder

I have in directory /media/AUDIO/WAVE many .mp3 files with names like: my filename_01of02.mp3 my filename_02of02.mp3 Your File_01of06.mp3 Your File_02of06.mp3 etc.... In the same directory, /media/AUDIO/WAVE, I have many folders with names like 9780743579490 9780743579491 etc.. Inside... (7 Replies)
Discussion started by: glev2005
7 Replies

9. UNIX for Dummies Questions & Answers

Copying multiple folders to local machine (don't know folder names)

Hi. I'm trying to copy multiple folders from the remote machine to the local machine. I wrote a batch file to run an ftp window. The problem I am having is that the only command to copy files is mget *, and this copies only files, not folders. For example, ftp ts555 cd ts555/test ' test... (5 Replies)
Discussion started by: leenyburger
5 Replies

10. UNIX for Dummies Questions & Answers

Getting all file names in a folder.

Hi All, I need help to create a shell script that does following: - do ls -1 and let all file names in the current folder - do wc for each file and if wc > 0 then send a mail. I am new to unix and do not know how to get filename one by one in a loop, so that wc can be done. I have done rest... (5 Replies)
Discussion started by: adadevil
5 Replies
Login or Register to Ask a Question