?
Solved

mv fails for  1000 files

Posted on 2001-08-09
8
Medium Priority
?
640 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 200 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 51

Expert Comment

by:ahoffmann
ID: 6371802
ls *.nc|sed -e ' s#\(.*\)\.nc#mv & \1.txt#'|sh
0
 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

850 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