Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: filename problem in bash script

  1. #1
    Join Date
    Nov 2012
    Location
    Great Britain
    Beans
    80

    filename problem in bash script

    I cobbled together a script using bits on the internet I found, to take a csv file of file locations, and copy those files into another tidier location:

    #!/bin/bash
    INPUT=filenames.csv
    OLDIFS=$IFS
    IFS=,
    [ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
    while read file1 file2 file3 file4 file5 file6
    do
    cp $file1 /home/<user>/Audio/language1
    cp $file2 /home/<user>/Audio/language2
    cp $file3 /home/<user>/Audio/language3
    cp $file4 /home/<user>/Audio/language4
    cp $file5 /home/<user>/Audio/language5
    cp $file6 /home/<user>/Audio/language6
    done < $INPUT
    IFS=$OLDIFS
    but when I run the script I get the error:
    cp: cannot stat `<filepath>\r': No such file or directory
    on each file... I don't know where the "`" is coming from, and each item at the end of the row has what appears to be a newline "\r".

    None of the files have copied, it can't find them, where did I go wrong?

  2. #2
    Join Date
    Apr 2012
    Beans
    5,339

    Re: filename problem in bash script

    Did the csv file originate from a Windows system? if so you may need to translate the \r characters e.g. pipe the file through tr

    Code:
    done < <(tr -d '\r' < $input)
    The "`" is just how stat reports filenames i.e.

    Code:
    $ stat nofile
    stat: cannot stat `nofile': No such file or directory
    [HINT: don't use all upper case variable names - you could also use a -d ',' delimiter to 'read' rather than messing with IFS, I think]

  3. #3
    Join Date
    May 2010
    Location
    Tewkesbury uk
    Beans
    7,617
    Distro
    Ubuntu Development Release

    Re: filename problem in bash script

    Hi

    As well as following steeldrivers good advice, also wrap $file in quotes for each copy in case the filename/path contains spaces.

    Code:
    cp "$file1" /home/<user>/Audio/language1
    ...
    Kind regards
    Join us on irc at #ubuntuforums. For web chat see here

    If you believe everything you read, you better not read. ~ Japanese Proverb

    Do not read newspapers on an empty stomach ~ Russian Proverb ~ BrunoLotse

  4. #4
    Join Date
    Nov 2012
    Location
    Great Britain
    Beans
    80

    Re: filename problem in bash script

    Quote Originally Posted by steeldriver View Post
    Did the csv file originate from a Windows system? if so you may need to translate the \r characters e.g. pipe the file through tr

    Code:
    done < <(tr -d '\r' < $input)
    The "`" is just how stat reports filenames i.e.

    Code:
    $ stat nofile
    stat: cannot stat `nofile': No such file or directory
    [HINT: don't use all upper case variable names - you could also use a -d ',' delimiter to 'read' rather than messing with IFS, I think]

    Yes, my csv file is from a windows system, I'll add that in, thanks


    Quote Originally Posted by matt_symes View Post
    Hi

    As well as following steeldrivers good advice, also wrap $file in quotes for each copy in case the filename/path contains spaces.

    Code:
    cp "$file1" /home/<user>/Audio/language1
    ...
    Kind regards

    Yes I've just spotted that there are spaces in there (its really messy, hence this script), thanks

  5. #5
    Join Date
    Nov 2008
    Location
    Metro Boston
    Beans
    8,753
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: filename problem in bash script

    Install tofrodos from the repositories. It will strip the excess returns. You can use it in a pipe, too.
    If you ask for help, please have the courtesy to check for responses and thank the people who helped you.

    Blog · Linode System Administration Guides · Android Apps for Ubuntu Users

  6. #6
    Join Date
    Nov 2012
    Location
    Great Britain
    Beans
    80

    Re: filename problem in bash script

    ok; thankyou for your help so far guys! I now have nearly 6000 relocated and renamed audio files, however; there are nearly 1000 missing.
    As these files have all come over from a windows system they've not bothered to make sure everything is case sensitive, so I've got nearly a thousand files that it 'can't find' because the case is different... Is there something I can do to ignore the case? Or if not, change the names of all the files to lower case... :S

  7. #7
    Join Date
    Apr 2012
    Beans
    5,339

    Re: filename problem in bash script

    You could maybe replace the lines like

    Code:
    cp $file1 /home/<user>/Audio/language1
    with

    Code:
    find . -ipath "./$file1" -exec cp -n -t "/home/<user>/Audio/language1/" {} \;
    which will perform a kind of case-insensitive lookup from the current dir (I think... haven't tested it)

    Alternatively you could add another 'tr' that will convert all the filenames from the csv file to upper (if that is the correct way around? are the actual files all-upper or the names in the spreadsheet?) e.g.

    Code:
    done < <( (tr -d '\r' | tr '[:lower:]' '[:upper:]') < "$input")
    EDIT: added 'no-clobber' ("-n") because you probably don't want to overwrite files if the case-insensitivity produces non-unique names
    Last edited by steeldriver; January 25th, 2013 at 05:06 PM. Reason: oops! added the \; terminator

  8. #8
    Join Date
    Nov 2012
    Location
    Great Britain
    Beans
    80

    Re: filename problem in bash script

    I got an error:
    "find: missing argument to `-exec'"
    on every attempt to copy

  9. #9
    Join Date
    Apr 2012
    Beans
    5,339

    Re: filename problem in bash script

    apologies - I omitted the terminator:

    Code:
    find . -ipath "./$file1" -exec cp -n -t "/home/<user>/Audio/language1/" {} \;

  10. #10
    Join Date
    Nov 2012
    Location
    Great Britain
    Beans
    80

    Re: filename problem in bash script

    the file-names' cases are erratic, there have been so many modifications to the files that there is no one pattern or style that they follow, some have descriptive names, some are capitals, some lower case, some numbers... Some are capitalised in the beginning of the name, and then the csv file that records each files location isn't necessarily written in the same case.

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

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