Find the answer to your Linux question:
Page 2 of 2 FirstFirst 1 2
Results 11 to 14 of 14
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #11

    Because filenames are versatile, and parsing `ls' output can be troublesome, when `find' has an -exec option, or if necessary a -print0 one.

    Generally speaking parsing the output of a command with a for loop is a bad idea, because it splits lines into words and loop on them, not on lines; then a while loop is the best choice.

  2. #12
    Quote Originally Posted by skitgubbe View Post
    "find" is for searching in all sub-directories! !
    If one wants to convert also in sub directories one has to use "find" but the basic loop does not change
    Code:
    for file in `find . -name '*.avi' -print | sed "s/\.avi//"`
    do
    echo input_file ${file}.avi
    echo output_file ${file}.mkv
    echo
    done
    Test run:
    Code:
    mats@linux-7bll:~/bin/test> try
    input_file ./file2.avi
    output_file ./file2.mkv
    
    input_file ./sub2/file2.avi
    output_file ./sub2/file2.mkv
    
    input_file ./sub2/file1.avi
    output_file ./sub2/file1.mkv
    
    input_file ./file1.avi
    output_file ./file1.mkv
    
    input_file ./sub1/file2.avi
    output_file ./sub1/file2.mkv
    
    input_file ./sub1/file1.avi
    output_file ./sub1/file1.mkv

  3. #13
    can you show a simple working example with a space in the filename ?
    no.
    That's why for loops should not be used with command output.

    if you want to loop over files in a directory, it's simple as
    Code:
    for f in ./*; do echo "$f"; done
    if you have a modern shell, and want to go down the hierrarchy
    Code:
    for f in ./**/*; echo "$f"; done
    but `find' is probably faster.

    you can swim across the river, and face dangers, or
    you can safely use the bridge.
    you choose, but don't tell people to wet themselves.

  4. $spacer_open
    $spacer_close
  5. #14
    Quote Originally Posted by skitgubbe View Post
    Why do you have bad feelings towards "ls"? Strange statement!
    i'd agree w/watael here.

    why?

    1. find has more features than ls, making it less CPU-expensive than piping multiple arguments together to get the same effect

    2. find is typically faster than ls

    3. find is easier to use when dealing with recursive searching (though be no means is limited to that)

    4. you can do anything with find that you can with ls (via the -exec arg), but the reverse is not true

Posting Permissions

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