bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

suggested feature/patch for ls: -P TYPES, --select-file-type=TYPES


From: Moreno Baricevic
Subject: suggested feature/patch for ls: -P TYPES, --select-file-type=TYPES
Date: Mon, 31 Oct 2005 19:29:19 +0100 (CET)

Hello all,

I'm new on this list and I'm not sure it's the right place for this kind
of mail/request, so please be kind ;)

I sometimes need to filter the output of ls in order to obtain only some
file-types, usually through cmdlines like these (silly example for
directories, symlinks and regular files):

        ls -l | grep '^[dl\-]'
        ls -l | egrep "^(d|l|\-)"
        find \( -type d -o -type l -o -type f \) -ls
        find \( -type d -o -type l -o -type f \) -exec ls -ld {} \;
        for file in * ; do test -f $file -o -S $file -o -L $file && somestuff 
|| someotherstuff ; done

(with optional -R for ls and -mindepth/-maxdepth for find`).

Yes, of course subdirectories could be easier to find, 'ls */ .[^.]*/ -d'
is sometimes enough (without considering some exceptions like symlink to
dir), but it's not the same for different types.

Therefore, I decided to write a little patch for ls which adds a new
feature and a cmdline option: '-P TYPES | --select-file-type=TYPES',
where TYPES can be one or more file-types identified by a character
(similar to 'test -X', '[ -X', 'find -type X', ...).
The code is indeed inspired (when not stealed) from 'test' and 'ls'
itself and adapted for this purpose.

ls -P TYPES
ls --select-file-type=TYPES

   TYPES
     f          regular file
     d          directory
     c          char device
     b          block device
     p          fifo/named pipe
     l          symbolic link
     s          socket
     D          door

This patch should allow ls to filter the output directly (and faster).

Example:

# NORMAL OUTPUT

# ls testdir/ -l
total 12
-rw-r--r-- 1 baro baro 1654 2005-10-27 17:39 Makefile
brw-r--r-- 1 root root 0, 0 2005-10-27 17:39 test_block
crw-r--r-- 1 root root 0, 0 2005-10-27 17:39 test_char
lrwxrwxrwx 1 root root    9 2005-10-27 17:39 test_deadlink -> test_none
drwxr-xr-x 2 root root 4096 2005-10-27 17:39 test_dir
-rw-r--r-- 1 root root    0 2005-10-27 17:39 test_file
lrwxrwxrwx 1 root root    9 2005-10-27 17:39 test_link -> test_file
lrwxrwxrwx 1 root root   11 2005-10-27 17:39 test_link_to_test_dir -> 
test_subdir
prw-r--r-- 1 root root    0 2005-10-27 17:39 test_pipe
srwxrwxrwx 1 root root    0 2005-10-27 17:39 test_socket
drwxr-xr-x 3 root root 4096 2005-10-27 17:39 test_subdir

# SELECT ONLY DIRECTORIES, NAMED SOCKETS, BLOCK DEVICES AND CHARACTER DEVICES

# ls testdir/ -l -P sbcd
total 8
brw-r--r-- 1 root root 0, 0 2005-10-27 17:39 test_block
crw-r--r-- 1 root root 0, 0 2005-10-27 17:39 test_char
drwxr-xr-x 2 root root 4096 2005-10-27 17:39 test_dir
srwxrwxrwx 1 root root    0 2005-10-27 17:39 test_socket
drwxr-xr-x 3 root root 4096 2005-10-27 17:39 test_subdir


I chose -P because some time ago (coreutils-5.2.1) it seemed somehow
related to -p/--file-type (now -F) ... and because it's one of the last
few unused short options ;)


Here's the patch:

        
http://www.democritos.it/~baro/sw/var/coreutils/5.92/ls-5.92.select-file-type.patch.gz

It has been successfully tested on any filesystem that my linux-box is
able to create (ext2, ext3, reiserfs, xfs, jfs, minix, fat{12,16,32}).
Just for fun, it has also been successfully tested on any
distro/kernel/arch/compiler I was able to find: slack, redhat, suse,
fedora, debian; 2.4.26, 2.4.29, 2.4.31, 2.6.8, 2.6.10; i686 (athlon,
pentium, xeon), x86_64 (emt64t, opteron); gcc-{2.95,2.96,3.3.4,3.4.2}.
Than, I had a coffee break...


I'd be glad to hear any comments, suggestions, critics or insults ;)


Best wishes

Moreno

PS: sorry for my awful English ;)





reply via email to

[Prev in Thread] Current Thread [Next in Thread]