1

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 directly mentioned the folder name essbase. But instead, I would like to find all the folders in the /fss/fin and use them all.

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

Clarification 1:

Yes I need to find only all folders in the directory /fss/fin directory using a Unix shell script and tar them 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 is located on the server s11003232sz.net
  4. On user requests it should untar the Folders and move them back to the orignal directory /fss/fin
Community
  • 1
  • 1
Shenna
  • 27
  • 1
  • 1
  • 3
  • do you need all the files in the folders or just the folders themselves? – seejay Oct 22 '10 at 08:24
  • Please edit the question to clarify the requirements. – Jonathan Leffler Oct 22 '10 at 14:00
  • You still need to think and explain more clearly what you are after. In particular, is there an arbitrary time gap between steps 1..3 and step 4? A single script is not a good way to handle such time gaps. Where should the tar file be placed? It is not clear what you mean in step 3 - what does the 'which in server xxxx' comment mean? Are you seeking to move the data to another machine? Why is 4 not 'move the folders back to the original location'? Why move the directories when you've made a copy in the tar file? Why not just remove the folders? What is the real objective? Homework? – Jonathan Leffler Oct 22 '10 at 14:09

5 Answers5

6

here is an example I am working with that may lead you in the correct direction

BackUpDIR="/srv/backup/"
SrvDir="/srv/www/"
DateStamp=$(date +"%Y%m%d");

for Dir in $(find $SrvDir* -maxdepth 0 -type d ); 
do
    FolderName=$(basename $Dir);
    tar zcf "$BackUpDIR$DateStamp.$FolderName.tar.gz" -P $Dir
done
Chris Hough
  • 3,389
  • 3
  • 41
  • 80
  • 1
    the question and your replay give us an idea to compress the cpanel daily folder when it use incremental method take a look to http://subversion.assembla.com/svn/lhth/trunk/bash/compress_folder_folders.sh so thanks for your replay and for your help – Mohammed Shannaq Jan 16 '12 at 22:54
2

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
  • 730,956
  • 141
  • 904
  • 1,278
  • Unix script should first find all the folders and then tar it ,so how we can write a unix shell script – Shenna Oct 22 '10 at 08:35
  • 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 Oct 22 '10 at 08:37
  • How we can write a script for that – Shenna Oct 22 '10 at 08:42
  • Im getting with the coding OK i want to make it clear with the requirement Shell Script should contains 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 – Shenna Oct 22 '10 at 09:30
0

Here is a bash example (change /fss/fin, /fs/fi with your paths):

dirs=($(find /fss/fin -type d))
for dir in "${dirs[@]}"; do
  tar zcf "$dir.tgz" "$dir" -P -C /fs/fi && mv -v "$dir" /fs/fi/
done

which finds all the folders, tar them separately, and if successful - move them into different folder.

kenorb
  • 155,785
  • 88
  • 678
  • 743
0
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.

-1

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
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Ankur Agarwal
  • 23,692
  • 41
  • 137
  • 208