Find a file and then copy to tmp folder

for file in `ls | grep -i 'mumbai|pune|delhi'` ; do cp $file /tmp/ ; done 
Select only the files with given name and copy them to /tmp/ folder.

-1
By: shantanuo
2020-02-09 06:56:45

What Others Think

This logic can be used in various cases like... for file in `ls *.bz2 | grep -i 'mumbai|pune|delhi'` ; do bunzip2 $file ; done
shantanuo · 84 weeks and 6 days ago
This logic can be used in various cases like... for file in `ls *.bz2 | grep -i 'mumbai|pune|delhi'` ; do bunzip2 $file ; done
shantanuo · 84 weeks and 6 days ago
You should never parse ls output. It is basically a bad idea. Linux and Unix allow almost any character in the file name. That would cause problems, and in certain cases can lead to data loss. See full explanation here. http://mywiki.wooledge.org/ParsingLs
researcher7 · 84 weeks and 6 days ago
This is much safer. Change to what pattern in the name you are looking for. find ./ -maxdepth 1 -regextype posix-extended -regex '.*(test|something|searchpattern).*\.txt' -exec cp {} /tmp/ \;
researcher7 · 84 weeks and 6 days ago
This is much safer. Change to what pattern in the name you are looking for. find ./ -maxdepth 1 -regextype posix-extended -regex '.*(test|something|searchpattern).*\.txt' -exec cp {} /tmp/ \;
researcher7 · 84 weeks and 6 days ago
Even safer version. This will prevent accidents if file names have unusual characters in them. find ./ -maxdepth 1 -regextype posix-extended -regex '.*(test|something|searchpattern).*\.txt' -type f -type f -print0 -exec cp {} /tmp/ \;
researcher7 · 84 weeks and 6 days ago
thanks for this bestlifecoachhouston.com
looneyhans · 43 weeks and 4 days ago
i should really be doing this too.. mold remediation thanks for sharing!
idasanka · 39 weeks ago
kitchen remodeling fresno Wow! I did not know you can use it that way! This is interesting
yeoyeo · 37 weeks and 2 days ago
love your ideas! stairs
sammm · 34 weeks and 1 day ago
Thank you so much green bay fencing companies
geebranz · 19 weeks and 3 days ago
My neighbor and I had been just debating this specific topic, he is normally seeking to prove me incorrect. Your view on this is outstanding and exactly how I truly think. I just now mailed him this blog to show him your point of view. After looking over your website I bookmarked and will be coming back to learn your new posts! caluanie muelear oxidize for sale
robinjack · 6 weeks and 6 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: