LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-15-2006, 11:56 AM   #1
thedarkdestroyer
LQ Newbie
 
Registered: Mar 2006
Posts: 7

Rep: Reputation: 0
How to get a files pathname into anoth script


I have this script that searches files checksums and then outputs them to another script, so basically the other script returns something like this:
#! /bin/sh
#rm 900150983cd24fb0d6963f7d28e17f72\ \ ./hendy.txt
#rm 900150983cd24fb0d6963f7d28e17f72\ \ ./hendy1.txt

i need to be able to include the pathname for hendy.txt and hendy.txt , is this possible???

so that the contents of the script looks like this:

#! /bin/sh
#rm /ramdisk/home/knoppix/tmp/hendy.txt
#rm /ramdisk/home/knoppix/tmp/hendy1.txt

The orignal script looks like this:
#############################
OUTF=henderson.sh;
echo "#! /bin/sh" > $OUTF;
find "$@" -type f -print0 |
xargs -0 -n1 md5sum |
sort --key=1,32 | uniq -w 32 -d --all-repeated=separate |
sed -r 's/^[0-9a-f]*( )*//;s/([^a-zA-Z0-9./_-])/\\\1/g;s/(.+)/#rm \1/' >> $OUTF;
chmod a+x $OUTF; ls -l $OUTF
#############################

Thanks
 
Old 03-16-2006, 04:59 AM   #2
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 37
what you want is to return the absolute path of files.
This script works: ./return-absolute-filename <somefile>
Quote:
Code:
  #!/bin/bash
  if [ -d "$1" ] ; then   # Only a directory name.
    dir="$1"
    unset file
  elif [ -f "$1" ] ; then # Strip off and save the filename.
    dir=$(dirname "$1")
    file="/"$(basename "$1")
  else
    # The file did not exist.
    # Return null string as error.
    echo
    return 1
  fi
 
  # Change to the directory and display the absolute pathname.
  cd "$dir" > /dev/null
  echo ${PWD}${file}
Here is my attempt at a one-liner to return the absolute path for files and dirs based on the script:
It does not like spaces in the filenames or directorynames ...
Code:
for i in `find -name "*"`;do dir=$PWD;cd $(dirname "$i");echo "$PWD/$(basename $i)";cd $dir; done

Last edited by muha; 03-16-2006 at 05:44 AM.
 
Old 03-16-2006, 10:33 AM   #3
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
find returns the absolute paths if you supply it an absolute directory to start with. It uses relative paths only if you give it one.
Examples:
Code:
find ./ -type f #returns all files from the current directory down. Files in the output will all have a path
                #starting with "./" (hence a relative path).
find /etc -type f #returns all files below /etc. All outputted files will have paths starting with /etc/
                  #(absolute paths) because you supplied it the absolute path (/etc) to start with.
So, why bother recreating the absolute path for files, if find can supply it to you directly?

Second point: I think the provided script can be simplified too. For instance, it's not very useful to "write" a script based on the output of find (processed via some programs like sed and stuff).
Instead, make your script output in a format you know. Make a second script that calls the first one, captures it's output (via $(script) or `script` syntax, as muha shows with the find command) and treat each line of output separately. Seems much easier than what you're trying to do.

Example:
Code:
script1.sh:

find "$@" -type f -print0 |
xargs -0 -n1 md5sum |
sort --key=1,32 | uniq -w 32 -d --all-repeated=separate |
sed -r 's/^[0-9a-f]*( )*//;s/([^a-zA-Z0-9./_-])/\\\1/g' 

script2.sh:
files=`/path/to/script1.sh`
for i in ${files}; do
   echo "#rm ${i}";
   #you can now do what you want with the files...
done
Replace the "echo #rm ${i}" with "rm ${i}" if you want to actually remove the files.

But all in all, I have to say it's a nice way of eliminating duplicate files...
 
Old 03-21-2006, 08:29 AM   #4
thedarkdestroyer
LQ Newbie
 
Registered: Mar 2006
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks for that guys, im still playing about with the scrpit, your input is greatly appreciated.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Inode to pathname conversion.. vishalbutte Programming 1 03-13-2006 01:35 AM
pathname in ps command kaplan71 Linux - General 1 10-31-2005 01:57 PM
Determine absolute pathname aecaudel Programming 5 09-19-2005 02:06 AM
Replacing All Instances of a pathname with another in Vi Editor kaplan71 Linux - General 1 06-30-2005 04:54 PM
Scripting help -- pathname expansion player_2 Programming 3 08-25-2003 06:03 PM


All times are GMT -5. The time now is 10:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration