Reputation: 87
For example, I have a folder new_folder
and I have a lot of folders and files in this folder. I want to list all the files that have math
in their file names. So files like maths.txt
, text.mathabc
would satisfy the requirement, and these files may be in some sub-folders of the new_folder
. Is there a command that I can use to perform this action under new_folder
as the current directory?
Upvotes: 1
Views: 4524
Reputation: 41
I think you should use grep
ls -1 | grep "math"
Or you can use find
Upvotes: 0
Reputation: 2539
find
might be your friend here.
find . -iname "*math*" -type f -ls
This should search the current folder (".") and all sub-folders for files ("-type f") that have the string "math" in the name in a case insensitive way. It will finally print out an ls -l
style list of the files found.
Upvotes: 4
Reputation: 125708
It depends on whether you're actually in the new directory or not (e.g. if you moved there with cd
), and whether you want to just list them, or do something with them. If you're in the directory and just want to list them, use:
ls *math*
If you're not in the directory, you need to specify the path from where you are to the new folder, something like:
ls new_folder/*math*
Note that this will list the entire path for each file, not just the filenames.
If you want to do something with them, do not use ls
. If the command can operate directly on a bunch of files, use the pattern directly with it, like this:
dosomethingwith *math* # If you're in the directory
dosomethingwith new_folder/*math* # If not
If you need to do something that needs files one-at-a-time, make a loop:
for file in *math*; do # Or new_folder/*math* if not in the directory
dosomethingwith "$file"
done
Upvotes: 1