Command Line Instructions

I have a folder with a group of folders inside of it. Using Terminal and a .Command file, I can somewhat automate a process I do regularly, to update these folders. I am even able to start in one folder and move from folder to folder using the CD command. all basic stuff. But what I would like to do, (if possible), rather than having to go in and manually add a folder each time that I add a new one, I am wondering, is there a way that I can tell the system to enter each folder and perform a task, without having to explicitly specify each folder in a CD command?
Hope I have explained myself clearly.

MacBook Pro (15-inch Late 2011), macOS Sierra (10.12.5)

Posted on Nov 7, 2018 3:56 PM

Reply
Question marked as Top-ranking reply

Posted on Nov 9, 2018 10:57 AM

first, i'm unsure if "automator" (see your launchpad) can do this for you - i've never used it. But if you can use automator i think you should prefer it. Also automator supports "Apple Script" which can do more than bash(1) can.


http://www.linuxquestions.org


the people in that forum will quickly answer your "bash shell" questions (Terminal opens a bash shell by default), i see people above have problems doing so


however let me warn you directory names matter. white spaces in the name means you need a script which properly handles spaces in names. it also matters if there are files in the (starting) directory, that your script processes only directories not files


Create this file below (using vi(1)*) and use "chmod +x file" to make it a script. Put it in "echo $PATH" so it can be found. Always run scripts like "sh file" not "./file" unless told not to. Insure your script name doesn't conflict with a system script name by using which(1). (run: man vi, vi has a manpage in section 1 which tells you how to use it)


#!/bin/sh

pwd="$PWD"

for x in *

do

[ -d "$x" ] || continue

cd "$x"

echo "$PWD"

cd "$pwd"

done



pwd= stores the name of the base directory in variable named pwd. $PWD is the value of the current directory (where you are). note the quote whenever $ is used anywhere, VERY IMPORTANT.


for do done forms a loop that continues for each item in the directory using * (you can use `find .` instead and find(1) options, in advanced situations this may matter)


you already know cd(1)


echo(1) has a manpage and just shows we've changed to a subdirectory and it's name. you'd insert whichever commands you need in place of it.

11 replies
Question marked as Top-ranking reply

Nov 9, 2018 10:57 AM in response to BDAqua

first, i'm unsure if "automator" (see your launchpad) can do this for you - i've never used it. But if you can use automator i think you should prefer it. Also automator supports "Apple Script" which can do more than bash(1) can.


http://www.linuxquestions.org


the people in that forum will quickly answer your "bash shell" questions (Terminal opens a bash shell by default), i see people above have problems doing so


however let me warn you directory names matter. white spaces in the name means you need a script which properly handles spaces in names. it also matters if there are files in the (starting) directory, that your script processes only directories not files


Create this file below (using vi(1)*) and use "chmod +x file" to make it a script. Put it in "echo $PATH" so it can be found. Always run scripts like "sh file" not "./file" unless told not to. Insure your script name doesn't conflict with a system script name by using which(1). (run: man vi, vi has a manpage in section 1 which tells you how to use it)


#!/bin/sh

pwd="$PWD"

for x in *

do

[ -d "$x" ] || continue

cd "$x"

echo "$PWD"

cd "$pwd"

done



pwd= stores the name of the base directory in variable named pwd. $PWD is the value of the current directory (where you are). note the quote whenever $ is used anywhere, VERY IMPORTANT.


for do done forms a loop that continues for each item in the directory using * (you can use `find .` instead and find(1) options, in advanced situations this may matter)


you already know cd(1)


echo(1) has a manpage and just shows we've changed to a subdirectory and it's name. you'd insert whichever commands you need in place of it.

Nov 9, 2018 11:18 AM in response to QuietMacFan

first, i'm unsure if "automator" (see your launchpad) can do this for you - i've never used it. But if you can use automator i think you should prefer it. Also automator supports "Apple Script" which can do more than bash(1) can.


http://www.linuxquestions.org


the people in that forum will quickly answer your "bash shell" questions (Terminal opens a bash shell by default), i see people above have problems doing so


however let me warn you directory names matter. white spaces in the name means you need a script which properly handles spaces in names. it also matters if there are files in the (starting) directory, that your script processes only directories not files


Create this file below (using vi(1)*) and use "chmod +x file" to make it a script. Put it in "echo $PATH" so it can be found. Always run scripts like "sh file" not "./file" unless told not to. Insure your script name doesn't conflict with a system script name by using which(1). (run: man vi, vi has a manpage in section 1 which tells you how to use it)


#!/bin/sh

pwd="$PWD"

for x in *

do

[ -d "$x" ] || continue

cd "$x"

echo "$PWD"

cd "$pwd"

done



pwd= stores the name of the base directory in variable named pwd. $PWD is the value of the current directory (where you are). note the quote whenever $ is used anywhere, VERY IMPORTANT.


for do done forms a loop that continues for each item in the directory using * (you can use `find .` instead and find(1) options, in advanced situations this may matter)


[ -d "$x" ] || continue. this uses test(1) which has shorthand [] with -d to insure we're using cd(1) on dictories not files in the directory. it uses "short circuit logic" || (OR) which continues the loop with the next * if the item "$x" is not a directory (it may be a file or something else).


you already know cd(1)


echo(1) has a manpage and just shows we've changed to a subdirectory and it's name. you'd insert whichever commands you need in place of it.


# man bash


will give you full instructions on what more you can do


# ls /bin

# ls /usr/bin


will give you a list of commands you can use as a normal user in your bash script


bash is good for quick things but unless you know awk(1) you will be somewhat limited what you can accomplish


https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Con ceptual/MacAutomationScriptingGuide/Crea…


Apple script can active a wider choice of script languages and abilities and has a more user friendly way of doing it (and help pages to more easily browse too). It may be a better thing to learn than bash(1) for you. It even allows cocoa and making graphics via script. you may need to install "xcode" from app store to use apple script. it's free and from Apple.


(in the 1980's scripting in a several powerful languages, graphics and minimal desktop manipulation too could be done in unix using bash, X11/Motif, ps and more - though few did so or knew how 🙂, cocoa isn't really new stuff!)


there is something nice things about bash(1) and Terminal. it gets the job done. it's small. it's quick to use. many people use it. it works about the same on (freeBsd and ubuntu). if you use it rarely (your not a script developing employee and needing graphics), bash is "appropriate". there are certain /usr/bin /usr/sbin that just aren't in automator or shouldn't be attempted by automator scripting, meaning you still need a Terminal.


so i can't really say learning cocoa is what you need. it may not be.

Nov 8, 2018 7:15 AM in response to NHBulldog

Hi, Recursive example...

You can also copy directories, including all the files they contain. This uses a special “flag” or “option” with the

cp command
: the
-R
or recursive flag. When you use options with commands, this additional letter—always preceded by a hyphen (-)—tells the command to do something a bit differently. The recursive option tells the
cp
command to copy every item in the folder: every sub-folder, every file and folder in every sub-folder, and so one, all the way down, to the new location. So you can copy a directory from your Desktop to your Documents folder like this:

cp -R ~/Desktop/MyFolder /Documents

https://www.macworld.com/article/2080814/master-the-command-line-copying-and-mov ing-files.html

Nov 8, 2018 12:33 PM in response to NHBulldog

I just used the cp command since I didn't & still have no idea what you're doing, but if all the folders re in Main & none re in Main that you don't wnt to act on, then the -R flag in/on Main should do it.


The important part is...

every item in the folder: every sub-folder, every file and folder in every sub-folder, and so one, all the way down

Nov 8, 2018 11:00 AM in response to BDAqua

Basically, right now I have it like this:
We start in... I'll call it the "main" folder. This folder holds the two .command files, as well as, for sake of example, I'll just call them, "Folder 1", "Folder 2", "Folder 3", etc.

So right now I have it where the primary .command file reads:


cd Folder1<calls secondary command file>

cd ..

cd Folder2

<calls secondary command file>

cd ..

<hard coded to proceed through the list of folders>

cd ..

exit

The secondary .command file lists the "update commands". This way I can simply modify the secondary .command file, rather than having to enter the same commands repeatedly. Keeps the files smaller, makes upkeep easier.

This works ok, however, it means that I have to go in and manually edit the primary file, any time that I add or remove a folder. Not a "major" thing, but if I can "automate" it, all the better.

What I would like to do is tell it, "Enter Folder 1, Run the secondary file, exit back to MAIN, enter Folder 2, etc. until it has gone through all of the folders in MAIN, and then exits. The recursive thing sounds like it is KINDA what I'm talking about, except I'm not copying files or folders, so I'm not sure if this will work for what I want to do.

Nov 8, 2018 2:13 PM in response to BDAqua

As I've looked at it, the "CP" command is the copy command. As I say, I don't need tu copy files.


To hopefully explain better, I am a Web Developer. I run MAMP on my development machine, and I have multiple sites that are in the htdocs folder. I'm using Composer, so to update a site, I just go to a site's folder, and enter, "composer update" and composer will check and update any files that may need to be updated.
Here is the script that I use:

#!/bin/bash

#this is a comment-the first line sets bash as the shell script

cd /Applications/MAMP/htdocs

cd F1
/Applications/MAMP/htdocs/n-stall.command

composer update

cd ..

cd F2

/Applications/MAMP/htdocs/n-stall.command

composer update

cd ..

cd F3

/Applications/MAMP/htdocs/n-stall.command

composer update

cd ..

cd F4

/Applications/MAMP/htdocs/n-stall.command

composer update

cd ..

cd F5

/Applications/MAMP/htdocs/n-stall.command

composer update

cd ..

cd F6

/Applications/MAMP/htdocs/n-stall.command

composer update

cd ..

cd F7

/Applications/MAMP/htdocs/n-stall.command

composer update

cd ..

cd F8

/Applications/MAMP/htdocs/n-stall.command

composer update

cd ..

cd F9

/Applications/MAMP/htdocs/n-stall.command

composer update

cd ..

cd F10

/Applications/MAMP/htdocs/n-stall.command

composer update

cd ..

exit

F1 thru F10 are the site folders. And n-stall.command is the secondary command file that contains any other commands that I may need to run. It's handy because instead of having to manually go into maybe a dozen or more folders and do the update, I can just run this script that will do it. However, this means if I add "F11" and/or remove "F5", I have to manually go in and edit this file for each change. As I want this process to happen in each folder within the htdocs folder, I'm looking for a way that it will do this in each folder within the htdocs folder without me having to manually update it for every addition or removal of sites.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Command Line Instructions

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.