Question:
How do I list just filenames in UNIX?
Jenni
2010-11-21 08:57:03 UTC
I need to know how to search a directory for files that contain the string 'sequentialInsert' and then just print the names of the files.
Five answers:
?
2010-11-21 09:13:35 UTC
grep -l sequentialInsert /directory/to/search/*



The -l (that's a lowercase L) lists only the files found and is a POSIX-standard flag for grep
es
2010-11-21 17:35:31 UTC
In UNIX, they say that "everything is a file"(stream) -- this means that you can "pipe" the output of one command as the input to another (general format is command1 | command2). You can chain together multiple commands this way (command1 | command2 | command3). It is one of the features which provides the great versatility of UNIX, because there are so many different ways to approach a problem.



The first commenter's answer was the simplest and most efficient (EDIT: as is the third poster's grep -l sequentialInsert /directory/to/search/* but that wasn't yet posted when I started writing this), but since you have to use grep:



You can use the output of ls as the input to a grep command, e.g.



ls | grep sequentialInsert



Or, since you have to use a different directory, you would give the path to that directory, in some form.



if the target directory is in your home directory and was named, for example, "target," I think most login shells have a $HOME variable that holds your home directory, so you could use



ls $HOME/target | grep sequentialInsert



If you are using an environmental variable like $HOME or a variable of your own, I was taught that it is generally safest to use double quotes to ensure proper evaluation although I can't remember the details, but that would be



ls "$HOME/target" | grep sequentialInsert



If you have an absolute pathname for your target directory, you can just use that, e.g.



ls /home/users/user1/target | grep sequentialInsert



One other thing, if you need to recursively check subdirectories contained in that directory, and not just the directory itself, use the recursive option of ls. I think BSD and ATT UNIX are different in this, but in the majority of the UNICES I used, it was just ls -R. Also, you can combine options together (e.g. ls -aR is the same as ls -a -R or ls -R -a or ls -Ra) so if you need to make sure to include all the hidden files (beginning with a dot), you can use both options.



ls -R pathname-to-target-directory | grep sequentialInsert

or

ls -aR pathname-to-target-directory | grep sequentialInsert



You don't need to put the asterisks in sequentialInsert because grep already picks it out of anywhere.



The first commenter also mentioned one of the best resources in UNIX: the man pages. If you are ever in a bind about a command like grep, just type man grep, and the man page will tell you the format and an explanation, and relevant pages.



Another cool thing about the man pages is that you can have the man pages check for a topic for you with "man -k", for example man -k filename and it will list one line about every command it thinks has something to do with filenames. It's only a keyword search, so it's not perfect, but the man pages are one of my favourite things about UNIX because I've learned so much from them that I wouldn't have known.
koppe74
2010-11-22 23:30:39 UTC
Using the -l option to the grep-family of commands, will just print the name of files with matches, rather than the default to print the matching lines:

fgrep -l "sequentialInsert" *



Use the -i option with *grep to ignore the case.



You can also do this by using find to call one of the grep commands (fgrep, grep and egrep)



To look for files (-type f) in a specific directory and it's sub-dirs (add -i to *grep to ignore the case):

find some_dir/ -type f -exec fgrep -l "sequentialInsert" {} \;



To look in the current directory and print filename and matching lines (-H) and not just filenames:

find . -type f -exec fgrep -H "sequentialInsert" {} \;



To use filenames (here *.txt):

find some_dir/ -type f -name "*.txt" -exec fgrep -l "sequentialInsert" {} \;



To ignore the case of the filename, use -iname instead of -name.



To combine several filenames (logically ORing tests):

find some_dir/ -type f \( -name "*.txt" -o -name "*.doc" -o -name "*.rtf" \) -exec fgrep -l "sequentialInsert" {} \;



Other useful tests for find are:

-user name = name of owner

-group name = name of group



-mtime n = last modified n days ago

-atime n = last accessed (read) n days ago

-mmin n = last modified n minutes ago

-amin n = last accessed n minutes ago

-size n = the file size (k, M and G can be used for kilo, mega and giga)



Usually you'll use +n or -n to specify older/larger than or newer/smaller than, and not just n (which would be exact).



So to find files owned by bok and modified between 3 and 7 days ago, use (here I just print the files, not look for text) The tests are logically ANDed:

find some_dir/ -type f -mtime +3 -mtime -7 -print
akthktd
2010-11-21 17:01:18 UTC
got to FIND menu



type 'sequentialInser'





to list file names



g to terminal and type



" ls " without quotes
2010-11-21 16:59:21 UTC
ls *sequentialinsert*


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...