Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am supposed to find the command that lists all the files created in September and are readable for the owner. Also explain how the command works.

I know I'm supposed to use

ls -l

and I will use | somewhere in it but not sure what comes before the pipe and what after.

share|improve this question

migrated from stackoverflow.com Sep 13 '12 at 21:03

This question came from our site for professional and enthusiast programmers.

1  
You should look into the find command. Also, don't attempt to parse ls - see mywiki.wooledge.org/ParsingLs. –  ziesemer Sep 13 '12 at 14:37
2  
I guess it isn't against the rules to ask StackExchange people to do your homework for you. But if you don't do it yourself, how are you going to learn? –  Isaac Rabinovitch Sep 13 '12 at 21:55
    
First, bear in mind that when you deal with timestamps on files, what you generally are dealing with is 'modification time'. There are two other times: 'change time' and 'access time'. These are also known respectively as mtime, ctime and atime. There is no 'creation' time in Unix filesystems (or at least, not on any that I have seen) –  Cameron Kerr Sep 10 at 9:42
    
The 'info' page for the 'touch' command is useful in explaining 'change time': "The inode change time represents the time when the file's meta-information last changed. One common example of this is when the permissions of a file change." It has some other relevant things to say also. Another useful 'info' page is the 'date' command -- it will give you ideas of how you can specify the --date argument. Reading 'info' pages is sometimes very useful... –  Cameron Kerr Sep 10 at 9:46

2 Answers 2

For the date range you can use a trick. First create two dummy files with touch like this:

touch -t 201209010000 /tmp/1sept
touch -t 201209302359 /tmp/30sept

As you can check they have now desired creation dates:

$ ls -l 1sept 30sept 
-rw-rw-r-- 1 topr topr 0 Sep  1 00:00 1sept
-rw-rw-r-- 1 topr topr 0 Sep 30  2012 30sept

Now you are ready to go with the final command:

find /thepath/you/like -type f -newer /tmp/1sept -and ! -newer /tmp/30sept -exec stat -c '%A %n' {} \; | grep '^.r'

A bit of exmplanation:

  • -type f search for files only
  • -newer ! -and -newer using dummy files as template set dates range you like
  • -exec fires a command on every found file written as {} \;
  • stat command prints info about the file, %A means human readable access rights string like -rw-rw-r-- and %n prints file name
  • grep '^.r' filter results with regular exception to show only lines starting from <anycharacter>r, so checks if read access by the file owner is granted
share|improve this answer
    
Except that ls -l | grep "^r.*Sep" will have the same result and is much faster. –  mikebabcock Sep 15 '12 at 13:50

ls -lh should list files in human readable long format (depending on your shell).

Now, you want to grep (that's a filter program) for the ones that match what you're looking for.

For example, to find all the files that say david and that were made in 2011, I could use:

ls -lh | grep "david" | grep "2011"

(Yes, there are faster, easier ways to do this, but the idea is to be simplistic for the asker).

In your case, you want to filter both that first block of text for the readable bit being set and the date being in September. Take a look at the output from ls -lh and you should be able to make your own grep command that works.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.