Results 1 to 7 of 7
If I do the following, I get the result that I want, with no .pk or .wav files:
find ./folder ! -iname '*.wav' -a ! -iname '*.pk'
However if I ...
- 11-23-2011 #1Just Joined!
- Join Date
- Mar 2009
- Posts
- 4
Having a problem using find with command substitution
If I do the following, I get the result that I want, with no .pk or .wav files:
find ./folder ! -iname '*.wav' -a ! -iname '*.pk'
However if I do a command substitution like so:
ls `find ./folder ! -iname '*.wav' -a ! -iname '*.pk'`
It lists the .wav and .pk files as well. What am I doing wrong?
- 11-23-2011 #2Guest
- Join Date
- Feb 2005
- Posts
- 314
Not sure what you're trying to do there, but assuming the output of find is a list of files why are you passing that whole list to ls as an argument? It seems a backward way of doing things - but as a disclaimer, I am certainly no expert.
Can't you achieve the same using grep or egrep? e.g.
Code:ls | egrep '(?!*.pk|?!*.wav)'
- 11-23-2011 #3Code:
find . -type f ! \( -iname "*.pk" -o -iname "*.wav" \)
Last edited by scathefire; 11-23-2011 at 02:50 PM. Reason: cleaned up the command
linux user # 503963
- 11-23-2011 #4Linux Guru
- Join Date
- May 2011
- Posts
- 1,838
- 11-25-2011 #5Just Joined!
- Join Date
- Mar 2009
- Posts
- 4
I was just using the list command to help me see what was happening. I was actually trying to pipe the result of the find to tar, although I'm not sure that is possible anyway. I don't understand how the ls command can show more files than what the find shows by itself. It somehow seems to add files to the list.
- 11-25-2011 #6
I think there's a problem with your original command as I couldn't get it to work. I've modified the command and this works for me. I created a test directory with some wmv, pk, and tar files:
If you want to run a command on each line of returned output, then use -exec with find:Code:krendoshazin@null:~> ls folder1/ 1.pk 1.wmv 2.pk 2.wmv 3.pk 3.wmv 4.pk 4.wmv new1.tar new2.tar new.tar krendoshazin@null:~> find folder1 -type f ! -iname *.wmv ! -iname *.pk folder1/new1.tar folder1/new.tar folder1/new2.tar
I hope that helps.Code:krendoshazin@null:~> find folder1 -type f ! -iname *.wmv ! -iname *.pk -exec ls -l '{}' ';' -rw-r--r-- 1 krendoshazin users 0 Nov 25 08:29 folder1/new1.tar -rw-r--r-- 1 krendoshazin users 146 Nov 25 08:12 folder1/new.tar -rw-r--r-- 1 krendoshazin users 0 Nov 25 08:29 folder1/new2.tar
--------------------------------------
p.s. I believe this is the reason ls lists the contents of the directory:
without '-type f', find returns folder1 itself as a result
when 'ls' is run on a directory it lists the contents, and you can see this clearer here if we tell ls to list the directory, not its contents:Code:krendoshazin@null:~> find folder1 ! -iname *.wmv ! -iname *.pk folder1 folder1/new1.tar folder1/new.tar folder1/new2.tar
Code:krendoshazin@null:~> ls -ld `find folder1 ! -iname *.wmv ! -iname *.pk` drwxr-xr-x 2 krendoshazin users 4096 Nov 25 08:29 folder1 -rw-r--r-- 1 krendoshazin users 0 Nov 25 08:29 folder1/new1.tar -rw-r--r-- 1 krendoshazin users 0 Nov 25 08:29 folder1/new2.tar -rw-r--r-- 1 krendoshazin users 146 Nov 25 08:12 folder1/new.tar
- 11-26-2011 #7Just Joined!
- Join Date
- Mar 2009
- Posts
- 4
Ah I see. Thanks!


Reply With Quote
