跳至主要内容

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...

LevelDB Source Reading (1): Structure

LevelDB Source Reading (1): Structure LevelDB “is an open source on-disk key-value store.” After I read some documents, I have some basic understanding of LevelDB. So I come up with some questions about structure of LevelDB to answer when reading the source code. Structure Log File: repair/recover db A log file (*.log) stores a sequence of recent updates. Each update is appended to the current log file. The log file contents are a sequence of 32KB blocks. The only exception is that the tail of the file may contain a partial block. Block format: Each block consists of a sequence of records: block := record* trailer? record := checksum: uint32 // crc32c of type and data[] ; little-endian length: uint16 // little-endian type: uint8 // One of FULL, FIRST, MIDDLE, LAST data: uint8[length] // data is LengthPrefixedSlice with type from batch data definition in Block : data: also named `writeBatch` in levelDB // WriteBatch header has an 8-byte ...

Install `nicstat`on Linux

Introduction nicstat is to network interfaces as “iostat” is to disks, or “prstat” is to processes. It is designed as a much better version of “netstat -i”. Its differences include: Reports bytes in & out as well as packets. Normalizes these values to per-second rates. Reports on all interfaces (while iterating) Reports Utilization (rough calculation as of now) Reports Saturation (also rough) Prefixes statistics with the current time With the help from nicstat , we can identify whether distributed Java application is saturating the network by view the utilization percentage of specific interface. Download Sourceforge Download Link Build Following the guide of README.txt, we build like following: $ mv Makefile.Linux Makefile $ make mv nicstat `./nicstat.sh --bin-name` Install $ make install gcc -O3 -m32 nicstat.c -o nicstat In file included from /usr/include/features.h: 392 : 0 , from /usr/include/stdio.h: 27 , from nicsta...