Solved

remove spaces from end of file name

Posted on 2006-06-16
26
792 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 15

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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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
 
LVL 15

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 15

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Zimbra on Amazon Linux help 7 111
The better OS Architecture 13 89
LINUX Field Separators 7 56
Shrink Linux Swap File Size CentOS 10 26
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
You ever wonder how to backup Linux system files just like Windows System Restore?  Well you can use Timeshift in Linux to perform those similar action.  This tutorial will show you how to backup your system files and keep regular intervals. Note…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

749 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