• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 806
  • Last Modified:

remove spaces from end of file name

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
akt-uk
Asked:
akt-uk
  • 9
  • 8
  • 4
  • +3
1 Solution
 
DonConsolioCommented:
try the "mmv" command

$ mmv '* ' '#1'
0
 
AutogardCommented:
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
 
LuxanaCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AutogardCommented:
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
 
ahoffmannCommented:
perl -e'for(@ARGV){($f=$_)=~s/\s$//;rename $_,$f;}' *
0
 
bryanlloydharrisCommented:
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
 
akt-ukAuthor Commented:
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
 
ahoffmannCommented:
what's wrong with the other suggstions?
0
 
akt-ukAuthor Commented:
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
 
ahoffmannCommented:
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
 
akt-ukAuthor Commented:
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
 
ahoffmannCommented:
it does not delete the file, it renames the file
0
 
AutogardCommented:
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
 
DonConsolioCommented:
"mmv" works for me - did you try it ?
0
 
akt-ukAuthor Commented:
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
 
AutogardCommented:
Akt-uk -- you seem to be ignoring my posts above.  Did you try my solutions?  They are pure Linux commands through the shell.
0
 
akt-ukAuthor Commented:
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
 
AutogardCommented:
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
 
AutogardCommented:
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
 
akt-ukAuthor Commented:
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
 
AutogardCommented:
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
 
akt-ukAuthor Commented:
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
 
AutogardCommented:
>>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
 
DonConsolioCommented:
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
 
akt-ukAuthor Commented:
Autogard,

I ran the command and it finds the files with spaces but it deletes them rather then removing spaces
0
 
AutogardCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 9
  • 8
  • 4
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now