Moving files and remaning them if that name is allready in use

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Moving files and remaning them if that name is allready in use
# 1  
Old 10-10-2009
Moving files and remaning them if that name is allready in use

Hi everyone.

I would like to how can i move a file to a directory (mv) but if in that directory there is a file with the same name it automatacly renames the file including .1 in the moving file name. For example:

I want to move file Test to the directory MyDirectory. But in MyDirectory there is allready a file with the name Test. After moving the Test file, the directory MyDirectory would have 2 files: Test and Test.1. If i would move another file with the name Test to MyDirectory it would end up like this: Test, Test.1 and Test.2.

I can't find a option on mv that would help me with this.
Thanks in advance.
# 2  
Old 10-10-2009
I dont think a simple "mv" can do this.

You should write some script to achieve this.
# 3  
Old 10-10-2009

There is no such option, but using basic shell stuff it should be easy enough.

bash code:
  1. #!/usr/bin/ksh
  3. [ $# -eq 0 ] && echo "Provide a filename" && exit 1
  5. C=1             # new default extention
  6. TARGET=test     # destination folder
  7. FILE=$1
  10. while true; do
  11.   if [ -f $TARGET/$NEWFILE ]; then
  12.     NEWFILE=$FILE.$C
  13.     C=$((C + 1))
  14.   else
  15.     mv $FILE $TARGET/$NEWFILE
  16.     break
  17.   fi
  18. done
# 4  
Old 10-10-2009
The GNU version of mv has a -b option that does what you want.
# 5  
Old 10-10-2009
Wow thanks for your promp answers guys. I'll try this. Thank you all Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Moving Hidden files to normal files

I have a bunch of hidden files in a directory in AIX. I would like to move these hidden files as regular files to another directory. Say i have the following files in directory /x .test~1234~567 .report~5678~123 .find~9876~576 i would like to move them to directory /y as test~1234~567... (10 Replies)
Discussion started by: umesh.narain
10 Replies

2. UNIX for Dummies Questions & Answers

Moving Files to VM

Hi guys, i need to test a script on my RedHat which it's mounted on a VirtualBox (oracle VM). So i need to copy a directory with subdirectories, from a remote host to my VM. I'd like to do that within cmd not with program like Filezilla or something like that. Any idea please? (4 Replies)
Discussion started by: Newer
4 Replies

3. UNIX for Dummies Questions & Answers

Moving files..

Selected directories on our system generate alerts when they exceed 60% of the disk space so I have used gzip to make the files smaller on one of the directories in question (AdminServer logs). I want to move these to another directory what is the best way to make this happen? Thanks.. (4 Replies)
Discussion started by: nosuchluck
4 Replies

4. Shell Programming and Scripting

Finding files with wc -l results = 1 then moving the files to another folder

Hi guys can you please help me with a script to find files with one row/1 line of content then move the file to another directory my script below runs but nothing happens to the files....Alternatively Ca I get a script to find the *.csv files with "wc -1" results = 1 then create a list of those... (5 Replies)
Discussion started by: Dj Moi
5 Replies

5. UNIX for Dummies Questions & Answers

Moving Multiple files to destination files

I am running a code like this foreach list ($tmp) mv *_${list}.txt ${chart}_${list}.txt #mv: when moving multiple files, last argument must be a directory mv *_${list}.doc ${chart}_${list}.doc #mv: when moving multiple files, last argument must be a... (3 Replies)
Discussion started by: animesharma
3 Replies

6. Shell Programming and Scripting

moving the files in a.txt files to a different directory

HI All, I am coding a shell script which will pick all the .csv files in a particular directoryand write it in to a .txt file, this .txt file i will use as a source in datastage for processing. now after the processing is done I have to move and archive all the files in the .txt file to a... (5 Replies)
Discussion started by: subhasri_2020
5 Replies

7. Shell Programming and Scripting

sed/shell scripting - add line if needed and not allready there

I am writing a shell script that checks all .c files to see if they use fprintf or printf. If a file does, then the line #include <stdio.h> is added to the top of the file, unless it's already there. This is what I've got: #!/bin/sh egrep -l f?printf *.c | while read file; do sed -i '1i\... (2 Replies)
Discussion started by: computethis
2 Replies

8. Shell Programming and Scripting

Moving files

I wrote a script which moves files on first in first out basis. for i in `ls -ltr | grep ^- | head -10 | awk '{print $9}'` do mv $i Test/ done But donno some reason, this is not working on my Linux box. May i know the reason? Can the above script be done by using positional... (2 Replies)
Discussion started by: venkatesht
2 Replies

9. Shell Programming and Scripting

Moving Files

Hi There, I am trying to move files, the file is present in this location: /iAm4Free/test/generate/txt/information.txt I need to move it to: /iAm4Free/test1/generate/txt/information.txt The only difference is the "test" is replaced with "test1". But the constraint is. The parent... (5 Replies)
Discussion started by: iAm4Free
5 Replies

10. UNIX for Dummies Questions & Answers

moving only files...

hi.. I want to move a set of files that contain a particular string. I wished to do that with find but i am unable to do that. can anybody give me a good method? :) (12 Replies)
Discussion started by: sskb
12 Replies
Login or Register to Ask a Question