[X4U] Deleting part of file names

David Ledger david.ledger at ivdcs.co.uk
Tue Jun 7 04:03:47 PDT 2011


At 00:20 -0500 7/6/11, Eugene wrote:
>On Mon, Jun 06, 2011 at 09:55:54AM CDT, Jim Moldenauer <m1sc at wi.rr.com> wrote:
>>
>>  How do you delete the first 10 words of the file names? I have over
>>  200 files with the same first 10 letters. The rest of the names are
>>  alphabetical but the first 10 letters are redundant.
>>  Command F (find) won't do it. I read up on AutoScript but that looks
>>  like it do it if it was folders. I could not see where I could do it
>>  with file names. It probably would if I could understand it. It
>>  doesn't speak about file names in particular. This is strange because
>>  I can't find out how to eliminate them. Any help would be appreciated.
>
>At a shell command line:
>
>shell> cd <directory-containing-the-redundantly-named-files>
>
>shell> for a in *; do b=`echo "$a" | sed 's/^abcde12345//'`; mv "$a" 
>"$b"; done
>
>where "abcde12345" are your "first 10 letters".

Or just

for a in *; do
	b="${a#??????????}"
	[ "${a%$b}" != "abcde12345"  ] && continue
	mv "$a" "$b"
done

where there are 10 x '?'s in each case, and abcŠ is as above. It 
doesn't need to exec sed every time round the loop.
If all the files in the folder have names starting with abcde12345 
then the line ending with 'continue' can be omitted. Also in that 
case the entire loop content can be reduced to

mv "$a"  "${a#??????????}"

David


-- 
David Ledger - Freelance Unix Sysadmin in the UK.
HP-UX specialist of hpUG technical user group (www.hpug.org.uk)
david.ledger at ivdcs.co.uk
www.ivdcs.co.uk


More information about the X4U mailing list