LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 02-24-2011, 07:20 AM   #16
Vodkaholic1983
Member
 
Registered: May 2010
Distribution: Windows / Debian
Posts: 163

Original Poster
Rep: Reputation: 0

Ok mate am lost now is this a sh script? if so i don't have sh on my server

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

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

I am really a newbie at linux so sorry if i read this wrong
Thank's
 
Old 02-24-2011, 07:55 AM   #17
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
Hi,
Quote:
Originally Posted by Vodkaholic1983 View Post
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 02:15 PM. Reason: Fixed typo's/spelling
 
1 members found this post helpful.
Old 02-24-2011, 03:12 PM   #18
Vodkaholic1983
Member
 
Registered: May 2010
Distribution: Windows / Debian
Posts: 163

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by druuna View Post
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 03:21 PM.
 
Old 02-24-2011, 03:53 PM   #19
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
Hi,

This will work on a single directory:
Code:
#!/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

If i could see you i would Kiss you
It's always nice to see a happy penguin

Hope this helps.
 
1 members found this post helpful.
Old 02-24-2011, 04:01 PM   #20
Vodkaholic1983
Member
 
Registered: May 2010
Distribution: Windows / Debian
Posts: 163

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by druuna View Post
Hi,

This will work on a single directory:
Code:
#!/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?
 
Old 02-24-2011, 04:07 PM   #21
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
Hi,
Quote:
Originally Posted by Vodkaholic1983 View Post
Thank you again!
You're welcome

Quote:
I also have a question about htaccess could I post about it on this site do you think?
Yes, open a new thread in Linux Newbie or one of the other sub-forums, if that one seems more appropriate.

BTW: If this is solved, can you put up the [SOLVED] flag (first post -> Thread Tools menu).
 
Old 04-13-2011, 01:48 PM   #22
Vodkaholic1983
Member
 
Registered: May 2010
Distribution: Windows / Debian
Posts: 163

Original Poster
Rep: Reputation: 0
Hey again question does this "delete" files if there names are the same?
I run this with 25k files and after it was done there was only 8k

Ive reupload the missing files
 
Old 04-13-2011, 03:09 PM   #23
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
You should use the 'mv -n' option, or it will overwrite files (deleting them).
 
Old 04-13-2011, 03:50 PM   #24
Vodkaholic1983
Member
 
Registered: May 2010
Distribution: Windows / Debian
Posts: 163

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by H_TeXMeX_H View Post
You should use the 'mv -n' option, or it will overwrite files (deleting them).
Hey thanks for that so is that
Code:
mv -n ./changer.sh
in the putty?
Thanks again
 
Old 04-14-2011, 01:04 AM   #25
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
Hi

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.
 
Old 04-14-2011, 07:52 AM   #26
Vodkaholic1983
Member
 
Registered: May 2010
Distribution: Windows / Debian
Posts: 163

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by druuna View Post
Hi

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
 
Old 04-23-2011, 10:56 AM   #27
Vodkaholic1983
Member
 
Registered: May 2010
Distribution: Windows / Debian
Posts: 163

Original Poster
Rep: Reputation: 0
Hi again (last lot of files) am on now
and the file names have change some

example

all now start with this

animelon.com animelon.com - 1 - akanbe itou_noiji nagato_yuki suzumiya_haruhi_no_yuutsu white 120.jpg
animelon.com animelon.com - 2 - akanbe itou_noiji nagato_yuki suzumiya_haruhi_no_yuutsu white 120.jpg
animelon.com animelon.com - 3 - akanbe itou_noiji nagato_yuki suzumiya_haruhi_no_yuutsu white 120.jpg

can this be changed to remove the "animelon.com animelon.com - 3 - "

Thanks again you guys have saved me soon very much work
 
Old 04-23-2011, 11:07 AM   #28
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Code:
sed -r 's/[a-zA-Z0-9]+\.[a-z]+[ \t]+[a-zA-Z0-9]+\.[a-z]+[ \t]+-[ \t]+[0-9]+[ \t]+-[ \t]+(.*)/\1/'
 
Old 04-23-2011, 11:15 AM   #29
Vodkaholic1983
Member
 
Registered: May 2010
Distribution: Windows / Debian
Posts: 163

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by MTK358 View Post
Code:
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
 
Old 04-23-2011, 11:18 AM   #30
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
If you want the result in a different file:

Code:
sed -r 'expression' source_file > dest_file
If you want the original file to be overwritten with the new data:

Code:
sed -i -r 'expression' file
Hopefully you can figure it out now.

EDIT: Also, you shold NEVER use ls output in a script. It's meant for human reading only.

Use this instead to loop over files in a directory:

Code:
for FILE in *
do
    # stuff
done

Last edited by MTK358; 04-23-2011 at 11:19 AM.
 
  


Reply

Tags
file, trim


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I/O problem -- dd if=/dev/zero of=bf bs=8k count=500000 fengzhong_77 Linux - Newbie 1 11-04-2009 06:28 PM
How can I get the edquota file's name? snowball0916 Ubuntu 10 07-17-2009 10:31 AM
How to get a file's symlinks jfrankman Linux - Newbie 10 11-02-2006 12:45 PM
file's extension??? amanjsingh Linux - Newbie 2 04-17-2004 10:22 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration