Solved

mv fails for  1000 files

Posted on 2001-08-09
8
620 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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!

 
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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
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…
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.

705 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