Solved

mv fails for  1000 files

Posted on 2001-08-09
8
609 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
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 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…
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.

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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now