Linux find directories with files

Finding directories that contains given files?

I hope this is an interesting question.. I want to find a directory that contains all the given files .. Until now what I have done is as follows Find multiple files in unix.

find . -type f \( -name "*cache" -o -name "*xml" -o -name "*html" \) 
find . -type f -name '*.pdf' |sed 's#\(.*\)/.*#\1#' |sort -u 

Reference : http://www.unix.com/unix-for-dummies-questions-and-answers/107488-find-files-display-only-directory-list-containing-those-files.html How can I make a command that will give me a directory which contains all the given files. ( The files must be in given directory only not in sub directory .. and all the files given in a list must be present ) Want to search for WordPress theme directories

index.php style.css page.php single.php comment.php basic files required for wordpress by a wordpress theme. My goal is to find all wordpress themes present in a given linux system.

2 Answers 2

You could use find like this:

find -type d -exec sh -c '[ -f "$0"/index.php ] && [ -f "$0"/style.css ]' '<>' \; -print 

To search for more files, simply add them like && [ -f «$0″/other_file ] . The return code of sh will indicate whether all the files could be found. The name of the directory will only be printed if sh has exited successfully, i.e. when all the files have been found.

$ mkdir dir1 $ touch dir1/a $ mkdir dir2 $ touch dir2/a $ touch dir2/b $ find -type d -exec sh -c '[ -f "$0"/a ] && [ -f "$0"/b ]' '<>' \; -print ./dir2 

Here I’ve created two directories, dir1 and dir2 . dir2 contains both files, so its name is printed.

As gniourf_gniourf has mentioned in the comments (thanks), it is not necessary to use sh to do this. Instead, you can do this:

find -type d -exec test -f '<>'/a -a -f '<>'/b \; -print 

[ and test do the same thing. This approach uses -a instead of && to combine multiple separate tests, which reduces the number of processes being executed.

In response to your comment, you can add all of the directories found to an archive like this:

find -type d -exec test -f '<>'/a -a -f '<>'/b \; -print0 | tar --null -T - -cf archive.tar.bz2 

The -print0 option prints the names of each of the directories, separated by a null byte. This is useful as it prevents problems with files containing spaces in their names. The names are read by tar and added to the bzip-compressed archive. Note that some versions of find do not support the -print0 option. If your version doesn’t support it, you may be able to use -print (and remove the —null option to tar ), depending on your directory names.

Читайте также:  Set from mail linux

Источник

10 ways to use the Linux find command

Discover what you’re looking for, find misplaced data, and troubleshoot everyday problems with this handy Linux command.

Magnifying glass with blue background

The find command is one of the most useful Linux commands, especially when you’re faced with the hundreds and thousands of files and folders on a modern computer. As its name implies, find helps you find things, and not just by filename.

Whether you’re on your own computer or trying to support someone on an unfamiliar system, here are 10 ways find can help you locate important data.

[ Keep your most commonly used commands handy with the Linux commands cheat sheet. ]

1. Find a single file by name

When you know the name of a file but can’t remember where you saved it, use find to search your home directory. Use 2>/dev/null to silence permission errors (or use sudo to gain all permissions).

$ find / -name "Foo.txt" 2>/dev/null /home/seth/Documents/Foo.txt

2. Find a single file by approximate name

If you can’t remember the exact name of the file, or you’re not sure whether you capitalized any characters, you can do a partial and case-insensitive search like this:

$ find / -iname "*foo*txt" 2>/dev/null /home/seth/Documents/Foo.txt /home/seth/Documents/foo.txt /home/seth/Documents/foobar.txt

3. Find everything

The ls -R command lists the contents of a directory recursively, meaning that it doesn’t just list the target you provide for it, but also descends into every subdirectory within that target (and every subdirectory in each subdirectory, and so on.) The find command has that function too, by way of the -ls option:

$ find ~/Documents -ls 3554235 0 drwxr-xr-x [. ] 05:36 /home/seth/Documents/ 3554224 0 -rw-rw-r-- [. ] 05:36 /home/seth/Documents/Foo 3766411 0 -rw-rw-r-- [. ] 05:36 /home/seth/Documents/Foo/foo.txt 3766416 0 -rw-rw-r-- [. ] 05:36 /home/seth/Documents/Foo/foobar.txt

IT Automation ebook

Notice that I don’t use 2>/dev/null in this instance because I’m only listing the contents of a file path within my home directory, so I don’t anticipate permission errors.

Читайте также:  Linux load elf file

4. Find by content

A find command doesn’t have to perform just one task. In fact, one of the options in find enables you to execute a different command on whatever results find returns. This can be especially useful when you need to search for a file by content rather than by name, or you need to search by both.

$ find ~/Documents/ -name "*txt" -exec grep -Hi penguin <> \; /home/seth/Documents/Foo.txt:I like penguins. /home/seth/Documents/foo.txt:Penguins are fun.

5. Find files by type

You can display files, directories, symlinks, named pipes, sockets, and more using the -type option.

$ find ~ -type f /home/seth/.bash_logout /home/seth/.bash_profile /home/seth/.bashrc /home/seth/.emacs /home/seth/.local/share/keyrings/login.keyring /home/seth/.local/share/keyrings/user.keystore /home/seth/.local/share/gnome-shell/gnome-overrides-migrated [. ]

As long as you’re using the GNU version of find , you can include multiple file types in your search results:

$ find ~ -type f,l -name "notebook*" /home/seth/notebook.org /home/seth/Documents/notebook-alias.org

6. List just directories

A shortcoming of the ls command is that you can’t filter its results by file type, so it can be noisy if you only want a listing of directories in a path. The find command combined with the -type d option is a better choice:

$ find ~/Public -type d find ~/Public/ -type d /home/seth/Public/ /home/seth/Public/example.com /home/seth/Public/example.com/www /home/seth/Public/example.com/www/img /home/seth/Public/example.com/www/font /home/seth/Public/example.com/www/style

7. Limit listing results

With hundreds of files in a default user directory and thousands more outside of that, sometimes you get more results from find than you want. You can limit the depth of searches with the -maxdepth option, followed by the number of directories you want find to descend into after the starting point:

$ find ~/Public/ -maxdepth 1 -type d /home/seth/Public/ /home/seth/Public/example.com

8. Find empty files

Sometimes it’s helpful to discover empty files as a way to declutter:

$ find ~ -type f -empty random.idea.txt

Great Linux resources

Technically, you can use find to remove empty files, but programmatic removal of files is dangerous. For instance, if you forget to include -type f in a search for empty files, you get directories in your results. By adding a delete flag, you would remove potentially important directory structures.

It’s vital to compose your find command and then verify the results before deleting. Furthermore, a misplaced delete flag in find can delete results before qualifying them (in other words, you can delete directories in a command intended to delete only files by placing the delete flag before the type flag).

Читайте также:  Astra linux создать пользователя командная строка

I prefer to use xargs or Parallel and a trash command on the rare occasion that I remove files with find .

9. Find files by age

The -mtime option allows you to limit a search to files older than, but also files newer than, some value times 24.

$ find /var/log -iname "*~" -o -iname "*log*" -mtime +30

The + before the -mtime number doesn’t mean to add that number to the time. It’s a conditional statement that matches (in this example) a value greater than 24 times 30. In other words, the sample code finds log files that haven’t been modified in a month or more.

To find log files modified within the past week, you can use the — conditional:

$ find /var/log -iname "*~" -o -iname "*log*" -mtime -7 /var/log/tallylog /var/log/cups/error_log /var/log/cups/access_log /var/log/cups/page_log /var/log/anaconda/anaconda.log /var/log/anaconda/syslog /var/log/anaconda/X.log [. ]

You already know about the -ls flag, so you can combine that with these commands for clarity:

$ find /var/log -iname "*~" -o -iname "*log*" -mtime -7 -ls -rw------- 1 root root 0 Jun 9 18:20 /var/log/tallylog -rw------- 1 root lp 332 Aug 11 15:05 /var/log/cups/error_log -rw------- 1 root lp 332 Aug 11 15:05 /var/log/cups/access_log -rw------- 1 root lp 332 Aug 11 15:05 /var/log/cups/page_log -rw------- 1 root root 53733 Jun 9 18:24 /var/log/anaconda/anaconda.log -rw------- 1 root root 835513 Jun 9 18:24 /var/log/anaconda/syslog -rw------- 1 root root 21131 Jun 9 18:24 /var/log/anaconda/X.log [. ]

10. Search a path

Sometimes you know the directory structure leading up to a file you need; you just don’t know where the directory structure is located within the system. To search within a path string, you can use the -ipath option, which treats dots and slashes not as regex characters but as dots and slashes.

$ find / -type d -name 'img' -ipath "*public_html/example.com*" 2>/dev/null /home/tux/Public/public_html/example.com/font

Found it

The find command is an essential tool for a sysadmin. It’s useful when investigating or getting to know a new system, finding misplaced data, and troubleshooting everyday problems. But it’s also just a convenience tool.

[ Download the Linux find cheat sheet to keep all these shortcuts in one place. ]

You don’t need a «good» reason to use find . Using find makes it easier to search for something instead of traversing your system. It’s an understated but infinitely useful tool that embodies the sublime pleasure of everyday Linux. Start using it today, and find out what makes it great.

Источник

Оцените статью
Adblock
detector