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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
|
07-02-2012, 01:38 PM
|
#1
|
LQ Newbie
Registered: Jun 2012
Posts: 23
Rep: 
|
How to delete a character.
Im trying to change a filename so that it doesn't have any hyphen/dashes in it. Basically, I have a file like a-1, I just want a1. any suggestions.
|
|
|
07-02-2012, 01:49 PM
|
#2
|
Member
Registered: Jul 2012
Distribution: OpenSUSE
Posts: 36
Rep: 
|
Have you tried anything to rename the file?
|
|
|
07-02-2012, 01:50 PM
|
#3
|
LQ Newbie
Registered: Jun 2012
Posts: 23
Original Poster
Rep: 
|
Well thats not actually the file name, just an example. But with the real file name ive changed it to make it all lowercase using tr.
|
|
|
07-02-2012, 01:59 PM
|
#4
|
Member
Registered: Jul 2012
Distribution: OpenSUSE
Posts: 36
Rep: 
|
So, did you figure it out? Do you still need help?
|
|
|
07-02-2012, 02:01 PM
|
#5
|
LQ Newbie
Registered: Jun 2012
Posts: 23
Original Poster
Rep: 
|
yeah, i dont know how to take out a hyphen from a file name
|
|
|
07-02-2012, 02:02 PM
|
#6
|
Member
Registered: Jul 2012
Distribution: OpenSUSE
Posts: 36
Rep: 
|
In your example it would be
|
|
|
07-02-2012, 02:06 PM
|
#7
|
LQ Newbie
Registered: Jun 2012
Posts: 23
Original Poster
Rep: 
|
hm, yeah that works great on my file, but for curiosities sake, how would you get rid of the hyphens in a string of randomly named files.
|
|
|
07-02-2012, 02:15 PM
|
#8
|
Member
Registered: Jul 2012
Distribution: OpenSUSE
Posts: 36
Rep: 
|
To remove the hyphens out of all the filenames in a directory you'd create a bash script:
Code:
#!/bin/bash
for i in ls
do
mv $i `echo $i | sed -e 's/-//g'`
done
|
|
|
07-02-2012, 02:17 PM
|
#9
|
LQ Newbie
Registered: Jun 2012
Posts: 23
Original Poster
Rep: 
|
im new, and to me that seems complex, could you explain it for me? that would be helpful.
|
|
|
07-02-2012, 02:27 PM
|
#10
|
Member
Registered: Jul 2012
Distribution: OpenSUSE
Posts: 36
Rep: 
|
Basically you put those commands into a text file in the directory in question. Change the file to be executable.
Then run it.
Here's a breakdown of what is happening:
This tells the system that it's a bash script.
(it should be `ls` not just ls) Take the output of the "ls" command and store it in the variable $i, repeat what is between "do" and "done" for every file in the list.
Code:
mv $i `echo $i | sed -e 's/-//g'`
mv $i will output "mv <filename>", the "sed" bit outputs the filename without hyphens. So it runs "mv <filename> <filename_without_hyphens>".
|
|
|
07-02-2012, 02:35 PM
|
#11
|
LQ Newbie
Registered: Jun 2012
Posts: 23
Original Poster
Rep: 
|
thanks, that worked. ok, since you are awesome, now lets suppose in these filenames there are numbers in order, 1,2,3,4,...etc. how could i then reformat them so that they are each four digits. 0001, 0002, 0003, 0004, etc.
|
|
|
07-02-2012, 02:40 PM
|
#12
|
Member
Registered: Jul 2012
Distribution: OpenSUSE
Posts: 36
Rep: 
|
Here's another thread talking about just that.
|
|
|
07-02-2012, 03:12 PM
|
#13
|
Moderator
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
|
Quote:
Originally Posted by earthnet
|
Using the output of ls in scripts is considered bad practice, since it can cause problems with filenames that have a space or a newline in them. I would change that to
|
|
1 members found this post helpful.
|
07-02-2012, 03:35 PM
|
#14
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
Quote:
Originally Posted by brock_pace
thanks, that worked. ok, since you are awesome, now lets suppose in these filenames there are numbers in order, 1,2,3,4,...etc. how could i then reformat them so that they are each four digits. 0001, 0002, 0003, 0004, etc.
|
brock;
If you look back over all the help you've been getting here, this should be pretty easy----e.g. have you read the SED reference we discussed earlier?
In SED, you can replace a pattern explicitly, or you can detect it and then replace it using a back reference. Here's an example of the latter: (eg imagine I'm acting on a string "xyz", and I want it to be "oooxyz")
Code:
sed 's/\(xyz\)/ooo\1/'
now, instead of the literal "xyz", suppose I want to do the same thing for any 3 characters:
Code:
sed 's/\(...\)/ooo\1/'
Now---in your question---do you want to change all files beginning is a single number? What exactly is the rule to be applied?
|
|
1 members found this post helpful.
|
07-09-2012, 03:16 PM
|
#15
|
LQ Newbie
Registered: Jun 2012
Posts: 23
Original Poster
Rep: 
|
I would like for all files to have 4 digit numbers. For instance 1 would be 0001. 257 would be 0257. Im doing this for 240 files.
|
|
|
All times are GMT -5. The time now is 01:06 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|