chop
chop

Reputation: 471

grep ls output across tabs

If I ls -l in a directory and get:

-rwxr-x---  1 user1  admin        0  8 Aug  2012 file.txt
-rwxr-x---  1 user1  admin  1733480 26 Jul  2012 Archive.pax.gz
drwxr-x---@ 7 user1  admin      238 31 Jul  2012 Mac Shots
-rwxr-x---@ 1 user3  admin   598445 31 Jul  2012 Mac Shots.zip
-rwxr-x---@ 1 user1  admin      380  6 Jul  2012 an.sh
-rwxr-x---  1 user2  admin       14 30 Jun  2012 analystName.txt
-rwxr-x---  1 user1  admin       36  8 Aug  2012 apple.txt
drwxr-x---@ 7 user1  admin      238 31 Jul  2012 iPad Shots
-rwxr-x---@ 1 user1  admin  7372367 31 Jul  2012 iPad Shots.zip
-rwxr-x---  1 user2  admin      109 30 Jun  2012 test.txt
drwxr-x---  3 user1  admin      102 26 Jul  2012 usr

but want to list only the files owned by "user1" which were modified in "Aug" to get

-rwxr-x---  1 user1  admin        0  8 Aug  2012 file.txt
-rwxr-x---  1 user1  admin       36  8 Aug  2012 apple.txt

What is the best method?

Upvotes: 2

Views: 364

Answers (4)

Gilles Quénot
Gilles Quénot

Reputation: 185560

Parsing ls output is never a good and reliable solution. ls is a tool for interactively looking at file information. Its output is formatted for humans and will cause bugs in scripts. Use globs or find instead. Understand why: http://mywiki.wooledge.org/ParsingLs

Instead, you can try :

find . -type f -user 'user1' -maxdepth 1

or

find . -type f -printf '%u %f\n' -maxdepth 1 # if you want to show the username

or

stat -c '%U %f' * | cut -d" " -f2-

See

man find
man stat

Upvotes: 6

user000001
user000001

Reputation: 33387

I think the safest way to do it is like this :

touch --date "2012-08-01" /tmp/start
touch --date "2012-09-01" /tmp/stop
find . -maxdepth 1 -type f -user user1 -newer /tmp/start -not -newer /tmp/stop -print0 | xargs -0 ls -l {}
rm /tmp/start /tmp/stop

Or as a one liner

touch --date "2012-08-01" /tmp/start; touch --date "2012-09-01" /tmp/stop; find . -maxdepth 1 -type f -user user1 -newer /tmp/start -not -newer /tmp/stop -print0 | xargs -0 ls -l {}; rm /tmp/start /tmp/stop

Advantages:

Disadvantages

  • It is a bit long

Explanation:

  • -maxdepth 1: restricts the results to the current directory
  • -type f: restricts the results to files
  • -user user1: resttrings the results to files that belong to user1
  • -newer /tmp/start: restring the results to files newer than /tmp/start, which was created with the desired date
  • -not -newer /tmp/stop: restring the results to files not newer than /tmp/stop, which was created with the desired date
  • -print0: so it can handle filenames with newlines in their name!

Upvotes: 1

Mark Reed
Mark Reed

Reputation: 95325

Or you can be more explicit, since Michael's grep would also find a file owned by user1 namedd 'August iPad Shots' no matter when it was modified:

ls -l | awk '($3=="user1" && $7=="Aug")'

Upvotes: 2

Michael Day
Michael Day

Reputation: 1015

How about ls -l | grep user1 | grep Aug?

Or you can combine the regexp: ls -l | grep 'user1.*Aug'

Upvotes: 0

Related Questions