跳至主要内容

Copy specific file and its structure

Description

Suppose the file structure under the directory /tmp/gsl is like following shows(notice that not all sub-directory has Makefile, for example ./block):

.
|-- INSTALL
|-- Makefile
|-- NEWS
|-- ...
|-- blas
|   |-- ChangeLog
|   |-- Makefile
|   |-- TODO
|   |-- blas.c
|   |-- ...
|-- block
|   |-- ChangeLog
|   |-- block.c
|-- bspline
|   |-- Makefile
|   |-- ...
|-- ...

There are many Makefile in different sub-directories and we want to copy those Makefile to home/usrname/gsl. And the result should be like this:

.
|-- Makefile
|-- blas
|   |-- Makefile
|-- bspline
|   |-- Makefile
|-- ...

Question

  • if the target directory already have necessary parent folder for Makefile, like blas, bspline, how to copy?
  • if the target directory don’t have necessary parent folder for Makefile, like blas, bspline, how to copy?

Answer only for question 1

cd /tmp/gsl
# use `find` to find out those file;
# use `sed` to substitute directory name:
# ':' used as substitution delimiter, '&' represent previous matched string -- i.e. whole file path, '\1' as matched content in '()' -- i.e. the directory structure of Makefile;
# and finally use `bash` to execute copy.
find . -name 'Makefile' | sed 's:\(.*\)/Makefile:cp & /home/username/gsl/\1 :' | bash -s 

Answer for question 1 & 2

simpler and cleaner.
use --parents flag to create necessary directory for copy.

–parents
Form the name of each destination file by appending to the target
directory a slash and the specified name of the source file. The
last argument given to cp must be the name of an existing
directory. For example, the command:
cp --parents a/b/c existing_dir
copies the file a/b/c to existing_dir/a/b/c, creating any
missing intermediate directories.

cd /tmp/gsl/
# the usage of `xargs -i` is much like for each file path of Makefile, `{}` represent the file path
find .  -name 'Makefile' | xargs -i cp --parents {} /home/usrname/gsl/

复制特定文件及其相关文件结构

假设当前Linux系统的目录/tmp/gsl下的文件结构如下图所示,其中有很多Makefile分散在各个子目录下(但并非每个子目录都存在Makefile,例如block目录下就没有)。

.
|-- INSTALL
|-- Makefile
|-- NEWS
|-- ...
|-- blas
|   |-- ChangeLog
|   |-- Makefile
|   |-- TODO
|   |-- blas.c
|   |-- ...
|-- block
|   |-- ChangeLog
|   |-- block.c
|-- bspline
|   |-- Makefile
|   |-- ...
|-- ...

要求将这些Makefile拷贝到目录/home/usrname/gsl的对应子目录下,使得拷贝后/home/usrname/gsl下的文件如下图所示:

.
|-- Makefile
|-- blas
|   |-- Makefile
|-- bspline
|   |-- Makefile
|-- ...

问题一:
假设/home/usrname/gsl下已经存在blas,bspline等子目录,如何实现?
问题二:
假设/home/usrname/gsl下不存在blas,bspline等子目录,如何实现?

问题1的答案

cd /tmp/gsl
# 使用 `find` 找出Makefile的路径;
# 使用 `sed` 替换路径:
# ':' 作为替换分割符, '&' 代表整个匹配度字符串-- i.e. 整个文件路径, '\1' 代表'()'匹配内容 -- i.e. Makefile路径前缀;
# 最后使用 `bash` 来执行复制.
find . -name 'Makefile' | sed 's:\(.*\)/Makefile:cp & /home/username/gsl/\1 :' | bash -s 

问题1 & 2的答案

使用 --parents flag 来创建需要的父目录.

--parents
Form the name of each destination file by appending to the target
directory a slash and the specified name of the source file. The
last argument given to cp must be the name of an existing
directory. For example, the command:
cp --parents a/b/c existing_dir
copies the file a/b/c to existing_dir/a/b/c, creating any
missing intermediate directories.

cd /tmp/gsl/
# `xargs -i` 就像对Makefile的文件路径集合进行循环操作, 而`{}`就像循环变量,代表每一个不同的路径
find .  -name 'Makefile' | xargs -i cp --parents {} /home/usrname/gsl/

Ref

Written with StackEdit.

评论

此博客中的热门博文

Spring Boot: Customize Environment

Spring Boot: Customize Environment Environment variable is a very commonly used feature in daily programming: used in init script used in startup configuration used by logging etc In Spring Boot, all environment variables are a part of properties in Spring context and managed by Environment abstraction. Because Spring Boot can handle the parse of configuration files, when we want to implement a project which uses yml file as a separate config file, we choose the Spring Boot. The following is the problems we met when we implementing the parse of yml file and it is recorded for future reader. Bind to Class Property values can be injected directly into your beans using the @Value annotation, accessed via Spring’s Environment abstraction or bound to structured objects via @ConfigurationProperties. As the document says, there exists three ways to access properties in *.properties or *.yml : @Value : access single value Environment : can access multi...

Elasticsearch: Join and SubQuery

Elasticsearch: Join and SubQuery Tony was bothered by the recent change of search engine requirement: they want the functionality of SQL-like join in Elasticsearch! “They are crazy! How can they think like that. Didn’t they understand that Elasticsearch is kind-of NoSQL 1 in which every index should be independent and self-contained? In this way, every index can work independently and scale as they like without considering other indexes, so the performance can boost. Following this design principle, Elasticsearch has little related supports.” Tony thought, after listening their requirements. Leader notice tony’s unwillingness and said, “Maybe it is hard to do, but the requirement is reasonable. We need to search person by his friends, didn’t we? What’s more, the harder to implement, the more you can learn from it, right?” Tony thought leader’s word does make sense so he set out to do the related implementations Application-Side Join “The first implementation ...

Learn Spring Expression Language

When reading the source code of some Spring based projects, we can see some code like following: @Value( "${env}" ) private int value ; and like following: @Autowired public void configure (MovieFinder movieFinder, @ Value ("#{ systemProperties[ 'user.region' ] } ") String defaultLocale) { this.movieFinder = movieFinder; this.defaultLocale = defaultLocale; } In this way, we can inject values from different sources very conveniently, and this is the features of Spring EL. What is Spring EL? How to use this handy feature to assist our developments? Today, we are going to learn some basics of Spring EL. Features The full name of Spring EL is Spring Expression Language, which exists in form of Java string and evaluated by Spring. It supports many syntax, from simple property access to complex safe navigation – method invocation when object is not null. And the following is the feature list from Spring EL document : ...