LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-25-2008, 02:03 PM   #1
womd
Member
 
Registered: Sep 2005
Location: Austria
Posts: 35

Rep: Reputation: 15
Question "strange" directory names


dear experts ,

i have some directories like this:

../sound/2\ Raum\ Wohnung\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ /

how can i get rid of the \ 's ? i guess they escape for a " "....

how to recursiveRremoveAllUnnecessaryCharactersWhileNotDestryoingTheyrNnames($path) ?

what should i do to get

../sound/2\ Raum\ Wohnung

in this case ?

thx

chris
 
Old 02-25-2008, 03:26 PM   #2
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
If you guess right and those are escaped spaces, then 'ls -F' should show it like this:
Code:
2 Raum Wohnung                /
"All unnecessary characters" is subjective. Please tell us specifically what you want! From your example, I could guess you want to omit trailing spaces? As a start,
Code:
rename -v 's/ +$//' *
As you want to do this recursively, try something like
Code:
find topdir -depth -type d | rename 's/ +//'
(I haven't tried this; might have typos.) I use the -depth option so subdriectories get renamed before parents; otherwise they might not get found any more. You might add a find predicate like "-name '* '" to limit find's output to directories that "rename" will actually rename.

BTW, I find any spaces in filenames very inconvenient. I'd change your example to "2-Raum-Wohnung". E.g., rename 'y/ /-/'.

P.S., My /usr/bin/rename (on Ubuntu) is a Perl script developed by Robin Barker from Larry Wall's original. There are other "rename"s around, so please check first!
 
Old 02-26-2008, 06:55 PM   #3
womd
Member
 
Registered: Sep 2005
Location: Austria
Posts: 35

Original Poster
Rep: Reputation: 15
hi ! first off... thanks for your help ... but i still have some troubles understanding ...

Quote:
Originally Posted by Quigi View Post
If you guess right and those are escaped spaces, then 'ls -F' should show it like this:
Code:
2 Raum Wohnung                /
yes it does.

Quote:
"All unnecessary characters" is subjective. Please tell us specifically what you want!
i came upon this problem wile experimenting around with php. when i try to copy / rename / delete such dirs , "it" does not "see" those spaces, and the directory is not found when i try to access it with an element of readdir-return... it came in my mind, that it would be best anyway to clean the directory names ... no '&%$§δόφ and such things .... ..
this would save some headache for the beginning ....


Quote:
From your example, I could guess you want to omit trailing spaces? As a start,
Code:
rename -v 's/ +$//' *
yes, but i could not get it to work...

the man-pages i got do not show a -v switch .. what does it do ?

the 's/ +$//' is it a regex ?

shouln't it be '\s then ?

any_non_whitespace_character, the end of the sting is at least once // ( the space ?)



.. i do not get what rename should rename to * ?

Quote:
As you want to do this recursively, try something like
Code:
find topdir -depth -type d | rename 's/ +//'
(I haven't tried this; might have typos.) I use the -depth option so subdriectories get renamed before parents; otherwise they might not get found any more. You might add a find predicate like "-name '* '" to limit find's output to directories that "rename" will actually rename.
ok, i will read some find man.

Quote:
BTW, I find any spaces in filenames very inconvenient. I'd change your example to "2-Raum-Wohnung". E.g., rename 'y/ /-/'.
.. this is also not doing anything ... i got something terrible wrong i think ....


Quote:
P.S., My /usr/bin/rename (on Ubuntu) is a Perl script developed by Robin Barker from Larry Wall's original. There are other "rename"s around, so please check first!
[/QUOTE]

ahh ... ok .. now i read it `... but also on my ubuntu the command's "hang" .. the other machine (where these directories are..) is a suse9.3 linux

i'll try to find this renamer an check back ...


thanks
 
Old 02-28-2008, 10:37 PM   #4
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
Post

To remove trailing spaces, I suggested
Code:
rename -v 's/ +$//' *
Quote:
Originally Posted by womd View Post
yes, but i could not get it to work...

the man-pages i got do not show a -v switch .. what does it do ?
It turns on verbose mode. You probably have a different rename. You could read the man page on your Ubuntu system, and copy the executable (a perl script, should be portable) from there to your Suse box, e.g., to ~/perl-rename.
Quote:
the 's/ +$//' is it a regex ?
It's a Perl statement -- s substitutes regexps. The regex is " +$", and matches 1 or more spaces at the end of the line. The command "s" replaces any match in one of the filenames by the empty string. "/" is the usual delimiter, but I could just as well write "s= +%==".
Quote:
shouln't it be '\s then ?
No.
Quote:
.. i do not get what rename should rename to * ?
The shell expands * to all files (including directories) in the current directory that don't start with "."; this happens before rename gets the argument list. Then "rename" runs the Perl statement on each of these file/directory names, and renames the corresponding file accordingly.
Quote:
ok, i will read some find man.
...
i'll try to find this renamer an check back
What the heck, I'll paste the whole utility (including documentation) below. Save, chmod +x, and you should be set.

/Quigi


Code:
#!/usr/bin/perl -w
#
#  This script was developed by Robin Barker (Robin.Barker@npl.co.uk),
#  from Larry Wall's original script eg/rename from the perl source.
#
#  This script is free software; you can redistribute it and/or modify it
#  under the same terms as Perl itself.
#
# Larry(?)'s RCS header:
#  RCSfile: rename,v   Revision: 4.1   Date: 92/08/07 17:20:30 
#
# $RCSfile: rename,v $$Revision: 1.5 $$Date: 1998/12/18 16:16:31 $
#
# $Log: rename,v $
# Revision 1.5  1998/12/18 16:16:31  rmb1
# moved to perl/source
# changed man documentation to POD
#
# Revision 1.4  1997/02/27  17:19:26  rmb1
# corrected usage string
#
# Revision 1.3  1997/02/27  16:39:07  rmb1
# added -v
#
# Revision 1.2  1997/02/27  16:15:40  rmb1
# *** empty log message ***
#
# Revision 1.1  1997/02/27  15:48:51  rmb1
# Initial revision
#

use strict;

use Getopt::Long;
Getopt::Long::Configure('bundling');

my ($verbose, $no_act, $force, $op);

die "Usage: rename [-v] [-n] [-f] perlexpr [filenames]\n"
    unless GetOptions(
        'v|verbose' => \$verbose,
        'n|no-act'  => \$no_act,
        'f|force'   => \$force,
    ) and $op = shift;

$verbose++ if $no_act;

if (!@ARGV) {
    print "reading filenames from STDIN\n" if $verbose;
    @ARGV = <STDIN>;
    chop(@ARGV);
}

for (@ARGV) {
    my $was = $_;
    eval $op;
    die $@ if $@;
    next if $was eq $_; # ignore quietly
    if (-e $_ and !$force)
    {
        warn  "$was not renamed: $_ already exists\n";
    }
    elsif ($no_act or rename $was, $_)
    {
        print "$was renamed as $_\n" if $verbose;
    }
    else
    {
        warn  "Can't rename $was $_: $!\n";
    }
}

__END__

=head1 NAME

rename - renames multiple files

=head1 SYNOPSIS

B<rename> S<[ B<-v> ]> S<[ B<-n> ]> S<[ B<-f> ]> I<perlexpr> S<[ I<files> ]>

=head1 DESCRIPTION

C<rename>
renames the filenames supplied according to the rule specified as the
first argument.
The I<perlexpr> 
argument is a Perl expression which is expected to modify the C<$_>
string in Perl for at least some of the filenames specified.
If a given filename is not modified by the expression, it will not be
renamed.
If no filenames are given on the command line, filenames will be read
via standard input.

For example, to rename all files matching C<*.bak> to strip the extension,
you might say

        rename 's/\.bak$//' *.bak

To translate uppercase names to lower, you'd use

        rename 'y/A-Z/a-z/' *

=head1 OPTIONS

=over 8

=item B<-v>, B<--verbose>

Verbose: print names of files successfully renamed.

=item B<-n>, B<--no-act>

No Action: show what files would have been renamed.

=item B<-f>, B<--force>

Force: overwrite existing files.

=back

=head1 ENVIRONMENT

No environment variables are used.

=head1 AUTHOR

Larry Wall

=head1 SEE ALSO

mv(1), perl(1)

=head1 DIAGNOSTICS

If you give an invalid Perl expression you'll get a syntax error.

=head1 BUGS

The original C<rename> did not check for the existence of target filenames,
so had to be used with care.  I hope I've fixed that (Robin Barker).

=cut
 
Old 03-02-2008, 07:15 PM   #5
womd
Member
 
Registered: Sep 2005
Location: Austria
Posts: 35

Original Poster
Rep: Reputation: 15
Thumbs up ok, wokring thx

thank you very much quigi! got it now !

will experiment with find a little more ...

cu

chris
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
"bad interpreter : no such file or directory" when configure "flex" acer_peri Linux - Software 10 11-10-2010 02:19 AM
strange "No such file or directory" errors on NFS volumes fishsponge Linux - General 2 01-14-2008 10:07 AM
What is the meaning of ".d" in linux directory names? hq4ever Linux - Server 1 03-19-2007 02:01 PM
how to get rid of " " spaces in file names in a directory lleb Linux - General 7 08-09-2006 05:08 PM
i just finished typing "./configure" and "make" in mplayer directory... kublador Linux - General 4 02-22-2003 04:12 PM


All times are GMT -5. The time now is 06:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration