5

We can copy some file by extensions like this:

cp *.txt ../new/

but how can I copy all files that have no extension?

  • What other things do the files have in common besides having no extension? – eyoung100 Jul 3 '15 at 1:58
  • Nothing. for example files are like this : a, a.txt, b, b.txt ... – Hossein Nasr Jul 3 '15 at 2:00
15

The answer from @ubaid-ashraf is almost there. The way to specify file with no extension, in ksh would be:

cp -- !(*.*) /new/path/

so that any file with dot in file name is skipped.

For that to work in bash, you need to enable the extglob option (shopt -s extglob) and the kshglob option in zsh (set -o kshglob).

  • the problem with this (and @ubaid-ashraf 's solution) is that it will also move directories , since most directories will not have any extensions. My solution would ensure that there are no directories moved – amisax Jul 3 '15 at 4:49
  • 2
    No it would not move directories. cp would skip directories unless used with -r or -R option. – Abel Cheung Jul 3 '15 at 4:56
  • And what if I have a file called my.file.txt? – Lambert Jul 3 '15 at 7:36
  • 1
    why do you use -- after cp? i do that without -- and it works. btw, what is usage of --? – Hossein Nasr Oct 31 '15 at 16:37
  • 2
    @hoosssein Double dash prevents anything that follows to be treated as command line options. If there are odd file names like "-r" or "-s", double dash can help guarding against potential disaster. – Abel Cheung Nov 2 '15 at 5:08
6

You can do something like:

cp -- !(*.txt) /path/to/directory

The above code will copy all the files without .txt extension. You can also give multiple extension via pipe character.

For example:

cp -- !(*.txt|*.c|*.py) /path/to/directory
3

You can use find+grep to get only files that have no extension

   find . -maxdepth 1 -type f | sed 's/^\.\///' | grep -v "\."

So your copy command will be

   cp ` find . -maxdepth 1 -type f | sed 's/^\.\///' | grep -v "\." ` destination_folder

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.