Connect with us

Linux

Find command in Unix/Linux with 30+ Examples [2020]

Last Updated on

Let’s Learn Find command in Unix/Linux with Practical Examples :

The find command in Unix/Linux is a nifty tool and comes in handy when searching files on the terminal or the command line. It helps users find files based on a wide spectrum of criteria such as file size, file ownership, date of modification, file permissions to mention just but a few. In this tutorial, you will learn how to search a file in Linux using the various parameter as mentioned earlier.

The find is installed by default on all Linux distributions, therefore there’s no need of installing it. Let’s dive in and see how you can use the command which is considered essential for beginners and advanced users alike.

The find command in Unix/Linux syntax structure is as shown below

1. How to List files in Current directory and sub-directory

To list files in the current directory inclusive of sub-directories run the command below

Sample Output

Find command in Unix/Linux

2. How to List files & subdirectories in a specific path or directory

You can also search a file or files in a specific directory using the syntax below

For example, to search for files in /etc directory run the command below

Sample Output

find files in a particular directory

3. How to List specific files in a specific path or directory

If you wish to narrow down your search for files and print out files according to their names, use the -name argument as shown

In our previous example in (2) above, to find files labeled apache2 in /etc directory, execute the command

Sample Output

find command- find specific files in directories

4. How to search files only or directories only

To search files only in a directory, use the -type f argument. For example, to search for files only in /etc directory run the command below and pipe results into less attribute

Sample Output

find command find files only

To search files only bearing certain names include the -type f argument as shown

For example, to search for ssh files only in /etc directory, execute

Sample Output

find particular file names using find command

To search directories only use the -type d argument

For example, to search for apache2 directories only located in /etc directory, execute

Sample Output

find directories only

5. How to search files with certain file permissions

The find command can also be used to search files with certain file permission using the -perm option followed by the octal value denoting the file permission. The syntax will be

For instance, to find files with file a permission value of 600 in /etc directory, run

Sample Output

find files with certain file permissions

Also, you can play around with the permissions syntax and search for files which are read-only, files which are executable and so on.
For example, to find files which are read-only in a certain directory execute

For example, to find all read-only files in the /etc directory execute the command

Sample Output

If you want to search for executable files, the command will be

Sample Output

search executable files

Feel free to add more options on the file permissions

6. How to recursively list files with a certain file extension

You can also search files with a specific file extension using the wildcard symbol (*) as shown below

For example, if you are wondering how to find a file with a .conf extension in /etc directory execute

Sample Output

find command recursively search for file extensions

7. Inverting search results

Suppose you want to search for files that do not fit a certain pattern. Let’s say you want to search for files that do not have a .conf extension in /etc directory. How do you go about it?
To accomplish that, use the -not -name or ! -name flags as shown

In this example, the command will be

Sample Output

invert find rsults using find command

OR

Sample Output

invert find results with ! -name flag

8. Searching hidden files

To search for hidden files in a directory, use the “.*” option. For example, to search for hidden files in /etc directory execute the command

Sample output

searching hidden files

9. Searching files owned by a particular user

The find command in Unix/Linux also allows you to find files owned by a certain user in the system
The syntax for achieving this is

For instance, to find files owned by a user ‘ james’ in the home directory run

Sample output

find files owned by a specific user

10. Searching files up to a certain directory depth

By default, the find command traverses down the entire directory tree structure in a recursive manner. I’m sure you must have noticed that at this point. However, you can dictate the search depth and specify the number of directories that you’d want your search to go down to. For instance, you may decide that you want your search to be restricted up to 2 or 3 directories. The option for specifying the depth of search is the -maxdepth option.

For instance, to limit the search of apache2to the 1st directories in the /etc directory run

Sample output

maxdepth search option -1

To go up to the 2nd directory run

Sample output

maxdepth search option -2

As you can clearly see, the search depth in the first result is limited to the /etc directory. However, in the second instance, the search has extended to /etc/init.d . /etc/cron.daily and /etc/logrotate directories.

11. Searching files of a given file size

Find can also be used to search files of specified file size. The syntax for this is

For instance, to find all files with 5kb file size in /etc directory, run

Sample output

search files of certain file sizes

If you want to search files in a given range e.g between 5kb and 10kb run

Sample output

search file size between file ranges

Wrapping Up

That was a summary of the find command in Unix/Linux. Find  is an essential tool that is a must know for beginners and seasoned system administrators and which makes searching files in Linux easy. Thank you for taking the time in this tutorial. Your feedback will be appreciated.

Continue Reading
Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Linux

Linux sed command with usage examples [2020]

Last Updated on

Short for stream editor, the Linux sed command is a useful utility used for manipulation text output. You can use it for substituting text, finding and replacing text, searching text and so much more. The command accord you the ability to edit files without having to open them. In this tutorial, we look at various ways you can use the Linux sed command.

1) Replacing or substituting strings

To elaborate on how the command can be used for text manipulation, we are going to use a sample text file linuxgeek.txt with the content below:

To replace all instances of the word Unix with Linux, run the command:

Output

Linux sed command

The ‘s’ flag stands for substitution. In the command above, the first instance of the word ‘Unix’ is replaced by ‘Linux’. By default, the substitution happens to the first string occurrence only, and not the subsequent occurrences.

2) Replace the nth occurrence of a word in a line

To replace the nth occurrence of a string in a file, you need to use the /nth flag. For example, to replace the 2nd occurrence of a string in a line, issue the command:

Output

In the example above, the second instance of the string ‘Unix’ has been replaced by ‘Linux’ in the 1st and 4th lines.

3. Substitute all instances of a string in a file

If you want to replace all instances of the string to be substituted, in this case, ‘Unix’ use the /g option. The ‘g’ stands for global and instructs sed to replace all the instances of the string.

Output

4) Replace a string on a specific line number

Additionally, you can instruct the Linux sed command to replace the keyword in a particular line. For example to replace the string in the 4th line only, use the command:

Output

Linux sed command

As observed, only the first instance is substituted. To substitute all the occurrences of the string, simple append ‘g’ after the last forward slash as shown:

Output

Linux sed command

5) Substitute a string in a range of lines

You can also opt to replace the search keyword for a range of lines, for example between lines 1 and 3. To accomplish this, issue the command:

Output

From the output above, we can observe that the substitution has only occurred in lines 1-3. The last line remains unchanged.

6) Print lines where substitution has occurred

To print the lines where substitution of the lines has occurred, use the -n flag immediately after the sed command as shown:

Output

7) Delete lines using Linux sed command

Sed command can also be used for deleting lines. This is possible using the ‘d’ flag. To delete the 3rd line, for example, run the command:

Output

To delete the last line only in the file, we are going to run the command:

8) Delete the last line

Output

9) Delete a range of lines

To delete a range of lines. for instance from 2 to 4 in a text file, run the command:

Output

10) Add blank lines or spaces

To add a blank line after every line run the command:

linux sed command

This wraps up our tutorial today. We have showcased some of the commonly used cases of the stream editor sed command.

Continue Reading

Linux

cat command in Unix/Linux with 10+ examples [2020]

Last Updated on

Let’s Learn cat command in Unix/Linux with Practical Examples :

The cat command in Unix/Linux is one of the most commonly used Linux commands on a day-to-day basis whether by novices or experienced Linux users. cat is short for concatenating and is mainly used for creating either single or several files at an instance, or viewing the contents of a file. In this article, we dive deeper and explore the cat command in Unix/Linux with example usages.

Cat command in Linux with examples

The most basic syntax for using cat command is as shown below

Let’s now take a closer look at the options that are mostly used with the cat command.

1. To view file content using cat command

To have a peek at a file without any options, simply use the syntax

For instance, to check the contents of a file /etc/hosts/code> run the command:

Sample output

cat command in Unix/Linux

2. View Mutiple files in a single command

To view multiple files at a go with cat command, use the syntax:

For example

Sample output

cat command in Linux

3. Display line numbers using the -n option

To make your display neater, and more presentable, you may want to number the lines of your output. To achieve this, use the -n option as shown below

For instance

Sample output

cat command in Unix/Linux

4. Create a file using the cat command

To create a file using cat command use the syntax

Next, type the contents of the file and finally hit CTRL +D to save the file

cat command in Linux create a new file

5. Copy contents of one file onto another

With cat command, you can easily copy file contents of one file into another using the standard redirection operator > as shown below.

If the file being copied to does not exist, it will be created automatically.

cat command in Linux

NOTE

Extra caution should be taken while using the standard redirection operator because it overwrites the contents of an existing file.

6. Redirect several files into one file

Similarly, you can redirect content from multiple files into one file using the syntax:

Sample output

7. Appending text to a file

if you want to append text and not overwrite the existing content, use the double greater sign symbol (>>). The content of one file will be added or appended at the end of the next text file.

8. Redirect standard input using redirection operator

you can use the redirection operator less than (<) to read contents from a file. The syntax for this is:

Sample output

9. Display the ‘$’ sign at the end of every line

If you wish to display the $ at the end of every line, use the -e option as shown below:

Sample output

From the output above, you can see that there a dollar sign ($) at the end of every file.

10. Display all files of a certain type

To display the content of all files of a certain file type, for example, text files use the wildcard symbol as seen in the command below

Sample output

As you can see from the output above, the command displays all content from all the text files with a .txt file extension.

Take away

In this article, we demonstrated the usage of cat command alongside some helpful examples and tips. The cat command is usually helpful in creating and viewing text files as well as appending content at the end of a file. We do hope that this tutorial was helpful. Fee free to try out some of the commands.

Continue Reading

Linux

Whereis command in Linux with 10+ Examples [2020]

Last Updated on

Let’s Learn whereis command in Linux with Simple Examples :

While working away on the command-line, one might need to find out the location of a command or binary file. The find command can be used for this task, but it is painfully slow and ends up taking much of your time and may also provide undesired results. The whereis command in Linux is ideal for quickly finding the location of a binary or source file in a system.

Whereis command in Linux is preferred due to its speed and accuracy in finding out the location of a command or binary files. More importantly, running the whereis command doesn’t require you to have root privileges. Let’s dive in and take a look at the whereis command usage with examples.

Whereis command usage in Linux

The basic syntax of the whereis command is:

1) Find the location of a command

To find out the location and man pages of a Linux command for instance,mkdir run the command

Sample output

whereis command in linux

2) Search for specific files/manuals

If you wish narrow down your search specifically to the binary file, use the -b option as shown in the command below.

Sample output

whereis command usage

To search for man pages only use the -m option as shown.

Sample output

3) View paths that whereis uses for searching

To take a quick peek at the directories that whereis command traverses while searching for command , files and man pages, use the -l option.

Sample output

whereis command in linux

4) Limit the search depth for whereis command

Usually, whereis command searches for commands and files in well-defined, hard-coded paths. However, you can limit the search depth using the -B option. For instance, to limit whereis to search binary files up to /bin, use the -B option as shown:

the -f option terminates the directory list whilst signaling the beginning of file names.

To limit the search directory for man pages, use the -M option as shown

Sample output

5) Check whereis version

if you wish to check the version of whereis command, use the -V option or –version flag as shown.

OR

Sample output

whereis command usage

6) Display whereis help options

To view the help page section that gives more insight on the options that can be used by whereis command, use the -h option or –help flag.

OR

Sample output

And with that, we come to the end of our tutorial. We welcome you to try out the commands and get back to us with your findings.

Continue Reading

Trending

Copyright © 2019 LinuxTeacher.com