1

I am trying to find all files inside /lib /lib64 folders that contain "lib" in their names and print that file names without extensions (.so or similar), avoiding identical names in output.

My way is: find /lib /lib64 -name "lib" -exec grep-Ev "*.[a-z]{2}" {} \;

I am stuck. It doesn't work. It would be grateful to hear any proposes!

fuser
  • 687
  • 3
  • 17
  • 30
  • Your first issue is the you need -name "*lib*". or just `"lib*" if you want them just starting with lib. – DanSut Oct 07 '15 at 15:10

3 Answers3

1
find /lib /lib64 -name '*lib*' -type f | sed 's/\(.*\)\..*/\1/' | xargs -n 1 basename | sort -u

Find all files in the lib directories. Strip off anything following the last period. Pass the list to basename to remove the directory name. Sort and remove duplicates.

doneal24
  • 5,059
1
find /lib* ! -type d -name \*lib\* | cut -d. -f1 | sort -u

So, the above command should work for filenames that do not contain newlines, otherwise you'd need to do some more explicit tests. As written, this command locates and file with a name that matches the \*lib\* glob and it does so at any depth - it will recursively search all child directories of the base trees returned by the /lib*/ glob.

find just prints its output - that's what it does. When you -exec another command, though, it passes the filenames as arguments to that command, and so you don't grep the file names but the file contents in your command.

So here we parse find's output in a pipeline. cut will print up to the first period it finds on any of its input lines, and sort will take those results and squeeze any repeats down to a single -unique record per.

mikeserv
  • 58,310
  • Just curious, what was the reason for using ! -type d instead of -type f? – rubynorails Oct 08 '15 at 05:10
  • @rubyonrails - well, maybe you're not looking for a regular file, but you're almost definitely not looking for a directory. – mikeserv Oct 08 '15 at 05:15
  • The requester specifically said "I am trying to find all files inside /lib /lib64." In your opinion what would constitute something that is not a directory and not a regular flle -- I guess an irregular file -- especially within those directories? I'm not trying to be nitpicky; I'm just trying to understand. – rubynorails Oct 08 '15 at 05:20
  • 1
    @rubyonrails - there are lots of kinds of files, and a regular file is only one kind. cd /tmp; mkfifo pipe; find . ! -name . -prune -type f. – mikeserv Oct 08 '15 at 05:23
1

It sounds like you are wanting only filenames without any type of extensions. If you are looking for a single pipe to knock this out, grep is not your man. Try sed:

$ find /lib /lib64 -type f -name '*lib*' | sed 's@/.*/@@;s@\..*$@@'

You can pipe to sort -u to remove any duplicates. The below command would be more accurate by using awk and cut (or awk again -- either/or -- cut is shorter/prettier, IMHO):

$ find /lib /lib64 -type f -name '*lib*' | awk -F '/' '{print $NF}' | cut -d. -f1 | sort -u

Returns:

20-libgphoto2-6 40-libsane klibc-P2s_k-gf23VtrGgO2_4pGkQgwMY libBrokenLocale-2 libSegFault libacl libaio libanl-2 libatasmart libattr libaudit libblkid libbsd libbz2 libc-2 libcap libcgmanager libcidn-2 libcom_err libcrypt-2 libcrypto libdbus-1 libdevmapper libdl-2 libe2p libexpat libext2fs libfuse libgcc_s libgcrypt libglib-2 libgpg-error libhistory libjson-c libkeyutils libkmod liblzma liblzo2 libm-2 libmemusage libmount libncurses libncursesw libnewt libnih libnih-dbus libnsl-2 libnss_compat-2 libnss_dns-2 libnss_files-2 libnss_hesiod-2 libnss_nis-2 libnss_nisplus-2 libntfs-3g libpam libpam_misc libpamc libparted libpci libpcprofile libpcre libpcsclite libply libply-boot-client libply-splash-core libply-splash-graphics libpng12 libpopt libprocps libpthread-2 libreadline libresolv-2 librt-2 libselinux libsepol libslang libss libssl libsystemd-daemon libsystemd-login libthread_db-1 libtinfo libudev libulockmgr libusb-0 libusb-1 libutil-2 libuuid libwrap libz

Otherwise, you don't need the pipe to cut to remove all extensions:

$ find /lib /lib64 -type f -name '*lib*' | awk -F '/' '{print $NF}' | sort -u

Returns:

20-libgphoto2-6.hwdb 40-libsane.rules klibc-P2s_k-gf23VtrGgO2_4pGkQgwMY.so libBrokenLocale-2.19.so libSegFault.so libacl.so.1.1.0 libaio.so.1.0.1 libanl-2.19.so libatasmart.so.4.0.5 libattr.so.1.1.0 libaudit.so.1.0.0 libblkid.so.1.1.0 libbsd.so.0.6.0 libbz2.so.1.0.4 libc-2.19.so libcap.so.2.24 libcgmanager.so.0.0.0 libcidn-2.19.so libcom_err.so.2.1 libcrypt-2.19.so libcrypto.so.1.0.0 libdbus-1.so.3.7.6 libdevmapper.so.1.02.1 libdl-2.19.so libe2p.so.2.3 libexpat.so.1.6.0 libext2fs.so.2.4 libfuse.so.2.9.2 libgcc_s.so.1 libgcrypt.so.11.8.2 libglib-2.0.so.0.4000.0 libgpg-error.so.0.10.0 libhistory.so.6.3 libjson-c.so.2.0.0 libkeyutils.so.1.4 libkmod.so.2.2.5 liblzma.so.5.0.0 liblzo2.so.2.0.0 libm-2.19.so libmemusage.so libmount.so.1.1.0 libncurses.so.5.9 libncursesw.so.5.9 libnewt.so.0.52.15 libnih-dbus.so.1.0.0 libnih.so.1.0.0 libnsl-2.19.so libnss_compat-2.19.so libnss_dns-2.19.so libnss_files-2.19.so libnss_hesiod-2.19.so libnss_nis-2.19.so libnss_nisplus-2.19.so libntfs-3g.so.841.0.0 libpam.so.0.83.1 libpam_misc.so.0.82.0 libpamc.so.0.82.1 libparted.so.0.0.1 libpci.so.3.2.1 libpcprofile.so libpcre.so.3.13.1 libpcsclite.so.1.0.0 libply-boot-client.so.2.1.0 libply-splash-core.so.2.1.0 libply-splash-graphics.so.2.1.0 libply.so.2.1.0 libpng12.so.0.50.0 libpopt.so.0.0.0 libprocps.so.3.0.0 libpthread-2.19.so libreadline.so.6.3 libresolv-2.19.so librt-2.19.so libselinux.so.1 libsepol.so.1 libslang.so.2.2.4 libss.so.2.0 libssl.so.1.0.0 libsystemd-daemon.so.0.0.10 libsystemd-login.so.0.7.1 libthread_db-1.0.so libtinfo.so.5.9 libudev.so.1.3.5 libulockmgr.so.1.0.1 libusb-0.1.so.4.4.4 libusb-1.0.so.0.1.0 libutil-2.19.so libuuid.so.1.3.0 libwrap.so.0.7.6 libz.so.1.2.8

rubynorails
  • 2,293