LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 10-03-2012, 06:03 AM   #16
Khandi
LQ Newbie
 
Registered: Sep 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled

In that case the date in the name of the file should proof to be the most efficient way of getting the appropriate files. Although as i have mentioned. The locations where the files are at, are not handled from that location by anyone. It is just storage. So it should not differ that much. But if we were to pick one, i'd rather go for filename.
 
Old 10-04-2012, 12:05 PM   #17
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
As mentioned, it's not advisable to parse ls. There are other ways to get the most recent file:

How can I get the newest (or oldest) file from a directory?
http://mywiki.wooledge.org/BashFAQ/099

But if you just want to move the last entry of each grouping, as shown by the natural sort order, I think we can make it a bit simpler.

Code:
target='/path/to/newfolder'

#loop through all xlsx files, in alphanumeric order
for i in *.xlsx; do

	#sets $last to $i for the first entry, otherwise keeps the original value
	last=${last:-$i}

	#compare the prefix of the last file with the prefix of the current file
	if [[ ! ${last%%_*} == ${i%%_*} ]]; then

		#if the two files have different prefixes, move the $last file
		mv "$last" "$target"

		# save the current file to last
		last=$i
done

#finish by moving the final leftover file, which should be the most recent.
mv "$last" "$target"
See parameter substitution for an explanation of the "${var}" patterns used.


Edit: Here's a much more streamlined solution, using a bash associative array:

Code:
declare -A files
target='/path/to/newfolder'

for i in *.xlsx; do
	files["${i%%_*}"]=$i
done

mv -t "$target" "${files[@]}"
The array is indexed by the file prefix. The loop sets the value of each index entry to each file in turn, so in the end only the final, newest one is stored in the array. Then it just takes a single mv command to shift them all at once.

Last edited by David the H.; 10-04-2012 at 12:20 PM. Reason: as posted
 
1 members found this post helpful.
  


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
Removing filename~ files from ls just like .filename files are hidden nkoplm Linux - General 3 10-04-2011 10:31 AM
copy all the files in the directory based on the modification date SriniKlr Programming 4 01-26-2011 10:08 AM
[SOLVED] Scheduled copy based on filename AndySocial Programming 10 06-18-2010 01:16 PM
need a script that will copy based on filename and then move to a network share when mrgreaper Linux - Newbie 6 09-27-2009 08:21 PM
how to remove long-windows-filename files based on exlusion list adamrosspayne Linux - Newbie 3 06-23-2006 02:25 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:23 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
Open Source Consulting | Domain Registration