Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


Closed Thread
  Search this Thread
Old 09-21-2008, 07:06 PM   #1
Registered: Sep 2008
Location: USA
Distribution: Linux Mint Qiana
Posts: 190

Rep: Reputation: Disabled
getting files except those with certain extension


I have a root directory which has a big number of other subdirectories and contains a big number of files. I want to copy all these files and directories to another folder (keeping the same hierarchy) except those files with a certain extension, say .txt, files - how may I do this?


Old 09-21-2008, 07:20 PM   #2
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
Placing an exclamation mark before a find condition reverses its logic -e.g.:

find / ! -name "*.txt" ! -name "*.doc"

... will return the filepaths of all files whose names do not end with .doc or .txt - this provides a list which can then be used in a for loop:

#note that those are back-ticks round find statement, not single quotes
for file in `find / ! -name "*.txt" ! -name "*.doc"` 
    cp $file ${newdir}$file

Last edited by Robhogg; 09-21-2008 at 07:36 PM.
Old 09-21-2008, 07:37 PM   #3
LQ Veteran
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1233Reputation: 1233Reputation: 1233Reputation: 1233Reputation: 1233Reputation: 1233Reputation: 1233Reputation: 1233Reputation: 1233
find / ! -name '*.txt' ! -name '*.doc'
Old 09-21-2008, 07:40 PM   #4
LQ Guru
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Please dont double post - especially when your question has already been answered

Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
remove all files with same extension? scratchnz Linux - General 1 11-04-2005 04:40 AM
files with extension rar ankscorek Linux - Software 5 07-25-2005 10:41 PM
Extension files BC1 Linux - Software 5 12-20-2004 04:41 AM
What to do about these files with .new extension? Slovak Slackware 1 11-23-2004 05:50 PM
How program to read files with extension .dat y .cfg files COMTRADE in fedora 1? ivonne Linux - Software 0 11-22-2004 12:42 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:36 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration