ls -l /directory/path | grep -f filter.txt NOT WORKING


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ls -l /directory/path | grep -f filter.txt NOT WORKING
# 1  
Old 05-30-2012
Question ls -l /directory/path | grep -f filter.txt NOT WORKING

Hi,

I have been searching all over and cannot find a script or command that simply search or match the filenames listed in a file and match it from a directory.

so far,

example:

Code:
cat filter.txt
file1.def
file2.conf
file3.def
 
ls -l /directory | grep -f filter.txt


Last edited by Scott; 05-30-2012 at 04:51 AM.. Reason: Code tags
# 2  
Old 05-30-2012
Code:
FILE=filter.txt
    for line in $( cat $FILE )
    do
ls -l /directory | grep $line
done


Last edited by Franklin52; 05-30-2012 at 04:57 AM.. Reason: Please use code tags
# 3  
Old 05-30-2012
"Not working" means what, exactly? It's displaying all files, no files? Could it be that filter.txt has a blank line at the end?
# 4  
Old 05-30-2012
Code:
[localuser@localhost ~]$ cat alice.filenames
request-key.conf
devscripts.conf
kernel-img.conf
nsswitch.conf
ca-certificates.conf
ntp.conf
updatedb.conf
libao.conf
deluser.conf
uniconf.conf
sysctl.conf
mtools.conf
preload.conf
netscsid.conf
mtab.defs
passwd.defs
sudoers.defs
hosts.defs
qemu-ifup
pbuilderrc.defs
profile.defs
debian_version.defs
networks.defs
bindresvport.blacklist.defs
lintianrc.defs
bash_completion.defs
locale.alias.defs
dput.cf.defs
inputrc.defs
protocols.defs
a2ps.cfg.defs

Code:
[localuser@localhost ~]$ ls -l wiz
total 808
-rw-r--r--. 1 localuser localuser 15070 May 25 00:14 a2ps.cfg.defs
-rw-r--r--. 1 localuser localuser 2562 May 25 00:14 a2ps-site.cfg.defs
-rw-r--r--. 1 localuser localuser 2980 May 25 00:14 adduser.conf
-rw-r--r--. 1 localuser localuser 64 May 25 00:14 aliases.defs
-rw-r--r--. 1 localuser localuser 390 May 25 00:14 anacrontab.defs
-rw-r--r--. 1 localuser localuser 1938 May 25 00:14 bash.bashrc.defs
-rw-r--r--. 1 localuser localuser 51762 May 25 00:14 bash_completion.defs
-rw-r--r--. 1 localuser localuser 344 May 25 00:14 bindresvport.blacklist.defs
-rw-r--r--. 1 localuser localuser 322 May 25 00:14 blkid.conf
-rw-r--r--. 1 localuser localuser 16 May 25 00:14 blkid.tab.defs
-rw-r--r--. 1 localuser localuser 7134 May 25 00:14 bogofilter.cf.defs
-rw-r--r--. 1 localuser localuser 34 May 25 00:14 brlapi.key.defs
-rw-r--r--. 1 localuser localuser 6332 May 25 00:14 ca-certificates.conf
-rw-r--r--. 1 localuser localuser 230 May 25 00:14 casper.conf
-rw-r--r--. 1 localuser localuser 3878 May 25 00:14 cowpoke.conf
-rw-r--r--. 1 localuser localuser 724 May 25 00:14 crontab.defs
-rw-r--r--. 1 localuser localuser 54 May 25 00:14 crypttab.defs
-rw-r--r--. 1 localuser localuser 88 May 25 00:14 cvs-cron.conf
-rw-r--r--. 1 localuser localuser 126 May 25 00:14 cvs-pserver.conf
-rw-r--r--. 1 localuser localuser 2968 May 25 00:14 debconf.conf
-rw-r--r--. 1 localuser localuser 12 May 25 00:14 debian_version.defs
-rw-r--r--. 1 localuser localuser 600 May 25 00:14 deluser.conf
-rw-r--r--. 1 localuser localuser 17268 May 25 00:14 devscripts.conf
-rw-r--r--. 1 localuser localuser 2740 May 25 00:14 dput.cf.defs
-rw-r--r--. 1 localuser localuser 78 May 25 00:14 environment.defs
-rw-r--r--. 1 localuser localuser 8662 May 25 00:14 ffserver.conf
-rw-r--r--. 1 localuser localuser 684 May 25 00:14 fstab.defs
-rw-r--r--. 1 localuser localuser 2986 May 25 00:14 gai.conf
-rw-r--r--. 1 localuser localuser 10852 May 25 00:14 gnome-vfs-mime-magic.defs
-rw-r--r--. 1 localuser localuser 5964 May 25 00:14 gram.yafray.defs
-rw-r--r--. 1 localuser localuser 1104 May 25 00:14 grep-dctrl.rc.defs
-rw-r--r--. 1 localuser localuser 988 May 25 00:14 group-.defs
-rw-r--r--. 1 localuser localuser 988 May 25 00:14 group.defs
-rw-r--r--. 1 localuser localuser 6748 May 25 00:14 hddtemp.db.defs
-rw-r--r--. 1 localuser localuser 4794 May 25 00:14 hdparm.conf
-rw-r--r--. 1 localuser localuser 92 May 25 00:14 host.conf
-rw-r--r--. 1 localuser localuser 4 May 25 00:14 hostname.defs
-rw-r--r--. 1 localuser localuser 580 May 25 00:14 hosts.allow.defs
-rw-r--r--. 1 localuser localuser 248 May 25 00:14 hosts.defs
-rw-r--r--. 1 localuser localuser 880 May 25 00:14 hosts.deny.defs
-rw-r--r--. 1 localuser localuser 1722 May 25 00:14 inputrc.defs
-rw-r--r--. 1 localuser localuser 884 May 25 00:14 insserv.conf
-rw-r--r--. 1 localuser localuser 24 May 25 00:14 issue.defs
-rw-r--r--. 1 localuser localuser 18 May 25 00:14 issue.net.defs
-rw-r--r--. 1 localuser localuser 88 May 25 00:14 kde4rc.defs
-rw-r--r--. 1 localuser localuser 168 May 25 00:14 kernel-img.conf
-rw-r--r--. 1 localuser localuser 1308 May 25 00:14 kerneloops.conf
-rw-r--r--. 1 localuser localuser 1128 May 25 00:14 ksysguarddrc.defs
-rw-r--r--. 1 localuser localuser 103056 May 25 00:14 ld.so.cache.defs
-rw-r--r--. 1 localuser localuser 34 May 25 00:14 ld.so.conf
-rw-r--r--. 1 localuser localuser 2670 May 25 00:14 legal.defs
-rw-r--r--. 1 localuser localuser 3588 May 25 00:14 lftp.conf
-rw-r--r--. 1 localuser localuser 20 May 25 00:14 libao.conf
-rw-r--r--. 1 localuser localuser 1294 May 25 00:14 lintianrc.defs
-rw-r--r--. 1 localuser localuser 2570 May 25 00:14 locale.alias.defs
-rw-r--r--. 1 localuser localuser 2818 May 25 00:14 localtime.defs
-rw-r--r--. 1 localuser localuser 10788 May 25 00:14 login.defs.defs
-rw-r--r--. 1 localuser localuser 598 May 25 00:14 logrotate.conf
-rw-r--r--. 1 localuser localuser 3236 May 25 00:14 lsb-base-logging.sh.defs
-rw-r--r--. 1 localuser localuser 102 May 25 00:14 lsb-release.defs
-rw-r--r--. 1 localuser localuser 15752 May 25 00:14 ltrace.conf
-rw-r--r--. 1 localuser localuser 112 May 25 00:14 magic.defs
-rw-r--r--. 1 localuser localuser 110 May 25 00:14 magic.mime.defs
-rw-r--r--. 1 localuser localuser 44824 May 25 00:14 mailcap.defs
-rw-r--r--. 1 localuser localuser 448 May 25 00:14 mailcap.order.defs
-rw-r--r--. 1 localuser localuser 124 May 25 00:14 mail.rc.defs
-rw-r--r--. 1 localuser localuser 5174 May 25 00:14 manpath.config.defs
-rw-r--r--. 1 localuser localuser 22274 May 25 00:14 mime.types.defs
-rw-r--r--. 1 localuser localuser 802 May 25 00:14 mke2fs.conf
-rw-r--r--. 1 localuser localuser 198 May 25 00:14 modules.defs
-rw-r--r--. 1 localuser localuser 14 May 25 00:14 motd
-rw-r--r--. 1 localuser localuser 1102 May 25 00:14 mtab.defs
-rw-r--r--. 1 localuser localuser 624 May 25 00:14 mtools.conf
-rw-r--r--. 1 localuser localuser 8452 May 25 00:14 nanorc.defs
-rw-r--r--. 1 localuser localuser 2064 May 25 00:14 netscsid.conf
-rw-r--r--. 1 localuser localuser 92 May 25 00:14 networks.defs
-rw-r--r--. 1 localuser localuser 512 May 25 00:14 nsswitch.conf
-rw-r--r--. 1 localuser localuser 1632 May 25 00:14 ntp.conf
-rw-r--r--. 1 localuser localuser 552 May 25 00:14 pam.conf
-rw-r--r--. 1 localuser localuser 8 May 25 00:14 papersize.defs
-rw-r--r--. 1 localuser localuser 1916 May 25 00:14 passwd-.defs
-rw-r--r--. 1 localuser localuser 1872 May 25 00:14 passwd.defs
-rw-r--r--. 1 localuser localuser 312 May 25 00:14 pbuilderrc.defs
-rw-r--r--. 1 localuser localuser 7648 May 25 00:14 pnm2ppa.conf
-rw-r--r--. 1 localuser localuser 350 May 25 00:14 popularity-contest.conf
-rw-r--r--. 1 localuser localuser 5298 May 25 00:14 preload.conf
-rw-r--r--. 1 localuser localuser 496 May 25 00:14 profile.defs
-rw-r--r--. 1 localuser localuser 2626 May 25 00:14 protocols.defs
-rw-r--r--. 1 localuser localuser 20 May 25 00:14 qemu-ifdown.defs
-rw-r--r--. 1 localuser localuser 18 May 25 00:14 qemu-ifup
-rw-r--r--. 1 localuser localuser 1024 May 25 00:14 rarfiles.lst.defs
-rw-r--r--. 1 localuser localuser 306 May 25 00:14 rc.local.defs
-rw-r--r--. 1 localuser localuser 372 May 25 00:14 rearj.cfg.defs
-rw-r--r--. 1 localuser localuser 2724 May 25 00:14 reportbug.conf
-rw-r--r--. 1 localuser localuser 1586 May 25 00:14 request-key.conf
-rw-r--r--. 1 localuser localuser 104 May 25 00:14 resolv.conf
-rw-r--r--. 1 localuser localuser 888 May 25 00:14 rpc.defs
-rw-r--r--. 1 localuser localuser 1216 May 25 00:14 rsyslog.conf
-rw-r--r--. 1 localuser localuser 3664 May 25 00:14 screenrc.defs
-rw-r--r--. 1 localuser localuser 1106 May 25 00:14 securetty.defs
-rw-r--r--. 1 localuser localuser 8596 May 25 00:14 sensors3.conf
-rw-r--r--. 1 localuser localuser 18832 May 25 00:14 services.defs
-rw-r--r--. 1 localuser localuser 180 May 25 00:14 shells.defs
-rw-r--r--. 1 localuser localuser 1132 May 25 00:14 smi.conf
-rw-r--r--. 1 localuser localuser 608 May 25 00:14 sudoers.defs
-rw-r--r--. 1 localuser localuser 192 May 25 00:14 su-to-rootrc.defs
-rw-r--r--. 1 localuser localuser 2028 May 25 00:14 sysctl.conf
-rw-r--r--. 1 localuser localuser 166 May 25 00:14 tidy.conf
-rw-r--r--. 1 localuser localuser 20 May 25 00:14 timezone.defs
-rw-r--r--. 1 localuser localuser 644 May 25 00:14 ts.conf
-rw-r--r--. 1 localuser localuser 1260 May 25 00:14 ucf.conf
-rw-r--r--. 1 localuser localuser 142 May 25 00:14 uniconf.conf
-rw-r--r--. 1 localuser localuser 300 May 25 00:14 updatedb.conf
-rw-r--r--. 1 localuser localuser 4496 May 25 00:14 wgetrc.defs
-rw-r--r--. 1 localuser localuser 1344 May 25 00:14 wodim.conf
-rw-r--r--. 1 localuser localuser 6452 May 25 00:14 zsh_command_not_found.defs

---------- Post updated at 12:58 AM ---------- Previous update was at 12:55 AM ----------

vi SearchWithinFileContent.sh#!/bin/bash
Code:
FILE=alice.filenames
for line in $(cat $FILE)
do
ls -l wiz | grep -f $line
done


I get this as a result:

Code:
[localuser@localhost ~]$ ./SearchWithinFileContent.sh
: No such file or directory
: No such file or directory
: No such file or directory
: No such file or directory
: No such file or directory
: No such file or directory
: No such file or directory
: No such file or directory
: No such file or directory
: No such file or directory
: No such file or directory
: No such file or directory
: No such file or directory
: No such file or directory
: No such file or directory
: No such file or directory
: No such file or directory
: No such file or directory
: No such file or directory
: No such file or directory
: No such file or directory
: No such file or directoryt.defs
: No such file or directory
: No such file or directory
: No such file or directory
: No such file or directory
: No such file or directory
: No such file or directory
: No such file or directory


Last edited by Franklin52; 05-30-2012 at 05:00 AM.. Reason: Please use code tags
# 5  
Old 05-30-2012
I'm going to make an assumption that filter.txt has ^M characters on each line.

What is the output of cat -v filter.txt?

(and please, don't use that for-loop method. It's pointless.)
# 6  
Old 05-30-2012
filter.txt are filenames

Quote:
Originally Posted by Scott
I'm going to make an assumption that filter.txt has ^M characters on each line.

What is the output of cat -v filter.txt?

(and please, don't use that for-loop method. It's pointless.)
Code:
>cat filter.txt
request-key.conf
devscripts.conf
kernel-img.conf
nsswitch.conf
ca-certificates.conf
ntp.conf
updatedb.conf
libao.conf
deluser.conf
uniconf.conf
sysctl.conf
mtools.conf
preload.conf
netscsid.conf
mtab.defs
passwd.defs
sudoers.defs
hosts.defs
qemu-ifup
pbuilderrc.defs
profile.defs
debian_version.defs
networks.defs
bindresvport.blacklist.defs
lintianrc.defs
bash_completion.defs
locale.alias.defs
dput.cf.defs
inputrc.defs
protocols.defs
a2ps.cfg.defs


Last edited by Scott; 05-30-2012 at 05:08 AM.. Reason: Use code tags, please...
# 7  
Old 05-30-2012
That's not quite what I asked for Smilie

Code:
cat -v filter.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

What is the difference ../directory path and ./directory path in ksh?

What is the difference ../directory path and ./directory path in ksh? (1 Reply)
Discussion started by: TestKing
1 Replies

2. UNIX for Beginners Questions & Answers

Convert Relative path to Absolute path, without changing directory to the file location.

Hello, I am creating a file with all the source folders included in my git branch, when i grep for the used source, i found source included as relative path instead of absolute path, how can convert relative path to absolute path without changing directory to that folder and using readlink -f ? ... (4 Replies)
Discussion started by: Sekhar419
4 Replies

3. Shell Programming and Scripting

Help with shell script - filter txt file full of ips

Hello again gentlemen. I would like to make a shell script to 'optimize' a plain text full of IPs. Let's suppose to have this text file: 1.192.63.253-1.192.63.253 1.0.234.46/32 1.1.128.0/17 1.116.0.0/14 1.177.1.157-1.177.1.157 1.23.22.19 1.192.61.0-1.192.61.99 8.6.6.6 I want to... (2 Replies)
Discussion started by: accolito
2 Replies

4. UNIX and Linux Applications

Ssmtp -t < /path/to/the/message.txt (How to format message.txt for html email)

ssmtp has been running well under Kubuntu 12.04.1 for plain text messages. I would like to send html messages with ssmtp -t < /path/to/the/message.txt, but I cannot seem to get the message.txt file properly formatted. I have tried various charsets, Content-Transfer-Encoding, rearranging the... (0 Replies)
Discussion started by: Ronald B
0 Replies

5. Shell Programming and Scripting

Filter specified path

We have 10 jobs entry in crontab like this 0 7 * * 0 && (source /x/y/z .bashrc ; /x/y/z /test.sh Table1 /ABC/TEST >x/y/z/log (every job have different o/p Path) can any one help me to filter only the output PATH /ABC/TEST from the file dup_cron. (1 Reply)
Discussion started by: netdbaind
1 Replies

6. Shell Programming and Scripting

"find . -printf" without prepended "." path? Getting path to current working directory?

If I enter (simplified): find . -printf "%p\n" then all files in the output are prepended by a "." like ./local/share/test23.log How can achieve that a.) the leading "./" is omitted and/or b.) the full path to the current directory is inserted (enclosed by brackets and a blank)... (1 Reply)
Discussion started by: pstein
1 Replies

7. UNIX for Dummies Questions & Answers

Present Working Directory (not the full path)

Hi, Is there a command that tells you right away the current working directory? I know the command "pwd", but that one gives the full path. if pwd gives me: /a/b/c/d/ggg/HERE I want something that will give me: HERE Thanks, Gaurab (13 Replies)
Discussion started by: gaurab
13 Replies

8. Shell Programming and Scripting

sed to read x.txt and grep from y.txt

How would I write a command(s) to read from a file (list) that looks like this: 29847374384 and grep from a second file (list) that looks like this: 29847374384, jkdfkjdf,3833,ddd:confused: (1 Reply)
Discussion started by: smellylizzard
1 Replies

9. Shell Programming and Scripting

how do I filter double lines from a txt file

Hi, I have a file with the following: access-list.txt router1 access-list 1 permit any any access-list 1 deny any any router2 access-list 2 permit any any access-list 2 deny any any router3 access-list 3 permit any any access-list 3 deny any any I want to hava an output that... (10 Replies)
Discussion started by: I-1
10 Replies

10. Shell Programming and Scripting

need help--script to filter specific lines from multiple txt files

Hi folks, - I have 800 txt files - those files are cisco router configs router1.txt router2.txt ... router800.txt I want to accomplish the following: - I want to have a seperate file with all the filenames that I want to process - I want a script that goes trough all those... (7 Replies)
Discussion started by: I-1
7 Replies
Login or Register to Ask a Question