[Solved] How to find particular files ina directory?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [Solved] How to find particular files ina directory?
# 1  
Old 03-03-2014
[Solved] How to find particular files ina directory?

Hi,

I am trying to write a script to find some files in a directory
Example: if i have files like
2014-02-01_aaaa.txt
2014-02-01_bbbb.txt
2014-02-01_cccc.txt
2014-02-01_dddd.txt and some other files how can i just check to see if there four files exits or not i tried some thing like this
Code:
FILE=/path/to/file
if [ -f $FILE ];
then
echo "File $FILE exists"
else
echo "File $FILE does not exists"
A=`find /path/to/file/ -type f -mtime -2 -exec echo '{}' ';' `
echo "$A"
fi

but this is printing all the other files names too.Any suggestions how to make it work,or any other ways to do that Smilie.
# 2  
Old 03-03-2014
FILECOUNT=$(ls | wc -l) ? Otherwise I don't understand your requirements...
# 3  
Old 03-03-2014
Code:
for file in \
2014-02-01_aaaa.txt \
2014-02-01_bbbb.txt \
2014-02-01_cccc.txt \
2014-02-01_dddd.txt
do
  if [ -f "$file" ]
  then
    echo "File $file exists"
  else
   echo "File $file does not exist"
  fi
done

# 4  
Old 03-03-2014
Quote:
Originally Posted by MadeInGermany
Code:
for file in \
2014-02-01_aaaa.txt \
2014-02-01_bbbb.txt \
2014-02-01_cccc.txt \
2014-02-01_dddd.txt
do
  if [ -f "$file" ]
  then
    echo "File $file exists"
  else
   echo "File $file does not exist"
  fi
done

Hi,
In this code how do i use path to the files
# 5  
Old 03-03-2014
It searches the files in the current directory.
Either change to the desired directory first
Code:
cd /path/to/files || exit

Or put the path into a variable and prefix it every time:
Code:
fpath=/path/to/files
for file in \
2014-02-01_aaaa.txt \
2014-02-01_bbbb.txt \
2014-02-01_cccc.txt \
2014-02-01_dddd.txt
do
  if [ -f "$fpath/$file" ]
  then
    echo "File $fpath/$file exists"
  else
   echo "File $fpath/$file does not exist"
  fi
done

This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 03-04-2014
Depending on the shell you use, which you fail to mention, you could also try
Code:
for file in 2014-02-01_{aaaa,bbbb,cccc,dddd}.txt;

This User Gave Thanks to RudiC For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Process a specific number of files ina list

Hello, I have a list of files that was created with, FILES='./'$FOLD'/'$FOLD'_continue/'$OPTIMIZE_ON'/'*'out.txt' I am doing a loop on this list for INPUT in $FILES do ... done but I may not want to process everything. Is there a simple way to just process the first 5,10,n, etc in... (2 Replies)
Discussion started by: LMHmedchem
2 Replies

2. UNIX for Advanced & Expert Users

Find all files in the current directory excluding hidden files and directories

Find all files in the current directory only excluding hidden directories and files. For the below command, though it's not deleting hidden files.. it is traversing through the hidden directories and listing normal which should be avoided. `find . \( ! -name ".*" -prune \) -mtime +${n_days}... (7 Replies)
Discussion started by: ksailesh1
7 Replies

3. Shell Programming and Scripting

[Solved] Issue with deleting files through find

Hi, I have a script similar to this #!/bin/ksh cd /orcl/bir/eod_badfiles find ./ -type f -name "*.csv" -mtime +6 -exec rm -f {} \; find ./ -type f -name "*.bad" -mtime +6 -exec rm -f {} \; cd /orcl/bir find ./ -type f -name "*.log" -mtime +6 -exec rm -f {} \; This was working fine in one... (5 Replies)
Discussion started by: Gangadhar Reddy
5 Replies

4. UNIX for Dummies Questions & Answers

How to Find Files other than specified directory ?

Hi All, I am creating one script to Archive the older log files to Archive folder and deleting older files. For example below path contains different sub folders. So searching for log files older than 2 days then zip and moving to Archive directory in the same directory. Source files :-... (4 Replies)
Discussion started by: vadlamudy
4 Replies

5. UNIX for Dummies Questions & Answers

[Solved] Writing a loop to changing the names of files in a directory

Hi, I would like to write a loop to change the names of files in a directory. The files are called data1.txt through data1000.txt. I'd like to change their names to a1.txt through a1000.txt. How do I go about doing that? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

6. UNIX for Dummies Questions & Answers

[Solved] take name of directory and files as variables

hi, want to create script that takes name of directory and all files and will copy each file to new directory. then fix errors like files do not exist or no permission to create new directory... these what I have so far... #!/bin/sh dir=~/Documents/Scripts/Copy for i in $(pwd) $(ls)... (23 Replies)
Discussion started by: me.
23 Replies

7. UNIX for Dummies Questions & Answers

[SOLVED] Delete files and folders under given directory

I have a requirement to delete the files and folders under a given directory. my directory structure is like this.. Data | A(Directory) |_PDF(Directory)----pdf files |_XML()Directory --xml files |--files | B(Directory) |_PDF(Directory)----pdf files |_XML()Directory --xml files ... (1 Reply)
Discussion started by: ramse8pc
1 Replies

8. Shell Programming and Scripting

[Solved] Find Files Created Recently and Print

Hi, I'm looking to create a script which will find all the files created in the last 24h in a directory starting with a few different letters and send them to the printer. This would be run through the cron each morning to print the last 24 hours files. I have started with this to find all... (2 Replies)
Discussion started by: rab
2 Replies

9. UNIX for Dummies Questions & Answers

Find files and display only directory list containing those files

I have a directory (and many sub dirs beneath) on AIX system, containing thousands of file. I'm looking to get a list of all directory containing "*.pdf" file. I know basic syntax of find command, but it gives me list of all pdf files, which numbers in thousands. All I need to know is, which... (4 Replies)
Discussion started by: r7p
4 Replies

10. Shell Programming and Scripting

awk command to find the count of files ina directory

hi Gurus, can anyone provide a awk command to get teh count of number of file sin a specific directory. appreciate any kind of information.. thanks (11 Replies)
Discussion started by: sish78
11 Replies
Login or Register to Ask a Question