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:
- Find all the folders in the directory
/fss/fin
- Tar the folders
- Move the folders in another directory
/fs/fi
which is located on the servers11003232sz.net
- 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
Here is a bash example (change
/fss/fin
,/fs/fi
with your paths):which finds all the folders, tar them separately, and if successful - move them into different folder.
Since
tar
does directories automatically, you really don't need to do very much. Assuming GNU tar:The '
-C
' option changes directory before operating. The firsttar
writes to standard output (the lone '-') everything found in theessbase
directory. The output of thattar
is piped to the secondtar
, which reads its standard input (the lone '-'; fun isn't it!).Assuming GNU find, you can also do:
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 '.':
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.
This should do it:
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
Original directory structure will be recreated after untar-ing.