Timeline for cd into all directories, execute command on files in that directory, and return to previous current directory
Current License: CC BY-SA 3.0
7 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Dec 11, 2019 at 5:03 | comment | added | dkb | if you need nested level, Ref: superuser.com/a/1132088/553982 | |
| Oct 26, 2019 at 12:56 | comment | added | starikcetin |
-bash: cd: ./*/: No such file or directory
|
|
| Sep 6, 2016 at 13:06 | comment | added | Michael Dimmitt |
this methodworks for sub directories of directories: for d in ./*/ ; do (cd "$d" && ls); done ,will not work. but, for d in ./*/ ; do (cd "$d" && for d in ./*/ ; do (cd "$d" && ls); done ); done will work. -using ls as the command in this example.
|
|
| Dec 6, 2014 at 3:25 | comment | added | Qix - MONICA WAS MISTREATED |
So since the answerer has omitted any sort of explanation, I'll attempt one. for d in ./*/ starts a loop that stores every item in ./*/ (a list of files/folders, in this case) in a variable $d. do (cd "$d" && somecommand); starts the body of the loop. Inside the body, it starts a subshell and runs the cd and somecommand commands. Since it is a child shell, the parent shell (the shell from which you're running this command) retains its CWD and other environment variables. done simply closes the loop body.
|
|
| Dec 6, 2014 at 3:13 | vote | accept | Something Jones | ||
| Dec 5, 2014 at 15:34 | history | edited | Stéphane Chazelas | CC BY-SA 3.0 |
added 3 characters in body
|
| Dec 5, 2014 at 15:32 | history | answered | psusi | CC BY-SA 3.0 |