Gablu
Gablu

Reputation: 149

Get size of file on remote server

I have a requirement where I need to do SFTP connection to remote server, get the size of the file on remote server and depending on the size, i need to get the file onto local server. Is there any command in SFTP to get the size of the file.

Upvotes: 7

Views: 25116

Answers (3)

Don-hashomi
Don-hashomi

Reputation: 1

you can get the size using James's with awk

ls -l | grep "filename" | awk '{print $5}'

If you are using it in a script and want to check using logic, you can store the file size in a variable like so.

varname=$(ls -l | grep "filename" | awk '{print $5}')

Then call sftp and the task

For a remote file maybe do this

filesize=$(ssh [email protected] << EOT ls -l | grep "filename" | awk '{print $5}' EOT)

Upvotes: 0

Jakuje
Jakuje

Reputation: 25966

You can get the file size of the remote files using the ls command by passing parameters.

To get Size of the file pass ls -l

To get Size of the file (HIdden files included) ls -al

To get it in human readable format pass ls -lh or ls -alh

Upvotes: 1

James Jones Jr.
James Jones Jr.

Reputation: 141

If you'd like the size output to be human readable, try: ls -lah

Upvotes: 12

Related Questions