If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > How to differentiate file and folder

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-28-04, 11:36
freelancers freelancers is offline
Registered User
 
Join Date: Jul 2004
Posts: 5
Exclamation How to compress a folder using shell script

;Hi,

hello friends,i would like to write a shell script which it can zip a folder in a certain directory.The problem is inside that directory,it contains some file and folder.I just want to zip the folder but not other file. The script should be able to check the directory,as long as it found a folder it will zip it.So is that any way to do that using shell script?Hope somebody can help me,thanks in advance

Last edited by freelancers; 07-28-04 at 11:53.
Reply With Quote
  #2 (permalink)  
Old 07-28-04, 12:00
SiverSloth SiverSloth is offline
Registered User
 
Join Date: Jun 2004
Posts: 29
Use the -f flag

example code
Code:
for file in ls
do
  if [ -f $file ]
  then 
    gzip $file
  fi
done
There are other variants
-r readable
-w writable
-x executable
and many others - I get mine from
http://www16.boulder.ibm.com/doc_lin....htm#a2659135b
because I work in ksh under AIX
Reply With Quote
  #3 (permalink)  
Old 07-28-04, 12:01
SiverSloth SiverSloth is offline
Registered User
 
Join Date: Jun 2004
Posts: 29
Ooops

Spot the deliberate mistake
it should be
Code:
for file in `ls`
....
Reply With Quote
  #4 (permalink)  
Old 07-28-04, 12:36
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Or slightly better, use a file expression to derive your file list...

for file in *
do ...



`ls` would be tokenised after the filenames are substituted, so filenames containing spaces would not be assigned to $file as expected.

e.g.

$ touch a "b c"
$ for fname in `ls`; do echo $fname; done
> a
> b
> c

$ for fname in *; do echo $fname; done
> a
> a b


...and the -d flag is probably what you want to test if it is a directory

See: man test

Last edited by Damian Ibbotson; 07-28-04 at 12:39.
Reply With Quote
  #5 (permalink)  
Old 07-30-04, 14:55
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
Cool

Why not just do:
Code:
find myDir/* -prune -type f -exec gzip {} \;
???
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On