Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

linux - Basic Bash Function to find and copy files based on filetype

I use the little bit of code below to find files in a folder with a given filetype and then copy them to a different folder.

find ./ -name '*.chk' -exec cp -prv '{}' "./" ';'

I tried to change this into a function in my .bash_profile so I could use it more quickly, but for some reason, it's not working. By not working, I mean nothing seems to happen when I execute "mymove .txt folder". Bash just goes to a new line, ready to accept more input.

mymove() {
        find ./ -name "$1" -exec cp -prv '{}' "$2" ';';
}

Any advice?

question from:https://stackoverflow.com/questions/65901490/basic-bash-function-to-find-and-copy-files-based-on-filetype

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You mention that you execute mymove with two parameters, .txt and folder:

$ mymove .txt folder

This will not work as .txt doesn't match any files, change it to:

$ mymove '*.txt' folder

Note the quotes, you do not want to shell to expand *.txt.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...