Move parent folder if contents match a parameter

Hi all,

Hoping someone can help.

I'm looking to be able to search a particular folder path for folders that contain certain files / formats and then move the parent folder.

eg.

/foo/bar/folder1/file.exe
/foo/bar/folder2/file.exe

and then move the folder1/2 tp another folder. So if the contents of the folder contain say a file format of .exe it will move it otherwise ignore it

Thanks

You can move a folder like you move a file: with the "mv" command:

mv /path/to/old/folder /path/to/new/folder

You just have to search for the files/folders or whatever triggers the moving action (look at the man page of "find" for this) and then cut off the last part(s) of the resulting path as parameter for "mv". You can do that with shell parameter expansion.

For instance: suppose you found a file "/path/we/indend/to/move/file.exe" and you want to move everything from "/path/we/intend/to" to "/somewhere/else":

foundfile="/path/we/intend/to/move/file.exe"     # this would have been found
target=""
destination="/somewhere/else"

target="${foundfile%/*}"                   # chops off the filename -> "/path/we/intend/to/move"
target="${foundfile%/*/*}"                 # chops off filename plus one dir -> "/path/we/intend/to"
target="${foundfile%/*/*/*}"               # chops off filename plus two dirs -> "/path/we/intend"

mv "$target" "$destination"                 # carry out the moving action

I hope this helps.

bakunin