Contents
How to do something in subdirectories in Unix?
Do something in directories, sent output to a spool file and move on to next directory. Consider Main Dir = /tmp Sub Dir = A B C D (Four sub directories) It searches only the subdirectories of the directory /path/to/dir. Note that the simple example above will fail if the directory names contain whitespace or special characters.
How to traverse all subdirectories in Bash shell?
Although doubtless this can be done in Bash shell scripting, in the old days we used find [-maxdepth ] -exec ; to achieve this. You could do a man find and play around, perhaps until someone tells you how to do it in bash!
How to run a file in a different directory?
Go to its directory and run to see all the executables in that directory. The $PATH shell variable contains the directories where the executables are searched. Add the directory containing your executable into $PATH and it can be executed from anywhere.
Can a script be run from any directory?
Although above method allows the script to be run from anywhere you should make sure the script doesn’t depend on the pwd (present working directory) for execution (unless intended). P.S.:
How to run a script in the same directory?
Since $0 holds the full path of the script that is running, you can use dirname against it to get the path of the script: so if you for example store it in /tmp/a.sh then you will see an output like: Using dirname “$0” will allow you to keep track of the original path. Again, since you have the path in $0 you can cd back to it.
How to run script in same directory as helper.sh?
In most cases, when helper.sh is in the same directory as script.sh, you can use in script.sh command: $0 stores the name of your process (in most cases it’s the full path to your script). $ {parameter%word} removes suffix pattern word from variable $parameter (in the command above it removes file name /* from the full path stored in variable $0 ).
How to Recursively list files and directories in shell?
Replace . by your directory of interest. On Windows, you can do it like this as most flexibile solution that allows you to additionally process dir names. You use FOR /R to recursively execute batch commands. Check out this batch file.
How to recursively execute batch commands in shell?
You use FOR /R to recursively execute batch commands. Check out this batch file. @echo off SETLOCAL EnableDelayedExpansion SET N=0 for /R %%i in (.) do ( SET DIR=%%i ::put anything here, for instance the following code add dir numbers.