user1299846
user1299846

Reputation: 387

Bash display files by date

I am just creating bash script to: Print a long listing of the files in the login directory for a specific month. The user is prompted to enter the first 3 letters of a month name, starting with a capital, and the program will display a long list of all files that were last modified in that month.

For example, if user entered “Jul”, all the files that were last modified in July will be listed.

Is it possible to sort files by date and then limit them? or can it be done differently?

Upvotes: 0

Views: 223

Answers (3)

Michael Ballent
Michael Ballent

Reputation: 1088

Here is the script that should do it

Month=Dec
ls -ltr |awk '$6 ~ /'$Month'/ {print $9}'

This will have awk look at the date field from the ls field ($6), ls -ltr will sort it by date. This will then expand the variable $Month and use that to search the $6 field, and print out the file name (the 9th field $9).

Upvotes: 0

Raghu
Raghu

Reputation: 2134

read mon

la -la | grep $mon

You can grep -i for case insensitive grep. So user inputs can become case insensitive.

Note: This is crude because it returns results that have the text matching the month name. Ex: it will return files that are name after month. TO refine this you will have to just look at the date column

Upvotes: 0

Jon Lin
Jon Lin

Reputation: 143856

Take a look at this answer: https://stackoverflow.com/a/5289636/851273

It covers both month and year, though you can remove the match against the year.

Upvotes: 1

Related Questions