ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Sometimes, we need to do repeated work for copy or mkdir directories. I am thinking is that possible to redirect the output of awk to input as command to the shell?
For example I have several files in different directories:
What I want to do is to copy all these files to a conman directories.
Can I do something like
Code:
awk 'END{for(i=1;i<=n;i++) getline < print "~/workingdir" i "/file1.ppt" " ~/commondir"}'
Similar to this, I would expect a general way to do this kind of repeating commanding with rm, mkdir, mv , etc, or even more complicated commanding in a simple one liner command.
I know I can write shell script to do this. But I am wondering if there are some easier ways with awk sinse with awk I just need one line and I do not need to generate a file then type chmod +x file, ./file. Or there are some other simple ways for the similar purpose? How to do it? Thanks for your kind help.
That's so simple! Thanks a lot for your quick and helpful answer!. Let's still keep this question open at this moment to see if some others come up with any different ideas.
Quote:
Originally Posted by firstfire
Hi.
You definitely need to look at shell globbing.
To copy files just do
Code:
cp ~/workingdir*/file*.ppt ~/commondir
Read about "Pattern Matching" and "Brace Expansion" in `man bash'.
Sometimes, we need to do repeated work for copy or mkdir directories. I am thinking is that possible to redirect the output of awk to input as command to the shell?
For example I have several files in different directories:
What I want to do is to copy all these files to a conman directories.
Can I do something like
Code:
awk 'END{for(i=1;i<=n;i++) getline < print "~/workingdir" i "/file1.ppt" " ~/commondir"}'
Similar to this, I would expect a general way to do this kind of repeating commanding with rm, mkdir, mv , etc, or even more complicated commanding in a simple one liner command.
I know I can write shell script to do this. But I am wondering if there are some easier ways with awk sinse with awk I just need one line and I do not need to generate a file then type chmod +x file, ./file. Or there are some other simple ways for the similar purpose? How to do it? Thanks for your kind help.
To answer your question: sure!
Code:
$ ls
test.png
$ echo | awk '{print "cp test.png test2.png"}'|bash
$ ls
test.png test2.png
If you have a list of files, that you want to copy or move to a target directory, look at the -t option of cp & mv.
You can pipe the list through "tr" to replace new lines with NULs and pipe the result to xargs. This will help with filenames containing "evil" characters. Also xargs can help where file globbing would result in out of memory errors, or when using the output of the find command.
Thank you so much guys! Thanks a lot for all the kind support and contributions! Hope this thread can help others who has the same question and help us learn more about shell command.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.