LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 02-05-2016, 08:15 PM   #1
offgridguy
Senior Member
 
Registered: Nov 2015
Location: Alberta Canada
Distribution: Windows10, Debian
Posts: 1,109

Rep: Reputation: 147Reputation: 147
Question Sort files from command line


In my ongoing efforts to learn the command line, I have found the
'sort' command to sort files, in this case document text files. In the books I have, I find the option to sort by size, ie; smallest to largest or vice versa, but I can't see an option to sort by date created. By default files seem to sort alphabetically or by date modified. I would prefer to sort them by date created and then rename them numerically to keep them in that order. I only have a few files at present, so it's not an issue to rename them individually, once I have them sorted into the proper order. I realize I can do this from the GUI but I want to learn how to do it from the command line. All help appreciated.
 
Old 02-05-2016, 08:31 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,148

Rep: Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124
No-one can help without knowing the format of your data - post a few lines, then what you expect to produce as output.

In the interim, look at "man sort". Manpages are always the best place to start.
 
Old 02-06-2016, 08:02 AM   #3
offgridguy
Senior Member
 
Registered: Nov 2015
Location: Alberta Canada
Distribution: Windows10, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 147Reputation: 147
Thank you for the reply.

Currently my files are text document, titled as,example; FileName.odt. I would like to sort them in order of date created and rename them with a common prefix, numerically as this example.
Cr(1)FileName.odt
Cr(2)FileName.odt
Cr(3)Filename.odt
Cr(4)FileName.odt

Naturally the (FileName) for each individual file would be unique to reflect the contents of the file. However having a common prefix as well as extension gives me a convenient way to move or copy them using wildcards.
Plus numbering them numerically will keep them sorted by folder, in the order I like them.

Renaming is not the issue, once they are sorted by date, the rename is relatively easy.
 
Old 02-06-2016, 09:06 AM   #4
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Hi!

You don't really need to use sort for this, you can use ls (from the man page):
Code:
      -t     sort by modification time, newest first
So, something like this works:
Code:
#!/bin/bash

i=1

for file in $(ls -t .); do
    if echo $file | grep -q '^FileName'; then
        echo "CR($i)"$file
        ((i++))
    fi  
done

exit 0
In this directory:
Code:
ls
FileNamea.odt  FileNamew.odt  sortRename.sh
FileName.odt   FileNamex.odt
I get this result:
Code:
./sortRename.sh 
CR(1)FileNamew.odt
CR(2)FileNamea.odt
CR(3)FileNamex.odt
CR(4)FileName.odt
Best regards,
HMW
 
1 members found this post helpful.
Old 02-06-2016, 09:44 AM   #5
offgridguy
Senior Member
 
Registered: Nov 2015
Location: Alberta Canada
Distribution: Windows10, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 147Reputation: 147
@ HMW,
Thank you very much. I appreciate the help.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: How to Resize, Rename, Sort and Proof Photos from the Command Line LXer Syndicated Linux News 0 02-05-2014 07:40 AM
[SOLVED] bash: sort lines in 2 files so that equal lines are at the same line number... masavini Programming 10 06-21-2012 01:58 PM
will it be poosile to sort the output of ls command based on timestamp of files. shad.ithbti Linux - Newbie 1 09-15-2009 01:51 AM
Is there a line limit with the sort utility? Trying to sort 130 million lines of text gruffy Linux - General 4 08-10-2006 08:40 PM
Command line to sort files satimis Programming 6 06-28-2005 02:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:52 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