Solved

mv fails for  1000 files

Posted on 2001-08-09
8
610 Views
Last Modified: 2011-09-20
I'm trying to rename 1000 files with extension *.nc to *.txt.

When I say
mv *.nc *.txt

It fails. It says
ksh: /bin/mv: arg list too long

Why does this happen and how to resolve it.?
0
Comment
Question by:prashant_n_mhatre
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 1

Accepted Solution

by:
gstoddart earned 50 total points
ID: 6370528
The reason it says the command is too long is there is a system setting called MAXPATHLEN.  Anyplace between 1K and 4K (Depending on system).  No command line can exceed that value.  You apparently did.

The command "mv *.nc *.txt" is definitely not the command you want since with multiple arguments, the last argument better be a directory.  (mv = move, renaming is a side effect)

You'll probably need to write a script that does it a little more exhaustively.  The following is a SAMPLE which works by making a few naive assumptions.  You would probably need to modify it if you had files that all looked like "aaaa-123456789.nc".

#!/bin/sh
#
# Change file extension of a group of files in a named dir

if [ $# -ne 3 ]; then
    echo "Usage:  rename_files dir suffix_1 suffix_2"
    exit 1
fi

TARGET_DIR=$1
SUFFIX_1=$2
SUFFIX_2=$3

cd $TARGET_DIR
for prefix in 0 1 2 3 4 5 6 7 8 9 a b c d e f g h i \
    j k l m n o p q r s t u v w x y z
do
    list=""
    list=`ls $prefix*.$SUFFIX_1 2>/dev/null`
    for file in $list
    do
        mv $file `basename $file $SUFFIX_1`$SUFFIX_2
    done
done
#EOF



Cheers
0
 
LVL 4

Expert Comment

by:Otetelisanu
ID: 6371305
#!/bin/sh
#
for file in `ls *.nc|cut -d. -f1`
do
mv $file.nc $file.txt
done

This script run
in the same directory
with file *.nc
and mv sequential.
0
 
LVL 4

Expert Comment

by:Otetelisanu
ID: 6371334
#!/bin/sh
#
for file in `ls *.nc|cut -d. -f1`
do
mv $file.nc /dir/$file.txt
done

This script run
in the same directory
with file *.nc
and mv sequential.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6371802
ls *.nc|sed -e ' s#\(.*\)\.nc#mv & \1.txt#'|sh
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 6373136
gstoddart:

>> (mv = move, renaming is a side effect)

Does Unix have any special command for renaming? (like 'ren' on DOS)
0
 
LVL 1

Expert Comment

by:gstoddart
ID: 6373885
Nothing portable.  Depending on your flavour of UNIX, there could be something defined for it.  There could be some file-manager type application which knows how as well.

Other than little shell hacks like I posted, I'm not aware of a really portable way to do what you're asking.

Either way, from a command line you stil have the max command line restriction, which is probably going to be shorter than the combined names of 1000 files.

Cheers

0
 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 6374066
Thank you all !!!
0
 
LVL 4

Expert Comment

by:pluim
ID: 6374165
find ./ -name "*.nc" | xargs -i basename {} ".nc" | xargs -i mv {}.nc {}.txt

This will find all files ending in .nc in the current directory, strip off the .nc extenstion (to get the base name xxxx), and then rename xxxx.nc to xxxx.txt

By using find and xargs, you avoid the shell limitation on the length of the arguments.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now