How to create a file from directory structure

shin

I have the following directory structure:

lib- 
    |-filled
            |-A4MpFILLED.svelte
            |-A5KFILLED.svelte
            |- // more files
    |-outlined
            |-A4MpOUTLINED.svelte
            |-A5KOUTLINED.svelte
            |- // more files
    |-round
            |-A4MpROUND.svelte
            |-A5KROUND.svelte
            |- // more files
    |-sharp 
            |-Add_cardSHARP.svelte
            |-Add_homeSHARP.svelte
            |- // more files

From this structure, I'd like to create a index.js file with the following content.

export { default as A4MpFILLED } from './filled/A4MpFILLED.svelte';
export { default as A5KFILLED } from './filled/A5KFILLED.svelte';
// more lines
export { default as A4MpOUTLINED } from './outlined/A4MpOUTLINED.svelte';
export { default as A5KOUTLINED } from './outlined/A5KOUTLINED.svelte';
// more lines
export { default as A4MpROUND } from './round/A4MpROUND.svelte';
export { default as A5KROUND } from './round/A5KROUND.svelte';
// more lines
export { default as Add_cardSHARP } from './sharp/Add_cardSHARP.svelte';
export { default as Add_homeSHARP } from './sharp/Add_homeSHARP.svelte';
// more lines

All file names are unique since added the directory name at the end of file names, like A4MpFILLED, A4MpOUTLINED, etc.

How can I do this using bash?

I started with the following but I'm not sure how to proceed after this.

# list file names
find . -type f '(' -name '*.svelte' ')' > index1
# remove ./ from each line
sed 's|.*/||' index1 > index2
# create a names.txt
sed 's|.svelte||' index2 > names.txt

Please explain what your code does. I'd like to learn as well.

thanasisp

Assuming your filenames have not newlines or more dots before .svelte:

cd lib && find . -type f -name '*.svelte' | sort | awk -F'[/.]' '{
    print "export { default as " $(NF-1) " } from \047" $0 "\047;"
}' > my/path/to/index.js

or

cd lib && printf '%s\n' ./*/*.svelte | awk -F'[/.]' '{
    print "export { default as " $(NF-1) " } from \047" $0 "\047;"
}' > my/path/to/index.js

(filenames are already sorted)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Qt: How to create a directory in project file?

use tree command output to create the same directory tree structure in different place

How to run Maven from another directory (without cd to project dir)?

create_directory()与create_directories()

使用 sed 抛出 No such file or directory

How to create idendical variables in MATLAB from an array of variable names?

How to create a github repo from a template repo and rename package

How to create a framework from a C library with nested headers

How to find a directory on linux?

Directory.Delete()/ .Create()是否同步?

How to move directory into a directory with the same name?

file.create语法

wp_delete_attachment,wp_delete_file_from_directory和wp_delete_file()不会从服务器删除文件

read files from a directory to a list

bat file - change directory and npm install in that directory, then switch again

How to detect "strikethrough" style from xlsx file in R

How to remove junk characters from the file generated by script command in linux

How to Extract Album Cover Image from MP3 file?

How to pass a path to a file through a call to perl from bash?

How to get an image file from API in Symfony2

How to pass a path to a file through a call to perl from bash?

how to extract path from file location using shell

.NET Directory.Move()与File.Move()

Xcode 错误 Alamofire.framework: No such file or directory

Print line for word found in file in directory

bourne shell,如果[-e $ directory / file。$ suffix]

Python内的Django File-Directory

How to take a screenshot on Windows 7 and auto create screenshot file on desktop like Mac?

Copy a specific percentage of each file in a directory to a new file