copy all files that have no extension
10,492
Solution 1
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
).
Solution 2
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
Solution 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
Related videos on Youtube
Author by
Hossein Nasr
Updated on September 18, 2022Comments
-
Hossein Nasr almost 2 years
We can copy some file by extensions like this:
cp *.txt ../new/
but how can I copy all files that have no extension?
-
amisax almost 9 yearsthe 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
-
Jerb almost 9 yearsThe pattern
*.*
matchesmy.file.txt
, so presumably the file would not be copied. -
Hossein Nasr over 8 yearswhy do you use
--
after cp? i do that without--
and it works. btw, what is usage of--
? -
Abel Cheung over 8 years@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.