Find file with extension and excluding directory


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Find file with extension and excluding directory
# 1  
Old 12-04-2018
Find file with extension and excluding directory

Hi,
I have an inquiry on how do I use the find command in Solaris Unix to find some file ends with extension : txt, err in the root directory with modified date of 30days and this find command will also need to exclude b directory and its subdirectory. All the files from the above find criteria will then move the files into c folder.

I have the below scripts but there is nothing being moved over to c folder even thought there are files existed in the root directory. Appreciate any help.
Code:
find . -name "*.txt" -o -name "*.err" -a -mtime -30 -a ! -type d -name "./b" -exec mv {} "./c" \;

# 2  
Old 12-04-2018
Code:
find ./ -type f -regex '.*\.\(txt\|err\)' ... -not -path "./b/*"


Last edited by nezabudka; 12-04-2018 at 03:29 AM..
# 3  
Old 12-04-2018
Hi nezabudka,
Note that most Solaris systems don't have the -regex, -path, and -not primaries.

Hi snowfrost88,
The find command that you're using has two groups of primaries separated by a logical OR operator (-o). The first group of primaries is just -name "*.txt". This group essentially finds all files with names ending with .txt and ignores them. The second group finds all files with names ending with .err that have been modified in the last 30 days that are not directories and that have the name ./b and moves all files that meet all of those requirements to the file named ./c (which we hope, but have not verified, is a file of type directory). Since no filename can ever contain a <slash> character, no filename can ever be selected by the -name ./b primary (and, therefore, no files will ever be moved by this command).

In addition to the problem noted above, I assume that what you're really trying to do is to move all files that have been modified within the last 30 days that either have a name ending in .txt or ending in .err that are not located in or under a directory named either b or c into the directory named c without duplicating any of the file hierarchy structure into the directory c. If that assumption is correct, you probably want something more like:
Code:
find . \( -type d \( -name b -o -name c \) -prune \) -o \( -type f -mtime -30 \( -name '*.txt' -o -name '*.err' \) -exec mv {} c/ \; \)

Note the sets of \( and \) grouping operators to alter the precedence the logical OR operators and the -prune primary to ignore all files under directories named b or c when selecting regular files with the extensions .txt and .err.

Last edited by Don Cragun; 12-04-2018 at 03:18 AM.. Reason: Add note explaining why nezabudka's suggestion won't work.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 12-04-2018
Hi Don Cragun,
I do not want to find those files in b folder and its sub directory. I have alot of sub directories in b folder . How do I exclude from b folder as the parent? I am not able to list down all the folder in the command.


find . \( -type d \( -name b -o -name c \) -prune \) -o \( -type f -mtime -30 \( -name '*.txt' -o -name '*.err' \) -exec mv {} c/ \; \)
# 5  
Old 12-04-2018
The \( -type d \( -name b -o -name c \) -prune \) in the code I suggested excludes everything in and under directories named b and c from being selected to be moved. (I exclude c because that is where you are moving all of your selected files and there is no need to move files from directory c to directory c, is there?)

Note also that if you have directories x, y, and z in the current directory and files with pathnames x/a.txt, y/a.txt, and z/a.txt that the code you have requested will effectively randomly delete two of those files and move the third to have the new pathname c/a.txt. And, in the above scenario, if there was a file with the pathname c/a.txt before the code you requested is run, that file will also be overwritten (i.e., destroyed).
These 2 Users Gave Thanks to Don Cragun For This Post:
# 6  
Old 12-04-2018
For testing put an echo before the mv command!
The -o is a logical OR, read "otherwise".
The -a is default; you can put it for clarity (if there is no -o of course): find . -type f -a -print is identical with find . -type f -print
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find/searching files in subdirectories excluding the fiels in Parent Directory

Hi All, requirement is to find and remove the files from sub directories but it should exclude the files from parent directory. At present i am using the below one but it finds and remove files from both parent and sub directories. find ${PATH} -type f \( -name securitas\* -o -name \*gz... (1 Reply)
Discussion started by: Naveenkk
1 Replies

2. UNIX for Advanced & Expert Users

Find all files in the current directory excluding hidden files and directories

Find all files in the current directory only excluding hidden directories and files. For the below command, though it's not deleting hidden files.. it is traversing through the hidden directories and listing normal which should be avoided. `find . \( ! -name ".*" -prune \) -mtime +${n_days}... (7 Replies)
Discussion started by: ksailesh1
7 Replies

3. Shell Programming and Scripting

Find command to search files in a directory excluding subdirectories

Hi Forum, I am using the below command to find files older than x days in a directory excluding subdirectories. From the previous forums I got to know that prune command helps us not to descend in subdirectories. Though I am using it here, not getting the desired result. cd $dir... (8 Replies)
Discussion started by: jhilmil
8 Replies

4. UNIX for Dummies Questions & Answers

Finding new file, but excluding directory..

hi, I need to find files that have been created less than 3 days ago. However, I need to only search specific directories. I've searched about the net and found some useful commands such as : find . -type d -name 'dir_to_exclude' -prune -o -print -mtime -3 however I cannot get it... (2 Replies)
Discussion started by: horhif
2 Replies

5. Shell Programming and Scripting

Find all files for a user, excluding a directory

I have been searching, and cannot find an answer for this. I am trying to find all files for a user, lets call him (test001), and I want to exclude a specific directory. Here is the command I run, it finds all files: find / -user test001 I get this result: > find / -user test001 ... (4 Replies)
Discussion started by: steve2x4
4 Replies

6. UNIX for Dummies Questions & Answers

Need help in excluding a particular directory using Find commad

Hi, I have a directory structure as below /home/gad/Merl/a/a1.txt /home/gad/Merl/b/a1.txt /home/gad/Merl/c/a1.txt How can I find the file a1.txt but not from directory 'a' and it(the filw) should loaded 6 days ago.. Can any one pls help,quick reply much appriciated.. Thanks. (1 Reply)
Discussion started by: jagadish_gaddam
1 Replies

7. Solaris

how to find out the file's name excluding string?

Hello, Under one directory, I can use below command to find out the file names with string "Export terminated successfully without warnings" grep -i -l "Export terminated successfully without warnings" *.* My question is : how I find out the file names without including string "Export... (5 Replies)
Discussion started by: GreatJerry
5 Replies

8. UNIX for Dummies Questions & Answers

Find Files in a Directory Excluding Subdirectories

Hi, I have a filename Location.txt in a directory /abc. Similar name file is present in its subdirectory /abc/xyz. I want to find the file which is present only in /abc and not in /abc/xyz. Please any1 of u can provide a quick suggestion. Its very urgent. Thanks, Amol (2 Replies)
Discussion started by: Amol_Dicholkar
2 Replies

9. UNIX for Advanced & Expert Users

find excluding a directory and a file

Hi I have some 5 folders and two files in the current directory. I want to delete all, expect one folder(files in the folder too should not be deleted) and a file in the current directory. Lets say the folder and file that should not be deleted as 'a'(folder name) and 'b'(file name). Can you... (1 Reply)
Discussion started by: ammu
1 Replies

10. UNIX for Dummies Questions & Answers

Get the name of the file excluding the extension

How to get the name of the file by excluding the extention of file name? For example, my filename is 'test.txt'. I want to get only the name 'test' but not the extention .txt. (2 Replies)
Discussion started by: vinay123
2 Replies
Login or Register to Ask a Question