To get only the regular files and directories but not the hidden files and directories - linux

find -type f | grep -ril 'String' ~
/home/ankit/.bash_history
/home/ankit/.filename.swo
/home/ankit/.filename.swp
/home/ankit/.perl_Eg.pl.swp
/home/ankit/.sample.swl
/home/ankit/.sample.swm
/home/ankit/.sample.swn
/home/ankit/.sample.swo
/home/ankit/.sample.swp
/home/ankit/.viminfo
/home/ankit/ankitrai
/home/ankit/array.sh
/home/ankit/array2.sh
/home/ankit/d1/text.txt
/home/ankit/direct/space
/home/ankit/filename
/home/ankit/function1.sh
/home/ankit/mail.pl
/home/ankit/perl_Eg.pl
/home/ankit/test.sh
/home/ankit/tmp
i'm using the above command to search a file with the string but it was giving the hidden files also,i dont need the hidden files and directories.

Related

How to search for all the hidden files in my computer?

I want to find all the hidden files inside a directory in linux terminal.
I have found out that we have a grep command to search for the file but I need to search for hidden files.
grep -r search *
Any help would be appreciated. Thanks.
try this on your terminal too show all the hidden files on your system:
find / -name ".*" 2> /dev/null
or you can use other way like in this web https://devconnected.com/how-to-show-hidden-files-on-linux/
Simply use (with GNU grep)
grep -r search .
if you want to search contents of files in the current directory and its subdirectories recursively.
Note: It isn't clear if you want to search filenames or contents of files.
The proper solution:
find /dir -name '.*' -type f
If by "hidden file" you mean Linux file names that begin with . that are often hidden by default, (and directories starting with . whose contents might also be considered "hidden") then try this command:
find . -print | grep '/\.'

How do I get all files with .md extension, in all subdirectories, that contain a phrase? [duplicate]

This question already has answers here:
Linux search text string from .bz2 files recursively in subdirectories
(4 answers)
Closed 6 months ago.
I have a parent folder named 'dev', and inside it are all my project folders. The ReadMe files of these projects contain the app type "type: game", for example. What I would like to do is to:
search through all subdirectories of the dev folder to find all the files with *.md" extension
then return the names of those directories which contain a .md files with containing the phrase "game"
I've tried piping find into grep like so:
find -type f -name "*.md" | grep -ril "type: game"
But it just returns the names of files from all subdirectories which contain the phrase "game" in any file.
find . -type f -name "*.md" -print0 | xargs -0 grep -il "type: game" | sed -e 's/[^\/]*$//'
This finds any files in the current directory and sub-directories with names ending with .md, then greps for files containing the string. We then use sed to trim the filename leaving only the directories containing a file ending in .md with the "type: game" inside.

Search all .htaccess files in a Linux server webroot for a word and return file paths to a text file

I have a Linux server and want to search all the .htaccess files in all the folders (public_html webroot and subfolders) that have a certain word (eg ldap) in it. I also want the file paths returned to these .htaccess files with the word in it and saved to a text file.
Can I do this with grep or find and what syntax is optimal.
I tried find . -type f -printf '"%p"\n' | xargs grep ldap > /tmp/results.txt but want to only search .htaccess files exclusively.
Thanks
Following your example, try using:
find . \( -type f -name .htaccess \) -print0 | xargs -0 grep -H ldap > /tmp/results.txt
this find will list null-terminated files .htaccess in . directory, and xargs -0 pass them to the grep. grep -H ldap will list files containing ldap string with filenames.

copying files from etc ending with digit to test1 directory

I'm new to linux and as an exercice I need to copy the "etc" files that end with a digit from home directory to the test1 directory
(with one command).
I tried this but it dosn't work
find /etc -type f -iname "*[3-9]" -exec cp {} ../test1/ \;
this should work for your home directory files ending with digit
mv `ls . |grep -Eo "^.*[0-9]$"` your-directory
lets says in the current directory you have some files like ofjweifhwef9 or kfhiofeh8 ( files ending with digit)
so ls will list them.
this grep expression "^.*[0-9]$"` will find only files ending with digit. ( because in your home directory system wont allow to have a file like this "/etc/somefile123")
and then mv will move those files to your-directory
note :- if grep cannot find the files ending with number you will see an error ofcourse because mv needs 2 operands but since it wasn't there so error.
mv: missing destination file operand after './your-directory'
It is probably because /etc is a link in the system that you're using, and find doesn't seem to consider it a path until you add an extra / at the end. Try this instead:
find /etc/ -type f -iname "*[3-9]" -exec cp {} ../test1/ \;
Notice the /etc/ instead of /etc. I get the same behavior on my Mac where /etc is a link to another directory.
Of course, also make sure that you have files which names end on a digit under the /etc/ directory tree. I have none in my mac. You should get some files when you run:
find /etc/ -type f -iname "*[3-9]"
If you don't, you don't have any files to copy. You may also try: find /etc/ to see all files under the directory tree.
Finally, you may want to add the option: -depth 1 if you only want to copy the files in the /etc/ directory, as opposed to all the files that match in the directory tree under /etc/.

How to dump list of file's from subfolders with its full path in Linux

I am not able to get the command (or group of commands)to do the following operation in Linux:
I am having a main project_folder with some 10 sub_folder containing all different extensions of files (for example *.cpp /*.txt/*.y / *.py etc ). But I want to just make a list of all REQUIRE_*.txt files from all SUB-FOLDERS with its complete path and dump into the text file .
For example :
result_dump.txt should include :
user/project_folder/sub_folder0/a0.txt
user/project_folder/sub_folder0/a6.txt
user/project_folder/sub_folder0/a11.txt
user/project_folder/sub_folder0/a12.txt
user/project_folder/sub_folder1/a1.txt
user/project_folder/sub_folder1/a13.txt
user/project_folder/sub_folder2/a14.txt
user/project_folder/sub_folder2/a15.txt
user/project_folder/sub_folder2/a2.txt
user/project_folder/sub_folder3/a3.txt
user/project_folder/sub_folder4/a4.txt
user/project_folder/sub_folder5/a5.txt
--
--
--
If I use below command ,then I am getting all files information which is not my intention:
find $(pwd) -maxdepth 1 -type f -not -path '*/\.*' | sort
Note: Please let me know how I can dump that result in text file !
Run the below command from the user's home directory
find project_folder/ -maxdepth 3 -type f -name REQUIRE_*.txt
Edit
You can then redirect the o/p using to a file adding > result_dump.txt after the command

Resources