Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 10-03-2012, 07:03 AM   #16
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, 01:05 PM   #17
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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?

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.


#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

	#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

#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:

declare -A files

for i in *.xlsx; do

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 01:20 PM. Reason: as posted
1 members found this post helpful.


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
Removing filename~ files from ls just like .filename files are hidden nkoplm Linux - General 3 10-04-2011 11:31 AM
copy all the files in the directory based on the modification date SriniKlr Programming 4 01-26-2011 11:08 AM
[SOLVED] Scheduled copy based on filename AndySocial Programming 10 06-18-2010 02: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 09:21 PM
how to remove long-windows-filename files based on exlusion list adamrosspayne Linux - Newbie 3 06-23-2006 03:25 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:17 PM.

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