I Have a list file File_Transfer_List.txt which contains list of file to do scp

My requirement is I need to do scp that files given in the list file and then delete the files from source location.

I tried this :

scp File_Name user@server:/destination && rm File_Name ;

I am unable to test it, I don't have my scp ready to test it; can any one correct me if I am wrong.

  • 3
    You said you tried that but immediately afterwards you said you are unable to test it. Which is it? In any case, for anything that involves deleting files (and could therefore be dangerous), the best advice for you is just to test it yourself. You can easily create a dummy throwaway file with unimportant data to see if your command works. When you test it, you will know whether or not it works, and you will not need to ask here if the command is OK!
    – Celada
    Commented May 13, 2015 at 18:09

2 Answers 2


You have two requirements here (files from filelist and remove source files) that scream for using rsync.

Depending on what your filelist contains (relative or absolute paths, preserve paths on backup, etc) you could just do:

rsync --files-from=filelist.txt --remove-source-files -avz \
      . user@remotehost:/path/to/backup/folder
  • ya tried it, it is wrking when both the servers in Unix....
    – deepuu
    Commented May 14, 2015 at 7:19
  • 2
    @deepu, if this answer solved your issue, please take a moment and accept it by clicking on the check mark to the left. That will mark the question as answered and is the way thanks are expressed on the Stack Exchange sites.
    – terdon
    Commented May 14, 2015 at 9:41

If I understand correctly, you have a file tat contains file names and you want to copy each of those files to the remote server and delete the local one if the copy was successful. If so, you can do:

while IFS= read -r file; do 
    scp -- "$file" [email protected]:/remote/path && rm -- "$file" 
done < files.txt
  • Ya almost same, My List file is on unix Server and I shuled read list file and after copying the files from windows to unix and then i shuled delete the files from windows server
    – deepuu
    Commented May 14, 2015 at 7:18
  • @deepuu I don't understand, if the files are on Windows, then this won't work.
    – terdon
    Commented May 14, 2015 at 9:43
  • while read file? Shouldn't it be for file in `ls`? Commented May 24, 2021 at 2:27
  • @TomRussell the OP has the list of target files stored in a file, so for file in `ls` wouldn't help. In any case, even if the OP did want to go over the list of files in the current directory, you never do for file in `ls` that is very bad practice and also known as bash pitfall #1. Always do for file in * and never for file in `ls`.
    – terdon
    Commented May 24, 2021 at 9:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .