LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 01-28-2009, 10:13 PM   #1
starmonche
Member
 
Registered: Jan 2007
Location: Overland Park
Distribution: Centos6
Posts: 60

Rep: Reputation: 15
Links to newest files


This one's a 2-parter:

First:

I have a folder of video clips (and corresponding images for aesthetic clickability) on a network share. I'd like to create a new folder that lists the videos from the last 90 days. I can get a list of the files that I want with this:

find . -name "*" -mtime -90 | egrep -v ".db|.jpg"

which then lists the files that I want (minus images and the ehthumbs.db cache file MCE generates) but how in the world do I pipe that through the ln command AND restore the escape characters (or surround each line in quotes)? I'm also noticing that the first "entry" on my generated list is a single period which I would like to eliminate.

Second:

I'd like to have my new folder link to each video's image as well. What command can I use to trim the rightmost 3 characters off of each line and replace them with "jpg"? Each video has a corresponding image but their time stamps may be drastically different.

Thanks to everyone who helps and you're welcome to anyone who finds this useful!
 
Old 01-29-2009, 10:26 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
find can filter for multiple conditions like file name - you combine logical statements with -a for "and" and -o for "or". You can also use ! for not, but name sure you quote it or escape it because the shell treats this as a special character.

Using grep as you did, you might exclude some files which you actually want, although your file naming convention might prevent that. e.g. the file name "me.jpg.movie.mp4". You could always use a $ to anchor the pattern to the end of the file name, but you can also do it in the find, like this (one less process = efficient happiness):
Code:
find . \! -name '*.jpg' -a \! -name '*.db' -a -mtime -90
...which means, "list files with the name does not match the glob pattern '*.jpg' and the glob pattern does not match '*.db' and the modification time is less than 90 days.

You can then pipe this into a little shell loop and do what you want with it. You might have file names with spaces in, so don't forget to quote your variables.
Code:
find . \! -name '*.jpg' -a \! -name '*.db' -a -mtime -90 | while read f; do
   echo "processing file: $f"
   ln -s "$f" "..."    # however you want the link command to be. 
done

Last edited by matthewg42; 01-29-2009 at 10:29 AM.
 
Old 01-29-2009, 10:35 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
Missed the second part. The shell has some simple modification expansions when evaluating a variable value. Handily, this includes removing prefixes and suffixes, including glob patterns.

e.g.
Code:
for f in test.jpg test.mpg test.mp4; do
    echo "filename is: $f"
    echo "filename without jpg suffix: ${f%jpg}"
    echo "filename without mpg suffix: ${f%mpg}"
    echo "filename without last 3 characters: ${f%???}"
done
Check out the "Parameter Expansion" section of the bash manual page for other substitutions.

Many people will pipe the value of a variable through sed or awk or something else to accomplish the same thing. This will work, but it will create a new process each time the external program is invoked and that is much heavier that doing it all within the shell (try it for some thousands of substitutions and you'll see the difference is significant).
 
Old 02-06-2009, 08:11 PM   #4
starmonche
Member
 
Registered: Jan 2007
Location: Overland Park
Distribution: Centos6
Posts: 60

Original Poster
Rep: Reputation: 15
Very cool, man. I put both of your posts together with the parts I needed and came up with this:

rm /linkshare/multimedia/recent/*
find /mnt/sdc/movies/* \! -name '*.jpg' -a \! -name '*.db' -a -mtime -$1 | while read f; do
ln -s "$f" "/linkshare/multimedia/recent/."
ln -s "${f%???}jpg" "/linkshare/multimedia/recent/."
done

It does exactly what I wanted. My next step will be to have the first line remove everything except the "ehthumbs.db" and "Folder.jpg" files that I'll keep in there (so the MCE interface stays 'pretty'). But that should be as easy as another modified "find" command piped into rm. Thanks again, Matt(hew).

Anyone else who cares: I named this script as "linktorecent.sh" and when I run "./linktorecent.sh 90" it creates links in /linkshare/multimedia/recent that point to videos dated in the last 90 days (and the jpegs of their covers) in /mnt/sdc/movies.

Last edited by starmonche; 02-07-2009 at 11:06 AM. Reason: misspoke
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to filter files from https links spide21 Linux - Newbie 1 10-18-2008 03:29 PM
find newest files in a directory macushk Linux - Newbie 3 05-05-2008 04:16 AM
Automated FTP script to grab ONLY newest dated files kaiserbeto Linux - Newbie 4 10-20-2006 02:46 AM
How to find newest files rookworm Linux - Newbie 4 10-28-2005 07:04 PM
download newest files on ftp exodist Linux - Networking 0 03-06-2004 03:14 PM


All times are GMT -5. The time now is 06:53 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration