Finding file path in linux

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.

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.

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)) 

Источник

Get Absolute File Path in Linux

Here are different ways to get the absolute file paths in Linux command line.

You can get the full path of a directory with the pwd command:

But how do you get the absolute path of a file in Linux?

There are several ways to print the full path of files:

Get file path in Linux

  • readlink
  • realpath
  • find
  • combining ls and pwd

Let me show you these commands one by one. But before that, I suggest brushing up on the basics of the absolute and relative path concept first.

The purpose of the readlink command is to resolve symbolic links. You can use it to display the full path of a file like this:

[email protected]:~$ readlink -f sample.txt /home/abhishek/sample.txt [email protected]:

Use realpath to get full file path

The realpath command is used for resolving the absolute file names. Among other uses, it can show the full path to a file.

Читайте также:  Удаление модуля ядра linux

Take a look at this example:

[email protected]:~$ realpath sample.txt /home/abhishek/sample.txt [email protected]:~$

If you use it with a symbolic link, it will show the real path of the original file. You can force it to not follow the symbolic link:

Here’s an example where it showed the full path to the source file by default and then I forced it to show the symbolic link, not its original file.

[email protected]:~$ realpath linking-park /home/abhishek/Documents/ubuntu-commands.md [email protected]:~$ realpath -s linking-park 

Use the find command to get the absolute file path

Here’s the thing with the find command. Everything is relative to the directory you give it for search location.

If you give it . it will show the relative path. If you give it the absolute path of the directory, you’ll get the absolute path of the files you are searching for.

Use the command substitution with the find command like this:

You can run it to find the full path of a single file:

[email protected]:~$ find $(pwd) -name sample.txt /home/abhishek/sample.txt

Or, you can use it with a bunch of files matching a certain pattern:

[email protected]:~/Documents/eBooks$ find $(pwd) -name "*.pdf" /home/abhishek/Documents/eBooks/think-like-a-programmer.pdf /home/abhishek/Documents/eBooks/linux-guide.pdf /home/abhishek/Documents/eBooks/absolute-open-bsd.pdf /home/abhishek/Documents/eBooks/theory-of-fun-for-game-design.pdf /home/abhishek/Documents/eBooks/Ubuntu 1804 english.pdf /home/abhishek/Documents/eBooks/computer_science_distilled_v1.4.pdf /home/abhishek/Documents/eBooks/the-art-of-debugging-with-gdb-and-eclipse.pdf

Now this one is a bit tricky and messy.

You can use the environment variable PWD with ls command like this to display the files and directories with their absolute path:

You get an output like this:

[email protected]:~/test$ ls -ld $PWD/* -r--rw-r-- 1 abhishek abhishek 0 Jul 27 16:57 /home/abhishek/test/file2.txt drwxrwxr-x 2 abhishek abhishek 4096 Aug 22 16:58 /home/abhishek/test/new 

However, to print the full path of a file with the ls command, you’ll have to use it like this:

Not the cleanest solution but it works.

[email protected]:~$ ls -l $PWD/sample.txt -rw-r--r-- 1 abhishek abhishek 12813 Sep 7 11:50 /home/abhishek/sample.txt [email protected]:~$ 

Conclusion

I showed four different ways to get the full file path in Linux. The find and ls commands are common while realpath and readlink are hardly known to many Linux users. It’s always good to learn new things, isn’t it?

Источник

Get absolute path of files using ‘find’ command

You can use bash ‘s Tilde Expansion to get the absolute path of the current working directory, this way find prints the absolute path for the results as well:

find ~+ -type f -name "filename" 

If executed in ~/Desktop , this is expanded to

find /home/yourusername/Desktop -type f -name "filename" 
/home/yourusername/Desktop/filename 

If you want to use this approach with the current working directory’s parent directory you need to cd before calling find :

cd .. && find ~+ -type f -name "filename" 
find "$(cd ..; pwd)" -name "filename" 

Thanks, I was just testing $(cd ..; pwd) . Here it works OK, but if I do it alone in a Terminal, I can’t get the parent dir. I get «Bash:

: Is a directory. And if I do $(cd ..; echo «something») I get «something: not a command»

Yes ( . ) means execute in a subshell, the output gets written to stdout. $( . ) stands for «command substitution». The latter can be used as if it were a variable expansion.

Читайте также:  Linux set time terminal

@Scrutinizer — I think you misunderstand how find works. If you use the full path in your search, you get the full path in your output. find /home/your_user -name foo . As OP is using «../» , hard to guess the full path to give in an answer.

Try using the -exec option of find :

find .. -name "filename" -exec readlink -f <> \; 

Note: readlink prints the value of a symbolic link or canonical file name.

I like the simplicity of this solution but I found xargs to be much faster. find .. -name «filename» | xargs readlink -f

This worked for me, but will only return the first occurrence.

realpath $(find . -type f -name filename -print -quit) 

To get full paths for all occurrences (as suggested by Sergiy Kolodyazhnyy)

find . -type f -name filename -print0 | xargs -0 realpath 

Exactly what I needed (just the first occurrence and to print the full path including the filename in the output. Thank you Sir

Try with -printf . This also works with files with blank spaces.

find .. -name «filename» -printf $PWD/»%f\n»

Unless I totally misunderstand, it is as simple as this:

find $(realpath .) -name 'river.jpg' 

By specifying the full real path as a start, find will implicitly output this full path as a search result.

The bash command realpath converts the current (or any other directory as ./images) into its real path). the $(realpath .) converts the output to a variable, as if it was typed manually, e.g. /home/myusername

Источник

How can I find a file/directory that could be anywhere on linux command line? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.

find [file or directory name] 

to report the paths with matching filenames/directories. Unfortunately this seems to only check the current directory, not the entire folder. I’ve also tried locate and which, but none find the file, even though I know its on the computer somewhere.

Not sure what the issue is, as find -name «filename» finds files recursively in the current working directory.

Sorry if it’s not clear, the file may not be in the current working directory. It could be anywhere on the computer

5 Answers 5

«Unfortunately this seems to only check the current directory, not the entire folder». Presumably you mean it doesn’t look in subdirectories. To fix this, use find -name «filename»

If the file in question is not in the current working directory, you can search your entire machine via

This also works with stuff like find / -name «*.pdf» , etc. Sometimes I like to pipe that into a grep statement as well (since, on my machine at least, it highlights the results), so I end up with something like

find / -name "*star*wars*" | grep star 

Doing this or a similar method just helps me instantly find the filename and recognize if it is in fact the file I am looking for.

Источник

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