Re: recursively changing chars in filenames

0 views
Skip to first unread message

Smoot Carl-Mitchell

unread,
Jun 27, 2009, 1:54:12 PM6/27/09
to Ubuntu user technical support, not for general discussions
On Sat, 2009-06-27 at 18:54 +0200, Soren Orel wrote:
> Hi
> I wrote a little script that removes spaces, dashes, and so on from
> the filenames in a directory ($1), and lowercase all filename letters,
> etc.
>
> how could I complete the script, that so it will "recursively" do the
> same thing?

You could stick it in "find" and have find do the recursion.

find . -type d -exec your_script {} \;
--
Smoot Carl-Mitchell
Computer Systems and
Network Consultant
sm...@tic.com
+1 480 922 7313
cell: +1 602 421 9005

--
ubuntu-users mailing list
ubuntu...@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users

Soren Orel

unread,
Jun 27, 2009, 4:19:03 PM6/27/09
to Ubuntu user technical support, not for general discussions
Thank you, but it still gives error, because I can't cd to a dir containing spaces, like:

cd /home/user/some dir what has spaces/something

but I think I found something, "renaming using inode"

Wade Smart

unread,
Jun 27, 2009, 4:26:24 PM6/27/09
to Ubuntu user technical support, not for general discussions
Soren Orel wrote:
> Thank you, but it still gives error, because I can't cd to a dir
> containing spaces, like:
>
> cd /home/user/some dir what has spaces/something
>
> but I think I found something, "renaming using inode"
>
> On Sat, Jun 27, 2009 at 7:54 PM, Smoot Carl-Mitchell <sm...@tic.com
> <mailto:sm...@tic.com>> wrote:
>
> On Sat, 2009-06-27 at 18:54 +0200, Soren Orel wrote:
> > Hi
> > I wrote a little script that removes spaces, dashes, and so on from
> > the filenames in a directory ($1), and lowercase all filename
> letters,
> > etc.
> >
> > how could I complete the script, that so it will "recursively" do the
> > same thing?
>
> You could stick it in "find" and have find do the recursion.
>
> find . -type d -exec your_script {} \;
> --
> Smoot Carl-Mitchell

20090627 1524 GMT-5

If you have a folder like:
/home/user/folder name
and you want to cd to it
cd /home/user/folder\ name

then wouldnt you want to scan the directory for any folder with the
space first, then remove the spaces, and the rescan again so it "could'
go into the next directory?

wade

--
Registered Linux User: #480675
Linux since June 2005

Soren Orel

unread,
Jun 27, 2009, 4:51:29 PM6/27/09
to Ubuntu user technical support, not for general discussions
Like this?

http://pastebin.com/f7787cfaa

thank you :) I only need to speed it up, but it's "good for me" :D:D

Smoot Carl-Mitchell

unread,
Jun 27, 2009, 5:31:24 PM6/27/09
to Ubuntu user technical support, not for general discussions
On Sat, 2009-06-27 at 22:19 +0200, Soren Orel wrote:
> Thank you, but it still gives error, because I can't cd to a dir
> containing spaces, like:
>
> cd /home/user/some dir what has spaces/something
>

Sure you can. Just enclose the $1 argument in your script in double
quotes. The cd will then take it as a single argument and do the right
thing. It is the shell that is expanding the $1 and then seeing the
whitespace as argument separators.

Christopher Kelley

unread,
Jun 27, 2009, 11:33:40 PM6/27/09
to ubuntu...@lists.ubuntu.com




<snip>

Thank you, but it still gives error, because I can't cd to a dir containing spaces, like:

cd /home/user/some dir what has spaces/something

but I think I found something, "renaming using inode"
</snip>

actually..yes you can.

instead of typing "cd /home/user/some dir", type "cd /home/user/some\ dir"

--cj

Tim Frost

unread,
Jun 28, 2009, 5:03:02 AM6/28/09
to Ubuntu user technical support, not for general discussions
On Sat, 2009-06-27 at 10:54 -0700, Smoot Carl-Mitchell wrote:
> On Sat, 2009-06-27 at 18:54 +0200, Soren Orel wrote:
> > Hi
> > I wrote a little script that removes spaces, dashes, and so on from
> > the filenames in a directory ($1), and lowercase all filename letters,
> > etc.
> >
> > how could I complete the script, that so it will "recursively" do the
> > same thing?
>
> You could stick it in "find" and have find do the recursion.
>
> find . -type d -exec your_script {} \;

This will break for directories that are renamed, because the script
will rename directories before they have been examined. By adding the
'-depth' option, you tell the find command to use 'depth-first' - i.e.
to process the contents of a directory before it processes the
directory.

So Soren can run
find . -depth -type d -exec your_script "{}" \;
after remembering to quote the parameter $1 in the script.


Making the existing script recursive is more complex, because it has to
handle the recursion itself. However, it does appear that the loop code
find . -depth -type d -print | while read d
do
pushd "$d"
...
popd
done

will work even for directories that have embedded spaces (where the line
'...' represents Soren's original code). Note that I use pushd/popd
because:
* the original code assumes that it is in the directory to be processed
* directory names are relative to the directory where the find started
The pushd needs quotes because otherwise a directory name with spaces
will still be split by the shell.

The code 'find . -type d -print | while read d ' works (unless the file
name DOES include a newline), because find prints a newline ("\n" or
0x0a) between file names, and the shell built-in read function uses
newline as its default delimeter.


Tim

--
Tim Frost <timf...@xtra.co.nz>

Smoot Carl-Mitchell

unread,
Jun 28, 2009, 10:34:30 AM6/28/09
to ubuntu...@lists.ubuntu.com
On Sun, 2009-06-28 at 21:03 +1200, Tim Frost wrote:

> > You could stick it in "find" and have find do the recursion.
> >
> > find . -type d -exec your_script {} \;
>
> This will break for directories that are renamed, because the script
> will rename directories before they have been examined. By adding the
> '-depth' option, you tell the find command to use 'depth-first' - i.e.
> to process the contents of a directory before it processes the
> directory.

Good point. I missed the fact he is also renaming directories.

> So Soren can run
> find . -depth -type d -exec your_script "{}" \;
> after remembering to quote the parameter $1 in the script.

--

Smoot Carl-Mitchell
Computer Systems and
Network Consultant
sm...@tic.com
+1 480 922 7313
cell: +1 602 421 9005

--

Reply all
Reply to author
Forward
0 new messages