Spaces in file names - Linux

This is a discussion on Spaces in file names - Linux ; OK, here's the problem. I need to automatically manipulate the file names of a number of files whose filenames have spaces in them. I'm not the one who creates the files (nor is any human) and they must continue to ...

+ Reply to Thread
Results 1 to 10 of 10

Thread: Spaces in file names

  1. Spaces in file names

    OK, here's the problem. I need to automatically manipulate the file names
    of a number of files whose filenames have spaces in them. I'm not the one
    who creates the files (nor is any human) and they must continue to have
    spaces in the filenames, so getting rid of the spaces is not an option. I
    need to be able to put the filenames string into a variable, manipulate the
    variable,a nd then use the new string to rename the file. Ordinarily I
    would do something like:

    for file in `ls -1 *`
    do

    done

    Doing so, however, results not in 1 iteration of the loop with a value for
    $file of "This is the first file", but rather 5 iterations of the loop with
    values of "This", "is", "the", "first", and "file", respectively. How can I
    load the entire file name into the variable? I tried using find instead of
    ls, but doing so using stdout winds up with the same result. I thought of
    using find to execute a subroutine, but the subroutine would take the spaces
    to mean each word was a different command line parameter. That wouldn't be
    so bad, except the number of words in the file name is variable, and I can't
    think off the top of my head of an easy way to handle a random number of
    command parameters.



  2. Re: Spaces in file names

    "Leslie Rhorer" writes:

    > OK, here's the problem. I need to automatically manipulate the file names
    > of a number of files whose filenames have spaces in them.


    Here is an example:

    $ touch "a b" "c d" "e f g"
    $ ls -1
    a b
    c d
    e f g

    $ find . -type f | while read line; do mv "$line" "$line.bak"; done
    $ ls -1
    a b.bak
    c d.bak
    e f g.bak

    Cheers,
    --
    In order to understand recursion you must first understand recursion.
    Remove /-nsp/ for email.

  3. Re: Spaces in file names

    On Sun, 30 Dec 2007 19:31:45 -0600, Leslie Rhorer wrote:

    > OK, here's the problem. I need to automatically manipulate the file names
    > of a number of files whose filenames have spaces in them.
    > for file in `ls -1 *`
    > do
    >
    > done


    How about something like (untested):

    #!/bin/bash
    IFS=$'\n'

    #IFS is the internal field separator in bash and
    #here we set it to be new line only.

    for file in $(find . -type f)
    do

    done

    HTH.
    Regards, Dan.

  4. Re: Spaces in file names

    The suggestion shown below won't work. As I said in theoriginal post, I
    already thought of this. I'm not just adding an extension or something like
    that. As an example of exactly what I need to do, I need to change the file
    named "The Hunt for Red October (Recorded 10/10/2007 18:03).TiVo" to "Hunt
    for Red October, The (Recorded 10/10/2007 18:03).TiVo".

    "Paul Pluzhnikov" wrote in message
    news:m3y7bbk4r9.fsf@somewhere.in.california.localh ost...
    > "Leslie Rhorer" writes:
    >
    >> OK, here's the problem. I need to automatically manipulate the file
    >> names
    >> of a number of files whose filenames have spaces in them.

    >
    > Here is an example:
    >
    > $ touch "a b" "c d" "e f g"
    > $ ls -1
    > a b
    > c d
    > e f g
    >
    > $ find . -type f | while read line; do mv "$line" "$line.bak"; done
    > $ ls -1
    > a b.bak
    > c d.bak
    > e f g.bak
    >
    > Cheers,
    > --
    > In order to understand recursion you must first understand recursion.
    > Remove /-nsp/ for email.




  5. Re: Spaces in file names

    Leslie Rhorer inquired:
    >>>OK, here's the problem. I need to automatically manipulate the file
    >>>names of a number of files whose filenames have spaces in them.


    Paul Pluzhnikov suggested by example:
    >>$ touch "a b" "c d" "e f g"
    >>$ ls -1
    >>a b
    >>c d
    >>e f g
    >>
    >>$ find . -type f | while read line; do mv "$line" "$line.bak"; done
    >>$ ls -1
    >>a b.bak
    >>c d.bak
    >>e f g.bak
    >>$


    Leslie Rhorer replied:
    > [That suggestion] won't work. As I said in theoriginal post, I
    > already thought of this.



    No, you didn't. Note Paul's use of "while read line". That works
    to deal with any character in a filename except newline, but using
    "for file in ..." does not (unless you adjust $IFS, and that has
    pitfalls of its own.)

    RTFM. Seriously: "man find" or "info find". The option "-print0"
    was made exactly for dealing with filenames that contain arbitrary
    characters, including newline. See also "man xargs" for the "-0" option.

    [Also, do not "top post." That might work for social or managerial
    correspondence, but it is obnoxious in a technical conversation.
    Instead, place your reply paragraph immediately _below_ the quoted
    portion to which it responds. Then the reader can follow the chronology
    much more easily.]

    --

  6. Re: Spaces in file names

    Hey, that works! Thanks.

    "Dan Mills" wrote in message
    news:477856c3$0$21094$da0feed9@news.zen.co.uk...
    > On Sun, 30 Dec 2007 19:31:45 -0600, Leslie Rhorer wrote:
    >
    >> OK, here's the problem. I need to automatically manipulate the file
    >> names
    >> of a number of files whose filenames have spaces in them.
    >> for file in `ls -1 *`
    >> do
    >>
    >> done

    >
    > How about something like (untested):
    >
    > #!/bin/bash
    > IFS=$'\n'
    >
    > #IFS is the internal field separator in bash and
    > #here we set it to be new line only.
    >
    > for file in $(find . -type f)
    > do
    >
    > done
    >
    > HTH.
    > Regards, Dan.




  7. Re: Spaces in file names

    "Leslie Rhorer" wrote:
    >Hey, that works! Thanks.


    But it would be a lot easier to just do this:

    #!/bin/bash

    for file in * ; do
    echo "$file"
    done
    exit 0

    The trick is putting quotes around $file. Using `ls -1 *`
    is unnecessary, and so is find.


    >"Dan Mills" wrote in message
    >news:477856c3$0$21094$da0feed9@news.zen.co.uk...
    >> On Sun, 30 Dec 2007 19:31:45 -0600, Leslie Rhorer wrote:
    >>
    >>> OK, here's the problem. I need to automatically manipulate the file
    >>> names
    >>> of a number of files whose filenames have spaces in them.
    >>> for file in `ls -1 *`
    >>> do
    >>>
    >>> done

    >>
    >> How about something like (untested):
    >>
    >> #!/bin/bash
    >> IFS=$'\n'
    >>
    >> #IFS is the internal field separator in bash and
    >> #here we set it to be new line only.
    >>
    >> for file in $(find . -type f)
    >> do
    >>
    >> done
    >>
    >> HTH.
    >> Regards, Dan.


    --
    Floyd L. Davidson
    Ukpeagvik (Barrow, Alaska) floyd@apaflo.com

  8. Re: Spaces in file names - watch for arg list too long with ls *

    On Mon, 31 Dec 2007 03:31:16 -0900, Floyd L. Davidson wrote:

    >"Leslie Rhorer" wrote:
    >>Hey, that works! Thanks.

    >
    >But it would be a lot easier to just do this:
    >
    >#!/bin/bash
    >
    >for file in * ; do
    > echo "$file"
    >done
    >exit 0
    >
    >The trick is putting quotes around $file. Using `ls -1 *`
    >is unnecessary, and so is find.
    >
    >
    >>"Dan Mills" wrote in message
    >>news:477856c3$0$21094$da0feed9@news.zen.co.uk...
    >>> On Sun, 30 Dec 2007 19:31:45 -0600, Leslie Rhorer wrote:
    >>>
    >>>> OK, here's the problem. I need to automatically manipulate the file
    >>>> names
    >>>> of a number of files whose filenames have spaces in them.
    >>>> for file in `ls -1 *`
    >>>> do
    >>>>
    >>>> done
    >>>
    >>> How about something like (untested):
    >>>
    >>> #!/bin/bash
    >>> IFS=$'\n'
    >>>
    >>> #IFS is the internal field separator in bash and
    >>> #here we set it to be new line only.
    >>>
    >>> for file in $(find . -type f)
    >>> do
    >>>
    >>> done
    >>>
    >>> HTH.
    >>> Regards, Dan.

    >
    >--
    >Floyd L. Davidson
    >Ukpeagvik (Barrow, Alaska) floyd@apaflo.com


    Another pitfall using the ls -1 *, if you have a large number of files in teh
    output list the command may fail with an argument list too long. (but may be
    OK without the astersk,) Depending on your default distribution's compilied
    options this may be as few as 2000 files.

    Regards,
    Bart



  9. Re: Spaces in file names

    "Leslie Rhorer" writes:

    > The suggestion shown below won't work.


    Yes, it will. You just need to apply a little more brain power.

    > As an example of exactly what I need to do, I need to change the file
    > named "The Hunt for Red October (Recorded 10/10/2007 18:03).TiVo" to "Hunt
    > for Red October, The (Recorded 10/10/2007 18:03).TiVo".


    There are only 2 characters that absolutely can not occur in a UNIX
    filename: the NUL character (used to terminate the name) and the '/'
    (used as directory separator). Since your file names above contain
    one of them, they are not a legal UNIX filenames, can not possibly
    happen on a UNIX filesystem, and your "exact" question is moot.

    Cheers,
    --
    In order to understand recursion you must first understand recursion.
    Remove /-nsp/ for email.

  10. Re: Spaces in file names

    On 31 Dez. 2007, 04:25, "Leslie Rhorer" wrote:
    > The suggestion shown below won't work. As I said in theoriginal post, I
    > already thought of this. I'm not just adding an extension or something like
    > that. As an example of exactly what I need to do, I need to change the file
    > named "The Hunt for Red October (Recorded 10/10/2007 18:03).TiVo" to "Hunt
    > for Red October, The (Recorded 10/10/2007 18:03).TiVo".


    As already pointed out: A UNIX file name can not contain a slash '/'.
    Therefore it is only possible to change the file named
    "The Hunt for Red October (Recorded 2007-10-10 18:03).TiVo" to
    "Hunt for Red October, The (Recorded 2007-10-10 18:03).TiVo".

    When it is not a requirement to use a shell script, I have a solution.
    The following Seed7 script changes file names according to your
    request:

    $ include "seed7_05.s7i";

    const proc: main is func
    local
    var array string: names is 0 times "";
    var string: currName is "";
    var string: newName is "";
    var integer: insertPos is 0;
    begin
    if length(argv(PROGRAM)) = 1 then
    names := read_dir(argv(PROGRAM)[1]);
    for currName range names do
    insertPos := pos(currName, " (");
    if startsWith(currName, "The ") and insertPos <> 0 then
    newName := currName[5 .. pred(insertPos)] &
    ", The " & currName[insertPos ..];
    rename(currName, newName);
    end if;
    end for;
    end if;
    end func;

    -------------------------
    I created the "The Hunt for .... " file and did a test:

    tm@penguin:~/seed7_5/prg$ ls T*
    The Hunt for Red October (Recorded 2007-10-10 18:03).TiVo
    tm@penguin:~/seed7_5/prg$ hi renfile .
    HI INTERPRETER Version 4.5.2791 Copyright (c) 1990-2007 Thomas Mertes
    258 syntax.s7i
    3355 seed7_05.s7i
    22 renfile.sd7
    3635 lines total
    363500 lines per second
    1580555 bytes
    tm@penguin:~/seed7_5/prg$ ls H*
    Hunt for Red October, The (Recorded 2007-10-10 18:03).TiVo
    tm@penguin:~/seed7_5/prg$

    Greetings Thomas Mertes

    Seed7 Homepage: http://seed7.sourceforge.net
    Seed7 - The extensible programming language: User defined statements
    and operators, abstract data types, templates without special
    syntax, OO with interfaces and multiple dispatch.

+ Reply to Thread