How to compare list of files with different extensions and delete extra files?Search for duplicate file names within folder hierarchy?Delete duplicate files with different extensionsHow to split the contents of a file?How can I delete all folders of a specific name without deleting Contents in it?How to delete all files except one named file from a specific folderzip multiple files with the same name but different extensionsregex to compare github links and list repos which end with the same name but have different ownerBash script to run python script for all images in all subdirectoriesHow to copy specific files in a list from subfolders to a folder?Create files if they don't already exist

How can I communicate my issues with a potential date's pushy behavior?

Piecewise convexity and global convexity

Why command hierarchy, if the chain of command is standing next to each other?

(A room / an office) where an artist works

Is there a way to proportionalize fixed costs in a MILP?

Will using a resistor in series with a LED to control its voltage increase the total energy expenditure?

Transition to "Starvation Mode" in Survival Situations

Would the USA be eligible to join the European Union?

"Mouth-breathing" as slang for stupidity

When was "Fredo" an insult to Italian-Americans?

A trip to the library

When did Bilbo and Frodo learn that Gandalf was a Maia?

Does fossil fuels use since 1990 account for half of all the fossil fuels used in history?

Why does cat'ing a file via ssh result in control characters?

Go to last file in vim

How did Arecibo detect methane lakes on Titan, and image Saturn's rings?

Telephone number in spoken words

In the movie Krull, what happened in the Spider Den?

Why does the cable resistance jump from a low value to high value at a particular frequency?

Is it possible to know the exact chord from the roman numerals

Finding the shaded region

Chunk + Enumerate a list of digits

How to not forget things?

Crippling fear of hellfire &, damnation, please help?



How to compare list of files with different extensions and delete extra files?


Search for duplicate file names within folder hierarchy?Delete duplicate files with different extensionsHow to split the contents of a file?How can I delete all folders of a specific name without deleting Contents in it?How to delete all files except one named file from a specific folderzip multiple files with the same name but different extensionsregex to compare github links and list repos which end with the same name but have different ownerBash script to run python script for all images in all subdirectoriesHow to copy specific files in a list from subfolders to a folder?Create files if they don't already exist






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








4















I have 2 folders one contains images files and the second contains text files each text file have the same name as the image file and contains information about the image.eg:



 -Labels:
-1.txt
-2.txt
-3.txt
-6.txt
-Images:
-1.jpg
-2.jpg
-3.jpg
-4.jpg
-5.jpg
-6.jpg


I want to delete images who has not a text file(in this example:4.jpg,5.jpg), I found a method how to determinate the different files but I can't delete them.



diff <(ls -1 ./Images | sed s/.jpg//g) <( ls -1 ./Labels | sed s/.txt//g)









share|improve this question
































    4















    I have 2 folders one contains images files and the second contains text files each text file have the same name as the image file and contains information about the image.eg:



     -Labels:
    -1.txt
    -2.txt
    -3.txt
    -6.txt
    -Images:
    -1.jpg
    -2.jpg
    -3.jpg
    -4.jpg
    -5.jpg
    -6.jpg


    I want to delete images who has not a text file(in this example:4.jpg,5.jpg), I found a method how to determinate the different files but I can't delete them.



    diff <(ls -1 ./Images | sed s/.jpg//g) <( ls -1 ./Labels | sed s/.txt//g)









    share|improve this question




























      4












      4








      4


      1






      I have 2 folders one contains images files and the second contains text files each text file have the same name as the image file and contains information about the image.eg:



       -Labels:
      -1.txt
      -2.txt
      -3.txt
      -6.txt
      -Images:
      -1.jpg
      -2.jpg
      -3.jpg
      -4.jpg
      -5.jpg
      -6.jpg


      I want to delete images who has not a text file(in this example:4.jpg,5.jpg), I found a method how to determinate the different files but I can't delete them.



      diff <(ls -1 ./Images | sed s/.jpg//g) <( ls -1 ./Labels | sed s/.txt//g)









      share|improve this question
















      I have 2 folders one contains images files and the second contains text files each text file have the same name as the image file and contains information about the image.eg:



       -Labels:
      -1.txt
      -2.txt
      -3.txt
      -6.txt
      -Images:
      -1.jpg
      -2.jpg
      -3.jpg
      -4.jpg
      -5.jpg
      -6.jpg


      I want to delete images who has not a text file(in this example:4.jpg,5.jpg), I found a method how to determinate the different files but I can't delete them.



      diff <(ls -1 ./Images | sed s/.jpg//g) <( ls -1 ./Labels | sed s/.txt//g)






      command-line bash






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 5 at 10:35









      pa4080

      16.3k7 gold badges34 silver badges81 bronze badges




      16.3k7 gold badges34 silver badges81 bronze badges










      asked Aug 2 at 15:22









      oussama bousselmioussama bousselmi

      415 bronze badges




      415 bronze badges























          3 Answers
          3






          active

          oldest

          votes


















          7














          Here is a small bash script that can help you to solve this task:





          #!/bin/bash
          for file in Images/*.jpg
          do
          if [[ ! -f "Labels/$(basename $file%.*).txt" ]]
          then
          echo rm "$file"
          fi
          done


          • Remove echo to do the actual changes.

          The script must be executed in the parent directory, here it is formatted as inline command:



          for f in Images/*.jpg; do if [[ ! -f "Labels/$(basename $f%.*).txt" ]]; then echo rm "$f"; fi; done





          share|improve this answer



























          • thank you it works.

            – oussama bousselmi
            Aug 2 at 16:12











          • I am way too fond of sed, your solution is more elegant, thanks!

            – Dries
            Aug 2 at 16:15











          • In zsh you can use $file:r instead of $file%.* to get the filename minus one extension. It's smarter about what that means, which can be an advantage over substring matches when you're dealing with mixed paths that don't all fit a simple pattern. (e.g. file="foo.bar/baz"; echo $file:r will return foo.bar/baz, not foo. Though file="foo.bar/.baz" will produce foo.bar/ by either method, so some care is still required.)

            – FeRD
            Aug 3 at 3:06



















          5














          I think I would do it like this:





          for i in Images/*; do file=`echo $i | sed -e 's/jpg/txt/' -e 's/Images/Labels/'`; if [ ! -f "$file" ] ; then rm $i ; fi; done


          If you want to make sure it works , before actually using it try this first:



          for i in Images/*; do file=`echo $i | sed -e 's/jpg/txt/' -e 's/Images/Labels/'`; if [ ! -f "$file" ] ; then echo rm $i ; fi; done


          It will show which commands will be executed.






          share|improve this answer


































            2
















            cd /path/to/Images
            LIST=$(find /path/to/Labels -iname *.txt -printf "%f|" | sed 's/.txt/.jpg/g')
            rm -i !($LIST)


            1. Generates a list of txt files.

            2. Changes their extension to jpg.


            3. Removes anything that is not on the list.




              • -i is just for safety.






            share|improve this answer





























              Your Answer








              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "89"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1162948%2fhow-to-compare-list-of-files-with-different-extensions-and-delete-extra-files%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              7














              Here is a small bash script that can help you to solve this task:





              #!/bin/bash
              for file in Images/*.jpg
              do
              if [[ ! -f "Labels/$(basename $file%.*).txt" ]]
              then
              echo rm "$file"
              fi
              done


              • Remove echo to do the actual changes.

              The script must be executed in the parent directory, here it is formatted as inline command:



              for f in Images/*.jpg; do if [[ ! -f "Labels/$(basename $f%.*).txt" ]]; then echo rm "$f"; fi; done





              share|improve this answer



























              • thank you it works.

                – oussama bousselmi
                Aug 2 at 16:12











              • I am way too fond of sed, your solution is more elegant, thanks!

                – Dries
                Aug 2 at 16:15











              • In zsh you can use $file:r instead of $file%.* to get the filename minus one extension. It's smarter about what that means, which can be an advantage over substring matches when you're dealing with mixed paths that don't all fit a simple pattern. (e.g. file="foo.bar/baz"; echo $file:r will return foo.bar/baz, not foo. Though file="foo.bar/.baz" will produce foo.bar/ by either method, so some care is still required.)

                – FeRD
                Aug 3 at 3:06
















              7














              Here is a small bash script that can help you to solve this task:





              #!/bin/bash
              for file in Images/*.jpg
              do
              if [[ ! -f "Labels/$(basename $file%.*).txt" ]]
              then
              echo rm "$file"
              fi
              done


              • Remove echo to do the actual changes.

              The script must be executed in the parent directory, here it is formatted as inline command:



              for f in Images/*.jpg; do if [[ ! -f "Labels/$(basename $f%.*).txt" ]]; then echo rm "$f"; fi; done





              share|improve this answer



























              • thank you it works.

                – oussama bousselmi
                Aug 2 at 16:12











              • I am way too fond of sed, your solution is more elegant, thanks!

                – Dries
                Aug 2 at 16:15











              • In zsh you can use $file:r instead of $file%.* to get the filename minus one extension. It's smarter about what that means, which can be an advantage over substring matches when you're dealing with mixed paths that don't all fit a simple pattern. (e.g. file="foo.bar/baz"; echo $file:r will return foo.bar/baz, not foo. Though file="foo.bar/.baz" will produce foo.bar/ by either method, so some care is still required.)

                – FeRD
                Aug 3 at 3:06














              7












              7








              7







              Here is a small bash script that can help you to solve this task:





              #!/bin/bash
              for file in Images/*.jpg
              do
              if [[ ! -f "Labels/$(basename $file%.*).txt" ]]
              then
              echo rm "$file"
              fi
              done


              • Remove echo to do the actual changes.

              The script must be executed in the parent directory, here it is formatted as inline command:



              for f in Images/*.jpg; do if [[ ! -f "Labels/$(basename $f%.*).txt" ]]; then echo rm "$f"; fi; done





              share|improve this answer















              Here is a small bash script that can help you to solve this task:





              #!/bin/bash
              for file in Images/*.jpg
              do
              if [[ ! -f "Labels/$(basename $file%.*).txt" ]]
              then
              echo rm "$file"
              fi
              done


              • Remove echo to do the actual changes.

              The script must be executed in the parent directory, here it is formatted as inline command:



              for f in Images/*.jpg; do if [[ ! -f "Labels/$(basename $f%.*).txt" ]]; then echo rm "$f"; fi; done






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Aug 2 at 16:09

























              answered Aug 2 at 16:02









              pa4080pa4080

              16.3k7 gold badges34 silver badges81 bronze badges




              16.3k7 gold badges34 silver badges81 bronze badges















              • thank you it works.

                – oussama bousselmi
                Aug 2 at 16:12











              • I am way too fond of sed, your solution is more elegant, thanks!

                – Dries
                Aug 2 at 16:15











              • In zsh you can use $file:r instead of $file%.* to get the filename minus one extension. It's smarter about what that means, which can be an advantage over substring matches when you're dealing with mixed paths that don't all fit a simple pattern. (e.g. file="foo.bar/baz"; echo $file:r will return foo.bar/baz, not foo. Though file="foo.bar/.baz" will produce foo.bar/ by either method, so some care is still required.)

                – FeRD
                Aug 3 at 3:06


















              • thank you it works.

                – oussama bousselmi
                Aug 2 at 16:12











              • I am way too fond of sed, your solution is more elegant, thanks!

                – Dries
                Aug 2 at 16:15











              • In zsh you can use $file:r instead of $file%.* to get the filename minus one extension. It's smarter about what that means, which can be an advantage over substring matches when you're dealing with mixed paths that don't all fit a simple pattern. (e.g. file="foo.bar/baz"; echo $file:r will return foo.bar/baz, not foo. Though file="foo.bar/.baz" will produce foo.bar/ by either method, so some care is still required.)

                – FeRD
                Aug 3 at 3:06

















              thank you it works.

              – oussama bousselmi
              Aug 2 at 16:12





              thank you it works.

              – oussama bousselmi
              Aug 2 at 16:12













              I am way too fond of sed, your solution is more elegant, thanks!

              – Dries
              Aug 2 at 16:15





              I am way too fond of sed, your solution is more elegant, thanks!

              – Dries
              Aug 2 at 16:15













              In zsh you can use $file:r instead of $file%.* to get the filename minus one extension. It's smarter about what that means, which can be an advantage over substring matches when you're dealing with mixed paths that don't all fit a simple pattern. (e.g. file="foo.bar/baz"; echo $file:r will return foo.bar/baz, not foo. Though file="foo.bar/.baz" will produce foo.bar/ by either method, so some care is still required.)

              – FeRD
              Aug 3 at 3:06






              In zsh you can use $file:r instead of $file%.* to get the filename minus one extension. It's smarter about what that means, which can be an advantage over substring matches when you're dealing with mixed paths that don't all fit a simple pattern. (e.g. file="foo.bar/baz"; echo $file:r will return foo.bar/baz, not foo. Though file="foo.bar/.baz" will produce foo.bar/ by either method, so some care is still required.)

              – FeRD
              Aug 3 at 3:06














              5














              I think I would do it like this:





              for i in Images/*; do file=`echo $i | sed -e 's/jpg/txt/' -e 's/Images/Labels/'`; if [ ! -f "$file" ] ; then rm $i ; fi; done


              If you want to make sure it works , before actually using it try this first:



              for i in Images/*; do file=`echo $i | sed -e 's/jpg/txt/' -e 's/Images/Labels/'`; if [ ! -f "$file" ] ; then echo rm $i ; fi; done


              It will show which commands will be executed.






              share|improve this answer































                5














                I think I would do it like this:





                for i in Images/*; do file=`echo $i | sed -e 's/jpg/txt/' -e 's/Images/Labels/'`; if [ ! -f "$file" ] ; then rm $i ; fi; done


                If you want to make sure it works , before actually using it try this first:



                for i in Images/*; do file=`echo $i | sed -e 's/jpg/txt/' -e 's/Images/Labels/'`; if [ ! -f "$file" ] ; then echo rm $i ; fi; done


                It will show which commands will be executed.






                share|improve this answer





























                  5












                  5








                  5







                  I think I would do it like this:





                  for i in Images/*; do file=`echo $i | sed -e 's/jpg/txt/' -e 's/Images/Labels/'`; if [ ! -f "$file" ] ; then rm $i ; fi; done


                  If you want to make sure it works , before actually using it try this first:



                  for i in Images/*; do file=`echo $i | sed -e 's/jpg/txt/' -e 's/Images/Labels/'`; if [ ! -f "$file" ] ; then echo rm $i ; fi; done


                  It will show which commands will be executed.






                  share|improve this answer















                  I think I would do it like this:





                  for i in Images/*; do file=`echo $i | sed -e 's/jpg/txt/' -e 's/Images/Labels/'`; if [ ! -f "$file" ] ; then rm $i ; fi; done


                  If you want to make sure it works , before actually using it try this first:



                  for i in Images/*; do file=`echo $i | sed -e 's/jpg/txt/' -e 's/Images/Labels/'`; if [ ! -f "$file" ] ; then echo rm $i ; fi; done


                  It will show which commands will be executed.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Aug 2 at 16:06









                  pa4080

                  16.3k7 gold badges34 silver badges81 bronze badges




                  16.3k7 gold badges34 silver badges81 bronze badges










                  answered Aug 2 at 16:02









                  DriesDries

                  1181 silver badge9 bronze badges




                  1181 silver badge9 bronze badges
























                      2
















                      cd /path/to/Images
                      LIST=$(find /path/to/Labels -iname *.txt -printf "%f|" | sed 's/.txt/.jpg/g')
                      rm -i !($LIST)


                      1. Generates a list of txt files.

                      2. Changes their extension to jpg.


                      3. Removes anything that is not on the list.




                        • -i is just for safety.






                      share|improve this answer































                        2
















                        cd /path/to/Images
                        LIST=$(find /path/to/Labels -iname *.txt -printf "%f|" | sed 's/.txt/.jpg/g')
                        rm -i !($LIST)


                        1. Generates a list of txt files.

                        2. Changes their extension to jpg.


                        3. Removes anything that is not on the list.




                          • -i is just for safety.






                        share|improve this answer





























                          2












                          2








                          2









                          cd /path/to/Images
                          LIST=$(find /path/to/Labels -iname *.txt -printf "%f|" | sed 's/.txt/.jpg/g')
                          rm -i !($LIST)


                          1. Generates a list of txt files.

                          2. Changes their extension to jpg.


                          3. Removes anything that is not on the list.




                            • -i is just for safety.






                          share|improve this answer

















                          cd /path/to/Images
                          LIST=$(find /path/to/Labels -iname *.txt -printf "%f|" | sed 's/.txt/.jpg/g')
                          rm -i !($LIST)


                          1. Generates a list of txt files.

                          2. Changes their extension to jpg.


                          3. Removes anything that is not on the list.




                            • -i is just for safety.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Aug 2 at 16:22

























                          answered Aug 2 at 16:15









                          RavexinaRavexina

                          35.7k14 gold badges97 silver badges126 bronze badges




                          35.7k14 gold badges97 silver badges126 bronze badges






























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Ask Ubuntu!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1162948%2fhow-to-compare-list-of-files-with-different-extensions-and-delete-extra-files%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Grendel Contents Story Scholarship Depictions Notes References Navigation menu10.1093/notesj/gjn112Berserkeree

                              Area configuration aggregation error after install Porto themeMagento 2.1 CE Installed but front/backend not loading/workingCSS not loading on page within Magento 2 pageCannot install module in Magento 2no commands defined in the “setup” namespace. in Magento2Magento 2: Static files are present but shows 404Why do i have to always run the commands to clean cache in Magento 2.1.8?Failure reason: 'Unable to unserialize value.'Error 500 after magento migrationIn production mode the site does not loadMagento 2 : Error 500 after installing

                              Middle Expansion Olielle Resaix Definition: Uttering songs of triumph shouting with joy triumphant exulting Sejunction Journal 붙다 달 고급 품목 외출 The stretch trades the screeching tin. Definition: The act of speaking with a drawl a drawl Cough Sand Definition: An uproar a quarrel a noisy outbreak Shake Iron Publicize Horse House Baby 사과 Resaix Flaggy Jelly Temporary Unequaled Puppet A drop in the bucket Shrew 성격 회원 성질 미팅 The burn frames the tacky quality. Materialistic The smoke reduces the way. Yammoe Nondescript Cheek 얼굴 배 약하다 날리다 타다 The illegal country shows the iron. Help Rule Drearien Smoke Teaching Meaty Wasp Abraham Lincoln Jaws 진심 수리하다 Size Cork Idea Convert Think Lark John Lennon 거울 청소 군 추천하다 아이스크림