I need to find files that match this pattern:
find root_folder/*/match_string/*.ext"*" means any levels of folders or files. So it means any file with an extention "ext" under root_folder or its sub folder and whose full path contains a folder called "match_string", for example:
root_folder/f1/f2/match_string/f3/f4/1.ext
root_folder/f1/f2/match_string/2.extBut the above command doesn't work. find -name also doesn't work.
And I need to output the result list of matched files to a file for later import into zip command. It seems not to be straightforward to use ">" if cascaded commands are used.
62 Answers
You can use
find /path/to/root_folder -type d -name "match_string" -exec find "{}" -type f -name "*.ext" \; > ~/file_listThe command will search for all folders named match_string, then search for all files which names end with .ext in them and their subfolders and list all found files with their absolute pathes. The list will be stored in ~/file_list.
If you use
cd /path/to/root_folder
find -type d -name "match_string" -exec find "{}" -type f -name "*.ext" \; > ~/file_listthe files will be listed with relative pathes to the current directory, but the name of the current directory (which is path/to/root_folder) will not be displayed, instead ./ is displayed.
Save filelist to file with "starting-point" removed (man find).
pwd /opt/askubuntu/
find /opt/askubuntu/ -type f -path '*/askubuntu/temp/*' -name '*.ext' -fprintf /opt/backup/zip-archive-file.list %P\\nzip-archive-file.list
cat /opt/backup/zip-archive-file.list temp/example/a/a/a.ext temp/example/a/a.ext temp/example/c/c.ext temp/example/c/c/c.ext temp/example/b/b.ext temp/example/b/b/b.extArchive from file (man zip).
zip /opt/backup/archive -@ < /opt/backup/zip-archive-file.list adding: temp/example/a/a/a.ext (stored 0%) adding: temp/example/a/a.ext (stored 0%) adding: temp/example/c/c.ext (stored 0%) adding: temp/example/c/c/c.ext (stored 0%) adding: temp/example/b/b.ext (stored 0%) adding: temp/example/b/b/b.ext (stored 0%)Pipe find result to zip.
find /opt/askubuntu/ -type f -path '*/askubuntu/temp/*' \ -name '*.ext' -printf %P\\n | zip /opt/backup/archive -@ 2