2

Though probably a good opportunity for using just a file manager like the Midnight Commander, I wonder if there is an elegant way to do it from the shell:

Let's say I want to copy a file from one directory to another. The directory is huge and has long, complicated or even partially cryptic filenames. I don't know the exact name, only that it contains penguin.

  1. I first do a search find . -iname "*penguin*" | less

  2. I look at the results:

    ./l1fe_of_Penguins
    ./Fw0rld_Of_penguins
    ./FUnNy_penguin
    
  3. I want to copy the "funny penguin" file. Then I remember some relevant parts of the intricate filename and type in the shell command, like

    cp FUn*penguin /media/share
    

How could I make this faster? How can I in all-text mode get the file name from the search results right as a command line argument?

  • 1 and 2 won't be needed with a shell like zsh. You can simply type cp ./<tab><tab> use the arrow keys to explore the directory and then continue typing out a path with *s if needed, Press tab again and fill in the path. One more tab and you can view/set the command's other arguments like -r or -v as well. – Hydranix Feb 10 '17 at 21:58
  • @Hydranix but with subdirectories it will be necessary. – wolf-revo-cats Feb 10 '17 at 22:21
  • @Hydranix also if you have a directory with a lot of files and a preliminary search is necessary – wolf-revo-cats Feb 10 '17 at 22:22
  • You can parse into subdirectories as well as parent directories if need be. **/*pattern*/** is valid and will search in both directions. – Hydranix Feb 10 '17 at 23:52
3

You can use the TAB key to auto complete filesystem paths for most shells.

bash supports this and a bit more.

The most interactive shell I've come across however is zsh, with a customized configuration file like grml's zsh config and oh-my-zsh.

With zsh you can do autocomplete with globs and even complete several files at once.

Without knowning what flavor of Unix or Linux you're using, the best I can tell you is to check your software repository or compile zsh yourself.

Assuming Linux due to your mention of penguins, check with your package manager for the following:

zsh

And for advanced rc files enabling support for many cool things in the shell

grml-zsh-config
oh-my-zsh
3

I found a way to do it:

find . -iname "*penguin*" | pick | xargs -J % cp -i % /media/share
  • actually, this has problems with file names with whitespaces. In GNU xargs one can use the -d \n which solves the problem. I don't know how to do it with FreeBSD xargs, though. – wolf-revo-cats Feb 10 '17 at 12:45
0

using -exec option find can also execute a command, ie:

find . -iname "*penguin*" -exec cp {} /media/share \;

where {} is substituted by a name.

btw. man find is great! :)

-1

you can try this.

remove the echo command, once you are happy with the output

find . -iname "*penguin*" | awk '/penguin$/' | while read filename
do
    echo "cp ${filename} /media/share"
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.