How to search and find location of a file in linux ?
In linux there are mainly two methods by which a file's location can be searched. They are
locate command is a Unix/*nix command used to find the location of a file. locate searches for a file in a pre-built database which is created with updatedb command or a background process. The syntax for locate command is
To find the location of a file containing the string file,type
To locate the file, file2.txt, type
locate command is fast but you will have to frequently update the database.To update the database run the command updatedb as root user. For more details and options see the man pages
-atime - Search files based on accessed time
-size - Search file based on file size
-user - Search files by owner
-empty - Search empty files
- locate command
- find command
locate command
locate command is a Unix/*nix command used to find the location of a file. locate searches for a file in a pre-built database which is created with updatedb command or a background process. The syntax for locate command is
locate [option] filename
Examples
To find the location of a file containing the string file,type
locate file
Sample Output:
/boot/grub2/i386-pc/configfile.mod
/boot/grub2/i386-pc/search_fs_file.mod
/etc/filesystems
/etc/profile
/etc/profile.d
/etc/rarfiles.lst
/etc/tmpfiles.d
/etc/cups/cups-files.conf
/etc/dbus-1/system.d/org.kde.nepomuk.filewatch.conf
/etc/dconf/profile
/etc/dconf/profile/gdm
/etc/dconf/profile/ibus
/etc/gconf/schemas/desktop_gnome_file_views.schemas
/etc/gconf/schemas/gnome-media-profiles.schemas
/etc/highlight/filetypes.conf
/etc/kde/env/gtk2_rc_files.sh
/etc/mail/Makefile
/etc/mail/helpfile
/etc/php-zts.d/fileinfo.ini
/etc/php.d/fileinfo.ini
/etc/pki/tls/certs/Makefile
/etc/profile.d/256term.csh
/etc/profile.d/256term.sh
/etc/profile.d/PackageKit.sh
/etc/profile.d/colorgrep.csh
/etc/profile.d/colorgrep.sh
/etc/profile.d/colorls.csh
/etc/profile.d/colorls.sh
/etc/profile.d/kde.csh
/etc/profile.d/kde.sh
/etc/profile.d/lang.csh
/etc/profile.d/lang.sh
/etc/profile.d/less.csh
/etc/profile.d/less.sh
/etc/profile.d/qt-graphicssystem.csh
/etc/profile.d/qt-graphicssystem.sh
/etc/profile.d/qt.csh
/etc/profile.d/qt.sh
/etc/profile.d/vim.csh
/etc/profile.d/vim.sh
/etc/profile.d/vte.sh
/etc/profile.d/which2.csh
:
/boot/grub2/i386-pc/search_fs_file.mod
/etc/filesystems
/etc/profile
/etc/profile.d
/etc/rarfiles.lst
/etc/tmpfiles.d
/etc/cups/cups-files.conf
/etc/dbus-1/system.d/org.kde.nepomuk.filewatch.conf
/etc/dconf/profile
/etc/dconf/profile/gdm
/etc/dconf/profile/ibus
/etc/gconf/schemas/desktop_gnome_file_views.schemas
/etc/gconf/schemas/gnome-media-profiles.schemas
/etc/highlight/filetypes.conf
/etc/kde/env/gtk2_rc_files.sh
/etc/mail/Makefile
/etc/mail/helpfile
/etc/php-zts.d/fileinfo.ini
/etc/php.d/fileinfo.ini
/etc/pki/tls/certs/Makefile
/etc/profile.d/256term.csh
/etc/profile.d/256term.sh
/etc/profile.d/PackageKit.sh
/etc/profile.d/colorgrep.csh
/etc/profile.d/colorgrep.sh
/etc/profile.d/colorls.csh
/etc/profile.d/colorls.sh
/etc/profile.d/kde.csh
/etc/profile.d/kde.sh
/etc/profile.d/lang.csh
/etc/profile.d/lang.sh
/etc/profile.d/less.csh
/etc/profile.d/less.sh
/etc/profile.d/qt-graphicssystem.csh
/etc/profile.d/qt-graphicssystem.sh
/etc/profile.d/qt.csh
/etc/profile.d/qt.sh
/etc/profile.d/vim.csh
/etc/profile.d/vim.sh
/etc/profile.d/vte.sh
/etc/profile.d/which2.csh
:
To locate the file, file2.txt, type
locate file2.txt
Sample Output:
/home/calypso/file2.txt
locate command is fast but you will have to frequently update the database.To update the database run the command updatedb as root user. For more details and options see the man pages
find command
find command searches for files in a directory hierarchy. i.e find command searches through one or more directories specified by the user and locates the required files. find is slower than locate command, but find is more powerful and has more features than locate command. find can search for files based on criteria like name, file type, time, path, size and so on.Syntax for find is
find [options] path [expression]
while using find atleast one path should be mentioned Some important filter
-name - Search for file with the name mentioned
-type - Search for a file of specific type. Different type filters are listed below
- b block (buffered) special
- c character (unbuffered) special
- d directory
- p named pipe (FIFO)
- f regular file
- l symbolic link
- s socket
-atime - Search files based on accessed time
-size - Search file based on file size
-user - Search files by owner
-empty - Search empty files
Operators
( expr ) | Force precedence. |
! expr | True if expr is false |
-not expr | Same as ! expr, but not POSIX compliant. |
expr1 expr2 | Two expressions in a row are taken to be joined with an implied "and"; expr2 is not evaluated if expr1 is false. |
expr1 -a expr2 | Same as expr1 expr2. |
expr1 -and expr2 | Same as expr1 expr2, but not POSIX compliant. |
expr1 -o expr2 | Or; expr2 is not evaluated if expr1 is true. |
expr1 -or expr2 | Same as expr1 -o expr2, but not POSIX compliant. |
expr1 , expr2 | List; both expr1 and expr2 are always evaluated. The value of expr1 is discarded; the value of the list is the value of expr2. |
Some Important Actions
-delete - delete the file
-exec - execute command
-ls - list the file in ls -dils format
-ok - same as exec but requires user confirmation
-prune - if the file is a directory, do not descend into it
Examples
To find a file named resolv.conf, type
find / -name resolv.conf
The above command searches for files with name resolv.conf under the / directory (i.e. the entire system) and prints the output Sample Output:
/etc/resolv.conf
/etc/sysconfig/networking/profiles/default/resolv.conf
/etc/sysconfig/networking/profiles/default/resolv.conf
To find a file name abc.txt under /home/calypso, type
find /home/calypso -name abc.txt
We know linux is case sensitive. In linux abc.txt and ABC.txt are entirely different file. Suppose if you have any confusion over the letter cases in the file name, yo can use find in case insensitive method
find /home/calypso -iname aBc.txt
To find directories with the name jre, type
find / -name jre -type d
The output will list only directories with name jre Sample Output:
/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre
To find all files whose owner is calypso
find / -user calypso
To find files whose file size is 5 MB
find / -size 5M
To find files whose file size is in between 500 MB and 1GB
find / -size +500M -size -1G
To search and delete all empty files under /home calypso, type
find /home/calypso/ -type f -empty -delete
To find and remove all regular files under directory /backup, modified 10 days before
find /backup -type f -mtime +10 -exec rm {} \;
To find all txt files in /home/calypso/Documents/ directory and list them in ls -dils format, type
find /home/calypso/Documents/ -type f -name "*.txt" -ls
To search all .mp3 files in every folder of the computer except /home/calypso/songs and its sub-directories, type
find / -path /home/calypso/songs/ -prune -o -type f -name "*.mp3" -print
To find .avi and .mp4 files under /home/calypso,type
find /home/calypso -name '*.avi' -or -name '*.mp4'
or
find /home/calypso -name '*.avi' -o -name '*.mp4'
or
find /home/calypso -name '*.avi' -o -name '*.mp4'
To find all files starting with abc and not ending with .txt, type
find / -name 'abc*' -not -name '*.txt'
or
find / -name 'abc*' ! -name '*.txt'
or
find / -name 'abc*' ! -name '*.txt'
find is a very powerful command and can be used in a number of ways based on your requirement. For more details see the man pages
Comments
Post a Comment