Apply same command in multiple directories?

research1695

New Member
Joined
Aug 22, 2020
Messages
13
Reaction score
6
Credits
108
Is there a way to apply the same command on multiple directories (say 20+) on linux?
Any insight would be appreciated!
 


Are they totally separate directories, or are some nested under a parent directory?

Do you only have to do one time on one computer?

My thinking is... by the time I wrote a perl/bash/ansible script to do this...
I still have to type all the paths (directory names) either way, so I don't see
how automating this would save much time. You could loop thru a list, but even then,
I still have to type out the directory names to loop thru.

If they are nested under a parent directory, then just use -R to recursively chmod or chown.
 
Is there a way to apply the same command on multiple directories (say 20+) on linux?
Any insight would be appreciated!
Can you give a more concrete example?
Are the paths all close together in the file-system? Or are they spread around the file system?
Also what command/commands do you want to run on each of the directories?

In Linux, the wide range of tools we have at our disposal mean that there’s often more than one way to skin the proverbial cat.

However, the best/most efficient method often depends on exactly what you’re trying to achieve. So if you could give a little more detail, it would help us to give you a better answer!
 
Is there a way to apply the same command on multiple directories (say 20+) on linux?
Any insight would be appreciated!
Yes. If, for this instance, these directories are in /home and you wanna remove them (again - just for this example), you can type
Code:
sudo rm -rv ~/home/directory*
where directory* means that if these dirs have the same name with different numbers, all that contain the word "directory" will be deleted.
You don't say what you wanna do exactly, so I have to guess here but basically the syntax is like this: sudo [command] -rv /path/to/directories. The "-rv" means "recursive" and "verbose". "Recursive" means "apply to all subdirectories in ~/home" and "verbose" means "display details in terminal about what you're doing".
Here's an example with one of my aliases:
Code:
alias ffc="rm -rv ~/.cache/mozilla/ && rm -rfv ~/.mozilla/firefox/tjdaag57.default-release/storage/default/https*"
This removes Firefox cache and all directories whose names contain "https" inside my Firefox's profile main directory.
 
Thank you @dos2unix @JasKinasis @rado84 for your insights!

Yes, I have 20 directories under a parent directory. I want to perform data analysis using same command and same input files from all these 20 directories.

One sample analysis (gromacs for simulations) command:
gmx rms -f md.trr -s em.tpr -o rmsd.xvg

where
gmx rms - a gromacs command
-f: input file (same name in all directories)
-s: structure file (same name in all directories)
-o: output file (want the same name)

My university IT support wrote the following command in parent directory

for ff in $(ls -F | grep "/"); do cd $ff; pwd; gmx rms -f md.trr -s em.tpr -o rmsd.xvg ; cd ..; done

It is working so well!
 
OK, sounds like your university IT support already solved your problem! Ha ha!
So all 20 directories were sub-directories of a single directory.

As they are all grouped together, your IT-support used an appropriate method to perform the task. If they had been spread around the file-system - you would have needed a slightly different solution.

I didn't want to try answering your question until I had a solid idea of where the directories were. Because there are a number of different ways of solving this kind of problem. And I didn't want to try suggesting anything that might not be appropriate for your situation.
Bit of a moot point now, I suppose! Ha ha!
 

Members online


Latest posts

Top