-2
#!/bin/sh
# This is comment!
echo Hello World
for file1 in /C:/Users/shubham.tomar/Desktop/Shell/Test1/*; 
do
filename1=$(basename "$file1")
echo $filename1
echo "------------------------"
for file2 in /C:/Users/shubham.tomar/Desktop/Shell/Test2/*;
do
filename2=$(basename "$file2")
echo $filename2
if [["filename1" = "filename2"]]; then
echo "---File matched---"
else
mv -f /C:/Users/shubham.tomar/Desktop/Shell/Test2/$filename2 /C:/Users/shubham.tomar/Desktop/Shell/Moved/
fi
echo "--------------File Moved-----------"
done
done

**

NOTE ABOUT PROBLEM

**

There are some files in a particular path for ex: Desktop/Test1 and Downloads/Test2 I want to write a shell script to move all the files present in Test2 and not in Test1 to a path for ex: Documents/MovedFiles files may be of any type

  • Please describe in more detail what you want to do and what you expect as a result. You can also add some examples to make it more clear. – Bodo Jan 22 '19 at 09:28
  • Ive done some edits im new to shell so it might contain some mistakes – BigArmsWriteSmallCode Jan 22 '19 at 09:37
  • 1
    I can only guess from your code that you may want to move files from Test2 that are not in Test1 (or vice versa?) to Moved. Please add some explanating text that describes what you want to do. You could also add some examples what might be in Test1 and Test2 and what you want to be the result in Moved. Nesting two for loops is probably wrong. I guess you don't want to compare filename1 with every file name in Test2 but only check if a file with the same name is present in Test2. – Bodo Jan 22 '19 at 09:43
  • yes you guessed it right it's exactly what i want the files which are not present in Test2 and present in Test1 should get moved to Moved folder Result must be the difference of these two must be in moved folder – BigArmsWriteSmallCode Jan 22 '19 at 10:06
  • Please, edit your question to add your explanation. And what about files present in Test2 and not present in Test1? – Bodo Jan 22 '19 at 10:15

2 Answers2

0

Using nested for loops is wrong. You don't want to compare every file name from Test1 with every file name in Test2.

Edit: Some additionall script lines for debugging and to avoid using file *.

#!/bin/sh

# Check if this is correct
dir1="/C:/Users/shubham.tomar/Desktop/Shell/Test1"
# maybe you have to write
# dir1="/c/Users/shubham.tomar/Desktop/Shell/Test1"
dir2="/C:/Users/shubham.tomar/Desktop/Shell/Test2"
targetdir="/C:/Users/shubham.tomar/Desktop/Shell/Moved"

# for debugging
echo contents of dir1 $dir1
ls "$dir1"
echo contents of dir2 $dir2
ls "$dir2"

# loop over all files in $dir2 because you want to find files in $dir2 that are not in $dir1
for file2 in "$dir2"/*
do
    # if $dir2 does not exist or is empty we will get something like file2="/C:/Users/shubham.tomar/Desktop/Shell/Test2/*" 
    # That's why check if the file exists
    if [ -f "$file2" ]
    then
        filename2=$(basename "$file2")
        echo $filename2
        echo "------------------------"

        # does a file with the same basename exist in $dir1?
        if [ ! -f "$dir1/$filename2" ]
        then
            # using "$targetdir"/." makes sure you get an error if $targetdir is a file instead of a directory
            mv -f "$file2" "$targetdir"/.
            echo "--------------File Moved-----------"
        else
            echo "file with the same name exists in $dir1"
        fi
    fi
done

If you want to try this first without actually moving files you can replace the line

        mv -f "$file2" "$targetdir"/.

with

        echo mv -f "$file2" "$targetdir"/.
Bodo
  • 6,068
  • 17
  • 27
  • Might be wrong I'm new to script. Do you know any other way of moving the difference of files between these two Dir into other Dir – BigArmsWriteSmallCode Jan 22 '19 at 11:32
  • AFTER REMOVING ALL EXTRA SPACES BETWEEN THA CODE
    WHILE EXECUTING I GOT THIS C:\Users\shubham.tomar\Desktop\Shell>bash s.sh s.sh: line 6: syntax error near unexpected token $'\r'' '.sh: line 6:for file2 in "$dir2"/*;
    – BigArmsWriteSmallCode Jan 22 '19 at 11:41
  • I removed a wrong ; at the end of the for statement. If the shell complains about a \r you probably used a Windows text editor for creating the script. You could use dos2unix to change the DOS/Windows line endings \r\n to UNIX line endings \n. Or use Notepad++ function to convert the line endings, see menu Edit - EOL conversion. Sure, there is more than one way to solve a problem. I tried to modify your script, but did not test it. You can use https://www.shellcheck.net/ to find problems in shell scripts. – Bodo Jan 22 '19 at 11:57
  • I was using notepad++. Syntax errors are removed thanks for this link shellcheck.net – BigArmsWriteSmallCode Jan 22 '19 at 12:36
  • NEW ERROR but directory is present in same location parallel to Test1 and Test2 and the output above the first -------- is the folder and files parallel to the bash file as you can relate in output it contains test1, test2, moved a.sh . C:\Users\shubham.tomar\Desktop\Shell>bash a.sh a.sh desktop.ini Moved Test1 Test2

    mv: cannot stat '/C:/Users/shubham.tomar/Desktop/Shell/Test2/*': No such file or directory --------------File Moved-----------

    – BigArmsWriteSmallCode Jan 22 '19 at 12:47
  • @ShubhamTomar I don't fully understand your comment. Please, edit your question and and add code blocks to show the output of ls -1 /C:/Users/shubham.tomar/Desktop/Shell/Test1 and ls -1 /C:/Users/shubham.tomar/Desktop/Shell/Test2. If you modied my proposed script, also add a code block with the new script. Last add a code block with the command you entered and the output including the error message. – Bodo Jan 22 '19 at 13:15
  • https://justpaste.it/65x4v go to the link I've created and tried my best to explain you and a big thanks for ur help. – BigArmsWriteSmallCode Jan 22 '19 at 15:24
  • It would be better to show additional information here not on some other site. This will help others to understand the problem. I edited my proposed script. I guess the directory /C:/Users/shubham.tomar/Desktop/Shell/Test2 is wrong. I added two ls commands to check this. This resulted in the file name pattern in for file2 in "$dir2"/* to expand to /C:/Users/shubham.tomar/Desktop/Shell/Test2/*. Then filename2 will be * and echo $filename being expanded as echo * to all file names in the current directory. – Bodo Jan 22 '19 at 17:15
0
sf() {
    # find all files in the directory $1 non-recursively and sort them
    find $1 -maxdepth 1 -type f printf '%f\n' | sort
}

Then run

join -v 1 <(sf Tes2) <(sf Tes1) | xargs -d '\n' -I file mv Tes2/file MovedFiles/

The left operand of the pipe finds all the files in Tes2 that are not in the directory Tes1.

The right operand of the pipe then moves those files to the directory MovedFiles.

Niko Gambt
  • 433
  • 3
  • 8