Removing certain file types that do not contain the hostname in the file nameRemoving the period from a hostnameAccess Windows machines from Ubuntu on local network by hostnameLocal hostname resolution with local Ubuntu DNS server failsAfter changing hostname, Ubuntu does not recognize the nameUse Nautilus to open an SSH connection using hostnamehow to remove old kernels in grub2, including files?

What is the purpose of R1 in this circuit?

Really bizarre dystopian children’s film with hundreds of young boys forced to play piano

Definition of NMR peak

Why didn't Abraham ask the single best question?

Where do I put nobles/royalty in a scifi military

Doing chemistry under water?

Does a small cup of coffee result in a 45% reduced blood flow to the brain?

Joining elements of a list if those elements are in between two whitespaces

Adding bug screen to 3/4 PVC

Christmas party and autism

What's the difference between words "tongue" and "lingua"?

Should I report a security vulnerability?

Are the expansion number tokens in 5-6 players expansion different from the basic Catan?

Average Two Letters

Why does the media continue to hide the identity of the Trump-Ukraine whistle blower when they have already been outed?

What is `stty line NUMBER` even doing?

Driverless car Puzzle

The quietest classical orchestra instrument to play at home

Is it possible to get reverse life insurance?

Are the DigiNotar certificates supposed to be present in Firefox in a clean install?

Did any astronauts on a mission complain about waking up?

Mordhau grip for bludgeoning damage?

MLE vs Binomial - Beta conjugate pairs

Is camera at risk of condensation in cold temperatures if never removed from bag?



Removing certain file types that do not contain the hostname in the file name


Removing the period from a hostnameAccess Windows machines from Ubuntu on local network by hostnameLocal hostname resolution with local Ubuntu DNS server failsAfter changing hostname, Ubuntu does not recognize the nameUse Nautilus to open an SSH connection using hostnamehow to remove old kernels in grub2, including files?






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









3

















I am doing a file deployment for multiple machines per site, so it is much easier for me to zip all the files once and deploy to every machine. However I would like to remove the config for all the other PC's.



The zip contains various generic files, and also a specific config for each machine.



For example a less of the zip would provide:



generic1.xjson
generic2.xjson
generic3.xjson
generic1.yjson
generic2.yjson
PC-1-1.json
PC-1-2.json
PC-1-3.json


So I am looking to remove every PC-1-*.json that does not contain the PC's hostname.



(the PC hostnames are always exactly PC-1-1, etc.)



I was trying to do something along the lines of:



ls -1 | grep -v *$hostname* | xargs -0 rm *PC-*-*.json


This would remove all the files including the one for the current hostname.



Looking for what I have wrong in this, or if there is a better way of doing this function.










share|improve this question
































    3

















    I am doing a file deployment for multiple machines per site, so it is much easier for me to zip all the files once and deploy to every machine. However I would like to remove the config for all the other PC's.



    The zip contains various generic files, and also a specific config for each machine.



    For example a less of the zip would provide:



    generic1.xjson
    generic2.xjson
    generic3.xjson
    generic1.yjson
    generic2.yjson
    PC-1-1.json
    PC-1-2.json
    PC-1-3.json


    So I am looking to remove every PC-1-*.json that does not contain the PC's hostname.



    (the PC hostnames are always exactly PC-1-1, etc.)



    I was trying to do something along the lines of:



    ls -1 | grep -v *$hostname* | xargs -0 rm *PC-*-*.json


    This would remove all the files including the one for the current hostname.



    Looking for what I have wrong in this, or if there is a better way of doing this function.










    share|improve this question




























      3












      3








      3








      I am doing a file deployment for multiple machines per site, so it is much easier for me to zip all the files once and deploy to every machine. However I would like to remove the config for all the other PC's.



      The zip contains various generic files, and also a specific config for each machine.



      For example a less of the zip would provide:



      generic1.xjson
      generic2.xjson
      generic3.xjson
      generic1.yjson
      generic2.yjson
      PC-1-1.json
      PC-1-2.json
      PC-1-3.json


      So I am looking to remove every PC-1-*.json that does not contain the PC's hostname.



      (the PC hostnames are always exactly PC-1-1, etc.)



      I was trying to do something along the lines of:



      ls -1 | grep -v *$hostname* | xargs -0 rm *PC-*-*.json


      This would remove all the files including the one for the current hostname.



      Looking for what I have wrong in this, or if there is a better way of doing this function.










      share|improve this question















      I am doing a file deployment for multiple machines per site, so it is much easier for me to zip all the files once and deploy to every machine. However I would like to remove the config for all the other PC's.



      The zip contains various generic files, and also a specific config for each machine.



      For example a less of the zip would provide:



      generic1.xjson
      generic2.xjson
      generic3.xjson
      generic1.yjson
      generic2.yjson
      PC-1-1.json
      PC-1-2.json
      PC-1-3.json


      So I am looking to remove every PC-1-*.json that does not contain the PC's hostname.



      (the PC hostnames are always exactly PC-1-1, etc.)



      I was trying to do something along the lines of:



      ls -1 | grep -v *$hostname* | xargs -0 rm *PC-*-*.json


      This would remove all the files including the one for the current hostname.



      Looking for what I have wrong in this, or if there is a better way of doing this function.







      bash files uninstall hostname xargs






      share|improve this question














      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 9 at 19:19









      Andrew WilliamsAndrew Williams

      182 bronze badges




      182 bronze badges























          1 Answer
          1






          active

          oldest

          votes


















          2


















          Your command does not work as expected because of rm *PC-*-*.json! it would expands by shell and includes all PC-x-x.json files.



          Actually you're using xargs in a wrong way.




          Something like this should do the job:



          ls -Q1 | grep -v $hostname | grep -E "PC-1-[0-9]+.json" | xargs echo


          change echo with rm when you where sure that it works.



          A better solution would be using find:



          find -type f -name "PC-*-*.json" -not -name "*$hostname*" -exec rm -i +


          • remove -i from rm after you were sure that command is working correctly.





          share|improve this answer




























          • I ended up using the: ls -Q1 | grep -v $HOSTNAME | grep -E "[0-99]+.json" | xargs rm Variant, I cut off the PC-1 part of the second grep, as there should never be any json in my environment that is formatted like this expect the files I want to target. Also, like an idiot, I was using $hostname in place of $HOSTNAME, which, being relatively new to this I didn't know was not printing the hostname in my query. Thanks for the help!

            – Andrew Williams
            Aug 9 at 19:52







          • 1





            Nice! I think it makes a difference if you switch the filename tests and do -iname "PC-*-*.json" -not -iname "*$hostname*" instead as the second one has to be evaluated much more seldom this way. Also, OP wants -type f (as the first test). Oh and wait, why -iname? OP definitely wants name for both tests.

            – dessert
            Aug 9 at 19:54












          • @dessert You're right... I'll update the answer. iname is somehow burned into my memory and I always use it...

            – Ravexina
            Aug 10 at 5:54











          • @AndrewWilliams You're welcome... about HOSTNAME you are correct! I thought hostname is a variable you defined youreslf ;)

            – Ravexina
            Aug 10 at 5:57













          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/4.0/"u003ecc by-sa 4.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%2f1164664%2fremoving-certain-file-types-that-do-not-contain-the-hostname-in-the-file-name%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown


























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2


















          Your command does not work as expected because of rm *PC-*-*.json! it would expands by shell and includes all PC-x-x.json files.



          Actually you're using xargs in a wrong way.




          Something like this should do the job:



          ls -Q1 | grep -v $hostname | grep -E "PC-1-[0-9]+.json" | xargs echo


          change echo with rm when you where sure that it works.



          A better solution would be using find:



          find -type f -name "PC-*-*.json" -not -name "*$hostname*" -exec rm -i +


          • remove -i from rm after you were sure that command is working correctly.





          share|improve this answer




























          • I ended up using the: ls -Q1 | grep -v $HOSTNAME | grep -E "[0-99]+.json" | xargs rm Variant, I cut off the PC-1 part of the second grep, as there should never be any json in my environment that is formatted like this expect the files I want to target. Also, like an idiot, I was using $hostname in place of $HOSTNAME, which, being relatively new to this I didn't know was not printing the hostname in my query. Thanks for the help!

            – Andrew Williams
            Aug 9 at 19:52







          • 1





            Nice! I think it makes a difference if you switch the filename tests and do -iname "PC-*-*.json" -not -iname "*$hostname*" instead as the second one has to be evaluated much more seldom this way. Also, OP wants -type f (as the first test). Oh and wait, why -iname? OP definitely wants name for both tests.

            – dessert
            Aug 9 at 19:54












          • @dessert You're right... I'll update the answer. iname is somehow burned into my memory and I always use it...

            – Ravexina
            Aug 10 at 5:54











          • @AndrewWilliams You're welcome... about HOSTNAME you are correct! I thought hostname is a variable you defined youreslf ;)

            – Ravexina
            Aug 10 at 5:57
















          2


















          Your command does not work as expected because of rm *PC-*-*.json! it would expands by shell and includes all PC-x-x.json files.



          Actually you're using xargs in a wrong way.




          Something like this should do the job:



          ls -Q1 | grep -v $hostname | grep -E "PC-1-[0-9]+.json" | xargs echo


          change echo with rm when you where sure that it works.



          A better solution would be using find:



          find -type f -name "PC-*-*.json" -not -name "*$hostname*" -exec rm -i +


          • remove -i from rm after you were sure that command is working correctly.





          share|improve this answer




























          • I ended up using the: ls -Q1 | grep -v $HOSTNAME | grep -E "[0-99]+.json" | xargs rm Variant, I cut off the PC-1 part of the second grep, as there should never be any json in my environment that is formatted like this expect the files I want to target. Also, like an idiot, I was using $hostname in place of $HOSTNAME, which, being relatively new to this I didn't know was not printing the hostname in my query. Thanks for the help!

            – Andrew Williams
            Aug 9 at 19:52







          • 1





            Nice! I think it makes a difference if you switch the filename tests and do -iname "PC-*-*.json" -not -iname "*$hostname*" instead as the second one has to be evaluated much more seldom this way. Also, OP wants -type f (as the first test). Oh and wait, why -iname? OP definitely wants name for both tests.

            – dessert
            Aug 9 at 19:54












          • @dessert You're right... I'll update the answer. iname is somehow burned into my memory and I always use it...

            – Ravexina
            Aug 10 at 5:54











          • @AndrewWilliams You're welcome... about HOSTNAME you are correct! I thought hostname is a variable you defined youreslf ;)

            – Ravexina
            Aug 10 at 5:57














          2














          2










          2









          Your command does not work as expected because of rm *PC-*-*.json! it would expands by shell and includes all PC-x-x.json files.



          Actually you're using xargs in a wrong way.




          Something like this should do the job:



          ls -Q1 | grep -v $hostname | grep -E "PC-1-[0-9]+.json" | xargs echo


          change echo with rm when you where sure that it works.



          A better solution would be using find:



          find -type f -name "PC-*-*.json" -not -name "*$hostname*" -exec rm -i +


          • remove -i from rm after you were sure that command is working correctly.





          share|improve this answer
















          Your command does not work as expected because of rm *PC-*-*.json! it would expands by shell and includes all PC-x-x.json files.



          Actually you're using xargs in a wrong way.




          Something like this should do the job:



          ls -Q1 | grep -v $hostname | grep -E "PC-1-[0-9]+.json" | xargs echo


          change echo with rm when you where sure that it works.



          A better solution would be using find:



          find -type f -name "PC-*-*.json" -not -name "*$hostname*" -exec rm -i +


          • remove -i from rm after you were sure that command is working correctly.






          share|improve this answer















          share|improve this answer




          share|improve this answer








          edited Aug 10 at 15:50

























          answered Aug 9 at 19:38









          RavexinaRavexina

          36.9k15 gold badges101 silver badges131 bronze badges




          36.9k15 gold badges101 silver badges131 bronze badges















          • I ended up using the: ls -Q1 | grep -v $HOSTNAME | grep -E "[0-99]+.json" | xargs rm Variant, I cut off the PC-1 part of the second grep, as there should never be any json in my environment that is formatted like this expect the files I want to target. Also, like an idiot, I was using $hostname in place of $HOSTNAME, which, being relatively new to this I didn't know was not printing the hostname in my query. Thanks for the help!

            – Andrew Williams
            Aug 9 at 19:52







          • 1





            Nice! I think it makes a difference if you switch the filename tests and do -iname "PC-*-*.json" -not -iname "*$hostname*" instead as the second one has to be evaluated much more seldom this way. Also, OP wants -type f (as the first test). Oh and wait, why -iname? OP definitely wants name for both tests.

            – dessert
            Aug 9 at 19:54












          • @dessert You're right... I'll update the answer. iname is somehow burned into my memory and I always use it...

            – Ravexina
            Aug 10 at 5:54











          • @AndrewWilliams You're welcome... about HOSTNAME you are correct! I thought hostname is a variable you defined youreslf ;)

            – Ravexina
            Aug 10 at 5:57


















          • I ended up using the: ls -Q1 | grep -v $HOSTNAME | grep -E "[0-99]+.json" | xargs rm Variant, I cut off the PC-1 part of the second grep, as there should never be any json in my environment that is formatted like this expect the files I want to target. Also, like an idiot, I was using $hostname in place of $HOSTNAME, which, being relatively new to this I didn't know was not printing the hostname in my query. Thanks for the help!

            – Andrew Williams
            Aug 9 at 19:52







          • 1





            Nice! I think it makes a difference if you switch the filename tests and do -iname "PC-*-*.json" -not -iname "*$hostname*" instead as the second one has to be evaluated much more seldom this way. Also, OP wants -type f (as the first test). Oh and wait, why -iname? OP definitely wants name for both tests.

            – dessert
            Aug 9 at 19:54












          • @dessert You're right... I'll update the answer. iname is somehow burned into my memory and I always use it...

            – Ravexina
            Aug 10 at 5:54











          • @AndrewWilliams You're welcome... about HOSTNAME you are correct! I thought hostname is a variable you defined youreslf ;)

            – Ravexina
            Aug 10 at 5:57

















          I ended up using the: ls -Q1 | grep -v $HOSTNAME | grep -E "[0-99]+.json" | xargs rm Variant, I cut off the PC-1 part of the second grep, as there should never be any json in my environment that is formatted like this expect the files I want to target. Also, like an idiot, I was using $hostname in place of $HOSTNAME, which, being relatively new to this I didn't know was not printing the hostname in my query. Thanks for the help!

          – Andrew Williams
          Aug 9 at 19:52






          I ended up using the: ls -Q1 | grep -v $HOSTNAME | grep -E "[0-99]+.json" | xargs rm Variant, I cut off the PC-1 part of the second grep, as there should never be any json in my environment that is formatted like this expect the files I want to target. Also, like an idiot, I was using $hostname in place of $HOSTNAME, which, being relatively new to this I didn't know was not printing the hostname in my query. Thanks for the help!

          – Andrew Williams
          Aug 9 at 19:52





          1




          1





          Nice! I think it makes a difference if you switch the filename tests and do -iname "PC-*-*.json" -not -iname "*$hostname*" instead as the second one has to be evaluated much more seldom this way. Also, OP wants -type f (as the first test). Oh and wait, why -iname? OP definitely wants name for both tests.

          – dessert
          Aug 9 at 19:54






          Nice! I think it makes a difference if you switch the filename tests and do -iname "PC-*-*.json" -not -iname "*$hostname*" instead as the second one has to be evaluated much more seldom this way. Also, OP wants -type f (as the first test). Oh and wait, why -iname? OP definitely wants name for both tests.

          – dessert
          Aug 9 at 19:54














          @dessert You're right... I'll update the answer. iname is somehow burned into my memory and I always use it...

          – Ravexina
          Aug 10 at 5:54





          @dessert You're right... I'll update the answer. iname is somehow burned into my memory and I always use it...

          – Ravexina
          Aug 10 at 5:54













          @AndrewWilliams You're welcome... about HOSTNAME you are correct! I thought hostname is a variable you defined youreslf ;)

          – Ravexina
          Aug 10 at 5:57






          @AndrewWilliams You're welcome... about HOSTNAME you are correct! I thought hostname is a variable you defined youreslf ;)

          – Ravexina
          Aug 10 at 5:57



















          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%2f1164664%2fremoving-certain-file-types-that-do-not-contain-the-hostname-in-the-file-name%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

          Tamil (spriik) Luke uk diar | Nawigatjuun

          Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

          Training a classifier when some of the features are unknownWhy does Gradient Boosting regression predict negative values when there are no negative y-values in my training set?How to improve an existing (trained) classifier?What is effect when I set up some self defined predisctor variables?Why Matlab neural network classification returns decimal values on prediction dataset?Fitting and transforming text data in training, testing, and validation setsHow to quantify the performance of the classifier (multi-class SVM) using the test data?How do I control for some patients providing multiple samples in my training data?Training and Test setTraining a convolutional neural network for image denoising in MatlabShouldn't an autoencoder with #(neurons in hidden layer) = #(neurons in input layer) be “perfect”?