Exclusion List of file and directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Exclusion List of file and directory
# 1  
Old 06-07-2005
Exclusion List of file and directory

dear all

i trying to list all files within a directory. I want to exclude all subdirectory and some files, with using below statement, but it not exclude the files which start with "&" and end with "SL" , is there any things wrong with the below statement ?

TIA

cd /myaccount/mydirectory
for each in `ls -l|grep -v "drw"|awk '{print $9}'|egrep -v "&|SL$"`
do
....
done
# 2  
Old 06-08-2005
ayang,

I dont know how files can start with a &. As far as I know, & is used to push a command to the background.

Anyway, keeping & aside, the rest of the script would like something like this...

Code:
for file in `ls`
do
[[ ! -d $file ]] && echo $file | sed -e '/SL$/d' && your command to carry out on $file
done

I have replaced grep, grep and awk with a sed. Better to use existing builtin commands to do what other external commands can do.

The sed will remove those files which end with SL

Vino
# 3  
Old 06-08-2005
Quote:
Originally Posted by vino
Code:
for file in `ls`
do
[[ ! -d $file ]] && echo $file | sed -e '/SL$/d' && your command to carry out on $file
done

Vino
The use of ls here is unnecessary, the shell can expand glob patterns to do this.
for example:

Code:
for file in *
do
[[ ! -d $file ]] && echo $file | sed -e '/SL$/d' && your command to carry out on $file
done

# 4  
Old 06-08-2005
Quote:
Originally Posted by reborg
The use of ls here is unnecessary, the shell can expand glob patterns to do this.
reborg...thanks Smilie

vino
# 5  
Old 06-08-2005
Quote:
Originally Posted by vino
reborg...thanks Smilie

vino
No problems, in a small script like is it won't make much difference, but where possible you should try to avoid making calls to external tools because the builtin functionality is much quicker.
# 6  
Old 06-08-2005
Sorry, but this does not have to be true always: If the directory has a real lot of files in it you might run into problems with the shell filename expansion.

In this case it is better to rely on a pipeline:

Code:
ls -1 | sed '/<filterexpression>/d' | while read file ; do whatever_you_want $file ; done

*sigh*
Still I agree to the sentiment that it is better to use the least resource-consuming solution possible. Whenever I see something like "...| awk 'print $1' | ..." i shake my head.

*deep sigh*

In more than 10 years of shell programming I encountered about a dozen of opportunities where the usage of awk made sense. In the same time i have seen tons of opportunities where awk was just a waste ofmachine resources. This is nothing to be said against awk, just against its usage as a tool for regexp lazybones.

*very deep sigh*

bakunin
# 7  
Old 06-13-2005
Dear All,

Thanks for guiding my error. Yes now i able to get the result i want.
Somefile in unix with "&" is actually directory file of database (call uniVerse)
thanks again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Move file from one directory and update the list file once moved.

Dears, I have a listfile contains list of files path. i need to read the line of the listfile mv the file to other directory and update the listfile by deleting the lines of the listfile. #!/bin/bash target=/fstest/INVESTIG/Sadiq/TEST_ARCH while read -r line || ]; do mv $line... (19 Replies)
Discussion started by: sadique.manzar
19 Replies

2. UNIX for Dummies Questions & Answers

List Directory names which have the file

Hi All, Can any one help me to list out the directory names which contain the specified file. See for example File name : file.201307014.LKT Have the directory structure as below. /app/work/data/INDIA/file.201307014.LKT /app/work/data/AMERICA/file.201307014.KTP... (5 Replies)
Discussion started by: Balasankar
5 Replies

3. Shell Programming and Scripting

Looping inside directories based on a file which contains file directory list

Hi All, Please help. I have got a file which contains a listing of a file and some directories after it, one by one. I am calling this file xyz.txt here file1 dir1 dir2 dir3 dir4 file2 dir5 dir6 dir7 dir8 file3 dir9 dir10 dir11 dir12 (6 Replies)
Discussion started by: Piyush Jakra
6 Replies

4. Shell Programming and Scripting

Separating delimited file by pattern with exclusion list

I have a file with the contents below jan_t=jan;feb_t=feb;mar_t=mar;year=2010 jan_t=null;feb_t=feb;mar_t=mar;year=2010 jan_t=jan;feb_t=feb;mar_t=mar;year=2010 I want to extract out all the fields values ending with "_t" , however, i want to exclude feb_t and mar_t from the results In... (6 Replies)
Discussion started by: alienated
6 Replies

5. Shell Programming and Scripting

Delete old files but with exclusion with file list

Hello Can you please help and check what im missing on script below the goal is to delete the old files more than 7 days old but not the excluded file list inside excluded.dat file #!/bin/sh EXCLUDE=/path/to/exclude/exclude.dat FIND=/bin/find for xfile in '(read $EXCLUDE)' do $FIND... (9 Replies)
Discussion started by: angst_nu
9 Replies

6. 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

7. Solaris

Search for string in a directory with binaries exclusion

Hi all, My need is to search for a given string "toto" within a directory, but the search should ignore binary and db files. It is in fact, a combination of xargs grep that I didn't succeed to manage. Let we say that we are under /etc/ and we want to search for all included files (except... (9 Replies)
Discussion started by: mdjebbi
9 Replies

8. UNIX for Dummies Questions & Answers

Searching a file with exclusion in the search

Hello, I'm looking for a bit of help. Im trying to search a file for lines that contain white spaces at the end of the lines. This is what I'm using where $param is the path and file name and it redirects the output to a txt file : echo | grep -n ' $' $param >> $2 Is it possible to have... (8 Replies)
Discussion started by: gintreach
8 Replies

9. Shell Programming and Scripting

ls > file - Creating file containing the list of all files present in a directory

Hi All, I need to create a file which contains the list of all the files present in that directory. e.g., ls /export/home/user/*.dat > list_file.dat but what i am getting is: $ ls /export/home/user/*.dat > list_file.dat /export/home/user/*.dat: No such file or directory But I have... (1 Reply)
Discussion started by: pranavagarwal
1 Replies

10. UNIX for Dummies Questions & Answers

how to list a directory & file

Good day every body, Iv've been given a list of files and directory requested by the Auditor's. My problem is how do i list the file with the permission given and also save the file into text.file so that i can passed the result/oucome to them?? Thanks alot guys!! (2 Replies)
Discussion started by: gagasan_makmur
2 Replies
Login or Register to Ask a Question