15

How to get absolute path by using find command.
actually I am running the following script:-

find . -size +20M | while read a
do
     i=$(echo $a | sed 's:/: :g')
     echo $a;
     j=($i)
     fileName=${j[${#j[@]}-1]}
     userName=${j[${#j[@]}-3]}
done

but here variable $a showing path starting from ./Downloads/filename but I want absolute path from /.

3 Answers 3

21

Use find with an absolute path.

find /path/ -size +20M

It will print the whole path.
If you do not know the working directory then use command substitution for pwd like this:

find "`pwd`" -size +20M 
   #or like this:
find "$(pwd)" -size +20M

To get your working directory

Anyway, it seems that Bash man now advise to use $() over `` so you should use the second form. You can also probably refer directly to the $PWD variable that contains the working directory of your script and it would be probably faster if you have to use in a loop.

find "$PWD" -size +20M
3
  • But i donot have path, I only have . as current directory then? Commented Apr 21, 2014 at 9:38
  • 2
    Don't start with ., use $(pwd) instead, that will give you a full path.
    – bsd
    Commented Apr 21, 2014 at 9:40
  • 1
    The command substition $(pwd) won't work if the path contains a newline. Instead, you can just use the paramater expansion $PWD
    – Pound Hash
    Commented Aug 26, 2023 at 20:22
10

You can use the pwd command or print out the file realpath:

$ find "$(pwd)" -size +20M
$ find . -size +20M -exec realpath {} +

Both commands real give you the absolute path to the files.

9
  • But 2nd one is giving find: `realpath': No such file or directory Commented Apr 21, 2014 at 9:57
  • Hum. Maybe you don't have it installed although it is provided by the coreutils package. Try to locate it with which realpath.
    – Spack
    Commented Apr 21, 2014 at 10:02
  • It is locating nothing.. Commented Apr 21, 2014 at 10:03
  • Which system are you using?
    – Spack
    Commented Apr 21, 2014 at 10:18
  • Linux BANE 3.7-trunk-amd64 #1 SMP Debian 3.7.2-0+kali8 x86_64 GNU/Linux Commented Apr 21, 2014 at 10:22
0

To List all PNG and JPEG files which are 7 days old with absolute path.

$ find $PWD/ -mtime -7 -print -exec grep -e ".png\|.jpg" {} \;

Here $PWD will be added to every file matched.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .