tags:

views:

75

answers:

3

Basically I need to run a Unix script to find all folders in the directory /fss/fin, if it exists; then I have tar it and move to another directory /fs/fi.

This is my command so far:

find /fss/fin -type d -name  "essbase" -print

Here I have mentioned folder name essbase instead of that I have to find all the folder in the /fss/fin.

How to find all folders in the /fss/fin directory & tar it to move to /fs/fi?

Clarification 1:

Yes I need to find only all folders in directory /fss/fin directory using Unix shell script and tar it to another directory /fs/fi.

Clarification 2:

I want to make it clear with the requirement. The Shell Script should contain:

  1. Find all the folders in the directory /fss/fin
  2. Tar the Folders
  3. Move the folders in another directory /fs/fi which in server s11003232sz.net
  4. When user requests untar the Folders and move it back to the orignal directory /fss/fin
A: 

Since tar does directories automatically, you really don't need to do very much. Assuming GNU tar:

tar -C /fss/fin -cf - essbase |
tar -C /fs/fi   -xf -

The '-C' option changes directory before operating. The first tar writes to standard output (the lone '-') everything found in the essbase directory. The output of that tar is piped to the second tar, which reads its standard input (the lone '-'; fun isn't it!).


Assuming GNU find, you can also do:

(cd /fss/fin; tar -cf - $(find . -maxdepth 1 -type d | sed '/^\.$/d')) |
              tar -xf - -C /fs/fi

This changes directory to the source directory; it runs 'find' with a maximum depth of 1 to find the directories and removes the current directory from the list with 'sed'; the first 'tar' then writes the output to the second one, which is the same as before (except I switched the order of the arguments to emphasize the parallelism between the two invocations).


If your top-level directories (those actually in /fss/fin) have spaces in the names, then there is more work to do again - I'm assuming none of the directories to be backed up start with a '.':

(cd /fss/fin; find * -maxdepth 0 -type d -print 0 | xargs -0 tar -cf -) |
 tar -xf - -C /fs/fi

This weeds out the non-directories from the list generated by '*', and writes them with NUL '\0' (zero bytes) marking the end of each name (instead of a newline). The output is written to 'xargs', which is configured to expect the NUL-terminated names, and it runs 'tar' with the correct directory names. The output of this ensemble is sent to the second tar, as before.

If you have directory names starting with a '.' to collect, then add '.[a-z]*' or another suitable pattern after the '*'; it is crucial that what you use does not list '.' or '..'. If you have names starting with dashes in the directory, then you need to use './*' and './.[a-z]*'.

If you've got still more perverse requirements, enunciate them clearly in an amendment to the question.

Jonathan Leffler
Unix script should first find all the folders and then tar it ,so how we can write a unix shell script
Shenna
That is a shell script (a two-liner); it doesn't need to find the folders because 'tar' is good about doing that for you. If you want more than just the 'essbase' directory, then we have a bit more work to do.
Jonathan Leffler
How we can write a script for that
Shenna
Im getting with the coding OK i want to make it clear with the requirementShell Script should contains1)Find all the folders in the directory /fss/fin2)Tar the Folders3)Move the folders in another directory /fs/fi which in server s11003232sz.net4)When user requests untar the Folders and move it back to the orignal directory /fss/fin
Shenna
A: 

This should do it:

'#!/bin/sh

list=find . -type d

for i in $list

do

if [ ! "$i" == "." ]; then

    tar -czf ${i}.tar.gz ${i}

fi

done

mv *.tar.gz ~/tardir'

abc
A: 
find /fss/fin -d 1 -type d -name "*" -print

The above command gives you the list of 1st level subdirectories of the /fss/fin. Then you can do anything with this. E.g. tar them to your output directory as in the command below

tar -czf /fss/fi/outfile.tar.gz `find /fss/fin -d 1 -type d -name "*" -print`

Original directory structure will be recreated after untar-ing.

Max Doronin