views:

193

answers:

4

With bash on linux, how would I write a command to recursively traverse shares mounted, and run commands on each file, to get the file type and size, permissions etc, and then output all of this to a file?

A: 

A CIFS share mount would look like a regular directory tree in the linux shell.
The command to search as you need is therefore generic.
From the base directory,

find . -type f -exec ls -lsrt {} \; > file.txt

Ok, this does not give you the file-type detail;
that can be done with a -exec file filename on each file.

nik
The first "." is the starting directory - so the given command will start in the current directory, but you could equally use "find /path/to/share ..."
caf
@caf, Very true. And, I have talked about that while answering his other question.
nik
A: 
mount -v | grep smbfs | awk '{print $3}' | xargs ls -lsR

which you can redirect to a file.

Petros
how would I include the output of the file command on each file as well?
Bill Gray
A: 
mount -v | awk '/smbfs/{
    cmd="ls -lsR "$3
    while((cmd | getline d)>0){
        print d "->file "$3
    }   
    close(cmd)
}'
ghostdog74
how would I include the output of the file command on each file as well?
Bill Gray
what do you mean by "output of the file command" ? for your information, there is actually a "file" executable command in unix. please be clear about what you are asking. show examples if necessary
ghostdog74
sorry, I meant having the output of ls, as well as file. eg "ls -lsR $3; file $3" but I am unsure how to do this
Bill Gray
A: 
find $(mount -t smbfs | awk '{print $3}') -mount -type f -ls -execdir file {} \;
...
33597911    4 -rw-rw-r--   2 peter    peter           5 Dec  6 00:09 ./test.d\ ir/base
./base: ASCII text
  3662    4 -rw-rw-r--   2 peter    peter           4 Dec  6 02:26 ./test.txt...
./test.txt...: ASCII text
  3661    0 -rw-rw-r--   2 peter    peter           0 Dec  6 02:45 ./foo.txt
./foo.txt: empty
...

If you used -exec file {} +, it would run file once with multiple arguments, but then the output wouldn't be nicely interleaved with find's -ls output. (GNU find's -execdir {} + currently behaves the same as -execdir {} \;, due to a bug workaround. Use -exec file {} \; if you want the full path in the file output as well as in the -ls output above it.

find -ls output is not quite the same as ls -l, since it includes inode an number of blocks as the first two fields.

Peter Cordes