LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-23-2011, 12:00 PM   #1
Vodkaholic1983
Member
 
Registered: May 2010
Distribution: Windows / Debian
Posts: 163

Rep: Reputation: 0
Question About 5000000 file's i need trim


I need to trim(delete) the first 2,3,4,5,6 character's from a set of file's (2 from some 3 from some etc...)

I run debian is there a easy way to do this as am useless are command's and really don't want to download all them file's and use file-monkey just to reupload them.

Thank's

Last edited by Vodkaholic1983; 02-23-2011 at 05:29 PM.
 
Old 02-23-2011, 12:43 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,050

Rep: Reputation: 1915Reputation: 1915Reputation: 1915Reputation: 1915Reputation: 1915Reputation: 1915Reputation: 1915Reputation: 1915Reputation: 1915Reputation: 1915Reputation: 1915
I'd use sed - use "in-place" and address of one to force only the first line to be the target. Wrap it in a simple "for i in ..."

Done.
 
Old 02-23-2011, 12:49 PM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

The cut command comes to mind:
Cut first 2 chars:
Code:
cut -b 1-2 --complement infile
Cut first 6 chars:
Code:
cut -b 1-6 --complement infile
Hope this helps.
 
Old 02-23-2011, 12:55 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

Sed is probably easier (thanks syg00 ):

Cut 2 chars:
Code:
sed -i 's/^..//' infile
Cut 6 chars:
Code:
sed -i 's/^......//' infile
Hope this helps.
 
Old 02-23-2011, 01:00 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,050

Rep: Reputation: 1915Reputation: 1915Reputation: 1915Reputation: 1915Reputation: 1915Reputation: 1915Reputation: 1915Reputation: 1915Reputation: 1915Reputation: 1915Reputation: 1915
I presumed only the first line needed alteration rather than each line - the OP can clarify.
 
Old 02-23-2011, 01:06 PM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,
Quote:
Originally Posted by syg00 View Post
I presumed only the first line needed alteration rather than each line - the OP can clarify.
That is indeed one way of reading it, now that I look at it more closely. OP does need to clarify, the cut command won't work if only the first line needs to be targeted. The sed command needs a small addition:
Code:
sed -i '1s/^..//' infile
That is a one (1), not an el (l).
 
Old 02-23-2011, 01:17 PM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
I need to trim(delete) the first 2,3,4,5,6 character's from a set of file's (2 from some 3 from some etc...)
Your request is just peculiar enough that I'm going to speculate that what you you really mean is that you want to trim the characters from the file names and not from the file contents. Can you confirm that this is correct or not. It would be a shame to see you try a script that wrecks that many files when you wanted to simply rename them.

--- rod.

Last edited by theNbomr; 02-23-2011 at 01:19 PM.
 
Old 02-23-2011, 01:38 PM   #8
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285
More details needed, provide a few examples, be clear and specific if you want us to be.
 
Old 02-23-2011, 04:55 PM   #9
Vodkaholic1983
Member
 
Registered: May 2010
Distribution: Windows / Debian
Posts: 163

Original Poster
Rep: Reputation: 0
Hey sorry here is a few example's

25 - bekkankou blush book bracelet cover crown dress elbow_gloves feena_fam_earthlight from_behind gloves green_eyes hat highres jewelry lavender_hair looking_back purple_hair sitting smile yoake_mae_yori_.png

804 - kanon minase_nayuki tsukimiya_ayu.png

7352 - ahoge armor chopsticks fate_stay_night rice saber solo translated yuuryuu_nagare.jpg


70002 - blue_eyes blue_hair crescent detached_sleeves hat original pointy_ears sakaki_(artist) short_hair wings.png

701358 - bad_id big_al brown_hair cosplay fingerless_gloves gloves guilty_gear headband jewelry kyuichi male necklace simple_background skull smile sol_badguy sol_badguy_(cosplay) solo vocaloid yellow_eyes.png

The part's in bold is what i want to get rid of (tho i can't bold the space's " - " everything up to the first Word basicly

Hope that clear's thing's up some thanks!

Edit = oh also file's are in folder's which say have
2 number's image's all in one folder
3 number's are in there own folder etc..

Last edited by Vodkaholic1983; 02-23-2011 at 05:02 PM.
 
Old 02-23-2011, 05:07 PM   #10
Vodkaholic1983
Member
 
Registered: May 2010
Distribution: Windows / Debian
Posts: 163

Original Poster
Rep: Reputation: 0
Also i ran that comment in a small test folder with 2 image's in it and got this

sed -i 's/^.........//' infile

sed: can't read infile: No such file or directory
 
Old 02-23-2011, 05:46 PM   #11
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
So, to repeat the question, are the things to be modified the file NAMEs or file CONTENT. Yup, it really matters.
--- rod.
 
Old 02-24-2011, 03:23 AM   #12
Vodkaholic1983
Member
 
Registered: May 2010
Distribution: Windows / Debian
Posts: 163

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by theNbomr View Post
So, to repeat the question, are the things to be modified the file NAMEs or file CONTENT. Yup, it really matters.
--- rod.
Yes thought I just showed that above that it is the file name's
Thank's
 
Old 02-24-2011, 03:54 AM   #13
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

Quote:
Edit = oh also file's are in folder's which say have
2 number's image's all in one folder
3 number's are in there own folder etc..
Are you talking about this:
Code:
1 - some file.png
2 - other file.jpg
10 - Directory 1
     01 - file xxxxxxx.jpg
     02 - file yyyyyyy.png
     200 - Directory A
           123 - file zzzzz.jpg
           321 - file aaaaa.png
And what you want to end up with looks like this:
Code:
some file.png
other file.jpg
Directory 1
     file xxxxxxx.jpg
     file yyyyyyy.png
     Directory A
           file zzzzz.jpg
           file aaaaa.png
If not suply us with a before and after example (without the extremely long names ).
 
Old 02-24-2011, 06:37 AM   #14
Vodkaholic1983
Member
 
Registered: May 2010
Distribution: Windows / Debian
Posts: 163

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

Are you talking about this:
Code:
1 - some file.png
2 - other file.jpg
10 - Directory 1
     01 - file xxxxxxx.jpg
     02 - file yyyyyyy.png
     200 - Directory A
           123 - file zzzzz.jpg
           321 - file aaaaa.png
And what you want to end up with looks like this:
Code:
some file.png
other file.jpg
Directory 1
     file xxxxxxx.jpg
     file yyyyyyy.png
     Directory A
           file zzzzz.jpg
           file aaaaa.png
If not suply us with a before and after example (without the extremely long names ).
Code:
Directory 1
     file 27 - xxxxxxx.jpg
     file 28 - yyyyyyy.png
     file 29 - yyyyyyy.png
     file 30 - xxxxxxx.jpg
     file 31 - yyyyyyy.png
     file 32 - yyyyyyy.png
Directory 2
     file 127 - xxxxxxx.jpg
     file 128 - yyyyyyy.png
     file 129 - yyyyyyy.png
     file 127 - xxxxxxx.jpg
     file 128 - yyyyyyy.png
     file 129 - yyyyyyy.png
Directory 3
     file 1127 - xxxxxxx.jpg
     file 1128 - yyyyyyy.png
     file 1129 - yyyyyyy.png
     file 1127 - xxxxxxx.jpg
     file 1128 - yyyyyyy.png
     file 1129 - yyyyyyy.png  
Directory 4
     file 11127 - xxxxxxx.jpg
     file 11128 - yyyyyyy.png
     file 11129 - yyyyyyy.png
     file 11127 - xxxxxxx.jpg
     file 11128 - yyyyyyy.png
     file 11129 - yyyyyyy.png
More like that mate
 
Old 02-24-2011, 07:07 AM   #15
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi again,

Code:
#!/bin/bash

find $PWD -type d | grep -v "^$PWD$" | \
while read THISDIR
do
  cd "$THISDIR"
  ls | while read FILE
  do
    mv "${FILE}" "${FILE/#[0-9]* - /}"
  done
done
Sample run:
Code:
# Before:
~/Temp $ ls -l *
-rwxr-x--- 1 druuna internet  172 Feb 24 12:56 changer.sh

Dir 1:
total 0
-rw-r----- 1 druuna internet 0 Feb 24 12:57 1 - file01.png
-rw-r----- 1 druuna internet 0 Feb 24 12:57 2 - file02.png

Dir 2:
total 0
-rw-r----- 1 druuna internet 0 Feb 24 12:57 11 - file11.png
-rw-r----- 1 druuna internet 0 Feb 24 12:57 12 - file12.png

Dir 3:
total 0
-rw-r----- 1 druuna internet 0 Feb 24 12:57 111 - file21.png
-rw-r----- 1 druuna internet 0 Feb 24 12:57 112 - file22.png

# run the script:
~/Temp $ ./changer.sh

# After:
~/Temp $ ls -l *
-rwxr-x--- 1 druuna internet  172 Feb 24 12:56 changer.sh

Dir 1:
total 0
-rw-r----- 1 druuna internet 0 Feb 24 12:57 file01.png
-rw-r----- 1 druuna internet 0 Feb 24 12:57 file02.png

Dir 2:
total 0
-rw-r----- 1 druuna internet 0 Feb 24 12:57 file11.png
-rw-r----- 1 druuna internet 0 Feb 24 12:57 file12.png

Dir 3:
total 0
-rw-r----- 1 druuna internet 0 Feb 24 12:57 file21.png
-rw-r----- 1 druuna internet 0 Feb 24 12:57 file22.png
Make sure you test this first to make sure thgis is what you actually want!

This does not check for possible duplicate files in a directory, dupes will be overwritten (ie 123 - file.abc.png and 321 - file.abc.png will be reduced to one file: file.abc.png).

Anyway, hope this helps.
 
  


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


All times are GMT -5. The time now is 02:29 AM.

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