11

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 /.

16

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
  • But i donot have path, I only have . as current directory then? – Shivam Sharma Apr 21 '14 at 9:38
  • 2
    Don't start with ., use $(pwd) instead, that will give you a full path. – bsd Apr 21 '14 at 9:40
8

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.

  • But 2nd one is giving find: `realpath': No such file or directory – Shivam Sharma Apr 21 '14 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 Apr 21 '14 at 10:02
  • It is locating nothing.. – Shivam Sharma Apr 21 '14 at 10:03
  • Which system are you using? – Spack Apr 21 '14 at 10:18
  • Linux BANE 3.7-trunk-amd64 #1 SMP Debian 3.7.2-0+kali8 x86_64 GNU/Linux – Shivam Sharma Apr 21 '14 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.