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
    284
    Thanked
    397 times in 231 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
    Vive le pants! directhex's Avatar
    Join Date
    Jul 2003
    Location
    /dev/urandom
    Posts
    17,074
    Thanks
    228
    Thanked
    1,027 times in 678 posts
    • directhex's system
      • Motherboard:
      • MSI X99A Gaming 7
      • CPU:
      • Intel Core i7 5280k
      • Memory:
      • 32GiB ADATA DDR4
      • Storage:
      • Corsair Neutron XT 960GB
      • Graphics card(s):
      • MSI GTX 980 Gaming 4G Twin Frozr 5
      • PSU:
      • Corsair AX860i
      • Case:
      • NZXT H440
      • Operating System:
      • Ubuntu 17.10, Windows 10
      • Monitor(s):
      • Dell U2713HM
      • 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
    284
    Thanked
    397 times in 231 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
    Vive le pants! directhex's Avatar
    Join Date
    Jul 2003
    Location
    /dev/urandom
    Posts
    17,074
    Thanks
    228
    Thanked
    1,027 times in 678 posts
    • directhex's system
      • Motherboard:
      • MSI X99A Gaming 7
      • CPU:
      • Intel Core i7 5280k
      • Memory:
      • 32GiB ADATA DDR4
      • Storage:
      • Corsair Neutron XT 960GB
      • Graphics card(s):
      • MSI GTX 980 Gaming 4G Twin Frozr 5
      • PSU:
      • Corsair AX860i
      • Case:
      • NZXT H440
      • Operating System:
      • Ubuntu 17.10, Windows 10
      • Monitor(s):
      • Dell U2713HM
      • 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
    Vive le pants! directhex's Avatar
    Join Date
    Jul 2003
    Location
    /dev/urandom
    Posts
    17,074
    Thanks
    228
    Thanked
    1,027 times in 678 posts
    • directhex's system
      • Motherboard:
      • MSI X99A Gaming 7
      • CPU:
      • Intel Core i7 5280k
      • Memory:
      • 32GiB ADATA DDR4
      • Storage:
      • Corsair Neutron XT 960GB
      • Graphics card(s):
      • MSI GTX 980 Gaming 4G Twin Frozr 5
      • PSU:
      • Corsair AX860i
      • Case:
      • NZXT H440
      • Operating System:
      • Ubuntu 17.10, Windows 10
      • Monitor(s):
      • Dell U2713HM
      • 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
    284
    Thanked
    397 times in 231 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
  •