I want to move files from directory A to directory B. But there are some conditions.

directory A structure:


So, I want to rename a.txt_20170502 to a.txt and move that file to directory B, but if a.txt is present in directory B, it will not move that file.



This process continue until all the candidate files are moved from directory A to B.

I am confused how I can check if files are already in that directory,It will not move that file.

Condition :- There is another script running in background which will fetching data from directory B. So, if any files are present in directory B , it will be automatically copied mainframe server.

closed as unclear what you're asking by countermode, Satō Katsura, GAD3R, Archemar, Shadur Jan 10 '17 at 16:01

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    You use both move and copy seemingly interchangeably. Please clarify the question - do you want to move (cut and pase) or copy (copy and paste) the files? what happens to other files like a.txt_xxxx and a.txt_yyyy - which one gets renamed, by what order? which one remains in A? – Dani_l Jan 10 '17 at 7:36
  • @Dani_l I want to use move (cut and paste), And first a.txt_xxxx is renamed to a.txt and copied to directory B than it will come to a.txt.bbbb, renamed it to a.txt and check into the destination folder , If files are present there it will not move that file and move to the next one . – Sagar2311 Jan 10 '17 at 7:45
  • do files have any special characters like _ before .txt – Wissam Roujoulah Jan 10 '17 at 7:47
  • @MhdWissamAl-Roujoulah No – Sagar2311 Jan 10 '17 at 7:49
  • @Sagar2311 Should the file be renamed but left in A if it is already present in B? – Kusalananda Jan 10 '17 at 8:42
for file in A/*.txt_*; do
  newfile="B/${file##*/}"  # remove A path, add B path
  newfile="${newfile%_*}"  # remove trailing suffix

  if [[ ! -f "$newfile" ]]; then
    mv "$file" "$newfile"

This will iterate over all files in A that matches *.txt_*. It will construct a new file path by replacing the A path with the B path and strip the trailing _xxxxxxxx suffix from the filename. If the new filename is not already present under B, the file will be moved there.


You can test the existence with:

if [!-f B/a.txt]
  mv a.txt_20170502 B/a.txt

You should replace a.txt_20170502 and a.txt by the variables that you use in your script.

  • 1
    OP question states - This process continue until all the files are copied from directory A to B. Your solution DOES NOT, and CANNOT guarantee that. – Dani_l Jan 10 '17 at 7:38
  • @Dani_I The OP also says I am confused how I can check if files are already in that directory. Perhaps he can clarify what he is looking for. The testing or the continuous copying. – Marco Jan 10 '17 at 7:41
  • @Marco I want ot make it dynamic. not by giving file names. – Sagar2311 Jan 10 '17 at 7:44

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