Find file only from a Directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find file only from a Directory
# 8  
Old 08-25-2011
Hi,
The above solution has worked prefectly as expected....
Incase if i want to proceed with moving the files from Source to Target , only if the log file exits,if not do not do the archive but proceed with the next step in the script.
how can this be achived
Below is the orignal query that i am using:
I have used an "if condtion" to check the status of previous, however it always return 0.
Code:
find $LOGDIR -maxdepth 1 \( -name "*.log" -o -name "*.out" \) |
if [ $? != 0 ]; then
  echo "No Log Files to Achrive"
else
  while IFS="." read FILE
  do
    BASE=`basename "$FILE"`
    echo "$BASE" >/tmp/$$
    IFS="." read BASE EXT < /tmp/$$
    mv "$FILE" "$LOGARCHIVE/$BASE.$datetimestamp.$EXT"
    if [ $? != 0 ]
    then
      echo "! Error - Error archiving log file !"
    fi
  done
fi


Last edited by Franklin52; 08-25-2011 at 03:01 PM.. Reason: Please use code tags for data and code samples, thank you
# 9  
Old 08-25-2011
Quote:
Originally Posted by ch33ry
Incase if i want to proceed with moving the files from Source to Target , only if the log file exits,if not do not do the archive but proceed with the next step in the script.
Sorry, what? I don't think that translated very well.
# 10  
Old 08-25-2011
Hi Corona688,

Sorry i could convey my message correctly......

In my query the "Find" is checking for files with ".log" and ".out" in the LOG_DIR,

I wanted to proceed with next steps only if the files exists.
If not exit from the while loop.

I tried checking with status of the previous command using if..but its not working.....

Please suggest.
# 11  
Old 08-25-2011
Quote:
Originally Posted by ch33ry
I wanted to proceed with next steps only if the files exists.
So it should only move in/file.ext to out/file-date.ext when out/file-date.ext already exists? Smilie

If not that, then, when what already exists? The file you haven't moved yet? It already exists by definition...
# 12  
Old 08-25-2011
if file.ext exits in Dir "in" then move it to out/file-date.ext.

else simply quit from the while loop....
# 13  
Old 08-25-2011
find never prints nonexistent files. If find finds no files at all, it prints exactly nothing.

If the while read loop reads nothing the very first time, the code inside it won't run even once.

That's why your error-checking code doesn't report any errors -- there weren't any.

It's not useless, though. You might be unable to move the files for other reasons, like directory permissions.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to find and get a file in an entire directory with an excluded directory specified?

How to get a file 'zlib.h' in an entire directory with an excluded directory specified lives under that starting directory by using find command, as it failed on: $ find . -name 'zlib.h' -a -ipath 'CHROME.TMP' -prune -o -print it'll just list entirely up (2 Replies)
Discussion started by: abdulbadii
2 Replies

2. Solaris

/bin/find: stat() error <File> : No such file or directory

Hi, I am getting below error in Solaris 10 SPARC when trying to issue a search on /var/tmp partition Below is the query /bin/find /var/tmp/ -type f -atime +1 Below is the result /bin/find: stat() error <File> : No such file or directory (28 Replies)
Discussion started by: prash358
28 Replies

3. Shell Programming and Scripting

how to find a pattern from an external file in a directory containing multiple file recursively

Hi, Need your help in this. I have an input file that has multiple enrollment_number, somewhat like 1234567 8901234 9856321 6732187 7623465 Now i have to search and delete these enrollment_number recursively from all the files that are within multiple sub-directories of a... (10 Replies)
Discussion started by: mukulverma2408
10 Replies

4. UNIX for Dummies Questions & Answers

cannot find file directory

hi, i use command at below to copy the file and put it in the new file (if no such file, it will create right ) cat id_rsa.pub >>~/.ssh/authorized_keys it give me error bash: /home/pc2/.ssh/authorized_keys : No such file or directory can anyone tell me why this happened ? thanks in... (1 Reply)
Discussion started by: Ericyue
1 Replies

5. Shell Programming and Scripting

Find file directory

Hi, I am using the below command for searching the file var_SearchFile= find $var_DataSourcePath -name $var_SourceFileName Output: c:\test\Sample\Testfile.txt But I need to retrive only the Path that is "c:\test\Sample\" (8 Replies)
Discussion started by: magesh_bala
8 Replies

6. Shell Programming and Scripting

Find file in a directory

Hi, I'm trying to write a script that search file in folder. I got problems with selecting specific file from the list of the files. when I runing the script I get a list of files instead of one specific file. I'm new with linux :) #!/bin/bash FILE=false ISFOUND=false while getopts... (6 Replies)
Discussion started by: SimonBASH
6 Replies

7. Shell Programming and Scripting

find and copy file to another directory..

Hi Everybody, i want a samll help to write a script. i had source location with :/user/bin (bin contains subdirectories with like names emails etc and had several files in each subdirectory) and target location with :/usr/scripts (having same subdirectories names and had some files)... (1 Reply)
Discussion started by: Reddy482
1 Replies

8. AIX

find for specific content in file in the directory and list only file names

Hi, I am trying to find the content of file using grep and find command and list only the file names but i am getting entire file list of files in the directory find . -exec grep "test" {} \; -ls Can anyone of you correct this (2 Replies)
Discussion started by: madhu_Jagarapu
2 Replies

9. UNIX for Dummies Questions & Answers

how to find a file named vijay in a directory using find command

I need to find whether there is a file named vijay is there or not in folder named "opt" .I tried "ls *|grep vijay" but it showed permission problem. so i need to use find command (6 Replies)
Discussion started by: amirthraj_12
6 Replies

10. UNIX for Dummies Questions & Answers

how to find a corrupted file in a directory

Hi, Dunno if this has already been discussed but can anyone tell me if there is any utility/command to find a corrupted file in an unix directory. With smile, karthik (1 Reply)
Discussion started by: skarthik_d
1 Replies
Login or Register to Ask a Question