Contact us

Kansioiden siistiminen turhasta tauhkasta Powershellillä

August 9, 2018 – Samuli Seppänen

Toisinaan voi tulla tarve säilyttää jonkin kansion hakemistorakenne, mutta poistaa sieltä suuria määriä tiedostoja. Näin voi käydä esimerkiksi silloin, kun halutaan säästää käännösprosessin tulokset, esim. exe-tiedostot, mutta päästä eroon lähdekoodista ja käännöksen tuottamista väliaikaistiedostoista. Tämä onnistuu kätevästi Powershellin Where-Object CmdLetillä. Alla oletetaan, että komennot ajetaan kansiosta, jonka sisältö halutaan siistiä. Lähtötilanteessa kansiossa on 91 tiedostoa ja hakemistoa:

PS> Get-Childitem -Recurse|Measure-Object|Select Count|Format-List
 Count : 91

Seuraavaksi poistetaan ne ei-hakemistot (~tiedostot), joiden tiedostopääte ei ole .exe:

PS> Get-ChildItem -Recurse|Where-Object { $_.Name -notlike "*.exe" -and -not $_.PsIsContainer }|Remove-Item
 
 PS> Get-Childitem -Recurse|Measure-Object|Select Count|Format-List
 Count : 12

Näin päästiin eroon lähes kaikesta tarpeettomasta. Tässä tapauksessa käännösprosessi oli kuitenkin tuottanut väliaikaishakemistoja, joita ei haluttu säilyttää. Niistä päästään kuitenkin eroon helposti:

PS> Get-ChildItem -Recurse|Where-Object { $_.Name -eq "unwanted-directory-name" }|Remove-Item -Recurse
 
 PS> Get-Childitem -Recurse|Measure-Object|Select Count|Format-List
 Count : 9

Vaihtoehtoisesti nämä kaksi siistimiskomentoa voi kytkeä yhteen ryhmittelemällä ehdot luovasti:

PS> Get-ChildItem -Recurse|Where-Object { ($_.Name -notlike "*.exe" -and -not $_.PsIsContainer) -or ($_.Name -eq "unwanted-directory-name") }|Remove-Item -Recurse

Huomaa, että jälkimmäinen ehto täsmäisi myös samannimisiin tiedostoihin, mikäli niitä olisi olemassa.

Did you like the article? Share it with others!
Categories

Tags

#aad #Access #acl #alertmanager #ansible #ansible module development #Apache #API #augeas #authentication #authorization #automation #automatization #aws #azure #backup #bash #bitbucket #buildbot #cache #centos #cloud #cloud-init #cloudflare #cloudfront #cluster #connectionsJpa #control repo #custom fact #database #debian #devops #digital sovereignty #DNS #docker #domain mode #duplo #edenred #ejabberd #email #encryption #erb #europe #eyaml #fabric #facter #facts #fargate #fedora #file #finnish #foreman #freeipa #git #github #gitlab #gnome #google #grafana #hammer #hiera #IAM #import #infinispan #Infrastructure as Code #ipmi #irc #jboss #jdk #jenkins #JMESPath #json #kanban #keycloak #letsencrypt #librarian-puppet #librenms #linkedin #Linux #Location #loop #marketing #mautic #Mellon #mfa #microsoft #monitoring #mysql #nagios #network-manager #oauth #oauth2 #office365 #oidc #openshift #open source #openvpn #oxygen #packer #paranormal #pdk #people #php #pkcs7 #pomodoro #Powershell #preseed #presentation #profiles #prometheus #provisioning #puppet #puppet-bolt #puppet-litmus #puppetboard #puppetdb #Puppetfile #puppetserver #puppet types and providers #pxeboot #qemu #quality #r10k #recruitment #redirect #Restrict #Reverse Proxy #robotframework #roles #rspec #ruby #SAML #sem #shell #showsql #snmp #snmpd #software developement #spam #ssh #sso #standardization #systemd #systemd-resolved #teams #terraform #twilio #ubuntu #user-data #vagrant #vanity awards #variable #vim #virtualbox #visualstudio #webdevelopment #wildfly #Windows #wireguard #wordpress #workflow #x11 #xmpp #zimbra
We are
 Puppeteers
menucross-circle