Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. System Admin
  4. Delete common files between directories A and B from directory B - UNIX

Delete common files between directories A and B from directory B - UNIX

Scheduled Pinned Locked Moved System Admin
9 Posts 3 Posters 6 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nt_virus
    wrote on last edited by
    #1

    --@--.--- [~/public_html/user/unixp]# ls ./ ../ 1 a/ b/ ls --@--.--- [~/public_html/user/unixp]# ls a ./ ../ a b --@--.--- [~/public_html/user/unixp]# ls b ./ ../ b c --@--.--- [~/public_html/user/unixp]# find ./b -name 'ls a' -exec rm {}\; find: missing argument to `-exec' Here i want to delete common files between directories A and B that is b from directory b.

    L 1 Reply Last reply
    0
    • N nt_virus

      --@--.--- [~/public_html/user/unixp]# ls ./ ../ 1 a/ b/ ls --@--.--- [~/public_html/user/unixp]# ls a ./ ../ a b --@--.--- [~/public_html/user/unixp]# ls b ./ ../ b c --@--.--- [~/public_html/user/unixp]# find ./b -name 'ls a' -exec rm {}\; find: missing argument to `-exec' Here i want to delete common files between directories A and B that is b from directory b.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      nt_virus wrote:

      'ls a'

      Are these normal or back quotes?

      N 1 Reply Last reply
      0
      • L Lost User

        nt_virus wrote:

        'ls a'

        Are these normal or back quotes?

        N Offline
        N Offline
        nt_virus
        wrote on last edited by
        #3

        yes. it is .. Single quote. 1-Key right of L.

        L 1 Reply Last reply
        0
        • N nt_virus

          yes. it is .. Single quote. 1-Key right of L.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          nt_virus wrote:

          yes. it is .. Single quote. 1-Key right of L.

          I think it should be the backquote '`' character which is just below the Esc key on my keyboard. I think in your case you have passed the string 'ls a' to the -name condition, whereas you actually want the output from the ls a command, so it needs to be put in backquotes for the shell to interpret correctly.

          N 1 Reply Last reply
          0
          • L Lost User

            nt_virus wrote:

            yes. it is .. Single quote. 1-Key right of L.

            I think it should be the backquote '`' character which is just below the Esc key on my keyboard. I think in your case you have passed the string 'ls a' to the -name condition, whereas you actually want the output from the ls a command, so it needs to be put in backquotes for the shell to interpret correctly.

            N Offline
            N Offline
            nt_virus
            wrote on last edited by
            #5

            I rectified with backquote now.. ----@---.-- [~/public_html/user/unixp]# find ./b -name `ls -a` -exec rm {}\; find: warning: Unix filenames usually don't contain slashes (though pathnames do). That means that '-name ./' will probably evaluate to false all the time on this system. You might find the '-wholename' test more useful, or perhaps '-samefile'. Alternatively, if you are using GNU grep, you could use 'find ... -print0 | grep -FzZ ./'. find: paths must precede expression Usage: find [-H] [-L] [-P] [path...] [expression] now ? :(

            L 1 Reply Last reply
            0
            • N nt_virus

              I rectified with backquote now.. ----@---.-- [~/public_html/user/unixp]# find ./b -name `ls -a` -exec rm {}\; find: warning: Unix filenames usually don't contain slashes (though pathnames do). That means that '-name ./' will probably evaluate to false all the time on this system. You might find the '-wholename' test more useful, or perhaps '-samefile'. Alternatively, if you are using GNU grep, you could use 'find ... -print0 | grep -FzZ ./'. find: paths must precede expression Usage: find [-H] [-L] [-P] [path...] [expression] now ? :(

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Erm, you changed ls a to ls -a!

              N 1 Reply Last reply
              0
              • L Lost User

                Erm, you changed ls a to ls -a!

                N Offline
                N Offline
                nt_virus
                wrote on last edited by
                #7

                ----@---.-- [~/public_html/user/unixp]# find ./b -name `ls a` -exec rm {}\; find: warning: Unix filenames usually don't contain slashes (though pathnames do). That means that '-name ./' will probably evaluate to false all the time on this system. You might find the '-wholename' test more useful, or perhaps '-samefile'. Alternatively, if you are using GNU grep, you could use 'find ... -print0 | grep -FzZ ./'. find: paths must precede expression Usage: find [-H] [-L] [-P] [path...] [expression] why this happening :(

                L B 2 Replies Last reply
                0
                • N nt_virus

                  ----@---.-- [~/public_html/user/unixp]# find ./b -name `ls a` -exec rm {}\; find: warning: Unix filenames usually don't contain slashes (though pathnames do). That means that '-name ./' will probably evaluate to false all the time on this system. You might find the '-wholename' test more useful, or perhaps '-samefile'. Alternatively, if you are using GNU grep, you could use 'find ... -print0 | grep -FzZ ./'. find: paths must precede expression Usage: find [-H] [-L] [-P] [path...] [expression] why this happening :(

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  nt_virus wrote:

                  find: warning: Unix filenames usually don't contain slashes (though pathnames do). That means that '-name ./' will probably evaluate to false all the time on this system. You might find the '-wholename' test more useful, or perhaps '-samefile'. Alternatively, if you are using GNU grep, you could use 'find ... -print0 | grep -FzZ ./'. find: paths must precede expression Usage: find [-H] [-L] [-P] [path...] [expression]

                  That's why! It has given you a complete explanation of the issue. Your ls command is still listing the directories with trailing '/' characters and the find command does not like it. I don't know which version of UNIX this is but there should be an option to suppress these characters. Take a look at the man page for ls. Also check whether you have aliased the command to set some default options.

                  1 Reply Last reply
                  0
                  • N nt_virus

                    ----@---.-- [~/public_html/user/unixp]# find ./b -name `ls a` -exec rm {}\; find: warning: Unix filenames usually don't contain slashes (though pathnames do). That means that '-name ./' will probably evaluate to false all the time on this system. You might find the '-wholename' test more useful, or perhaps '-samefile'. Alternatively, if you are using GNU grep, you could use 'find ... -print0 | grep -FzZ ./'. find: paths must precede expression Usage: find [-H] [-L] [-P] [path...] [expression] why this happening :(

                    B Offline
                    B Offline
                    BTKnight
                    wrote on last edited by
                    #9

                    You're on the right track. You don't need the -name parameter, though you'll probably want to make sure that each thing "find" locates is a file, and not something else like a directory or symlink. "find" can be somewhat annoying in that it will spit out the entire relative pathname for each file it finds. That is, find ./dirA -type f will yield ./dirA/file1 ./dirA/file2 To get around that, just chdir into dirA before doing the find.

                    $ cd dirA
                    $ touch file1 file2 file4 file5
                    $ cd ../dirB
                    $ touch file1 file3 file5 file6
                    $ cd ..
                    $ ls dirA
                    file1 file2 file4 file5
                    $ ls dirB
                    file1 file3 file5 file6
                    $ cd dirA
                    $ find . -type f -exec rm -f ../dirB/{} \;
                    $ cd ..
                    $ ls dirA
                    file1 file2 file4 file5
                    $ ls dirB
                    file3 file6
                    $

                    I would have answered earlier, but I only joined last week. :) Hope this late reply helps.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups