Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2006
    Posts
    15

    Question Unanswered: separating filenames

    I am trying to write a script that will take a list of filenames, operate on them individually and output a changed file name - mainly for renaming a large number of files.

    So far I have

    TARGET="$(ls -1 |)"

    for FILE in "$TARGET"; do

    NEW=$(echo "$FILE" | 'do some stuff to file')

    mv $FILE $NEW

    done

    but all the filenames are kept as one long string in $FILE, and $NEW, so I can't do anything with them once I have changed them. How can I get the script to see each filename separately?
    Thanks

  2. #2
    Join Date
    Aug 2004
    Posts
    10
    Quote Originally Posted by davidcollins001
    I am trying to write a script that will take a list of filenames, operate on them individually and output a changed file name - mainly for renaming a large number of files.

    So far I have

    TARGET="$(ls -1 |)"

    for FILE in "$TARGET"; do

    NEW=$(echo "$FILE" | 'do some stuff to file')

    mv $FILE $NEW

    done

    but all the filenames are kept as one long string in $FILE, and $NEW, so I can't do anything with them once I have changed them. How can I get the script to see each filename separately?
    Thanks
    **************************
    for file in `ls -l|grep "^-"|awk '{print $9}'`
    do
    # do whatever u want to do on file
    echo $file
    #1st option
    echo $file>${file}Newname
    #2nd option
    mv $file newfile
    done
    **********************
    ur code seems correct except $NEW ,bcz if u allready defined NEW as a variable then it will alws take the constant value of that variable ..which is defined before the loop

  3. #3
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Cool


    Remove the double quotes in:

    Code:
    ...
    for FILE in $TARGET; do
    ...


    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  4. #4
    Join Date
    Feb 2006
    Posts
    15
    thanks guys,

    Don't the double quotes just maintain the format? So everything remains on separate lines instead of being placed on one?

  5. #5
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    The double quotes will make it one parameter e.g.
    #!/usr/bin/ksh
    echo $1
    echo $2
    echo $3
    # s1 "a b c" d e
    a b c
    d
    e
    # s1 a b c d e
    a
    b
    c

Posting Permissions

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