List directories on the basis of name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting List directories on the basis of name
# 1  
Old 11-10-2013
List directories on the basis of name

I have below directories. All directories create as per some some logging software by today so all directories current time is today date.

Code:
Direct 2013-08-12 23123
Direct 2013-08-13 24121
Direct 2013-08-14 34513

Direct 2013-08-31 15435
...........

Direct 2013-09-12 53145
Direct 2013-09-30 65234

Direct 2013-09-30 89642

What i want, i just want to extract following directories which name is like, means current date and last two days old date format directory name list rest not list.

Code:
Direct 2013-11-10 12341
Direct 2013-11-09 12323
Direct 2013-11-08 12235

# 2  
Old 11-10-2013
Hi,

I don't quite get your requirments, but if you need to list today, yesteraday and the day before try something like the following.

Code:
$ date +%Y-%m-%d
2013-11-10
$ date +%Y-%m-%d --date='yesterday'
2013-11-09
$ date +%Y-%m-%d --date='-2 days'
2013-11-08

so for example,
Code:
ls -d Direct\ $(date +%Y-%m-%d --date='-2 days')\ *

would list all directories fitting the pattern

Moderator's Comments:
Mod Comment edit by bakunin: corrected second CODE-tag
# 3  
Old 11-10-2013
@Skrynesaver: this will work only if you GNU-date, which will be so at Linux-, FreeBSD- and similar systems. Any Unix using a POSIX-compatible date-command will not process this.

I hope this helps.

bakunin
# 4  
Old 11-10-2013
Here is an awk solution. You can use date command to calculate the 2 days from the current date and pass along with the current date to awk as variables.

Code:
$ find . -name "Direct*" -type d -print | awk '$2 <= "2013-11-10"  && $2 >= "2013-11-08" {print substr($0,3)}' 

Direct 2013-11-08 12235
Direct 2013-11-09 12323
Direct 2013-11-10 12341

# 5  
Old 11-10-2013
Quote:
Originally Posted by mjf
Here is an awk solution. You can use date command to calculate the 2 days from the current date and pass along with the current date to awk as variables.

Code:
$ find . -name "Direct*" -type d -print | awk '$2 <= "2013-11-10"  && $2 >= "2013-11-08" {print substr($0,3)}' 

Direct 2013-11-08 12235
Direct 2013-11-09 12323
Direct 2013-11-10 12341


This is working fine, but how i can automate that it will automatically put today and last two days date. First it will list the directories and then i can delete the directories.
# 6  
Old 11-11-2013
Quote:
Originally Posted by learnbash
This is working fine, but how i can automate that it will automatically put today and last two days date. First it will list the directories and then i can delete the directories.
The reading of the man page of date, especially the various format strings to shape the output, is left as an exercise to the reader.

Moderator's Comments:
Mod Comment Thread closed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

List the directories, having given pattern in the directories name, sorted by creation date

It is for HP-Unix B.11.31. Requirement: 1. List the directories, having given pattern in the directories name, sorted by creation date. Example: Directories with name "pkg32*" or "pkg33*" 2. On the output of 1. list the directories by creation date as sort order, with creation date... (2 Replies)
Discussion started by: Siva SQL
2 Replies

2. Shell Programming and Scripting

How to list all the files, directories and sub-directories in the current path except one directory?

Can anyone come up with a unix command that lists all the files, directories and sub-directories in the current directory except a folder called log.? Thank you in advance. (7 Replies)
Discussion started by: Manjunath B
7 Replies

3. UNIX for Dummies Questions & Answers

List directories and sub directories recursively excluding files

Hi, Please help me, how to get all the direcotries, its sub directories and its sub directories recursively, need to exclude all the files in the process. I wanted to disply using a unix command all the directories recursively excluding files. I tried 'ls -FR' but that display files as... (3 Replies)
Discussion started by: pointers
3 Replies

4. Shell Programming and Scripting

How to list all the directories, sub directories in a mount along with size in ascending order?

Hi , I am very new to unix as well as shell scripting. I have to write a script for the following requirement. In a particular mount, have to list all the directories and sub directories along with size of the directory and sub directory in ascending order. Please help me in this regard and many... (4 Replies)
Discussion started by: nmakkena
4 Replies

5. Shell Programming and Scripting

Want to Get List of All directories

:confused: Hi, Is there any one-lier through which I will be able to reach to last directory excluding all files only directrory and sub-directoris should be displayed from the current path. Thanks in advance (7 Replies)
Discussion started by: niceboykunal123
7 Replies

6. Shell Programming and Scripting

Hiding Directories on a Session by Session basis

Hi, Apologies if anyone has read my recent post on the same subject in the Linux forum, just thought actually the solution might more likely come from scripting. Essentially, I am trying to restrict access to directories based on the user's name AND their location on a session-by-session... (3 Replies)
Discussion started by: en7smb
3 Replies

7. Shell Programming and Scripting

List of directories into a nested list

I have a list of directories like this a a/b a/c a/d a/d/e a/d/f a/d/g a/d/g/h a/i I would like to convert this list into another list, nested like this a{b{} c{} d{e{} f{} g{h{}}} i{}} Here is a pseudo algorithm for this Add a: (1 Reply)
Discussion started by: Ilja
1 Replies

8. UNIX for Dummies Questions & Answers

how to list directories only

I would like to know how to list directories only without a / at the end. I would like to only see them in my current dir. for example ls - d */ gives dir1/ dir2/ dir3/ dir4/ but is there a way to get only dir1 dir2 dir3 i need it to use them as inputs in a foreach loop... (3 Replies)
Discussion started by: yodadbl07
3 Replies

9. UNIX for Dummies Questions & Answers

List directories

Is there any way to list only directories in particular file system? Malay (3 Replies)
Discussion started by: malaymaru
3 Replies

10. Shell Programming and Scripting

Split/create directories on basis of their size

Hi, I would really apprciate it if someone expert in shell, "shell guru" scripting can help me. with a shell command how can I create directories and populate on basis of size. Before I confuse you, let me tell you the scenerio. We take hot database backup of oracle on disk, and... (0 Replies)
Discussion started by: amitoverseas40
0 Replies
Login or Register to Ask a Question