Linux scp all files in directory

copy file/folder using scp command

How can I copy a file/folder from windows to linux (putty), probably using scp command? I used scp user@hostname(windows):c:\folder\filname user@hostname(Linux):/folder/filename(destination), but unfotunately I got an error. Basically, I am trying to copy from windows to Linux. Hope it works whether I am on windows or Linux.

3 Answers 3

I don’t think this can work in this form, with the backslash \ separators:

scp user@hotname:c:\folder\filname user@hostname:\folder\filename(destination) 

First of all, the path separator in Linux is / instead of \ , so this would be better:

scp user@hotname:c:\folder\filname user@hostname:/folder/filename 

Secondly, your command looks like as if you’re running this command on a third PC, on machineC to copy files from machineA to machineB. If this is not the case and you are in fact on machineA copying files to machineB, then this would be better:

scp c:\folder\filname user@hostname:/folder/filename 

If you don’t have the scp command in Windows, here are a few options:

  • Install Git. Even if you don’t use Git, this installer includes an excellent terminal and common *nix commands, and scp too
  • Download PuTTY. You can use pscp.exe instead of scp , the above syntax will work.
  • Install WinSCP. It has scripting features, but if you want to use the command line the previous two options are easier.

If I do from windows I get ‘scp’ is not recognized as an internal or external command,operable program or batch file. If I do from Linux to my windows machine then I get «ssh: connect to host port 22: Connection refused lost connection» I think I have to use Winscp.

In *nix systems, this should work:

# to copy file.ext from remote server to current working directory # note the dot (.) at the end, which means current directory $ scp user@remote.server.com:~/desired/folder/file.ext . # to copy all files in a folder from remote server # to current directory # note the dot (.) at the end, which means current directory $ scp -r user@remote.server.com:~/desired/folder/* . # copy a folder and all its contents as it is from remote server # to current directory # note the dot (.) at the end, which means current directory $ scp -r user@remote.server.com:~/dersired/folder . 

More information can also be found in this article: scp command syntax

Читайте также:  Linux headers to build package

Источник

How to filter files when using scp to copy dir recursively?

I need to copy all the .class files from server to local with all dir reserved. e.g. server:/usr/some/unknown/number/of/sub/folders/me.class will be /usr/project/backup/some/unknown/number/of/sub/folders/me.class the problem is, there are many other useless files such as .svn-base files that i don’t want. how can i filter them so I only scp .class files?

I like the rsync option mentioned. You didn’t mention if this is a one-off operation, or if you’ll be automating this repeatedly. For a one-off operation, the judicious use of find, grep -v, xargs and temporary files should make short work of this.

9 Answers 9

I’d probably recommend using something like rsync for this due to its include and exclude flags, e.g:-

rsync -rav -e ssh --include '*/' --include='*.class' --exclude='*' \ server:/usr/some/unknown/number/of/sub/folders/ \ /usr/project/backup/some/unknown/number/of/sub/folders/ 
  • -r for recursive
  • -a for archive (mostly all files)
  • -v for verbose output
  • -e to specify ssh instead of the default (which should be ssh, actually)

Anyway to make this ignore subfolders that don’t have *class files in them? (i.e. I don’t want a bunch of empty dirs)

Can you explain —include, not —include= In the MAN pages, I could find explanation on —include= but not —include

To exclude dotfiles in base directory:

scp -r [!.]* server:/path/to/something 

[!.]* is a shell glob that expands to all files in working directory not starting with a dot.

This indicates to exclude files like being asked, but how can this be achieved for a whole directory?

There is no feature in scp to filter files. For «advanced» stuff like this, I recommend using rsync:

rsync -av --exclude '*.svn' user@server:/my/dir . 

(this line copy rsync from distant folder to current one)

Recent versions of rsync tunnel over an ssh connection automatically by default.

Since you can scp you should be ok to ssh ,
either script the following or login and execute.

# After reaching the server of interest cd /usr/some/unknown/number/of/sub/folders tar cfj pack.tar.bz2 $(find . -type f -name *.class) 

return back (logout) to local server and scp ,

# from the local machine cd /usr/project/backup/some/unknown/number/of/sub/folders scp you@server:/usr/some/unknown/number/of/sub/folders/pack.tar.bz2 . tar xfj pack.tar.bz2 

If you find the $(find . ) is too long for your tar change to,

find . -type f -name *.class | xargs tar cfj pack.tar.bz2 

Finally, since you are keeping it in /usr/project/backup/ ,
why bother extraction? Just keep the tar.bz2 , with maybe a date+time stamp.

Читайте также:  Astra linux восстановить удаленный файл

Источник

How do I copy a folder from remote to local using scp?

How do I copy a folder from remote to local host using scp ? I use ssh to log in to my server.
Then, I would like to copy the remote folder foo to local /home/user/Desktop . How do I achieve this?

The OP’s question was whether it is possible to copy file from remote to local host while ssh’d to remote host. I’m not sure why no single answer has correctly addressed his/her question.

The premise of the question is incorrect. The idea is, once logged into ssh, how to move files from the logged-in machine back to the client that is logged in. However, scp is not aware of nor can it use the ssh connection. It is making its own connections. So the simple solution is create a new terminal window on the local workstation, and run scp that transfers files from the remote server to local machine. E.g., scp -i key user@remote:/remote-dir/remote-file /local-dir/local-file

@sjas: in mc it’s easier to use Left/Right on the menu > Shell link where you can type the alias you have in your ~/.ssh/config e.g. myhost: > OK

@jeffmcneill yes your right. But you didn’t address directly JeffDror, so I guess most people did not realize that your are answering JeffDror’s question.

13 Answers 13

scp -r user@your.server.example.com:/path/to/foo /home/user/Desktop/ 

By not including the trailing ‘/’ at the end of foo, you will copy the directory itself (including contents), rather than only the contents of the directory.

-r Recursively copy entire directories

Two nice-to-knows I found: the -C flag adds compression and the -c flag lets you pass in other cipher types for better performance, like scp -c blowfish a@b:something . as seen in dimuthu’s answer

This answer lacks important explanation. Will you end up with Desktop/foo or will you have Desktop/allcontentsofFooGohere scp seems to act weird sometimes to me it does one thing then another

@Toskan with scp -r user@your.server.example.com:/path/to/foo /home/user/Desktop/ you should end up with Desktop/foo . With scp -r user@your.server.example.com:/path/to/foo/. /home/user/Desktop/ you will end up with the contents of foo in Desktop and all the sub-dirs of foo strewn under Desktop

Читайте также:  Whatsapp desktop linux rpm

To use full power of scp you need to go through next steps:

Then, for example if you have this ~/.ssh/config:

Host test User testuser HostName test-site.example Port 22022 Host prod User produser HostName production-site.example Port 22022 

you’ll save yourself from password entry and simplify scp syntax like this:

scp -r prod:/path/foo /home/user/Desktop # copy to local scp -r prod:/path/foo test:/tmp # copy from remote prod to remote test 

More over, you will be able to use remote path-completion:

scp test:/var/log/ # press tab twice Display all 151 possibilities? (y or n) 

For enabling remote bash-completion you need to have bash-shell on both and hosts, and properly working bash-completion. For more information see related questions:

Источник

How to copy all files from a directory to a remote directory using scp?

My goal is copy only all files from ~/local_dir to user@host.com /var/www/html/target_dir using scp and do not create local_dir category in local_dir.

/var/www/html/target_dir/files.. 

Does scp * user@host.com:/var/www/html/target_dir not do what you want? If so, please edit with more detail of what you’re trying to do & have tried so far.

5 Answers 5

scp has the -r argument. So, try using:

$ scp -r ~/local_dir user@host.com:/var/www/html/target_dir 

The -r argument works just like the -r arg in cp, it will transfer your entire folder and all the files and subdirectories inside.

Sorry, I got it wrong. Then just repeat the command, but like this: $ scp -r ~/local_dir user@host.com:/var/www/html/ Then rename new directory if needed.

Change the wildcard * to a dot . and you’ll copy the directory contents (including any dot files) without copying the directory itself. scp -pr ~/local_dir/. user@example.com:/path/to/target_dir

Unfortunately, using . has been broken by a poorly implemented scp «bugfix» (see

If your goal is to transfer all files from local_dir the * wildcard does the trick:

$ scp ~/local_dir/* user@host.com:/var/www/html/target_dir 

The -r option means «recursively», so you must write it when you’re trying to transfer an entire directory or several directories.

-r Recursively copy entire directories. Note that scp follows symbolic links encountered in the tree traversal. 

So if you have sub-directories inside local_dir , the last example will only transfer files, but if you set the -r option, it will transfer files and directories.

Источник

Оцените статью
Adblock
detector