Deleting Files/Folders based on OWNERSHIP

1. List the files and directories.

$ ls -lah

total 52K
drwxrwxrwt 11 root root 4.0K Nov 4 03:52 .
drwxr-xr-x 25 root root 4.0K Oct 6 03:54 ..
srwxr-xr-x 1 george george 0 Oct 8 00:47 .dguardianipc
srwxr-xr-x 1 george george 0 Oct 8 00:47 .dguardianurlipc
-rw-r--r-- 1 root root 0 Nov 4 03:52 file1
-rw-r--r-- 1 gopher gopher 0 Nov 4 03:52 file2
-rw-r--r-- 1 gopher gopher 0 Nov 4 03:52 file3
-rw-r--r-- 1 gopher gopher 0 Nov 4 03:52 file4
-rw-r--r-- 1 gopher gopher 0 Nov 4 03:52 file5
-rw-r--r-- 1 root root 0 Nov 4 03:52 file6
-rw-r--r-- 1 root root 0 Nov 4 03:52 file7
drwxrwxrwt 2 root root 4.0K Oct 1 07:55 .font-unix
drwxrwxrwt 2 root root 4.0K Oct 1 07:55 .ICE-unix
drwxr-xr-x 2 root root 4.0K Nov 4 03:43 test1
drwxr-xr-x 2 root root 4.0K Nov 4 03:51 test2
drwxr-xr-x 2 root root 4.0K Nov 4 03:43 test[2-7]
drwxr-xr-x 2 root root 4.0K Nov 4 03:43 test2-7
drwxr-xr-x 2 george george 4.0K Nov 4 03:51 test3
drwxr-xr-x 2 george george 4.0K Nov 4 03:51 test4
drwxr-xr-x 2 george george 4.0K Nov 4 03:51 test5

2. Let's try deleting the files with an owner 'gopher'

List the files with gopher owner.

$ ls -lah | grep gopher

output:
-rw-r--r-- 1 gopher gopher 0 Nov 4 03:52 file2
-rw-r--r-- 1 gopher gopher 0 Nov 4 03:52 file3
-rw-r--r-- 1 gopher gopher 0 Nov 4 03:52 file4
-rw-r--r-- 1 gopher gopher 0 Nov 4 03:52 file5

The output are from file2 to file5, next task is to list the exact filenames using awk.

$ ls -lah | gopher | awk '{print $9}

Output:
file2
file3
file4
file5

Now that the output list only filenames, we can now delete the files.

$ rm -fv `ls -lah | grep gopher | awk '{print $9}'`

Output:
removed `file2'
removed `file3'
removed `file4'
removed `file5'

If its a directory you can issue this pattern:

$ rm -fvR `ls -lah | grep george | awk '{print $9}'`

Output:
removed directory: `test3'
removed directory: `test4'
removed directory: `test5'

Note: be sure to replace the 'grep (owner)' with the specified owner/user.

Comments

Popular posts from this blog

Scraping an Entire Website using LINUX

How to enable clustering in Openfire Enterprise?

Google Sitemap Generator Installation in a CentOS Platform