我正在编写一个bash脚本,该脚本需要*_out.csv
从远程服务器上的目录中提取所有内容。所有这些文件都是另一个目录内部的几个目录。例如,假设目录名为ox_20190404/
。我可以通过以下方式找到我的所有文件:
find ox_20190404/assessment/LWR/validation -type f -name "*_out.csv"
这个问题回答了我的部分问题,但是由于我不想完整地复制目录,因此我需要弄清楚如何实现上面的代码。假设我从这里开始:
$ dir="/projects/ox/git"
$ server="myusername@server"
$ scp $server:$dir/$(ssh $server 'ls -t $dir | head -1') .
我将如何从那里获取我需要的文件?
我的问题的最后一部分想知道是否有办法将所有复制的文件放置在远程服务器上相同的文件路径和目录中。
这是最终为我工作的代码。我可能没有很好地描述我的问题,但是我没有找到最新更改的目录的麻烦。然后,我的问题是找到该目录中的所有文件,并确保将它们放在我的本地计算机上的正确位置。这是执行此操作的bash脚本:
# Grab the most recently updated ox file off of server; return as string
# in the form of ox_XXXXXXXX/assessment/LWR/validation/*
newest=$(
ssh -qn username@server 'find /projects/ox/git/* -mindepth 0 -maxdepth 0 -type d -printf "%T@\t%f\n"' |
sort -t$'\t' -r -nk1,2 |
head -n1 |
cut -f2- |
awk '{print "/projects/ox/git/"$1"/assessment/LWR/validation/HBEP/analysis/BK363/*"}'
)
# Take $newest and find all associated *_out.csv files beneath that directory
newestf=$(
ssh -qn username@server "find $newest -type f -name '*_out.csv'"
)
# Write these filepaths to a .csv on the local machine
echo "$newestf" | tr " " "\n" remote_fp.csv
# Run Rscript to parse and transform the filepaths so they go to the write place on local machine
Rscript ~/transform_fp.R
# Read content from .csv remote file paths - we'll need these to actually pull the files using scp
get_scp_fp=$(awk -F "\"*,\"*" '{print $1}' ~/remote_fp.csv)
# Read content from .csv local file paths - we'll need these to actually write the data locally
get_local_fp=$(awk -F "\"*,\"*" '{print $1}' ~/local_fp.csv)
# Loop through file paths and pull data from remote to local.
for i in $get_scp_fp; do
for j in $get_local_fp; do
scp -p username@server:"$i" "$j"
done
done
脚本:
suppressPackageStartupMessages(library(tidyverse))
test <- read_csv("remote_fp.csv", col_names = FALSE)
str_replace_all(test$X1, "/projects/ox/git/ox_[0-9]{8}", "~/Documents/projects/ox") %>%
str_replace_all("(?:analysis).*$", paste0("doc/figures/", basename(.))) %>%
tibble() %>%
write_csv(path = "~/local_fp.csv", col_names = FALSE)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句