Find file and folder in Linux

Search Files And Folders in Linux

There is 4 method for this:

Method One (find)

To find a file in the given path:

~# find / -iname "sshd_config"
/etc/ssh/sshd_config

To find a given folder in system:

~# find / -type d -iname "ssh"
/usr/lib/ssh
/usr/lib/go/src/cmd/vendor/golang.org/x/crypto/ssh
/usr/lib/go/pkg/linux_amd64/cmd/vendor/golang.org/x/crypto/ssh
/etc/ssh

Use wildcard option to search:

~# find / -name "*.config"
/usr/lib/mono/gac/avahi-sharp/1.0.0.0__4d116c78973743f5/avahi-sharp.dll.config
/usr/lib/mono/gac/avahi-ui-sharp/0.0.0.0__4d116c78973743f5/avahi-ui-sharp.dll.config
/usr/lib/python2.7/config/Setup.config
/usr/share/git/mw-to-git/t/test.config
/var/lib/lightdm/.config
/home/daygeek/.config
/root/.config
/etc/skel/.config

To find empty files:

~# find / -empty

Find files that have special name and look for special content in them:

~# find / -type f -exec grep "Port 22" '{}' \; -print
~# find / -type f -print | xargs grep "Port 22"
~# find / -type f | xargs grep 'Port 22'
~# find / -type f -exec grep -H 'Port 22' {} \;

Method-2 (updatedb and locate)

~# updatedb

~# locate --basename '\ssh'
/etc/ssh
/usr/bin/ssh
/usr/lib/ssh
/usr/lib/go/pkg/linux_amd64/cmd/vendor/golang.org/x/crypto/ssh
/usr/lib/go/src/cmd/go/testdata/failssh/ssh
/usr/lib/go/src/cmd/vendor/golang.org/x/crypto/ssh

Method-3 (which command)

The which command returns the full path of the executable that would have been executed when the command had been entered in terminal.

Which command searches the directories listed in the current user’s PATH environment variable not for all the users. I mean, when you are logged in your own account and you can’t able to search for root user file or directory.

~# which vi
/usr/bin/vi

~# which -a vi sudo
/usr/bin/vi
/bin/vi
/usr/bin/sudo
/bin/sudo

Method-4 (whereis command)

The whereis command used to search the binary, source, and man page files for a given command.

~# whereis vi
vi: /usr/bin/vi /usr/share/man/man1/vi.1p.gz /usr/share/man/man1/vi.1.gz

Search Results