Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

CODE EDIT

So now that I'm off work I can do a little coding in my free time. The way that I think I want this to work is take a tarball (what I would normally extract to a folder anyway) and compare that to our working directory. So....

I managed to remove the top level directories by counting their string length and subtracting that from my echo length !! Baby steps 0=)

#!/bin/bash

# $1 is the file to test
# $2 is the base folder

# minus the length of the top level directories and slashes
len=`expr length "$1$2"`-2

for file in $( tar --list -f $1 | perl -pe'chomp;$_=qq["'$2'$_" ]' )
do
  # work around bash deficiency
  if [[ -e "$( perl -eprint$file )" ]]
    then
     # do nothing, we need a file doesn't exist flag =(
     echo '';
    else
      echo `rm -f $2${file:len:-1}` # remove dir length and quotes
  fiy
done



$ ./stree mot-add.tar.gz mot/

    [output]
        # this blank space is left from removing top level directory
    server/
    server/artwork/
    server/artwork/erase-me2.jpg
    server/artwork/erase-me1.jpg
    server/artwork/erase-me3.jpg
    server/erase-me.nfo
    erase-me.log
    erase-me/
    client/
    client/dependancies/
    client/dependancies/erase-me.func.php
    client/erase-me.phtml

I'm still displaying directories that shouldn't be there. =(

removing quotes was fixed with echo ${file:len:-1} but leaves newlines for the top level directory

END CODE EDIT

So I'm thinking that since the directories now exist in two places, the will display or not display regardless. Would it be better idea to:

  1. Use the program on a switch to add/remove
  2. On add, image the directory structure to install to $ tar cf dirs.tar --no-recursion --files-from <( find . -type d )
  3. add it to the installation tar
  4. use it later maybe, as a diff file for the directories?

I dunno, opinions?

It also looks like my PERL script isn't really matching for some reason, anyone have an idea? After running a test erase I got this.

$ tree mot
mot
├── client
│   ├── dependancies
│   │   ├── err_config.ini.php
│   │   ├── modernizr.custom.97882.js
│   │   ├── properCountry.js
│   │   └── sender.php
│   └── index.php
└── server
    ├── artwork
    │   ├── cert01.jpg
    │   ├── cert02.jpg
    │   ├── cert03.jpg
    │   ├── cert04.jpg
    │   ├── cert05.jpg
    │   ├── cert06.jpg
    │   └── gdincludes
    │       ├── overlay.png
    │       └── renaissance.ttf
    ├── cache
    ├── dependancies
    │   ├── cert05.jpg
    │   ├── crypt.func.php
    │   ├── err_config.ini.php
    │   ├── gdimageprocess.func.php
    │   └── pdo.func.php
    ├── erase-me.log
    ├── listener.log
    └── receiver.php

7 directories, 21 files

$ ./stree mot-add.tar.gz mot/
mot/
mot/server/
mot/server/artwork/
mot/server/artwork/erase-me2.jpg
mot/server/artwork/erase-me1.jpg
mot/server/artwork/erase-me3.jpg
mot/server/erase-me.nfo
mot/erase-me.log
mot/erase-me/
mot/client/
mot/client/dependancies/
mot/client/dependancies/erase-me.func.php
mot/client/erase-me.phtml

Old

this is a little conceptual so I'll explain it the best I can.

I have a Copy of files that I've placed in a directory, I'd like to use that skeleton of our copy to remove all files that match on our server. It would have to be able to know if there are other files or directories that are not part of that tree and leave them. So lets do an example real quick, just in case I'm not being very clear.

Here was the origional structure

$ tree mot-origional
mot-origional
├── client
│   ├── dependancies
│   │   ├── err_config.ini.php
│   │   ├── modernizr.custom.97882.js
│   │   ├── properCountry.js
│   │   └── sender.php
│   └── index.php
└── server
    ├── artwork
    │   ├── cert01.jpg
    │   ├── cert02.jpg
    │   ├── cert03.jpg
    │   ├── cert04.jpg
    │   ├── cert05.jpg
    │   ├── cert06.jpg
    │   └── gdincludes
    │       ├── overlay.png
    │       └── renaissance.ttf
    ├── cache
    ├── dependancies
    │   ├── crypt.func.php
    │   ├── err_config.ini.php
    │   ├── gdimageprocess.func.php
    │   └── pdo.func.php
    ├── listener.log
    └── receiver.php

7 directories, 20 files

Here's what was added

$ tree add-to-mot
add-to-mot/
├── client
│   ├── dependancies
│   │   └── erase-me.func.php
│   └── erase-me.phtml
├── erase-me.log
└── server
    ├── artwork
    │   ├── erase-me1.jpg
    │   ├── erase-me2.jpg
    │   └── erase-me3.jpg
    └── erase-me.nfo

4 directories, 7 files

Then the combined would look like this

$ tree mot
mot
├── client
│   ├── dependancies
│   │   ├── erase-me.func.php
│   │   ├── err_config.ini.php
│   │   ├── modernizr.custom.97882.js
│   │   ├── properCountry.js
│   │   └── sender.php
│   ├── erase-me.phtml
│   └── index.php
├── erase-me.log
└── server
    ├── artwork
    │   ├── cert01.jpg
    │   ├── cert02.jpg
    │   ├── cert03.jpg
    │   ├── cert04.jpg
    │   ├── cert05.jpg
    │   ├── cert06.jpg
    │   ├── erase-me1.jpg
    │   ├── erase-me2.jpg
    │   ├── erase-me3.jpg
    │   └── gdincludes
    │       ├── overlay.png
    │       └── renaissance.ttf
    ├── cache
    ├── dependancies
    │   ├── cert05.jpg
    │   ├── crypt.func.php
    │   ├── err_config.ini.php
    │   ├── gdimageprocess.func.php
    │   └── pdo.func.php
    ├── erase-me.nfo
    ├── listener.log
    └── receiver.php

7 directories, 27 files

So I keep a directory fleshed out like add-to-mot/ always for backup purposes, but I would like to try taking the output from a tree for instance....

[output]
add-to-mot
client
dependancies
erase-me.func.php
erase-me.phtml
erase-me.log
server
artwork
erase-me1.jpg
erase-me2.jpg
erase-me3.jpg
erase-me.nfo

$ tree -i add-to-mot -o rm-file.nfo
$ cat rm-file.nfo | rm -rf mot/*

Or something like the above, where I pipe the output of one directory tree, into a remove statement for a different directory tree. Does that make sense? I thought it would be an interesting "How would I?" question. I'm not aversed to writing a bash script to do this, it doesn't necessarily have to be a one liner.

That above will delete without prejudice though, I'd like to be able to sense other files and directories, and not delete willy-nilly. Hey it might be nice to even create a backup (mot-treetrim.tar.gz) until I can verify that I haven't mass erased important files/folders.

Thanks greatly guys!

share|improve this question
    
It doesn't use tree, but you could just use find ./, cut off the leading bit, replace it with the real leading bit, and feed it to rm. It's a functional solution –  hbdgaf Apr 18 '12 at 18:57
2  
The title made me think this should be moved to gardening.stackexchange.com ;) –  David Oneill Apr 19 '12 at 3:48
    
Hahaha, right? Organic coding man..... its the .... wait, what? lol –  ehime Apr 19 '12 at 3:59
add comment

1 Answer 1

up vote 0 down vote accepted

I think about something like this:

for h in `tree -i -f add-to-mot | sed s/add-to-mot/mot/`; do rm -f $h; done

So you just list files with paths, replace add-to-mot with mot in path, and remove files. It won't work with files with spaces in their names :)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.