linux find command is not working properly - linux

I am using Linux(Ubuntu), I am trying to find the files, but it is not working properly.
I have created some files in my directory structure, for example: World/India/Maharashtra/Pune/filename.xml
When I use the find command like:
find /home/lokesh/Desktop/Testing_India2/Test/World/India/Maharashtra/ -name filename*.xml -mmin -3000
It is giving the result perfectly.
But, when I am using the same command at "World" or "India" level:
find /home/lokesh/Desktop/Testing_India2/Test/World/ -name filename*.xml -mmin -3000
it does not give any result.
I have lots of directories at "India" level as well as at "Maharashtra" level and may be some directories within "Maharashtra's" inner directories. I have to find each file created in all directories.
And I have mounted all folders from different machine.(I mean some state from different and some from different machine.)
If someone knows how to solve this problem please reply me as soon as possible.

Double quote your search string and -L to make it follow symbolic links:
find -L /home/lokesh/Desktop/Testing_India2/Test/World/ -name "filename*.xml" -mmin -30000

This is something I ran into earlier today actually when using the '*' wildcard. I couldn't get it to continually traverse the subdirectories unless I escaped the * with a .
Give this a try:
find -L /home/lokesh/Desktop/Testing_India2/Test/World/ -name filename\*.xml -mmin -30000

Yes, as mentioned you have to double qoute your -name argument or use a backslash prior to the *. The reason for it not working from one directory, but working fine in other directories, is that the * character is used for filename generation by your shell. This of course happens before the find command is executed. Therefore, if you have a file that match the filename*.xml pattern in your current directory it will be substituted before find is executed, which is not what you want. On the other hand, if there is no pattern match in the current directory, the * character is passed on to the find command unmodified. By qouting you protect the string from shell filename generation.
Regards

Related

How to traverse dirs in linux and cd into a specific dir

I have a file structure as following:
/home/myhome/me/staging/15/1234/my_stats/
/home/myhome/me/staging/16/5678/my_stats/
/home/myhome/me/staging/17/7890/my_stats/
/home/myhome/me/staging/18/3456/my_stats/
I need to travel to the dir "my_stats" and execute query to find files in my cmd. There are multiple dirs in "staging" and I need to go into every one of them and check if 'my_stats' dir exists. If it exists, then I need to run a cmd query in "my_stats" dir.
The dir structure will always be in the following format:
/home/myhome/me/staging/<2 digit name>/<4 digit name>/my_stats/
I have tried iterating through the structure using a nested for loop and checking all dirs in 'staging' which is proving to be slow. Is there a way to using the 'find' command with 'depth' to do the same?
Or can we implement this with pattern matching ?
Appreciate the help. Thanks!
found the answer!
we can use * for it.
/home/myhome/me/staging/*/**/my_stats/*
Will try to find a better solution which can maybe use len of dir to better differentiate it
Try this one
find . -type f -path "./[0-9][0-9]/[0-9][0-9][0-9][0-9]/my_stats/*"
can replace the . with your own path.

Find and move files based on filenames in txt file oneline

I'm sure I had a working oneliner that allowed me to search a directory (or .) for files containing names matching names in a txt file and copying these to a new directory.
Somehow I cannot get it to work - any help please.
Sorry if this is a duplicate - I have really searched for an answer (here and elsewhere), but cannot find a solution.
foo/movehere/sample.txt file:
141516
141619
Files I want to find and move i.e.:
foo/folder/folder2/141516_S2_R1.fastq.gz
foo/folder/folder2/141516_S2_R1.fastq.gz
Where I want to move them:
foo/movehere/
my current (nonfunctioning) oneliner:
while read -r FILE; do find . -name "$FILE*.fastq.gz" -type f -exec cp {} /foo/movehere/ \;;done </foo/movehere/sample.txt
There are some errors in the oneliner. It still does not work.
you can use eval in your code
SEARCH="-name '$FILE*.fastq.gz'"
eval "find . $SEARCH -type f exec cp '{}' /foo/movehere/ \";
security note : do not put user supplied data into eval.
Not sure if I should delete the post - but I'll leave my solution here if anyone else encounter the exact same problem.
Still not 100% sure I understand why it failed, but I got the oneliner working by copying all the sample names from the txt to a unedited file with no suffix.
I guess some (hidden) "\r" editing in the txt file messed up the "$FILE" so that it searched for something like this:
151617*fastq.gz\r
Perhaps someone with a better understanding of terminal scripts may confirm this.
EDIT 190128: happened across my old question, and just in case anyone struggle with something similar, make sure you have UNIX or similar line shifts, my txt files had weird window line shifts.

Find command with quotation marks results in "no such file"

In my directory there are the files:
file1.txt fix.log fixRRRRRR.log fixXXXX.log output.txt
In order to understand the find command, I tried a lot of stuff among other things I wanted to use 2 wildcards. Target was to find files that start with an f and have an extension starting with an l.
$ find . f*.l*
./file1.txt
./fix.log
./fixRRRRRR.log
./output.txt
./fixXXXX.log
fix.log
fixRRRRRR.log
fixXXXX.log
I read in a forum answer to use quotation marks with find find . "f*.l*" with the result: `
./file1.txt
./fix.log
./fixRRRRRR.log
./output.txt
./fixXXXX.log
It results in find: ‘f*.l*’: No such file or directory
What am I doing wrong, where is my error in reasoning?
Thanks for an answer.
find doesn't work like that. In general find's call form looks like:
find [entry1] [entry2] ... [expressions ...]
Where an entry is a starting point where find starts the search for files.
In your case, you haven't actually supplied any expressions.
In the first command (without quotes), the shell expands the wildcards to a list of matching files (in the current directory), then passes the list to find as arguments. So find . f*.l* is essentially equivalent to find . fix.log fixRRRRRR.log fixXXXX.log. As a result, find treats all of those arguments as directories/files to search (not patterns to search for), and lists all files under ., (everything) then all files under fix.log (it's not a directory, so that's just the file itself), then all files under fixRRRRRR.log and finally all files under fixXXXX.log.
In the second one (with quotes) it searches for all files beneath the current directory (.) and tries the same for the file literally called "f*.l*".
Actually you are likely seeking for the "-name" expression, which may be used like this:
find . -name "f*.l*"

Find files using wildcard in directory using Linux

So I have a directory that has a number of files. I'm wondering what I need to find files within that directory. My files have a naming convention and then a last name. I'd like to find by the naming convention.
/MyDir/MySubDir/2016_01_randomLastNameABC
/MyDir/MySubDir/2016_01_randomLastNameDEF
/MyDir/MySubDir/2016_01_randomLastNameGHD
/MyDir/MySubDir/2016_01_randomLastNameDGD
find "/MyDir/MySubDir/2016_01_*"
I keep getting an error that says:
"linux file names usually don't contain slashes"
It must not like the fact that I'm trying to search a directory
Try
find ./MyDir/MySubdir/ -name "2016_01_*"
find /MyDir/MySubDir -name "2016_01_*"
This will do
find /MyDir/MySubDir/ -name "2016_01_*"
test,
~$ find ./market/public/static/ -name *.js
./market/public/static/view3dview.js
./market/public/static/ng-file-upload.min.js
./market/public/static/codemirror-min.js
./market/public/static/model_upload_app.js

BASH Linux Run code for all file extensions ".fal"

I am not a Linux user, so bash and shell are new for me.
I need a code that runs 2 scripts for all file extensions ".fal" that are located in the folder(and sub-folders preferably) that I run the code in.
E.g:
dos2unixfortxtandfal """""""that code runs for all files in the folder already
and
for all ".fal" files in this folder,
Do
eine_fal_macher (here the .fal files 1 by one) Versuch.txt
Done
eine_fal_marcher --> this is the script that runs in the moment only once
(here the .fal files 1 by one) --> this is input file 1
Versuch.txt--> this is input file 2 (same for all) (from the same
folder)
In the end I want to do the following in the terminal:
frdc09927:\Frdc09927\z183464\DOE_Wellen\21a>
frdc09927:\Frdc09927\z183464\DOE_Wellen\21a>script.bash --> Enter
frdc09927:\Frdc09927\z183464\DOE_Wellen\21b>script.bash --> Enter
frdc09927:\Frdc09927\z183464\DOE_Wellen\21c>script.bash --> Enter
find . -name \*.fal -exec eine_fal_macher {} Versuch.txt \;
This runs for all *.fal files in the current directory and its subdirectories. Use -maxdepth 1 as first option to limit it to the current directory only, or give a different working directory than . to have find search somewhere else. {} is replaced with the "found" filename, honoring things like spaces in the filename automatically.
I could start explaining find at this point, but you should really rather have a look at man find instead. This tool is extremely powerful, and can reduce rather complex problems (like acting on the age of files, their owners etc.) to a one-liner.
Try something like this:
for i in `ls *.fal`; do command1 $i && command2 $i; done
command2 is only executed for a specific file if command1 does not return an errorcode
I'm not sure I fully understand the requirement, but here goes (trying to follow your pseudo code):
for FILE in `find . -name "*.fal"`
do
eine_fal_macher "${FILE}" Versuch.txt
done

Resources