Unix/Linux Go Back    

Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Move parent folder if contents match a parameter

Shell Programming and Scripting

Thread Tools Search this Thread Display Modes
Old Unix and Linux 01-05-2013   -   Original Discussion by springs2
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 Unix and Linux 01-05-2013   -   Original Discussion by springs2
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
Join Date: May 2005
Last Activity: 15 November 2017, 4:57 AM EST
Location: In the leftmost byte of /dev/kmem
Posts: 5,633
Thanks: 107
Thanked 1,603 Times in 1,180 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

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 09:43 AM.