Solved

remove spaces from end of file name

Posted on 2006-06-16
26
781 Views
Last Modified: 2012-08-14
Hi, i have many files which all have a space at the end of each file name.  How can i take out these spaces at the end of each file name?  I have a suse linux but can also browse the files via windows.

0
Comment
Question by:akt-uk
  • 9
  • 8
  • 4
  • +3
26 Comments
 
LVL 14

Expert Comment

by:DonConsolio
ID: 16920870
try the "mmv" command

$ mmv '* ' '#1'
0
 
LVL 8

Expert Comment

by:Autogard
ID: 16921834
I don't believe suse has "mmv" by default.  At least I am running SuSE and it's not installed by default on my machine.

Try something like this:

for i in *; do mv "$i" "${i/\ }"; done

(derived from http://www.debian-administration.org/articles/150)
0
 
LVL 10

Expert Comment

by:Luxana
ID: 16925071
Autogard's soulution should work but anyway, I had a similar problem with multiple spaces in files and I used this script to resolve this problem:

http://www.pigstye.net/article.php/20021122075650404

hope it helps
0
 
LVL 8

Expert Comment

by:Autogard
ID: 16925393
If you have one space at the end of your filename then my solution should work -- if you have multiple spaces you may need to try this:

for i in *; do mv "$i" "${i/[\ ]*}"; done
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16925981
perl -e'for(@ARGV){($f=$_)=~s/\s$//;rename $_,$f;}' *
0
 
LVL 3

Accepted Solution

by:
bryanlloydharris earned 500 total points
ID: 16999190
Simple find seems to work.  The regular expression option finds files that end in one or more spaces, then deletes them.

First try this to make sure you're not about to delete something important.
$ find . -type f -regex ".*[ ]+"

Then this to delete them.
$ find . -type f -regex ".*[ ]+" -exec rm {} \;
0
 

Author Comment

by:akt-uk
ID: 16999613
bryanlloydharris

This worked thanks alot, i don't want to delete the file is there another command for removing the spaces? you help is much apreciated!

Thanks
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16999806
what's wrong with the other suggstions?
0
 

Author Comment

by:akt-uk
ID: 16999831
the other suggestion finds the spaces which i want and when i run the delete command it deletes the file but not the spaces.

I am not a unix expert, but can anyone confirm if this command removes spaces or delete files?

$ find . -type f -regex ".*[ ]+" -exec rm {} \;
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16999877
your question was:
> How can i take out these spaces at the end of each file name?
the answer is http:#16925981

so whatelse do you need?
0
 

Author Comment

by:akt-uk
ID: 16999903
i ran the command and it deleted a file which had spaces, can you please confirm if the command removes spaces and does not delete the file with spaces?

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 17000219
it does not delete the file, it renames the file
0
 
LVL 8

Expert Comment

by:Autogard
ID: 17000627
akt-uk

I posted two simple results above.  They were some of the first results posted.  The first one removes one space from the end, the second removes zero or more spaces from the end:

for i in *; do mv "$i" "${i/\ }"; done
for i in *; do mv "$i" "${i/[\ ]*}"; done

Did you try those?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 14

Expert Comment

by:DonConsolio
ID: 17001361
"mmv" works for me - did you try it ?
0
 

Author Comment

by:akt-uk
ID: 17007659
donconsolio,

i havent tried mmv, but can you please explain to me how would i run the command? i do not have pearl installed on the linux server, i am running commands through shell console.
0
 
LVL 8

Expert Comment

by:Autogard
ID: 17008995
Akt-uk -- you seem to be ignoring my posts above.  Did you try my solutions?  They are pure Linux commands through the shell.
0
 

Author Comment

by:akt-uk
ID: 17009025
Autogard,

How do i run the command? $ mmv '* ' '#1' doesn't really give me enough info to use it, how do i use a this command to remove a space at the end of the file name? sorry to sound stupid but i am not an expert in linux, please help

ps i am not ignoring you you i just didn't know how to use the command and i really don't want to use the command and accidently do somthing silly on the live server.
0
 
LVL 8

Expert Comment

by:Autogard
ID: 17009102
My solution is not to use "mmv", that is DonConsolio's solution (see my last post "Autogard" above).  If you are worried about making a mistake on a live server just create a test directory and put some files in there with spaces on the end and then try my solution:

1. mkdir testdirectory
2. cd testdirectory
3. touch "thisfilehas0spacesontheend"
4. touch "thisfilehas1spaceontheend "
5. touch "thisfilehas2spacesontheend  "
6. touch "thisfilehas3spacesontheend   "
7. touch "thisfilehas4spacesontheend    "
8. ls
8. for i in *; do mv "$i" "${i/[\ ]*}"; done
9. ls

Voila! No spaces on the end of file names.
0
 
LVL 8

Expert Comment

by:Autogard
ID: 17009117
Oh well, you have already accepted an answer, but hopefully this might help someone in the future with a similar problem.

Linux can take a bit of time to learn, but it is worth it (and fun)! :) Good luck akt-uk!
0
 

Author Comment

by:akt-uk
ID: 17009153
Autograd,

i will try this but i also have another question, i have over 100's of files with spaces in a directory, so how can i removes spaces with a fewer or single command?
0
 
LVL 8

Expert Comment

by:Autogard
ID: 17009202
Command #8 is the only command that you need to run to trim off all of the spaces at the end of all of the filenames.

Commands #1-7 and #9 were just extra steps I added so that you could test it out on a test directory first before touching your live data.  Also note that if a file doesn't have spaces on the end it will let you know about it, but the file will not be renamed.
0
 

Author Comment

by:akt-uk
ID: 17009622
Autogard,

I ran the command #8 but i get the following error message:

mv: cannot stat '$' : No such file or directory

Can you please help?
0
 
LVL 8

Expert Comment

by:Autogard
ID: 17009799
>>1. mkdir testdirectory
>>2. cd testdirectory
>>3. touch "thisfilehas0spacesontheend"
>>4. touch "thisfilehas1spaceontheend "
>>5. touch "thisfilehas2spacesontheend  "
>>6. touch "thisfilehas3spacesontheend   "
>>7. touch "thisfilehas4spacesontheend    "
>>8. ls
>>8. for i in *; do mv "$i" "${i/[\ ]*}"; done
>>9. ls

I just noticed I got my numbering mixed up in the above solution -- you should be running the second #8:

for i in *; do mv "$i" "${i/[\ ]*}"; done

I'm not sure why you are getting that error though, are you copying the command exactly as it's shown (a direct copy/paste)?
0
 
LVL 14

Expert Comment

by:DonConsolio
ID: 17009999
sample with "one ", "two " , "three " and "test" :

don@lexa:~> cd data
don@lexa:~/data> ls -1b
one\
test
three\
two\
don@lexa:~/data> mmv '* ' '#1'
don@lexa:~/data> ls -1b
one
test
three
two
don@lexa:~/data>
0
 

Author Comment

by:akt-uk
ID: 17010876
Autogard,

I ran the command and it finds the files with spaces but it deletes them rather then removing spaces
0
 
LVL 8

Expert Comment

by:Autogard
ID: 17011037
Works fine here.  It seems strange to me that if you follow those exact steps (1-9) that I listed earlier that it deletes the files instead of renaming them, especially since you and I are running the same operating system (SuSE).  Sorry you are having trouble with it.  Are you getting any messages written to your screen?

One side-effect to note (and maybe this is what is happening) is that if your file name has a space somewhere in the middle of it it will get rid of that space AND all characters after it.  This only works for taking spaces off the END of filenames.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

If you use Debian 6 Squeeze and you are tired of looking at the childish graphical GDM login screen that is used by default, here's an easy way to change it. If you've already tried to change it you've probably discovered that none of the old met…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now