Store the files details in different file using bash

Hi all,

this is output of ls command !!

there is differen different files permission are there , and my requirement is each file permission is stored in different different file.

rwxr-xr-x  1 root root       0 Mar 29  2011 2
-rwxr-xr-x  1 root root       0 Mar 29  2011 20
drwxr-xr-x  2 root root    4096 Mar 16 23:12 a
-rwxr-xr-x  1 root root    1050 Mar 16 08:06 anaconda-ks.cfg
drwxr-xr-x  2 root root    4096 Mar 29  2011 anish
drwxr-xr-x  2 root root    4096 Mar 19 05:36 av
-rw-r--r--  1 root root  854994 Oct 25 18:39 boss.tgz
drwxr-xr-x  2 root root    4096 Mar 16 23:12 c
drwxr-xr-x  6 root root    4096 Mar 25  2011 del
drwxr-xr-x  6 root root    4096 Mar 20 14:59 Desktop
-rwxr-xr-x  1 root root 1141195 Mar  9 15:27 disketa-tim.tgz
-rwxr-xr-x  1 root root       0 Jan 16 00:00 dummy
drwxr-xr-x  2 root root    4096 Mar 16 23:12 e
drwxr-xr-x  2 root root    4096 Mar 17 00:10 flo
-rwxr-xr-x  1 root root      54 Mar 20 15:35 for.sh
drwxr-xr-x  2 root root    4096 Mar 16 23:12 gfg
-rwxr-xr-x  1 root root   30632 Mar 16 08:06 install.log
-rwxr-xr-x  1 root root    4492 Mar 16 08:03 install.log.syslog
-rw-r--r--  1 root root       0 Mar 19 23:16 logfile.txt
-rw-r--r--  1 root root       0 Mar 20 15:37 ls.txt
-rw-------  1 root root   17215 Mar 20 01:38 mbox
drwxr-xr-x  2 root root    4096 Mar 20 15:09 scripts
-rwxr-xr-x  1 root root   38946 Mar 16 08:12 scsconfig.log
-rwxr-xr-x  1 root root     195 Mar 16 08:12 scsrun.log
drwxr-xr-x 20 root root    4096 Mar 18 08:01 uat
drwxr-xr-x  2 root root    4096 Mar 16 23:12 w1


like this.

output :

file 1:
rwxr-xr-x  1 root root       0 Mar 29  2011 2


file 2:
drwxr-xr-x  2 root root    4096 Mar 16 23:12 e
drwxr-xr-x  2 root root    4096 Mar 17 00:10 flo
drwxr-xr-x  2 root root    4096 Mar 16 23:12 c
drwxr-xr-x  6 root root    4096 Mar 25  2011 del


file 3:

-rwxr-xr-x  1 root root   38946 Mar 16 08:12 scsconfig.log
-rwxr-xr-x  1 root root     195 Mar 16 08:12 scsrun.log

file 4:

-rw-------  1 root root   17215 Mar 20 01:38 mbox

file 5:

-rw-r--r--  1 root root       0 Mar 19 23:16 logfile.txt
-rw-r--r--  1 root root       0 Mar 20 15:37 ls.txt

how is it possible using shell scripting , please guide me

With Regards
Anish Kumar.V

#!/bin/bash
ls -l |\
while read line
do
        case ${line%% *} in
                 rwxr-xr-x)     echo "$line" >> file1;;
                drwxr-xr-x)     echo "$line" >> file2;;
                -rwxr-xr-x)     echo "$line" >> file3;;
                -rw-------)     echo "$line" >> file4;;
                -rw-r--r--)     echo "$line" >> file5;;
        esac
done
sort inputfile | awk '{if($1!=a){a=$1;print $0 >"file_"++i}else{a=$1;print $0>>"file_"i}}'

[/CODE]

1 Like

Hi all,

Thanks for your replies !!! it works