7

I would like to sort all files by date with a Shell script.

For example, in /Users/KanZ/Desktop/Project/Test/ there are the files M1.h, A2.h and F4.h.

Each file has a different time. How do I sort all these files from oldest to current by date and time?

Currently I have a rename script:

cd /Users/KanZ/Desktop/Project/Test/ 
n=1
for file in *.jpg;
do 
  echo $file prefix=M file_name=M$n.jpg 
  echo $file_name n=$(( $n+1 ))
  mv $file $file_name 
done 

The first time I run script the JPG files will be M1.jpg, M2.jpg and M3.jpg but if I add a new file named A1.jpg to this directory and run the script again, M1.jpg, M2.jpg and M3.jpg will be replaced by M4.jpg (before running the script, this file was named A1.jpg) because the first letter is A and came before M.

I would like to get M1, M2, M3 and M4.jpg.

1
  • The question changed mid-stream. It started out sorting *.h files but the commentary and code reflect sorting *.jpg files. Consistency is important when asking questions. Commented May 22, 2017 at 20:27

4 Answers 4

10

The ls command can easily sort by last modified time:

$ ls -1t /Users/KanZ/Desktop/Project/Test

To reverse the sort, include the -r option:

$ ls -1tr /Users/KanZ/Desktop/Project/Test

Including the 1 tells ls to output one file per line without extra metadata (like the length, modification time, etc), which is often what you need in a shell script if you need to send the list to other commands for further processing.

Sign up to request clarification or add additional context in comments.

2 Comments

This sorts from new to old - this is not what was ask.
Then use the -r flag as appropriate to change the order.
3

(Completely new because the question changed)

Try this:

cd /Users/KanZ/Desktop/Project/Test
n=1
for f in `ls -tr *.jpg`; do
  mv $f M$n.jpg
  n=$(( n + 1 ))
done

6 Comments

Nothing is happend. Don't u should specify the type of files? e.g. *.h *.jpg
What did you try? What is the result? (There is no need to specify the suffix of the file: if not given, all files are shown.)
I would like to sort the jpg files by time&date first, after that I want to rename them like A1.jpg A2.jpg A3.jpg. Now I can rename the all files but I want to sort them from old to current before rename them.
This has nothing to do with your question: please reformulate / update you initial question.
@kantawit, you need to edit your question to add that information.
|
0

You need to pass the -t option to the ls command. The -t option sorts by time modified, i.e. the most recently modified first before sorting the operands by lexicographical order. In other words, last downloaded file can be displayed using the following command.

Open the Terminal and type the following command.

ls -t

Comments

0

In case if you need to sort file paths in ascending order

ls -tUr -d $PWD/* 

might be useful

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.