Active1 year ago
I would like to determine the location of a file using command-line. I have tried:
and
to no avail. What is the command to determine the file's directory, provided its name?
This guide shows how to use the Linux FILE command to find out the file type of any given file or list of files. This guide shows how to use the Linux FILE command to find out the file type of any given file or list of files. Lifewire How to Determine the File Type of a File Using Linux. Search the site GO. For example, as a system admin, you have done some configuration on your Linux system, but forgot where it was saved. You want to verify whether/how your Linux file system has been tampered with by someone recently. If you would like to find recently updated files on Linux, you can use find command as follows.
Sam007Sam0071,51899 gold badges2020 silver badges2727 bronze badges
8 Answers
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.Goodluck
adempewolffadempewolff9,26022 gold badges2424 silver badges4848 bronze badges
I would try:
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.Community♦
reverendj1reverendj113k22 gold badges3838 silver badges3838 bronze badges
find is one of the most useful Linux/Unix tools.
Try
Mitch♦Mitchfind . -type d | grep DIRNAME
88.9k1515 gold badges181181 silver badges239239 bronze badges
The other answers are good, but I find omitting
Permission denied
statements gives me clearer answers (omits stderr
s due to not running sudo
):where:
/
can be replaced with the directory you want to start your search fromf
can be replaced withd
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
An alternative is:
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
zanbrizanbri
Try
find . -name '*file_name*'
- 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.
RuiVBoasRuiVBoas
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
ptetteh227ptetteh227
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: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:
And how it works:
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.As one-liner this can be done as so:
Full script would look like so:
Sergiy KolodyazhnyySergiy Kolodyazhnyy79.4k1111 gold badges171171 silver badges354354 bronze badges
$ 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_*
JadeyeJadeye