Re: How to find dirs with single item
On Thu, 29 May 2014 14:31:03 -0500 Dennis Wicks w...@mgssub.com wrote: Can't quite figure out how to do this. I'd like to be able to scan a Volume or directory and find all directories that have only one item in them. Either directory or file. Any ideas?? This one * wraps around find utility so that you can easily add further constraints (those will only apply to the directory you search, not to the child), * works in good-old `sh`, * returns true if found at least 1 (similar to grep), * does not slurp the whole list, i.e. should start printing matches ASAP instead of waiting for find to finish: #!/bin/sh filter_single_item() { rv=1 while read path; do test $(ls $path | wc -l) -eq 1 echo $path rv=0 done return $rv } find $@ -type d | filter_single_item Yes it's quite ineffective as it creates at least 3 processes per each dir it scans, but it depends on your case if it's problem. OTOH, if you are sure no dir contains space, you could use `echo` instead of ls and add -w option to wc. Eventually you could even replace wc with some trick with variable expansion. (probably switching to bash for better tricks). aL. -- Alois Mahdal -- To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: https://lists.debian.org/20140602034936.18a1d...@hugo.daonet.home
Re: How to find dirs with single item
On Fri, May 30, 2014 at 3:38 PM, Marko Randjelovic marko...@eunet.rs wrote: On Thu, 29 May 2014 22:43:29 +0100 Clive Standbridge list-u...@tgstandbridges.plus.com wrote: find temp |sed 's|[^/]*$||' |sort |uniq -u This doesn't work on this directory. Can you try the Python script on that, please? File system encodings can be a bit of a pain at times. If it doesn't work as python, try python3, as the two are a bit different as regards Unicode. ChrisA -- To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: https://lists.debian.org/captjjmorv6cyknmcl9+0a+o_nssm2ka-woom+m-uypltnn7...@mail.gmail.com
Re: How to find dirs with single item
On Fri, 30 May 2014 17:17:29 +1000 Chris Angelico ros...@gmail.com wrote: On Fri, May 30, 2014 at 3:38 PM, Marko Randjelovic marko...@eunet.rs wrote: On Thu, 29 May 2014 22:43:29 +0100 Clive Standbridge list-u...@tgstandbridges.plus.com wrote: find temp |sed 's|[^/]*$||' |sort |uniq -u This doesn't work on this directory. Can you try the Python script on that, please? File system encodings can be a bit of a pain at times. If it doesn't work as python, try python3, as the two are a bit different as regards Unicode. ChrisA No problem, but please give me the python code to execute. Regards -- http://markorandjelovic.hopto.org Please make your donation for humanitarian aid for flood victims in Serbia: http://www.floodrelief.gov.rs/eng/ -- To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: https://lists.debian.org/20140530132002.7000a...@eunet.rs
Re: How to find dirs with single item
On Fri, May 30, 2014 at 9:20 PM, Marko Randjelovic marko...@eunet.rs wrote: Can you try the Python script on that, please? File system encodings can be a bit of a pain at times. If it doesn't work as python, try python3, as the two are a bit different as regards Unicode. ChrisA No problem, but please give me the python code to execute. The few lines I posted previously: import os for root, dirs, files in os.walk('.'): if len(dirs + files) == 1: print(root) Save those to a file, run that. It should print out, one per line, the names of directories with just one file or subdir; but I don't know how it'll go with different FS encodings. ChrisA -- To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: https://lists.debian.org/CAPTjJmo_YDCaXTPj7YV7ExYfHwYmTxjHTeU14PLDPQ8gD=k...@mail.gmail.com
How to find dirs with single item
Can't quite figure out how to do this. I'd like to be able to scan a Volume or directory and find all directories that have only one item in them. Either directory or file. Any ideas?? TIA! Dennis -- To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: https://lists.debian.org/53878af7.4010...@mgssub.com
Re: How to find dirs with single item
Hi. On Thu, 29 May 2014 14:31:03 -0500 Dennis Wicks w...@mgssub.com wrote: Can't quite figure out how to do this. I'd like to be able to scan a Volume or directory and find all directories that have only one item in them. Either directory or file. Try this (yep, links should be 3, not 1): find where_you_want_to_search -type d -links 3 Reco -- To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: https://lists.debian.org/20140530005319.a06b755b9639bdf226281...@gmail.com
Re: How to find dirs with single item
Dennis Wicks wrote: I'd like to be able to scan a Volume or directory and find all directories that have only one item in them. Either directory or file. What is a Volume? I immediately think of an LVM volume group. But that isn't exposed as a file system. Technically speaking a directory can never have only one item in them because all directories always start with two items in them when they are created empty. All directories are created with . and .. entries. Therefore you are looking for directories that contain exacty three entries. Normally you won't see the dot entries because they are hidden files unless you give ls the -a option. Normally I would always say use find. But there isn't a test that matches what you want exactly. Therefore the way I would do this would be to write a script. That makes answering this quickly too complicated for an on the fly response. But basically use 'find' to find all directories. Then list each directory to see if it matches your criteria. I will leave that exercise to someone with more time on their hands. :-) Bob signature.asc Description: Digital signature
Re: How to find dirs with single item
Reco wrote: Dennis Wicks wrote: I'd like to be able to scan a Volume or directory and find all directories that have only one item in them. Either directory or file. Try this (yep, links should be 3, not 1): find where_you_want_to_search -type d -links 3 Good try. That will almost work. Using -links 3 will find any directory that has exactly one directory in it. But it won't match exactly one file in a directory. It is the .. link from the subdirectory up that increases the link count from 2 to 3. But files don't create links to the directory and therefore don't increase the link count. The size of the directory isn't of help either. I think the only way is to actually list each directory in turn and count. Perhaps someone else will have a better idea. Bob signature.asc Description: Digital signature
Re: Re: How to find dirs with single item
Hi. On Thu, 29 May 2014 14:31:03 -0500 Dennis Wicks w...@mgssub.com wrote: Can't quite figure out how to do this. I'd like to be able to scan a Volume or directory and find all directories that have only one item in them. Either directory or file. Try this (yep, links should be 3, not 1): find where_you_want_to_search -type d -links 3 Reco That looked neat (just a single process) but it doesn't quite answer the question. It finds directories containing exactly one subdirectory and any number of other files. clive@rimmer:~$ find temp -type d -links 3 | xargs -r ls -A1F temp/for-brum/notes: general/ temp/here: sub-here/ temp/offlineimap-dummy-config: .mutt/ .offlineimaprc .offlineimaprc.~1~ temp/stuff: a/ temp/stuff/a: b/ xyz temp/there: sub-there/ A different approach: find everything, strip off the leaf entry and filter out duplicate parent dirs: find temp |sed 's|[^/]*$||' |sort |uniq -u For example: clive@rimmer:~$ find temp |sed 's|[^/]*$||' |sort |uniq -u | xargs -r ls -A1F temp/for-brum/notes/: general/ temp/for-brum/usbmount/mount.d/: 00_create_model_symlink* temp/for-brum/usbmount/umount.d/: 00_remove_model_symlink* temp/here/: sub-here/ temp/here/sub-here/: extra-here temp/plates/clive-favourites/: clive-favourites.control.template temp/plates/clive-mail-client/: clive-mail-client.control.template temp/plates/clive-mail-local-delivery/: clive-mail-local-delivery.control.template temp/plates/clive-mail/: clive-mail.control.template temp/stuff/: a/ temp/there/: sub-there/ temp/there/sub-there/: extra-there PS the sort is probably unnecessary, just being careful. -- Cheers, Clive -- To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: https://lists.debian.org/20140529214320.GA9259@rimmer.localdomain
Re: How to find dirs with single item
On Thu, 29 May 2014 14:31:03 -0500 Dennis Wicks w...@mgssub.com wrote: Can't quite figure out how to do this. I'd like to be able to scan a Volume or directory and find all directories that have only one item in them. Either directory or file. Any ideas?? TIA! Dennis If you're willing to write a fairly simple Python program it would be pretty easy. You could probably even do it with a find piped to sort piped to a very simple AWK script. SteveT Steve Litt* http://www.troubleshooters.com/ Troubleshooting Training * Human Performance -- To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: https://lists.debian.org/20140529183223.11381b2b@mydesk
Re: How to find dirs with single item
On Fri, May 30, 2014 at 8:32 AM, Steve Litt sl...@troubleshooters.com wrote: On Thu, 29 May 2014 14:31:03 -0500 Dennis Wicks w...@mgssub.com wrote: Can't quite figure out how to do this. I'd like to be able to scan a Volume or directory and find all directories that have only one item in them. Either directory or file. Any ideas?? TIA! Dennis If you're willing to write a fairly simple Python program it would be pretty easy. You could probably even do it with a find piped to sort piped to a very simple AWK script. Challenge accepted :) import os for root, dirs, files in os.walk('.'): if len(dirs + files) == 1: print(root) That'll search the current directory and all subdirectories. (Change the argument to os.walk() to start someplace else.) Should run in any Python from 2.3 onwards, although I only tested it on 2.7 and 3.5ish. As long as you have Python 3.x installed, it can be done as a shell one-liner: rosuav@sikorsky:~/Gypsum$ python3 -c 'import os; [print(r) for r,d,f in os.walk(.) if len(d+f)==1]' ./.git/info rosuav@sikorsky:~/Gypsum$ ls -la .git/info total 12 drwxr-xr-x 2 rosuav rosuav 4096 Feb 20 2013 . drwxr-xr-x 8 rosuav rosuav 4096 May 30 02:33 .. -rw-r--r-- 1 rosuav rosuav 240 Feb 20 2013 exclude (Could be done as a Py2 one-liner, but not as cleanly.) ChrisA -- To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: https://lists.debian.org/captjjmqt4ho-p24vkza3zw41icqmvvutsnjdafpqlzrt8cs...@mail.gmail.com
Re: How to find dirs with single item
On Thu, 29 May 2014 22:43:29 +0100 Clive Standbridge list-u...@tgstandbridges.plus.com wrote: find temp |sed 's|[^/]*$||' |sort |uniq -u This doesn't work on this directory. $ find cenovnici |sed 's|[^/]*$||' |sort |uniq -u cenovnici/comtel_2011-12-20/Aktivna mre� cenovnici/comtel_2011-12-20/Brand ra cenovnici/comtel_2011-12-20/Ext.Aktivna Mre� cenovnici/comtel_2011-12-20/Ext.Potro� cenovnici/comtel_2011-12-20/Inkjet �tampa cenovnici/comtel_2011-12-20/Ku�i� cenovnici/comtel_2011-12-20/Laserski �tampa cenovnici/comtel_2011-12-20/Mati�ne plo cenovnici/comtel_2011-12-20/Memorijske kartice i �ita cenovnici/comtel_2011-12-20/Mi� cenovnici/comtel_2011-12-20/Pasivna mre� cenovnici/comtel_2011-12-20/Potro� cenovnici/comtel_2011-12-20/Potro�ni materijal za �tampa cenovnici/comtel_2011-12-20/Zvu First, it displays an empty line in place of base dir. This can be solved by modified sed expression: sed -n '2~1s|[^/]*$||p' Second the lines are truncated: $ ls -R1 cenovnici cenovnici: comtel.tar.bz2 comtel_2011-12-20 cenovnici/comtel_2011-12-20: Aktivna mre?na oprema.html Brand ra?unari.html DVD,DivX i MP3,MP4 Player.html DVD-RW.html Digitalni aparati i kamere.html Dodatna oprema za notebook.html Ext.Aktivna Mre?na Oprema.html Ext.Modemi.html Ext.Multimedia.html Ext.Potro?ni materijal.html Fiksna telefonija.html Game Controlleri Konzole.html Hard diskovi.html Inkjet ?tampa?i.html Kuleri.html Ku?i?ta i Napajanja.html Laserski ?tampa?i.html Mati?ne plo?e.html Medije (CD,Diskete,ZIP).html Memorije.html Memorijske kartice i ?ita?i.html Mi?evi.html Monitori.html Multimedia.html Notebook.html Pasivna mre?na oprema.html Potro?ni materijal za ?tampa?.html Potro?ni materijal.html Procesori.html Projektori i oprema.html Softver.html Tastature.html UPS.html USB Memorije.html Video kartice.html Zvu?nici.html ? is in place of character codes which correcspond to ex-Yugoslav-specific latin characters in cp1250 encoding and lines are truncated before or after such character, but apparently not always so this is really strange for me. -- http://markorandjelovic.hopto.org Please make your donation for humanitarian aid for flood victims in Serbia: http://www.floodrelief.gov.rs/eng/ -- To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: https://lists.debian.org/20140530073849.137c0...@eunet.rs