Move parent folder if contents match a parameter | Unix Linux Forums | Shell Programming and Scripting

  Go Back    

Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Move parent folder if contents match a parameter

Shell Programming and Scripting

Closed Thread    
Thread Tools Search this Thread Display Modes
Old 01-05-2013
springs2 springs2 is offline
Registered User
Join Date: Jan 2013
Last Activity: 26 April 2013, 7:33 AM EDT
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
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.



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

Sponsored Links
Old 01-05-2013
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
Join Date: May 2005
Last Activity: 28 February 2015, 1:05 PM EST
Location: In the leftmost byte of /dev/kmem
Posts: 4,467
Thanks: 50
Thanked 910 Times in 713 Posts
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="${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.

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grep with parent folder OmarKN UNIX for Dummies Questions & Answers 9 07-11-2012 10:27 AM
copy folder and its contents to another folder amvarma77 Shell Programming and Scripting 1 06-06-2012 09:50 AM
want to move set of file from one folder to another folder natraj005 Shell Programming and Scripting 6 08-23-2011 09:30 AM
Pass parameter from a child make to a parent marina_lmv Programming 4 10-04-2010 11:24 AM
File Management: How do I move all JPGS in a folder structure to a single folder? guptaxpn Shell Programming and Scripting 4 06-11-2009 03:41 AM

All times are GMT -4. The time now is 12:25 AM.