TechTalkz.com Logo Ask the Expert

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Linux & Opensource > Unix

Notices

xargs help me figure out how to write this command

Unix


Reply
 
Thread Tools Display Modes
Old 26-10-2007, 04:33 AM   #1
mazzawi@gmail.com
Guest
 
Posts: n/a
xargs help me figure out how to write this command

I have a bunch of files in a directory,

some of them, not all of them, have a ".tmp" at the end of the file
name i want to rename the files to remove the .tmp ie

before

#ls
1.tmp
2.tmp
3.tmp
4
5

#magic command

#ls
1
2
3
4
5

i think it can be done with something like
ls | xargs | mv

i know how to make a command to add the .tmp's but i don't know how to
remove it.
ls | xargs -t mv {} {}.tmp


Thanks.

  Reply With Quote
Old 26-10-2007, 04:33 AM   #2
Stephane CHAZELAS
Guest
 
Posts: n/a
Re: xargs help me figure out how to write this command

2007-10-25, 15:50(-07), mazzawi@gmail.com:
> I have a bunch of files in a directory,
>
> some of them, not all of them, have a ".tmp" at the end of the file
> name i want to rename the files to remove the .tmp ie
>
> before
>
> #ls
> 1.tmp
> 2.tmp
> 3.tmp
> 4
> 5
>
> #magic command
>
> #ls
> 1
> 2
> 3
> 4
> 5
>
> i think it can be done with something like
> ls | xargs | mv
>
> i know how to make a command to add the .tmp's but i don't know how to
> remove it.
> ls | xargs -t mv {} {}.tmp

[...]

With zsh:

autoload -U zmv # usually in ~/.zshrc
zmv '(*).tmp' '$1'

It's not obvious how xargs can help here.

POSIXly, you can do

for f in *.tmp; do
mv -i -- "$f" "${f%.tmp}"
done

or

ls | sed -n 's/\(.*\)\.tmp$/mv -- "&" "\1"/p' | sh

But that assumes none of the filenames contain any ", \, $, `
characters.

--
Stéphane
  Reply With Quote
Old 26-10-2007, 03:31 PM   #3
Keith Willis
Guest
 
Posts: n/a
Re: xargs help me figure out how to write this command

On Thu, 25 Oct 2007 15:50:50 -0700, mazzawi@gmail.com wrote:

>I have a bunch of files in a directory,
>
>some of them, not all of them, have a ".tmp" at the end of the file
>name i want to rename the files to remove the .tmp ie
>
>before
>
>#ls
>1.tmp
>2.tmp
>3.tmp
>4
>5
>
>#magic command
>
>#ls
>1
>2
>3
>4
>5
>
>i think it can be done with something like
>ls | xargs | mv


for i in `ls $DIRECTORY`
do
mv $i `basename $i .tmp`
done

You'll get warnings for the files without the suffix, but so what?!
--
PGP key ID 0xEB7180EC
  Reply With Quote
Old 26-10-2007, 07:32 PM   #4
Andrew
Guest
 
Posts: n/a
Re: xargs help me figure out how to write this command

On 2007-10-26, Keith Willis <me@privacy.net> wrote:
>
> for i in `ls $DIRECTORY`
> do
> mv $i `basename $i .tmp`
> done
>
> You'll get warnings for the files without the suffix, but so what?!


Not only that, but it is a useless use of ls and will barf of any
special characters (spaces etc) in the filenames (although admitted
it may be known that these are not present). To refine your idea
slightly:

for i in *.tmp
do
mv "$i" "`basename \"$i\" .tmp`"
done

--
Andrew Smallshaw
andrews@sdf.lonestar.org

  Reply With Quote
Old 09-11-2007, 01:34 AM   #5
mazzawi@gmail.com
Guest
 
Posts: n/a
Re: xargs help me figure out how to write this command

thanks guys,
I was able to have the tmp files put in their own directory without
the .tmp extension,
and now i can just do a mv * without having to change the file names.

  Reply With Quote
Reply

Thread Tools
Display Modes



< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT +5.5. The time now is 10:19 PM.


vBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2010, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional