Tech 2 Game - Ultimate Tech and Game Forum
Welcome to Tech 2 Game - Ultimate Tech and Game Forum Guest!    Register | Lost Password?




Tags: Finding, Files, Unix,
Post Reply 
 
Thread Rating:
  • 1 Votes - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Share Post: Digg Delicious Reddit Face Book Stumbleupon
Finding Files in Unix
11-14-2009, 07:08 PM
Post: #1
Finding Files in Unix :
There are at least three ways to find files when you don't know their exact location:

* find

If you have a rough idea of the directory tree the file might be in (or even if you don't and you're prepared to wait a while) you can use find:

$ find directory -name targetfile -print

find will look for a file called targetfile in any part of the directory tree rooted at directory. targetfile can include wildcard characters. For example:

$ find /home -name "*.txt" -print 2>/dev/null

will search all user directories for any file ending in ".txt" and output any matching files (with a full absolute or relative path). Here the quotes (") are necessary to avoid filename expansion, while the 2>/dev/null suppresses error messages (arising from errors such as not being able to read the contents of directories for which the user does not have the right permissions).

find can in fact do a lot more than just find files by name. It can find files by type (e.g. -type f for files, -type d for directories), by permissions (e.g. -perm o=r for all files and directories that can be read by others), by size (-size) etc. You can also execute commands on the files you find. For example,

$ find . -name "*.txt" -exec wc -l '{}' ';'

counts the number of lines in every text file in and below the current directory. The '{}' is replaced by the name of each file found and the ';' ends the -exec clause.

For more information about find and its abilities, use man find and/or info find.

* which (sometimes also called whence) command

If you can execute an application program or system utility by typing its name at the shell prompt, you can use which to find out where it is stored on disk. For example:

$ which ls
/bin/ls

* locate string

find can take a long time to execute if you are searching a large filespace (e.g. searching from / downwards). The locate command provides a much faster way of locating all files whose names match a particular search string. For example:

$ locate ".txt"

will find all filenames in the filesystem that contain ".txt" anywhere in their full paths.

One disadvantage of locate is it stores all filenames on the system in an index that is usually updated only once a day. This means locate will not find files that have been created very recently. It may also report filenames as being present even though the file has just been deleted. Unlike find, locate cannot track down files on the basis of their permissions, size and so on.
Find all posts by this user
Sponsored by
Post Reply 


Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  For each of ur directories how to Check the disk usage in unix rin2 0 113 12-11-2009 08:20 PM
Last Post: rin2
  How to see the Disk usagein unix rin2 0 61 12-11-2009 08:11 PM
Last Post: rin2
  What is the "Top" command in unix rin2 0 79 12-11-2009 07:53 PM
Last Post: rin2
  Finding Text in Files in Unix rin2 0 129 11-14-2009 07:03 PM
Last Post: rin2
  Handling Removable Media in Unix rin2 0 128 11-14-2009 06:56 PM
Last Post: rin2
  Unix File Compression and Backup rin2 1 97 11-10-2009 08:01 PM
Last Post: Sukanjan.K
  Find UNIX Directory Structure rin2 0 269 11-08-2009 07:11 PM
Last Post: rin2


[-]
Permissions Box
You cannot Post Threads.
You cannot Post Replies.
You cannot Post Attachments.
You cannot Edit Your Posts.
HTML is off
MyCode is on
Smilies is on
[img]-code is on