xargs help me figure out how to write this command - Unix
This is a discussion on xargs help me figure out how to write this command - Unix ; 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
...
-
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.
-
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
-
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
-
Re: xargs help me figure out how to write this command
On 2007-10-26, Keith Willis 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
-
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.