Find the answer to your Linux question:
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just 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?

  2. #2
    Guest
    Join Date
    Feb 2005
    Posts
    312
    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)'

  3. #3
    Linux Enthusiast scathefire's Avatar
    Join Date
    Jan 2010
    Location
    Western Kentucky
    Posts
    626
    Code:
    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

  4. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    Quote Originally Posted by caravel View Post
    Can't you achieve the same using grep or egrep? e.g.

    Code:
    ls | egrep '(?!*.pk|?!*.wav)'
    I don't know if it matters to the OP, but the ls command would not be recursive, whereas the find command (as written) would.

  5. #5
    Just 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.

  6. #6
    Linux User Krendoshazin's Avatar
    Join Date
    Feb 2005
    Location
    London, England
    Posts
    471
    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:

    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
    If you want to run a command on each line of returned output, then use -exec with find:

    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
    I hope that helps.

    --------------------------------------

    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

    Code:
    krendoshazin@null:~> find folder1 ! -iname *.wmv ! -iname *.pk
    folder1
    folder1/new1.tar
    folder1/new.tar
    folder1/new2.tar
    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:~> 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

  7. #7
    Just Joined!
    Join Date
    Mar 2009
    Posts
    4
    Ah I see. Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •