Linux

How to Find Files or Folders in Linux

When your first trying to deal with a Linux machine you had to learn some basic commands to control it. Today we will help you to figure it out some basic but very essential stuff you need to manage your Linux server/vps or computer.  If you are first time linux user you may not know all basic commands you need, so the main command for VPS users is to find ( locate) files they need to change.

Find

We will start with the most common command.  Find is one of the essential Linux utilities. It looks for a string in the directories you’ve set according to parameters (“switches”) that you’ve included. This example:

find /home/username/Documents -iname “writ*” -type f

means that you’re performing a case-insensitive (-iname) search for files (-type f) in the Documents folder, and their filenames begin with “writ”. As you can see, find supports wildcards, and you can also use them to find files by their extension (for example, “*.pdf” to find all PDF files in a folder).

You can search for empty files with the -empty option, or find files by size and modification time. Find supports regular expressions, and if you want to search file contents, you can combine it with grep.

Finding by Type

You can specify the type of files you want to find with the “-type” parameter. It works like this:

find -type type_descriptor query

Some of the most common descriptors that you can use to specify the type of file are here:

  • f: regular file
  • d: directory
  • l: symbolic link
  • c: character devices
  • b: block devices

For instance, if we wanted to find all of the character devices on our system, we could issue this command:

find / -type c
/dev/parport0
/dev/snd/seq
/dev/snd/timer
/dev/autofs
/dev/cpu/microcode
/dev/vcsa7
/dev/vcs7
/dev/vcsa6
/dev/vcs6
/dev/vcsa5
/dev/vcs5
/dev/vcsa4
. . .

We can search for all files that end in “.conf” like this:

find / -type f -name "*.conf"
/var/lib/ucf/cache/:etc:rsyslog.d:50-default.conf
/usr/share/base-files/nsswitch.conf
/usr/share/initramfs-tools/event-driven/upstart-jobs/mountall.conf
/usr/share/rsyslog/50-default.conf
/usr/share/adduser/adduser.conf
/usr/share/davfs2/davfs2.conf
/usr/share/debconf/debconf.conf
/usr/share/doc/apt-utils/examples/apt-ftparchive.conf
. . .

locate

An alternative to using find is the locate command. This command is often quicker and can search the entire file system with ease.

You can install the command with apt-get:

sudo apt-get update
sudo apt-get install mlocate

Locate uses a different approach. It relies on the updatedb utility which creates a database of your files and periodically updates it via cron scheduling. This lets locate know which files are currently present on your filesystem. You can also update the database manually whenever you want.

Locate can search for files by name, and you can use wildcards and regular expressions in your query. For instance:

locate -ei grub.cfg

will list the paths to all existing (-e) files called “grub.cfg”. The -i option stands for “case-insensitive”. If you don’t know the full name of the file you’re looking for, just type a part of it, and locate will display all files with the word in their name.

whereis

This command has a very specific purpose, so you probably won’t use it every day. Whereis shows you the location of the source, binaries, and user manuals for a given application. This means you won’t run whereis when you want to find a random text file. You will, however, use it when you need to check where GIMP or Firefox keep their configuration and executable files.

You can run whereis without any options to get a list of all files, or add switches for their respective functions (-b for binaries, -s for source, and -m for manuals).

To Top