3

how can we copy only file details (filename, size, time) from a remote machine in Unix?

For example: I have a directory (/opt/apache/…/webapps/Context) placed on a remote machine. Now I want to copy only the metadata (size, time, filename) of the files that reside in this directory and its subdirectories to my local machine, something like root root 1150 Dec 30 12:11 file.txt.

  • 1
    How do you want the output? Do you want just a directory listing or must there be a file on your local system per file on the remote system? – Chris Davidson Dec 30 '14 at 7:05
  • I need all file names with size, time in the directory/subdirectories. To copy actual file is not required. – Sak Dec 30 '14 at 7:31
3

If you want to get a detailed listing of the files in directory opt/apache../webapps/Context on remote machine remotemachine, use:

ssh remotemachine  "ls -l /opt/apache../webapps/Context"

If you want to search recursively for all files in that directory and all its subdirectories, then use:

ssh remotemachine  "find /opt/apache../webapps/Context -type f -exec ls -l {} +"

How it works

  • ssh remotemachine command

    This executes command on remotemachine using secure-shell (ssh). command can be any command of your choosing. In the two examples above, I used:

  • ls -l /opt/apache../webapps/Context

    This displays the directory listing of /opt/apache../webapps/Context in the "long" format. You may use any of ls's options to select the format or sorting that you prefer. See man ls.

  • find /opt/apache../webapps/Context -type f -exec ls -l {} +

    This uses find to search recursively through subdirectories. The files it finds are again displayed with ls. -type f tells find to show only regular files and not directories. find has many options which you may use to select just the files that interest you. See man find.

More Options

If you want to save the output to a file, use redirection. For example:

ssh remotemachine  "ls -l /opt/apache../webapps/Context" >outputfile

If you want both to display the output on the screen and also to save to a file, use tee:

ssh remotemachine  "ls -l /opt/apache../webapps/Context" | tee outputfile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.