Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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!
hi ! first off... thanks for your help ... but i still have some troubles understanding ...
Quote:
Originally Posted by Quigi
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.