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 · 8 weeks and 2 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 · 8 weeks and 2 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 · 8 weeks and 2 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 · 8 weeks and 2 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 · 8 weeks and 2 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 · 8 weeks and 2 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: