![]() ![]() root # grep -w int hello.c int main(int, argc, char **argv) /root # If you wanted to only return instances where the pattern was a word, you can add the "-w" option after grep. This is the reason that "printf" is returned in the example - it contains "int" inside the word. Notice though that the lines that are returned aren't looking for the word "int", they are looking for the string "int", which can be inside a word. root # grep int hello.c int main(int, argc, char **argv) printf("Hello World\n") /root # Grep follows the syntax below:Īs an example, let's search through the file hello.c within your home directory to see if it contains the string "int". What if you didn't know the name of the file, but remembered something within the file? To find something within a file, you can use the command "grep", which stands for Global Regular Expression Print. ![]() So we found the files we were looking for that started with "a". These are all the files that start with an "a" and are any length longer than just "a". Notice that the command returned both files and directories and even files in sub-directories from where the command was run. Where -name indicates that it will search for the name of the file. The syntax for the find command is as follows: Be careful when doing this if you have a lot of sub-directories containing many files, as it can take a very long time to search all of the content. And, searches with find will search the current directory and any sub-directories. Using the "find" command, you can find those missing files using wildcards. But what about if you don't know where a file is, but remember part of its name? You can still use wildcards, but you will need more functionality than just "ls". Using wildcards is great if you know where certain types of files are located. If you wanted the "ls" command to return both capital and lowercase letters, you would need to include both of them within the brackets separated by a comma. This will display a file listing that returns one filename: Changelog. If you wanted to list all the files that started with an A through G (capitals matter!), you could do that within a bracketed list like so: ~/dos/asm-1.9 # ls * This wildcard represents any number of characters, digits, or whitespace followed by the last 4 characters being exactly ".txt". The wildcard in this case is the special character "*". txt files only, you would enter your command as: ~/dos/asm-1.9 # ls *.txt Navigate to the "asm-1.9" directory under /root/dos. These are special characters that can be used like wild cards in a card game - they can be anything you want them to be. In a Unix environment, you can find, list, sort and copy all the files of one type by using wildcards. In a GUI (graphical user interface) environment, you would probably sort the file list by type/extension and then just select the desired files, which are now listed in a block. Let's say you wanted to copy all of the ".txt" files from one directory to another, but there were several different file types present in that directory. ![]() Now, let's move to looking at larger amounts of files and those from which you may need specific information. Thus far we've explored only a handful of files in a couple directories. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |