Results 1 to 6 of 6

Thread: bash command-line question

  1. #1
    Get in the van. Fraz's Avatar
    Join Date
    Aug 2007
    Location
    Bristol
    Posts
    2,919
    Thanks
    283
    Thanked
    396 times in 230 posts
    • Fraz's system
      • Motherboard:
      • Gigabyte X58A-UD5
      • CPU:
      • Watercooled i7-980X @ 4.2 GHz
      • Memory:
      • 24GB Crucial DDR3-1333
      • Storage:
      • 240 GB Vertex2E + 2 TB of Disk
      • Graphics card(s):
      • Water-cooled Sapphire 7970 @ 1175/1625
      • PSU:
      • Enermax Modu87+
      • Case:
      • Corsair 700D
      • Operating System:
      • Linux Mint 12 / Windows 7
      • Monitor(s):
      • Dell 30" 3008WFP and two Dell 24" 2412M
      • Internet:
      • Virgin Media 60 Mbps

    bash command-line question

    Ok, so what I want to be able to do is the equivalent of a "not" or "invert selection" in a file browser. I.e if I have a whole bunch of files and folders, and what to keep some particular ones and move/remove the rest, how do I do it? I guess i want something like:

    rm -rf NOT *.type

  2. #2
    Comfortably Numb directhex's Avatar
    Join Date
    Jul 2003
    Location
    /dev/urandom
    Posts
    17,074
    Thanks
    228
    Thanked
    1,026 times in 677 posts
    • directhex's system
      • Motherboard:
      • Asus ROG Strix B550-I Gaming
      • CPU:
      • Ryzen 5900x
      • Memory:
      • 64GB G.Skill Trident Z RGB
      • Storage:
      • 2TB Seagate Firecuda 520
      • Graphics card(s):
      • EVGA GeForce RTX 3080 XC3 Ultra
      • PSU:
      • EVGA SuperNOVA 850W G3
      • Case:
      • NZXT H210i
      • Operating System:
      • Ubuntu 20.04, Windows 10
      • Monitor(s):
      • LG 34GN850
      • Internet:
      • FIOS

    Re: bash command-line question

    i don't know whether there's a cleaner way to do it, but:

    Code:
    ls | grep -v '.type$' | xargs rm
    "ls" lists files in a directory. when you run it as a user, it shows multiple files per line, but when being read by another program (the | passes output from onr program into another as input) then it does one file per line. this is what you want.

    "grep" checks input against a "regular expression", a pattern to match written in a specific pattern-matching language. in this instance, the "-v" flag means "invert result", and ".type$" means "lines which end in .type" ($ means the end-of-line character)

    "xargs" is a utility command which takes a whole bunch of input in, and passes it to another program which might otherwise refuse to run given too many input parameters. if you're talking about 10 matches it doesn't matter, if you're talking about 10 million, it's required.

  3. #3
    Get in the van. Fraz's Avatar
    Join Date
    Aug 2007
    Location
    Bristol
    Posts
    2,919
    Thanks
    283
    Thanked
    396 times in 230 posts
    • Fraz's system
      • Motherboard:
      • Gigabyte X58A-UD5
      • CPU:
      • Watercooled i7-980X @ 4.2 GHz
      • Memory:
      • 24GB Crucial DDR3-1333
      • Storage:
      • 240 GB Vertex2E + 2 TB of Disk
      • Graphics card(s):
      • Water-cooled Sapphire 7970 @ 1175/1625
      • PSU:
      • Enermax Modu87+
      • Case:
      • Corsair 700D
      • Operating System:
      • Linux Mint 12 / Windows 7
      • Monitor(s):
      • Dell 30" 3008WFP and two Dell 24" 2412M
      • Internet:
      • Virgin Media 60 Mbps

    Re: bash command-line question

    Cheers - yeah, I know all those commands. I've come up with something similar myself using find ! -name "*.type". Was just wondering if there was something cleaner. Tis funny really, you'd think there would be!

  4. #4
    Comfortably Numb directhex's Avatar
    Join Date
    Jul 2003
    Location
    /dev/urandom
    Posts
    17,074
    Thanks
    228
    Thanked
    1,026 times in 677 posts
    • directhex's system
      • Motherboard:
      • Asus ROG Strix B550-I Gaming
      • CPU:
      • Ryzen 5900x
      • Memory:
      • 64GB G.Skill Trident Z RGB
      • Storage:
      • 2TB Seagate Firecuda 520
      • Graphics card(s):
      • EVGA GeForce RTX 3080 XC3 Ultra
      • PSU:
      • EVGA SuperNOVA 850W G3
      • Case:
      • NZXT H210i
      • Operating System:
      • Ubuntu 20.04, Windows 10
      • Monitor(s):
      • LG 34GN850
      • Internet:
      • FIOS

    Re: bash command-line question

    i've asked around, and had a few more suggestions:

    Code:
    rm !(*.type)
    (may require you to run "shopt -s extglob" first to enable that functionality)

    Code:
    find . -maxdepth 1 ! -name *.type | xargs rm
    you'll note i'm not using "-rf" anywhere - that's because -rm should be considered dangerous. the "-r" means "if a folder is named like this, remove it and everything in it", which is sometimes not what you want. the "-f" means "don't ask, don't check, just kill it dead", which is usually the last thing you see before shouting "oh ****"

  5. #5
    Comfortably Numb directhex's Avatar
    Join Date
    Jul 2003
    Location
    /dev/urandom
    Posts
    17,074
    Thanks
    228
    Thanked
    1,026 times in 677 posts
    • directhex's system
      • Motherboard:
      • Asus ROG Strix B550-I Gaming
      • CPU:
      • Ryzen 5900x
      • Memory:
      • 64GB G.Skill Trident Z RGB
      • Storage:
      • 2TB Seagate Firecuda 520
      • Graphics card(s):
      • EVGA GeForce RTX 3080 XC3 Ultra
      • PSU:
      • EVGA SuperNOVA 850W G3
      • Case:
      • NZXT H210i
      • Operating System:
      • Ubuntu 20.04, Windows 10
      • Monitor(s):
      • LG 34GN850
      • Internet:
      • FIOS

    Re: bash command-line question

    avoid my first solution, it breaks on files with a space or newline in their filename

  6. #6
    Get in the van. Fraz's Avatar
    Join Date
    Aug 2007
    Location
    Bristol
    Posts
    2,919
    Thanks
    283
    Thanked
    396 times in 230 posts
    • Fraz's system
      • Motherboard:
      • Gigabyte X58A-UD5
      • CPU:
      • Watercooled i7-980X @ 4.2 GHz
      • Memory:
      • 24GB Crucial DDR3-1333
      • Storage:
      • 240 GB Vertex2E + 2 TB of Disk
      • Graphics card(s):
      • Water-cooled Sapphire 7970 @ 1175/1625
      • PSU:
      • Enermax Modu87+
      • Case:
      • Corsair 700D
      • Operating System:
      • Linux Mint 12 / Windows 7
      • Monitor(s):
      • Dell 30" 3008WFP and two Dell 24" 2412M
      • Internet:
      • Virgin Media 60 Mbps

    Re: bash command-line question

    Ahh awesome. Yes, the first one and the "shopt -s extglob" malarky does the trick. Hmm, will have to look up shopt...

    Cheers dude.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Network Files Transfer Question
    By milanlad in forum Networking and Broadband
    Replies: 6
    Last Post: 01-04-2005, 06:46 PM
  2. The 78th Annual Hexus Quiz!
    By Stewart in forum General Discussion
    Replies: 19
    Last Post: 23-01-2005, 02:05 PM
  3. A question about RAID 1
    By Red10 in forum Software
    Replies: 6
    Last Post: 07-01-2005, 04:02 PM
  4. Fans - newbie question (hides in embarrassment)
    By spindle in forum Chassis and Mods
    Replies: 13
    Last Post: 28-04-2004, 02:20 PM
  5. What is Question Time
    By Saracen in forum Question Time
    Replies: 0
    Last Post: 12-08-2003, 05:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •