|
Home > Archive > Unix Shell > August 2007 > rename question
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| hantechs@gmail.com 2007-08-21, 1:21 pm |
| Such a task:
find all files with name such like *.new and clip the part of .new,
for example:
rename abc.new to abc
I have tried to use find and xargs, but did not get it.
Is there a simpler command to do this?
Thanks.
| |
| Glenn Jackman 2007-08-21, 1:21 pm |
| At 2007-08-21 10:50AM, "hantechs@gmail.com" wrote:
> Such a task:
> find all files with name such like *.new and clip the part of .new,
> for example:
> rename abc.new to abc
> I have tried to use find and xargs, but did not get it.
> Is there a simpler command to do this?
(Assuming you're only interested in the current directory) I usually
take this approach for this task:
for f in *.new; do echo mv "$f" "${f%.new}"; done
Remove the echo when you're ready for action.
--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
| |
| Glenn Jackman 2007-08-21, 1:21 pm |
| At 2007-08-21 10:50AM, "hantechs@gmail.com" wrote:
> Such a task:
> find all files with name such like *.new and clip the part of .new,
> for example:
> rename abc.new to abc
> I have tried to use find and xargs, but did not get it.
> Is there a simpler command to do this?
(Assuming you're only interested in the current directory) I usually
take this approach for this task (bash or ksh):
for f in *.new; do echo mv "$f" "${f%.new}"; done
Remove the echo when you're ready for action.
--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
| |
|
| On Aug 21, 9:50 am, "hante...@gmail.com" <hante...@gmail.com> wrote:
> Such a task:
> find all files with name such like *.new and clip the part of .new,
> for example:
> rename abc.new to abc
> I have tried to use find and xargs, but did not get it.
> Is there a simpler command to do this?
> Thanks.
Here is a script which will accept two patterns and output a another
script - which can be run to rename the files.
#!/usr/bin/ksh93
#
# Name: global rename
#
# Purpose: to rename a set of files matching a pattern
#
# Date: 2005
#
# Version: 1.1
#
# Required Parameters:
#
-------------------------------------------------------------------------
# old pattern to search for
# new pattern to replace with
#
# Optional Parameters:
#
-------------------------------------------------------------------------
# None.
#
#
# Change History:
#
# Date Name Comments
#
________________________________________
_________________________________
# 2005 purdym new
#
#
#
########################################
########################################
###
# print_instructions
########################################
########################################
###
print_instructions () {
cat <<EOF
Syntax: $0 pattern_to_search_for pattern_to_replace_with
Parameters:
This script does the following:
1. search for file names matching pattern_to_search_for
2. create a script to rename the files
EOF
}
########################################
########################################
###
# Verify command line arguments
########################################
########################################
###
if (( $# != 2 ))
then
print_instructions
exit -1
fi
########################################
########################################
###
# command line args
########################################
########################################
###
OLD=$1
NEW=$2
########################################
########################################
###
# Variables
########################################
########################################
###
DEBUG=0 # 0=false; 1=true
PROGRAM_NAME=$0
set_variables
RETCODE1=$?; if (( $RETCODE == 0 )); then RETCODE=$RETCODE1; fi
(( $DEBUG == $TRUE )) && print "DEBUG: "
OLD_NAMES=$WORK_DIR/${0##*/}.old.$$.tmp
NEW_NAMES=$WORK_DIR/${0##*/}.new.$$.tmp
COMMAND_FILE=$WORK_DIR/${0##*/}.command.$$.tmp
########################################
########################################
###
# Start
########################################
########################################
###
print_start_date $PROGRAM_NAME
########################################
########################################
###
# Main Line Here
########################################
########################################
###
ls -1 > $OLD_NAMES
cat $OLD_NAMES | while read LINE
do
echo "mv \"$LINE\" \"\c" >>$COMMAND_FILE
TEMP=$(echo $LINE | sed "s/$OLD/$NEW/g")
echo "$TEMP\"" >>$COMMAND_FILE
done
chmod o+x $COMMAND_FILE
cat $COMMAND_FILE
########################################
########################################
###
# exit
########################################
########################################
###
print_end_date $PROGRAM_NAME
exit $RETCODE
| |
| hantechs@gmail.com 2007-08-22, 1:22 am |
| On Aug 21, 11:42 pm, Glenn Jackman <gle...@ncf.ca> wrote:
> At 2007-08-21 10:50AM, "hante...@gmail.com" wrote:
>
>
> (Assuming you're only interested in the current directory) I usually
> take this approach for this task:
>
> for f in *.new; do echo mv "$f" "${f%.new}"; done
>
> Remove the echo when you're ready for action.
>
> --
> Glenn Jackman
> "You can only be young once. But you can always be immature." -- Dave Barry
Thanks. What about if I'm interested in the current directory and its
all subdirectories?
| |
| Glenn Jackman 2007-08-22, 1:23 pm |
| At 2007-08-21 09:12PM, "hantechs@gmail.com" wrote:
> On Aug 21, 11:42 pm, Glenn Jackman <gle...@ncf.ca> wrote:
>
> Thanks. What about if I'm interested in the current directory and its
> all subdirectories?
find . -name \*.new | while read f
do
mv "$f" "${f%.new}"
done
--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
| |
|
| -----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
hantechs@gmail.com sent the following transmission through subspace:
> find all files with name such like *.new and clip the part of .new,
> for example:
> rename abc.new to abc
man rename
Redhat/Mandriva version of rename:
find /some/folder -name *\.new | xargs rename '.new' ''
Debian version of rename:
find /some/folder -name *\.new | xargs rename 's/\.new//g'
- --
Solbu - http://www.solbu.net
Remove 'ugyldig.' for email
PGP key ID: 0xFA687324
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)
iD8DBQFGzPUbT1rWTfpocyQRAtffAKCjYdR3bFo8
i9FGDny2HNvNs9FsPQCggdpX
4iDAD80DgpcfuMuQPosP2mo=
=N3UJ
-----END PGP SIGNATURE-----
|
|
|
|
|