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.
Basically you put those commands into a text file in the directory in question. Change the file to be executable.
Code:
chmod 755 <file>
Then run it.
Code:
./<file>
Here's a breakdown of what is happening:
Code:
#!/bin/bash
This tells the system that it's a bash script.
Code:
for i in `ls`
(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>".
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.
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
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.