[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

mv fails for  1000 files

Posted on 2001-08-09
8
Medium Priority
?
635 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 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

650 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