Loop through directory names


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Loop through directory names
# 1  
Old 07-25-2013
Loop through directory names

Some guidance is highly appreciated.

I have 10 directories with names ending with 'xyz', each of them have about 30000 files. I want to loop through the contents of each directory and produce a single output per directory. So I want to have 10 output files named 'directory_name'_out.

With multiple files I can do

Code:
for file in  *xyz
do
some command $file > $file_out
done

How do I do the same with directories?
# 2  
Old 07-25-2013
* will match directory and file names alike, so you can handle it exactly the same way.

30,000 files may easily be too many to fit into *, however. You could use ls with a read loop instead. Just beware that variables you change inside the 'while read' loop will not be affected outside the loop, because of the pipe.

Code:
for DIR in *
do
        [ -d "$DIR" ] || continue # Skip non-directories

        ls $DIR | while read FILE
        do
                do_something_to "$DIR/$FILE"
        done
done

This User Gave Thanks to Corona688 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

Using nested for loop to iterate over file names

I'm trying to grab a list of file names from a directory, then process those files 5 at a time. In the link below. Instead of using files I'm using the files array which contains 15 strings starting with AAA. So I'm trying to assign $fileset 5 of the strings at a time to pass to a command. So... (4 Replies)
Discussion started by: zBernie
4 Replies

2. Shell Programming and Scripting

Print file names in a loop

OS : RHEL 6.1 Shell : Bash I have lots of files in /tmp/stage directory as show below. Using a loop, I need to print all the filenames in this directory except those ending with a number. How can I do this ? # pwd /tmp/stage # # # ls -l * -rw-r--r--. 1 root root 0 Oct 7 18:38 stmt1... (2 Replies)
Discussion started by: kraljic
2 Replies

3. Shell Programming and Scripting

How to make dynamic variable names for use in while loop?

i=0 while do sizesfor0=`cat 16 | grep 'pickSize' -A 1 | grep '_sz' | cut -d'_' -f1` sizesfor0=${sizesfor0//id=\"lll/:} IFS=: array0=( $sizesfor0 ) echo ${array0} i=$(( $i + 1 )) done So, right now I have two variables in the while statement above sizesfor0 and array0 The... (1 Reply)
Discussion started by: phpchick
1 Replies

4. UNIX for Dummies Questions & Answers

[Solved] Writing a loop to changing the names of files in a directory

Hi, I would like to write a loop to change the names of files in a directory. The files are called data1.txt through data1000.txt. I'd like to change their names to a1.txt through a1000.txt. How do I go about doing that? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

5. Shell Programming and Scripting

Grepping file names, comparing them to a directory of files, and moving them into a new directory

got it figured out :) (1 Reply)
Discussion started by: sHockz
1 Replies

6. UNIX for Dummies Questions & Answers

Loop through directory and extract sub directory names

I am trying to loop through folders and extract the name of the lowest level subfolder I was running the script below, it returns /bb/bin/prd/newyork /bb/bin/prd/london /bb/bin/prd/tokyo I really want newyork london tokyo I couldn't find a standard variable for the lowest level... (1 Reply)
Discussion started by: personalt
1 Replies

7. UNIX for Dummies Questions & Answers

How to display only Owner and directory/sub directory names under particular root

hai, I am new to Unix, I have a requirement to display owner name , directory or sub directory name, who's owner name is not equal to "oasitqtc". (here "oasitqtc" is the owner of the directory or sub directory.) i have a command (below) which will display all folders and sub folders, but i... (6 Replies)
Discussion started by: gagan4599
6 Replies

8. Shell Programming and Scripting

Searching for file names in a directory while ignoring certain file names

Sun Solaris Unix Question Haven't been able to find any solution for this situation. Let's just say the file names listed below exist in a directory. I want the find command to find all files in this directory but at the same time I want to eliminate certain file names or files with certain... (2 Replies)
Discussion started by: 2reperry
2 Replies

9. Shell Programming and Scripting

File Names in a Variable in a loop

Hi All , I am having confusion in a shell script. Please guide me. I need to get multiple files (number of files vary time to time, file names are separated by '|') using FTP get from the remote server. Actually, i call the FTP function in a loop. At the last step, i need to move all the get... (3 Replies)
Discussion started by: spkandy
3 Replies

10. UNIX for Dummies Questions & Answers

Use of $ in directory names

I have a TAR (compressed) file that I uncompressed using the following command $ tar xvf uagent.tar It created a directory structure as follows <root>/arcagent/UAGENT/reloc/$UAGENT_HOME/ How do I access files under the $UAGENT_HOME? Every time I do change directory command, the OS... (3 Replies)
Discussion started by: pkappaz
3 Replies
Login or Register to Ask a Question