How to find file in linux terminal

Find a file by name using command-line

If it is already installed, new filename might not be postgis-2.0.0 anymore. Usually after installations via package managers, executables would be in one of the $PATH folders, try which postgis to see the location. If it returns nothing, only then you should manually look for file location.

9 Answers 9

Try find ~/ -type f -name «postgis-2.0.0» instead.

Using . will only search the current directory. ~/ will search your entire home directory (likely where you downloaded it to). If you used wget as root, its possible it could be somewhere else so you could use / to search the whole filesystem.

I get find: /Users/UserName//Library/Saved Application State/com.bitrock.appinstaller.savedState: Permission denied error. it appears on every execution of the command. How to get rid of it?

sudo find / -type d -name "postgis-2.0.0" 

The . means search only in the current directory, it is best to search everything from root if you really don’t know. Also, type -f means search for files, not folders. Adding sudo allows it to search in all folders/subfolders.

Your syntax for locate is correct, but you may have to run

first. For whatever reason, I never have good luck with locate though.

locate uses database of files and directories made by updatedb . So if you have downloaded a new file there is more chance that your updatedb has not updated the database of files and directories. You can use sudo updatedb before using locate utility program. updatedb generally runs once a day by itself on linux systems.

The other answers are good, but I find omitting Permission denied statements gives me clearer answers (omits stderr s due to not running sudo ):

find / -type f -iname "*postgis-2.0.0*" 2>/dev/null 
  • / can be replaced with the directory you want to start your search from
  • f can be replaced with d if you’re searching for a directory instead of a file
  • -iname can be replaced with -name if you want the search to be case sensitive
  • the * s in the search term can be omitted if you don’t want the wildcards in the search
find / -type f 2>/dev/null | grep "postgis-2.0.0" 

This way returns results if the search-term matches anywhere in the complete file path, e.g. /home/postgis-2.0.0/docs/Readme.txt

There are -regex and -iregex switches for searching with Regular Expressions , which would find the path mentions as well. Suggestion to find any item which is a file ( -type f ) then grep is more resource expensive. Permission denied happens when user doesn’t have access to files or folders, using sudo before find will allow find to see all files.

Читайте также:  Лучший live linux дистрибутив

find is one of the most useful Linux/Unix tools.

Try find . -type d | grep DIRNAME

  • where you can change ‘.'(look into the Current Directory) to ‘/'(look into the entire system) or ‘~/'(look into the Home Directory).
  • where you can change «-name» to «-iname» if you want no case sensitive.
  • where you can change «file_name«(a file that can start and end with whatever it is) to the exactly name of the file.

This should simplify the locating of file:

This would give you the full path to the file

Tree lists the contents of directories in a tree-like format. the -f tells tree to give the full path to the file. since we have no idea of its location or parent location, good to search from the filesystem root / recursively downwards. We then send the output to grep to highlight our word, postgis-2.0.0

$ find . -type f | grep IMG_20171225_*
Gives
./03-05—2018/IMG_20171225_200513.jpg
The DOT after the command find is to state a starting point,
Hence — the current folder,
«piped» (=filtered) through the name filter IMG_20171225_*

While find command is simplest way to recursively traverse the directory tree, there are other ways and in particular the two scripting languages that come with Ubuntu by default already have the ability to do so.

bash

bash has a very nice globstar shell option, which allows for recursive traversal of the directory tree. All we need to do is test for whether item in the ./**/* expansion is a file and whether it contains the desired text:

bash-4.3$ for f in ./**/* ;do [ -f "$f" ] && [[ "$f" =~ "postgis-2.0.0" ]] && echo "$f"; done ./testdir/texts/postgis-2.0.0 

Perl

Perl has Find module, which allows to perform recursive traversal of directory tree, and via subroutine perform specific action on them. With a small script, you can traverse directory tree, push files that contain the desired string into array, and then print it like so:

#!/usr/bin/env perl use strict; use warnings; use File::Find; my @wanted_files; find( sub< -f $_ && $_ =~ $ARGV[0] && push @wanted_files,$File::Find::name >, "." ); foreach(@wanted_files)
$ ./find_file.pl "postgis-2.0.0" ./testdir/texts/postgis-2.0.0 

Python

Python is another scripting language that is used very widely in Ubuntu world. In particular, it has os.walk() module which allows us to perform the same action as above — traverse directory tree and obtain list of files that contain desired string.

Читайте также:  Оперативная система linux установка

As one-liner this can be done as so:

$ python -c 'import os;print([os.path.join(r,i) for r,s,f in os.walk(".") for i in f if "postgis-2.0.0" in i])' ['./testdir/texts/postgis-2.0.0'] 

Full script would look like so:

#!/usr/bin/env python import os; for r,s,f in os.walk("."): for i in f: if "postgis-2.0.0" in i: print(os.path.join(r,i)) 

Источник

How can I find a specific file from a Linux terminal?

I am trying to find where index.html is located on my linux server, and was wondering if there was a command to do that. Very new to linux and appreciate any help I can get.

Searching from / would take a few days to complete. And spit out a bunch of (non-critical) errors for certain types of files while searching. Not the most efficient.

Several years later, Googling «find file in linux» gives this as a top 10 result. I’m sure glad the question was asked and answered.

6 Answers 6

Find from root path find / -name «index.html»

Find from current path find . -name «index.html»

The below line of code would do it for you.

However, on most Linux servers, your files will be located in /var/www or in your user directory folder /home/(user) depending on how you have it set up. If you’re using a control panel, most likely it’ll be under your user folder.

Ah, liked stated below, the find program might take a while to complete. I’m not aware of any other option.

update db locate index.html 

Replace /var with your best guess as to the directory it is in but avoid starting from /

All other answers suggest using find (well, this answer also suggests using find but only after locate ), however, after several months of using find I realise locate is more convenient indeed! By the way, what is update db ? Do I need it?

@shintaroid locate does not actually search on your files system for a given file. What it does is it caches the information about the files into a database which is refreshed by a job on a given time interval. So what updatedb does is just to update the database manually.

Solution: Use unix command find

The find utility recursively descends the directory tree for each path listed, evaluating an expression (composed of the ‘primaries’ and ‘operands’) in terms of each file in the tree.

  • You can make the find action be more efficient and smart by controlling it with regular expressions queries, file types, size thresholds, depths dimensions in subtree, groups, ownership, timestamps , modification/creation date and more.
  • In addition you can use operators and combine find requests such as or/not/and etc.
Читайте также:  Setting mtu in linux

The Traditional Formula would be :

Easy Examples

1.Find by Name — Find all package.json from my current location subtree hierarchy.

2.Find by Name and Type — find all node_modules directories from ALL file system (starting from root hierarchy )

sudo find / -name "node_modules" -type d 

Complex Examples:

More Useful examples which can demonstrate the power of flag options and operators:

3.Regex and File Type — Find all javascript controllers variation names (using regex) javascript Files only in my app location.

find /user/dev/app -name "*contoller-*\.js" -type f 

-type f means file -name related to regular expression to any variation of controller string and dash with .js at the end

4.Depth — Find all routes patterns directories in app directory no more than 3 dimensions ( app/../../.. only and no more deeper)

find app -name "*route*" -type d -maxdepth 3 

-type d means directory -name related to regular expression to any variation of route string -maxdepth making the finder focusing on 3 subtree depth and no more /depth1/depth2/depth3 )

5.File Size , Ownership and OR Operator — Find all files with names ‘sample’ or ‘test’ under ownership of root user that greater than 1 Mega and less than 5 Mega.

find . \( -name "test" -or -name "sample" \) -user root -size +1M -size -5M 

-size threshold representing the range between more than (+) and less than (-) -user representing the file owner -or operator filters query for both regex matches

6.Empty Files — find all empty directories in file system

7.Time Access, Modification and creation of files — find all files that were created/modified/access in directory in 10 days

# creation (c) find /test -name "*.groovy" -ctime -10d # modification (m) find /test -name "*.java" -mtime -10d # access (a) find /test -name "*.js" -atime -10d 

8.Modification Size Filter — find all files that were modified exactly between a week ago to 3 weeks ago and less than 500kb and present their sizes as a list

find /test -name "*.java" -mtime -3w -mtime +1w -size -500k | xargs du -h 

Источник

Оцените статью
Adblock
detector