LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-12-2008, 03:23 PM   #1
Ryzol
Member
 
Registered: Jun 2007
Distribution: Ubuntu, Slackware
Posts: 50

Rep: Reputation: 15
shell script help: operating on files returned by find


My goal is to write a script that finds all the files with a certain extension and then make symbolic links to those files. The way I've tried to go about this in a prototype is by:
Code:
files=$(find $HOME -type f -name "*.test")
for file in $files
do
    echo $file
    ln -s $file /tmp/test/$file
done
There's a couple problems with this method though.

1. Let's say $file's path is "/home/user/dir/f.test" . This gets expanded into [code]ln -s /home/user/dir/f.test /tmp/test/user/dir/f.test giving a directory not found error. That also isn't what I want it to do. I want it do ln -s /home/usr/dir/f.test /tmp/test/f.test . I've tried using cut to get the field I want, but as far as I know there isn't a way to get the last field unless you know how many there are.

2. If I give find the $HOME directory then find will search the Trash which is something I don't want it to do. I've kludged around this problem by chmoding the Trash folder to 000.


So I'm looking for some way to exclude specific sub-directories from find, a way to parse the absolute filepaths it provides, or a different, better solution.

Thanks for any tips!
 
Old 06-12-2008, 04:59 PM   #2
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
Quote:
Originally Posted by Ryzol View Post
as far as I know there isn't a way to get the last field unless you know how many there are.
Try using the basename command to give you just the file name from the complete path.
Code:
basename $file
Quote:
Originally Posted by Ryzol View Post
I'm looking for some way to exclude specific sub-directories from find, a way to parse the absolute filepaths it provides, or a different, better solution.
I don't know of anything you can pass to the find command to do this. I would probably set up a conditional in the for loop and only create links to those files NOT in .Trash.
 
Old 06-12-2008, 06:04 PM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Another way to get the file name from a path without having to invoke basename is with the shell parameter expansion operators:
Code:
path=/path/to/some/file
echo ${path##*/}
 
Old 06-12-2008, 08:42 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
2. If I give find the $HOME directory then find will search the Trash which is something I don't want it to do. I've kludged around this problem by chmoding the Trash folder to 000.

So I'm looking for some way to exclude specific sub-directories from find, a way to parse the absolute filepaths it provides, or a different, better solution.

Thanks for any tips!
Code:
~/tmp$ find . -name file
./findme/file
./dontfind/file
~/tmp$ find . -name dontfind -prune , -name file 
./findme/file
 
Old 06-12-2008, 09:38 PM   #5
grizly
Member
 
Registered: Nov 2006
Location: Melbourne Australia
Distribution: Centos, RHEL, Debian, Ubuntu, Mint
Posts: 128

Rep: Reputation: 16
Quote:
find $HOME -type f -name "*.test" -not \( -name "excluded.ext" -o -name "other.excluded" \) -exec ln -s {} $1 \;
Save and chmod +x, then execute like so:

Quote:
./script /path/destination
Will make symbolic link for each file ending in .test in home directories in specified destination.

Quote:
[user@machine test]$ pwd
/tmp/test
[user@machine test]$ cat script
find $HOME -type f -name "*.test" -not \( -name "excluded.ext" -o -name "other.excluded" \) -exec ln -s {} $1 \;
[user@machine test]$ chmod +x script
[user@machine test]$ ls
script
[user@machine test]$ ls /home/user/test
1.test 2.test file.test file.text test.test
[user@machine test]$ ./script /tmp/test
[user@machine test]$ ls
1.test 2.test file.test script test.test
Works for me.. although those filenames are simple.. the -not \( -name "excluded.ext" -o -name "other.excluded" \) bit, enables you to block things you don't want find to send to ln.. although, you could pipe it through grep for more granularity.. and I think xargs is more powerful in that it buffers the command line inputs, if you do this and there are too many files, it may overflow the command line producing an error..

EDIT: That script should all be one line.. and I don't think it excludes your .trash..

find $HOME !$HOME/.trash [...] might work..

Last edited by grizly; 06-12-2008 at 09:52 PM. Reason: EDIT:
 
  


Reply



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
Small shell script to get trackpad operating at startup trentfox Linux - Newbie 2 06-12-2008 11:15 PM
Shell script: not able to find jags.singh Programming 8 06-15-2007 10:12 AM
how to find a pattern using shell script sharad Linux - General 1 05-23-2006 03:50 AM
find shell script help liren Linux - Newbie 3 05-02-2005 03:05 PM
how to find the pid of a perl script from shell script toovato Linux - General 1 12-19-2003 06:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:23 AM.

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
Open Source Consulting | Domain Registration