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.
Ok mate am lost now is this a sh script? if so i don't have sh on my server
This is indeed a shell script. The first line of the script itself (#!/bin/bash) points to bash (which just about every linux distribution has). Linux does not look at extensions, the .sh part is there for the user to recognize it as a shell script.
Quote:
also will this delete all numbers? the reason i ask is this bit
Code:
mv "${FILE}" "${FILE/#[0-9]* - /}"
I don't know what it does but the 0-9 bit scares me as some of my image's have numbers in them which i need to keep
This won't delete all the numbers. It (#[0-9]* - ) looks for files that start with numbers, followed by a space, a dash and a space.
123 - abc.abc.png will become: abc.abc.png 321 - abc.123.png will become: abc.123.png 345 - 123.123.png will become: 123.123.png
Quote:
last time i did this i had a file i moved in each folder than edited it abit like this you said before
Code:
sed -i 's/^......//' infile
the more dots it had the more it trim'ed from the start of the file's
You shouldn't use the sed examples given before in this post. It was assumed that you wanted to change the content of the file, not the file name itself.
How to use the script I provided in post #15:
- cd into the directory that holds Directory 1, Directory 2 etc,
- using an editor, safe the code that I provided (I used changer.sh as the script name, but it can be almost anything you like),
- change the permissions to make the script executable: chmod 750 changer.sh
- execute the script: ./changer.sh
About the red part: I would strongly suggest to create a temporary directory and copy some of the files and directories into that temporary directory and do the given steps there. If something goes wrong or isn't to your liking you still have the original files (ie: no harm done). Once you are satisfied with the results you can run the script from your original directory (and remove the temporary directory).
If unsure: Please ask.
Hope this clears things up a bit.
Last edited by druuna; 02-24-2011 at 01:15 PM.
Reason: Fixed typo's/spelling
Hi,
This is indeed a shell script. The first line of the script itself (#!/bin/bash) points to bash (which just about every linux distribution has). Linux does not look at extensions, the .sh part is there for the user to recognize it as a shell script.
This won't delete all the numbers. It (#[0-9]* - ) looks for files that start with numbers, followed by a space, a dash and a space.
123 - abc.abc.png will become: abc.abc.png 321 - abc.123.png will become: abc.123.png 345 - 123.123.png will become: 123.123.png
You shouldn't use the sed examples given before in this post. It was assumed that you wanted to change the content of the file, not the file name itself.
How to use the script I provided in post #15:
- cd into the directory that holds Directory 1, Directory 2 etc,
- using an editor, safe the code that I provided (I used changer.sh as the script name, but it can be almost anything you like),
- change the permissions to make the script executable: chmod 750 changer.sh
- execute the script: ./changer.sh
About the red part: I would strongly suggest to create a temporary directory and copy some of the files and directories into that temporary directory and do the given steps there. If something goes wrong or isn't to your liking you still have the original files (ie: no harm done). Once you are satisfied with the results you can run the script from your original directory (and remove the temporary directory).
If unsure: Please ask.
Hope this clears things up a bit.
Thank you this sound's like a dream to me
Test folder worked!
Here goes one big folder.
Wow lolol Thank you so much 100000 folder done in about 20 second's lol
If i could see you i would Kiss you
Last edited by Vodkaholic1983; 02-24-2011 at 02:21 PM.
#!/bin/bash
find . -maxdepth 1 -type f | sed 's%^\./%%' | grep "^[0-9]* - " | \
while read FILE
do
mv "${FILE}" "${FILE/#[0-9]* - /}"
done
The above should work on single directories (again: test this first. It does work on my side, but yours might be different).
BTW: You do not have to move the script itself into the directories. I'm not familiar with your set-up so I keep it as simple as possible:
- Go to your home directory: cd ~
- Using an editor, safe the new script (I called it changer2.sh, but that's up to you)
- Make it executable: chmod 750 changer2.sh
- Go to the desired (test) directory: cd /some/dir/with/files
- To run the changer2.sh script in the directory you are now standing in: ~/changer2.sh
Quote:
Test folder worked!
Here goes one big folder.
Wow lolol Thank you so much 100000 folder done in about 20 second's lol
#!/bin/bash
find . -maxdepth 1 -type f | sed 's%^\./%%' | grep "^[0-9]* - " | \
while read FILE
do
mv "${FILE}" "${FILE/#[0-9]* - /}"
done
The above should work on single directories (again: test this first. It does work on my side, but yours might be different).
BTW: You do not have to move the script itself into the directories. I'm not familiar with your set-up so I keep it as simple as possible:
- Go to your home directory: cd ~
- Using an editor, safe the new script (I called it changer2.sh, but that's up to you)
- Make it executable: chmod 750 changer2.sh
- Go to the desired (test) directory: cd /some/dir/with/files
- To run the changer2.sh script in the directory you are now standing in: ~/changer2.sh
It's always nice to see a happy penguin
Hope this helps.
Thank you again!
I also have a question about htaccess could I post about it on this site do you think?
You don't use the mv -n on the script, you need to add -n to the mv command:
Code:
mv -n "${FILE}" "${FILE/#[0-9]* - /}"
Linux is case sensitive which means that if you have these 3 files in one directory: Foo, FoO and FOO and move (mv) them to foo you end up with one file called foo, the -n option (Do not overwrite an existing file.) prevents that from happening.
You don't use the mv -n on the script, you need to add -n to the mv command:
Code:
mv -n "${FILE}" "${FILE/#[0-9]* - /}"
Linux is case sensitive which means that if you have these 3 files in one directory: Foo, FoO and FOO and move (mv) them to foo you end up with one file called foo, the -n option (Do not overwrite an existing file.) prevents that from happening.
Yea I worked that out in the end hehe thanks again
sed -r 's/[a-zA-Z0-9]+\.[a-z]+[ \t]+[a-zA-Z0-9]+\.[a-z]+[ \t]+-[ \t]+[0-9]+[ \t]+-[ \t]+(.*)/\1/'
Thanks could you show me where I add that in the change.sh file?
As I don't see any sed in there
Code:
#!/bin/bash
find $PWD -type d | grep -v "^$PWD$" | \
while read THISDIR
do
cd "$THISDIR"
ls | while read FILE
do
mv -n "${FILE}" "${FILE/#[0-9]* - /}"
done
done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.