1

I have a directory with multiple files of same name.

../directory ls
File.txt
File.txt
File1.txt

How can I move all files to a new directory (/new) using mv

../new ls
File.txt
File.txt
File1.txt

Update: The files are being FTPed to an UNIX directory by a source system, which needs to be processed using shell script. Once done I need to move multiple instances of the same file (if any) to an Archive directory.

And by "processed" all I am doing is Copying it to one Dir. And Archive in another using MV. Using cp n mv only one instance of the file is being processed.

  • Thanks @icarus. Have updated the description with a detailed scenario. – Dud Nov 28 '16 at 2:44
  • Ah, so does this mean that you have a fixed name, e.g. results, which is being pushed in via FTP, and you need to move this to a variable filename like Archive/results.2016.11.27.1315 ? – icarus Nov 28 '16 at 2:47
  • Yes. I need to handle multiple instances of the file (which does has a fixed name). Say suppose I have input.txt with 10 EMP details and I unable to process the data. When the next set of file comes with the same name, I am able to move only one file for processing. The most recent one. – Dud Nov 28 '16 at 2:52
  • 1
    Because you have a fixed filename, the FTP process will overwrite the previous input.txt, so you will only have a single file called input.txt at a time (which will be the most recent one). – icarus Nov 28 '16 at 3:02
0

I have a directory with multiple files of same name.

Well, you shouldn't. How can you even cat their contents? (that is, without getting only the first file of the same name each time).

How can I move all files to a new directory (/new) using mv ../new ls File.txt File.txt File1.txt

I'd settle for trying to give them different names:

mv File.txt ../new/File.txt.1
mv File.txt ../new/File.txt.2

and see if you end up with distinct files that way.

Update: The files are being FTPed to an UNIX directory by a source system, which needs to be processed using shell script.

This is overwhelmingly unlikely to result in a situation in which you have multiple files with the same name in the same directory. So don't sweat that case, I guess.

  • The files are being FTPed to an UNIX directory by a source system, which needs to be processed using shell script. Once done I need to move multiple instances of the same file (if any) to an Archive directory. – Dud Nov 28 '16 at 2:40
  • @Dudul Please update your question with more details of this process. – DepressedDaniel Nov 28 '16 at 2:41
  • Thanks Daniel. I hope this makes it clearer. Should had put in the description. – Dud Nov 28 '16 at 2:42
0

Can you not use a wildcard to move it all e.g.

mv file.tx* /new/

This will move all files that have a name that starts with file.tx to your directory /new. It will leave behind any files that don't match, so file1.txt will stay in the directory. If you want to move any file with name file.txt and file1.txt you could move your wildcard * to after file e.g. file* this will then move any file in that directory that has a name starting with file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.