find a file using find command only in particular directories even though that file is present in other directories - linux

I have a file named pqr.txt This file, is present in many of the subdirectories, but i want to find only the ones whose path is ../../whatever/whatever/pqr/pqr.txt
I am doing this operation with find command using name option but how can i add this constarint that it has to find pqr.txt only in pqr directories?
Also i am passing name parameter as argument from command-line.
find . -name pqr.txt

If you want to find only the pqr.txt that are in a directory named pqr,
you can use the -path option of find:
find . -path '*/pqr/pqr.txt'

Related

copy/move same name multiple files in different folder

I have multiple files in different folders with the same name and extension. For example: There are 460 folders and each folder has one file with the name of snps.vcf. I want to copy/move these files to one folder and later on, I will do some analysis that I need to do.
I have tried:
find -type f -name "*.vcf" -exec cp {} /home/AWAN/try';'
but this code overwrites the files and only one file remains there in the end.
I have tried rename but I don't know how to select multiple files by find command then rename. Even with the mmv I couldn't find the possible solution.
You need to write an external script and pass it to -exec.
Your script may use mktemp to generate a random file name. Example:
mktemp /your/directory/try-XXX
The XXX part will be replaced by mktemp with a different value for each call.

Linux "find" returns all files

A few days ago I was reading about the Linux find tool and based on that I issued the following command to see if I have the Python.h file:
find . 'Python.h'
The problem is that all files in current dir and subdirs are returned. Shouldn't I get what I'm looking for?
You left out the parameter specifier -name:
find ./ -name 'Python.h'
find will recurse through all directories in the current directory. If you just want to see whether you have a file in the current directory, use ls:
ls Python.h
Use -name switch:
find . -name 'Python.h'
Otherwise it takes the name as location to look at.

linux find not working from other mount points

This works:
:/# find . -name *gedit*
It finds all given files on local disk as well as a backup disk mounted on /media/backup/root
However, change directory to /media/backup/root/ and the same command does not work:
:/media/backup/root# find . -name *gedit*
It finds nothing.
Why?
You need quotes around your search term
:/media/backup/root# find . -name "*gedit*"
The first parameter to find (in this case .) tells find where it should start searching.
So when your current directory is / (The root of the file system) the whole system gets searched. Whereas when your current directory is /some/other/path only this path downward is searched.
If you want to always search the whole file system use
find / -name "*gedit*"
The quotes are important, they prevent the shell from expanding the *.

use ls command to find some *.app files on macos

There are some .app files in the folder, such as
folder_A/1.app
/2.app
/subF/3.app
/3.txt
Then i want to use ls command to check if there are any .app files under folder_A, i can use ls -R folder_A to list all the files under "folder_A" and sub folder "subF", but on Macos, the app file is also considered as an directory that ls will list all the files contained in 1.app,2.app and so on.
For example, 1.app contains some .png,.txt; then ls -R folder_A will return all the png and txt files, not the 1.app itself. But i want to list all the app files under folder_A and its sub folder without list all the files included in .app.
The trick is to use the right tool for the job.
find folder_A -name '*.app'
The find command is better suited for traversing a directory hierarchy.
find folder_A -name '*.app'
Find can be easily used to search on specific files
find folder_name -name "*.app" -print
folder_name can be an absolute path or a relative path.

Newbie: linux command

Two questions to ask:
1. I am using scp command to copy a file from a remote server, I am wondering how can I specify the place where to paste the copied file on my local computer?
for example, if I wanna copy a test.txt file from a remote server and paste it on my local computer under /home/myname/tmp/ what is the proper command?
is it
scp SERVER_ADDRESS /home/myname/tmp/
2. If I want to search a file whose name contain text of "test" , what is the command I should use? I mean search for any file with name test , ('_' is a wildcard)
--------------------------- update ------------------------
what is the difference between "find" and "grep"?
1:
scp SERVER_ADDRESS:/path/to/remote/file.txt /path/to/local/file.txt
2:
find . -name "*test*"
This will search for files/directories containing "test" anywhere in the filename. The search will start from the current directory . To search in another path, use find /path/ -name "*test*". If you only want to search in files, that is, exclude directories, then add -type f before the -name option.
First man scp is your friend (as are all man pages in general).
Yes: in full, that'd be like scp server:/path/to/file.txt /local/path/.
Your main options here are:
locate test (if you have locate installed and its database is up to date)
-or-
find /path/name -name '*test*' to find any named files inside the /path/name directory and all its children.

Resources