2

I am supposed to find the command that lists all the files that are created in September and 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.

4
  • 3
    You should look into the find command. Also, don't attempt to parse ls - see mywiki.wooledge.org/ParsingLs.
    – ziesemer
    Sep 13, 2012 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? Sep 13, 2012 at 21:55
  • 1
    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) Sep 10, 2014 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... Sep 10, 2014 at 9:46

3 Answers 3

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
2
  • Except that ls -l | grep "^r.*Sep" will have the same result and is much faster. Sep 15, 2012 at 13:50
  • This includes the temporary end date file...
    – Michael
    Apr 20, 2023 at 21:36
0

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.

-1

Try this one. Hope it will help:

ls -la | grep -e "Sep"
1
  • 2
    Please consider editing this answer to explain what this command is doing and why you are suggesting it. The asker clearly is unsure of what to do and why. Your guidance would likely be extremely helpful. May 10, 2018 at 8:30

You must log in to answer this question.