How to run a script/command on all the directories in a directory tree?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to run a script/command on all the directories in a directory tree?
# 1  
Old 10-15-2015
Tools How to run a script/command on all the directories in a directory tree?

How to run a script/command on all the directories in a directory tree?

The below script is just for the files in a single directory, how to run it on all the directories in a directory tree?

Code:
#!/bin/sh

for audio_files in *.mp3
do
  outfile="${audio_files%.*}.aiff"
  sox "$audio_files" "$outfile"
done

# 2  
Old 10-15-2015
Hi Temp-usr,

The below command runs on recursive inside 5 folders and get the desired result.
If the same command has been added in a script it will also do the same.

Code:
[root@localhost workout]# ls -lR
.:
total 4
drwxr-xr-x. 3 root root 4096 Oct 15 00:03 1
-rw-r--r--. 1 root root    0 Oct 15 00:03 test1

./1:
total 4
drwxr-xr-x. 3 root root 4096 Oct 15 00:03 2
-rw-r--r--. 1 root root    0 Oct 15 00:02 test2

./1/2:
total 4
drwxr-xr-x. 3 root root 4096 Oct 15 00:03 3
-rw-r--r--. 1 root root    0 Oct 15 00:03 test3

./1/2/3:
total 4
drwxr-xr-x. 3 root root 4096 Oct 15 00:03 4
-rw-r--r--. 1 root root    0 Oct 15 00:03 test4

./1/2/3/4:
total 4
drwxr-xr-x. 2 root root 4096 Oct 15 00:03 5
-rw-r--r--. 1 root root    0 Oct 15 00:03 test5

./1/2/3/4/5:
total 0
[root@localhost workout]# find /root/workout/ -iname "test*"
/root/workout/1/test2
/root/workout/1/2/test3
/root/workout/1/2/3/4/test5
/root/workout/1/2/3/test4
/root/workout/test1

cat > Recursive.sh
#/bin/sh

ls -lR
find /root/workout/ -iname "test*"

Thanks
LND

Last edited by Don Cragun; 10-15-2015 at 05:32 AM.. Reason: Add CODE tags.
This User Gave Thanks to LND For This Post:
# 3  
Old 10-15-2015
Hello temp-user,

You should place this script in parent(first level directory) and could run from there. Also for double checking the working for script, you could use following to make sure first files are getting picked from all the paths.
Code:
#!/bin/sh
find -type d 2>/dev/null > Input_file
awk '{sub(/^\./,X,$0);print}' Input_file > Files
 while read path
do
cd $path
   for audio_files in *.mp3
   do
       echo $audio_files
   done
done < "Files"

Once you are fine with above script you could use following actual script then.
Code:
#!/bin/sh
find -type d 2>/dev/null > Input_file
awk '{sub(/^\./,X,$0);print}' Input_file > Files
 while read path
do
cd $path
   for audio_files in *.mp3
   do
      outfile="${audio_files%.*}.aiff"
      sox "$audio_files" "$outfile"
   done
done < "Files"

Where file named Input_file has all the paths. You should place this script too in parent(first level directory) and could run from there.


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Application Script didn't run during AIX shutdown using RC directories

Hi, I am trying to bring down the application gracefully before bringing down AIX OS/LPAR using RC directories. Issue: Application script is not working properly during/before AIX OS/LPAR shutdown. looks like "rc.d" directories doesn't recognize Kill script. But Startup script (using... (0 Replies)
Discussion started by: System Admin 77
0 Replies

2. Shell Programming and Scripting

Run getfacl command only on directories

bash on RHEL 5.9 i have directory with several subdirectories within it. I want to find the ACL for all the directories and subdirectories within it. i need to run getfacl on all directories and subdirectories (not regular files ) in a loop. How do I do this ? Or , is there another... (1 Reply)
Discussion started by: b_pascal
1 Replies

3. Shell Programming and Scripting

How to run perl script on multiple files of two directories?

Hi I have 100 files under file A labled 1.txt 2.txt.....100.txt(made up name) I have 1 files under file B labled name.txt How can i run the same perl script on 100 files and file name.txt I want to run perl script.pl A/1.txt B/name.txt perl script.pl A/2.txt B/name.txt ....... perl... (3 Replies)
Discussion started by: grace_shen
3 Replies

4. Shell Programming and Scripting

Shell script to build directory tree and files

Hi all, I'm trying at the moment to write a shell script to build a directory tree and create files within the built directories. I've scoured through sites and text books and I just can't figure out how to go about it. I would assume that I need to use loops of some sort, but I can't seem... (8 Replies)
Discussion started by: Libertad
8 Replies

5. Shell Programming and Scripting

Simple directory tree diff script

I have had some issues with a data drive and have copied all of the data to a new drive. The size used is not the same on both drives with a 3GB difference (less on the new drive). There are millions of files on the data drive, so it is not an easy task to determine if there are some files missing... (17 Replies)
Discussion started by: LMHmedchem
17 Replies

6. Shell Programming and Scripting

Script for telnet and run one command kill it and run another command using while loop

( sleep 3 echo ${LOGIN} sleep 2 echo ${PSWD} sleep 2 while read line do echo "$line" PID=$? sleep 2 kill -9 $PID done < temp sleep 5 echo "exit" ) | telnet ${HOST} while is executing only command and exits. (5 Replies)
Discussion started by: sooda
5 Replies

7. UNIX for Dummies Questions & Answers

Using tree to display only certain directories

I want to use tree to display the directory tree. It is easy to use tree -d -L 2 However I would like to run it on specific list of directories. Can such a thing be performed? Example: /media/academic-repo/chrisd/literature tree -d prose -L 2 prose ├── computer-technology... (1 Reply)
Discussion started by: kristinu
1 Replies

8. Shell Programming and Scripting

Specific directory parsing in a directory tree

Hi friends, Hello again :) i got stuck in problem. Is there any way to get a special directory from directory tree? Here is my problm.." Suppose i have one fix directory structure "/abc/xyz/pqr/"(this will be fix).Under this directory structure i have some other directory and... (6 Replies)
Discussion started by: harpal singh
6 Replies

9. Shell Programming and Scripting

Run perl script on files in multiple directories

Hi, I want to run a Perl script on multiple files, with same name ("Data.txt") but in different directories (eg : 2010_06_09_A/Data.txt, 2010_06_09_B/Data.txt). I know how to run this perl script on files in the same directory like: for $i in *.txt do perl myscript.pl $i > $i.new... (8 Replies)
Discussion started by: ad23
8 Replies

10. UNIX for Dummies Questions & Answers

Move all files in a directory tree to a signal directory?

Is this possible? Let me know If I need specify further on what I am trying to do- I just want to spare you the boring details of my personal file management. Thanks in advance- Brian- (2 Replies)
Discussion started by: briandanielz
2 Replies
Login or Register to Ask a Question