Maybe an awk command is enough

Posted on 2001-06-30
Last Modified: 2013-12-27
I want to find in a big directory (where  the owner and the file name have the same name) which files have not the same name as owner (in order to correct this situation).

Question by:vage78

Expert Comment

ID: 6240928
Sorry, I'm a perl weanie.  Ignore this if you want.

This works:

     ls -l | perl -nale 'print unless $F[2] eq $F[-1]'

There are a couple caveats...  I tested this on a linux box, and the file's owner gets truncated to 8 characters, but the filenames weren't truncated, so this second version is preferable in that case:

     ls -l | perl -nale 'print unless $F[-1] =~ /^$F[2]/'

But then if user 'rj' owns the file 'rjackson', then the second one won't work.  So I guess you could do something like this:

     ls -l | perl -nale 'print unless (length $F[2]<8 && $F[-1] eq $F[2] || $F[-1]=~/^$F[2]/)'

which mitigates the problem slightly, but it's still not possible to solve this fully unless you stat the file directly or convince ls to not truncate the owner's name.

Caveat two:  Outputs of ls on different unicies may use different columns for the file owner.  Column -1 is the last colum, column 2 is the 3rd column.  Monkey with this until the outputs look right:

     ls -l | perl -nale 'print "$F[2]\t$F[-1]"'

the owner is on the left and the filename is on the right.
LVL 38

Accepted Solution

yuzh earned 15 total points
ID: 6243460
Hi vage78,

   A "find" command will do the job for you:

   cd mybig-dir
    find . -name myfile -user my-user-name -print

   If you need to search the dir own by the other,  then you need to su
as root, and use the above command.

   You can redirect the output to a file:
    find . -name myfile -user my-user-name -print >Filelist



Expert Comment

ID: 6245318
If all your files are directly under your big directory

ls -l | awk '$3 != $9{print $9}'

will do what you need.

You can filter before the | if you require a better input to the awk oneliner (to eliminate subdirs for example) but this should work.

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question