Help with folder and file difference


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with folder and file difference
# 1  
Old 11-24-2010
Help with folder and file difference

Hello,

I was wondering how to make difference between file and folder while using ls -l?

I see it on the output because "d" is for dir and "-" for file, but how to make this input to other command?

Because I am trying to bzip2 if it's the file and tar if it's a directory.
# 2  
Old 11-24-2010
using tests:
Code:
if [ -f $file ]
then
   echo " $file is an ordinary file"
fi
if [ -d $file ]
then 
     echo " $file is a directory " 
fi

These 2 tests, you should be able to figure out how to use them for you needs
# 3  
Old 11-24-2010
You can use find command along with '-type' switch to filter out files and dirs. E.g.:

Code:
find /somewhere -type d | xargs tar -cvf something.tar

# 4  
Old 11-24-2010
Okey, so I tried using both methods. With first one I jsut failed, because I tried something like that:
Code:
ls -lrt | tail -1
if [ -f $file ]
then 
bzip2 $file #
fi
if [ -d $file ]
then
tar -xf $file 
fi

OUTPUT:
Code:
  is an ordinary file
  is a directory

I know it may look dumb for you, but I know it's got wrong syntax.

second methods just zips me ALL the files and DIRS in the folder. :/

Last edited by juzt1s; 11-24-2010 at 10:51 AM..
# 5  
Old 11-24-2010
Code:
FILE=$(ls -rt | tail -1)

if [ -f $FILE ]
then
   echo " $FILE is an ordinary file, using bzip..."
   bzip2 $FILE 
elif  [ -d $FILE ]
then
   echo " $file is a directory, using tar "
   tar -xf $FILE
fi


Last edited by vbe; 11-24-2010 at 11:23 AM.. Reason: typos...
# 6  
Old 11-24-2010
Quote:
Originally Posted by vbe
Code:
FILE=$(ls -rt | tail -1)

if [ -f $FILE ]
then
   echo " $FILE is an ordinary file, using bzip..."
   bzip2 $FILE 
elif  [ -d $FILE ]
then
   echo " $file is a directory, using tar "
   tar -xf $FILE
fi

nearly OK, but my terminal spams:
Code:
bzip2: Input file 1.sh.bz2 already has .bz2 suffix.
bzip2: Input file 1.sh.bz2 already has .bz2 suffix.
bzip2: Input file 1.sh.bz2 already has .bz2 suffix.
bzip2: Input file 1.sh.bz2 already has .bz2 suffix.
bzip2: Input file 1.sh.bz2 already has .bz2 suffix.

# 7  
Old 11-24-2010
So it was done the first time already...

You would have to test if the file given in input ($FILE) has already the .bz2 suffix...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk code to find difference in second file which is not present in first file .

Hi All, I want to find difference between two files and output only lines which are not present in second file .I am using awk and I am getting only the first difference but I want to get all the lines which are not present in file2 .Below is the code I am using . Please help to get the desired... (7 Replies)
Discussion started by: srinivasrao
7 Replies

2. UNIX for Beginners Questions & Answers

UNIX utility to find difference in folder, file and contents of file against a base version

Hi, I am trying to find out whether there are any Unix utilities that compares folders, files and contents within the file and provides a comprehensive report. The comparison can be against base version of a folder and file with content. Can you please let me know of such a utility? Thanks,... (6 Replies)
Discussion started by: Sripathi_ks
6 Replies

3. Shell Programming and Scripting

How do you compare two folders and copy the difference to a third folder in a remote server?

How do you compare one local folder and a remote folder and copy the difference to a third folder in a remote folder.e.g. Folder A -- Is in a remote server and it has the following files TEST1.OUT TEST2.OUT TEST3.OUT Folder B --Is in a local server and it has the following files ... (5 Replies)
Discussion started by: cumeh1624
5 Replies

4. Shell Programming and Scripting

want to move set of file from one folder to another folder

Hi all, let me explain my requirments i am having 5 folder with different name for eg) abc , cdf , efd, rtg, ead each 5 folders contain 15 files i want to move 10 files to some other folder, remain 5 files should be there in the same folder. give me some suggestion on this. (6 Replies)
Discussion started by: natraj005
6 Replies

5. Shell Programming and Scripting

File Management: How do I move all JPGS in a folder structure to a single folder?

This is the file structure: DESKTOP/Root of Photo Folders/Folder1qweqwasdfsd/*jpg DESKTOP/Root of Photo Folders/Folder2asdasdasd/*jpg DESKTOP/Root of Photo Folders/Folder3asdadfhgasdf/*jpg DESKTOP/Root of Photo Folders/Folder4qwetwdfsdfg/*jpg DESKTOP/Root of Photo... (4 Replies)
Discussion started by: guptaxpn
4 Replies

6. Shell Programming and Scripting

Move the file from one folder to another folder

Hi, I have a requirement to move a file from one folder(a) to another folder(b) only when folder (b) have a write permission. Folder permission is 755 If the permission is otherthan 755 we need to come out of the loop I will appreciate your help Thanks Soll (1 Reply)
Discussion started by: sollins
1 Replies

7. Shell Programming and Scripting

Calculate the time difference between a local file and a remote file.

I m stuck with a issue. I need to calculate the time difference between two files.. one on the local machine and one on the remote machine using a script. Can any one suggest the way this can be achevied Thanks, manohar (1 Reply)
Discussion started by: meetmano143
1 Replies

8. Shell Programming and Scripting

compare 2 file and print difference in the third file URG PLS

Hi I have two files in unix. I need to compare two files and print the differed lines in other file Eg file1 1111 2222 3333 file2 1111 2222 3333 4444 5555 newfile 4444 5555 Thanks In advance (3 Replies)
Discussion started by: evvander
3 Replies

9. Shell Programming and Scripting

Parse the .txt file for folder name and FTP to the corrsponding folder.

Oracle procedure create files on UNIX folder on a regular basis. I need to FTP files onto windows server and place the files, based on their name, in the corresponding folders. File name is as follows: ccyymmddfoldernamefile.txt; Folder Name length could be of any size; however, the prefix and... (3 Replies)
Discussion started by: MeganP
3 Replies

10. UNIX for Dummies Questions & Answers

How to compare the difference between a file and a folder??

Hi, I have a .txt file which has to be compared with a folder and print the difference to some other .txt file. I did try with the diff command..i mean diff /tmp/aaa/bbb.txt /space/aaa/bbb/ /***bbb.txt contains all the files names which may or may not exist in the folder bbb..so i need... (2 Replies)
Discussion started by: kumarsaravana_s
2 Replies
Login or Register to Ask a Question