Check if files inside a text file are found under a directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check if files inside a text file are found under a directory
# 1  
Old 10-16-2014
Check if files inside a text file are found under a directory

Hi all,

Please somebody help me with this:

I want to check if the files listed in a text file, are found under a directory or not.

For example: the file is list_of_files.txt, which contains inside this rows:

Code:
[root@localhost ~]# cat list_of_files
logs
errors
paths
debug
[root@localhost ~]#

I want to check if these 4 files, are found under directory, for example /tmp

If so, everything is OK, if not, print the files which are missing.

Thank you!

Last edited by rbatte1; 10-16-2014 at 07:21 AM.. Reason: Added CODE tags
# 2  
Old 10-16-2014
Quote:
Originally Posted by arrals_vl
Hi all,

Please somebody help me with this:

I want to check if the files listed in a text file, are found under a directory or not.

For example: the file is list_of_files.txt, which contains inside this rows:

[root@localhost ~]# cat list_of_files
logs
errors
paths
debug
[root@localhost ~]#

i want to check if these 4 files, are found under directory, for example /tmp.

If so, everything is ok, if not, print the files which are missing.

Thank you!
Hello arrals_vl,

Could you please try following script.

Code:
echo "please enter the complete path where you want to search for files."
read path
while read line
do
    NAME=`find "$path" -type f -name "$line" 2>/dev/null`
    if [[ -n "$NAME" ]]
    then
        echo "$line" file has been found.
    else
        echo "$line" file NOT found.
    fi
done < "files"

In my case output is as follows.
Code:
please enter the complete path where you want to search for files.
/tmp
chumma file NOT found.
tets100 file has been found.
test98 file has been found.
test8 file has been found.
test7 file has been found.
test6 file has been found.
test5 file has been found.
test4 file has been found.
test3 file has been found.
test2 file has been found.
test1 file has been found.

Where file named files is the input file with all file names you want to search for.

Thanks,
R. Singh
# 3  
Old 10-16-2014
If you mean the specific directory and not searching all sub-folders, you can simplify this with:-
Code:
while read file
do
   [ -f /tmp/$file ]
   then
      print "Found file $file"
   else
      print "File $file not found."
   fi
done < list_of_files


I hope that this helps,
Robin
# 4  
Old 10-16-2014
Depending on your version of grep, this might produce the missing files:
Code:
ls -1 /tmp | grep -vxf - list

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to check the files existence inside a directory.

Hello Folks, On Below Script, I want to apply condition. Condition that it check the directory for files if not found keep checking. Once directory have files and founded do the calculation and finish the code. Script to check the files existence inside a directory, If not then keep... (16 Replies)
Discussion started by: sadique.manzar
16 Replies

2. Shell Programming and Scripting

Append string to all the files inside a directory excluding subdirectories and .zip files

Hii, Could someone help me to append string to the starting of all the filenames inside a directory but it should exclude .zip files and subdirectories. Eg. file1: test1.log file2: test2.log file3 test.zip After running the script file1: string_test1.log file2: string_test2.log file3:... (4 Replies)
Discussion started by: Ravi Kishore
4 Replies

3. Shell Programming and Scripting

Copy files listed in text file to new directory

I am trying to write a script that will copy all file listed in a text file (100s of file names) to a new directory Assume script will run with main as current working directory and I know how many files/lines will be in List.txt Im trying to work up a test script using this model Contents of... (2 Replies)
Discussion started by: IAmTheGrass
2 Replies

4. Shell Programming and Scripting

How to grep all the files inside the directory and Sub directory

Hi, I have used the command cat * | grep -r <<String>> * It returns: cat : JAN : is directory *********************** ********************* My directory structure: log - JAN -catalina.out -FEB -catalina.out -MARCH ... (11 Replies)
Discussion started by: nanthagopal
11 Replies

5. Shell Programming and Scripting

Replace Filename and text inside of directory

I have a directory that has directories that contain Dir-20111114-xyz and I want to change them to Dir-20111121-xyz. Inside of Dir-20111114-xyz, I have a config.xml file that also contains the date that I need changed from 20111114 to 20111121 I have used sed to replace inside of file not... (4 Replies)
Discussion started by: icculus99
4 Replies

6. Homework & Coursework Questions

copy files inside a text file

Hi Guys , I am new to this and Hi to all ,Need your help I am trying to copy Files which are inside file.txt The files inside file.txt are inthe below order file1.log file2.log file3.log ....... I want to copy these files to an output Directory , Please help (1 Reply)
Discussion started by: hc17972
1 Replies

7. Homework & Coursework Questions

copy files inside a text file

Hi Guys , I am new to this and Hi to all ,Need your help I am trying to copy Files which are inside file.txt The files inside file.txt are inthe below order file1.log file2.log file3.log ....... I want to copy these files to an output Directory , Please help (1 Reply)
Discussion started by: hc17972
1 Replies

8. UNIX for Dummies Questions & Answers

Zip files inside the directory, but not the directory itself

Hi, Im facing a problem that im stucked, I have the following structure: thales@pereirtc-vbox:/home/VfARM$ ls code config doc lib manifest.bak manifest.rel manifest.v3 ns pub if i try to execute zip -q -o arm.zip VfARM/* it will create a zip file with the folder VfARM.... (2 Replies)
Discussion started by: Thales.Claro
2 Replies

9. Shell Programming and Scripting

How to strip ^M at end of each files for all files found in current directory

I am trying to use a loop to strip of the funny character ^M at the end of all lines in each file found in current directory and I have used the following in a script: find . -type f -name '*.txt' | while read file do echo "stripping ^M from ..." ex - "$file" > $tempfile %s/^M//g wq! # mv... (4 Replies)
Discussion started by: bisip99
4 Replies

10. UNIX for Advanced & Expert Users

Replacing text inside a directory

This is my first post to this group.I am new to Unix.I have one requirement I need to replace a text in all the files in a directory and sub-directory.Eg i have a folder structure like /usr/local/abc , inside abc i have several directories and several files. All the files inside abc and all the... (1 Reply)
Discussion started by: vatsan
1 Replies
Login or Register to Ask a Question