开发者

Unix script to find all folders in the directory, then tar and move them

开发者_运维百科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


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


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.


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.


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜