Find File by Name in Linux – 7 Basic Examples

Linux find file by name command

Finding files in Linux can be tricky, especially when you need to search through thousands of files across multiple directories. However, this is achievable, using the Linux find command. It lets you search for files in Linux recursively by full name, partial name, or pattern.

Many users waste time manually browsing folders. With the right Linux find command syntax, you can locate anything fast. This guide shows how to use the Linux find command to search files recursively by exact name, extension, regex, and more. You’ll also learn how to combine find with grep for finding which of the files contain a certain string. (more…)

Format USB Drive in Linux – Command Line

Formatting USB drives in Linux from the command line (terminal) is very easy.

In this note i will show how to format an external HDD, SSD or USB flash drive in Linux from the command line using mkfs utility.

I will provide the examples of disk formatting to the most popular file system types: FAT32, exFAT, NTFS, EXT4, XFS and will show how to list the all supported file systems. (more…)

HowTo: Generate and Print a Sequences of Numbers with BASH

You don’t have to type a range of numbers by hands!

BASH already has a built-in utility, named seq, for generating and printing a sequence of numbers.

Generate a sequence of numbers

Syntax: seq [OPTION]… FIRST
Syntax: seq [OPTION]… FIRST LAST

Print a sequence of numbers from 1 to 10:

$ seq 1 5
1
2
3
4
5

Generate a sequence of numbers with increment

Syntax: seq [OPTION]… FIRST INCREMENT LAST

Print a sequence of numbers from 0 to 20 with increment 5:

$ seq 0 5 20
0
5
10
15
20

Equalize width by padding with leading zeroes

Syntax: seq -w…

Equalize width by padding with leading zeroes:

$ seq -w 1 10
01
02
03
04
05
06
07
08
09
10

Use another separator

Syntax: seq -s SEPARATOR… – use SEPARATOR to separate numbers.

Default separator is a new line – "\n".

Use space to separate numbers:

$ seq -s " " 1 10
1 2 3 4 5 6 7 8 9 10

HowTo: Count Number of Files in a Directory

Here are the best ways of finding and counting the number of files or folders in a particular directory.

Count the number of files in a current directory

Using “ls” command:

This method is the best, if you need to count a large number of files.

$ ls -f . | wc -l

This command also enables -a option, so . , .. and other files starting with . , will be counted.

Using “find” command:

$ find . -type f  -maxdepth 1 | wc -l

Count the number of files recursively

The following command recursively counts the number of files in a current directory and all its sub-directories:

$ find . -type f | wc -l

Count the number of folders recursively

The following command recursively counts the number of folders in a current directory and all its sub-directories:

$ find . -type d | wc -l

Count the number of files by “file type”

The following command recursively counts the number of files, with “.txt” extension, in a current directory and all its sub-directories:

$ find . -type f -name "*.txt" | wc -l

HowTo: Find Large Files in Linux

Finding the largest files is extremely useful especially when you you’re low on disk space and want to free it up.

The best way to find the largest files on your Linux system is to use the command line.

Actually there is no simple command to list the largest files in Linux.

However, you can easily find the largest files, using a combination of the several simple commands.

Find The Largest Files in Linux

Run the combination of the following commands to find the largest files on your Linux system, beginning from the <DIR> directory (change <DIR> to whatever directory you need), and list top 10 of them.

$ find <DIR> -mount -type f -ls 2> /dev/null | sort -rnk7 | head -10 | awk '{printf "%10d MB\t%s\n",($7/1024)/1024,$NF}'

Find 10 largest files, starting from ‘/’ (root)

$ find / -mount -type f -ls 2> /dev/null | sort -rnk7 | head -10 | awk '{printf "%10d MB\t%s\n",($7/1024)/1024,$NF}'

       106 MB	/var/lib/mysql/ibdata1
        94 MB	/usr/lib/locale/locale-archive
        41 MB	/scripts/20130206-015833.tar.gz
        41 MB	/scripts/20130206-004839.tar.gz
        41 MB	/scripts/20130206-130400.tar.gz
        41 MB	/scripts/20130206-000442.tar.gz
        41 MB	/scripts/20130206-132019.tar.gz
        41 MB	/root/20130208-133954.tar.gz
        33 MB	/var/log/messages-20130303
        32 MB	/var/lib/rpm/Packages

Find 10 largest files, starting from ‘/home’

$ find /home -mount -type f -ls 2> /dev/null | sort -rnk7 | head -10 | awk '{printf "%10d MB\t%s\n",($7/1024)/1024,$NF}'

      3007 MB	/home/user/Desktop/share/linux-65835.iso
       448 MB	/home/user/Pictures/Turkey/SAM_4590.AVI
       266 MB	/home/user/Pictures/Turkey/SAM_4588.AVI
       173 MB	/home/user/Camera/VID_20130909_120713.mp4
       152 MB	/home/user/Camera/VID_20130909_115427.mp4
       133 MB	/home/user/Camera/VID_20130909_210904.mp4
       133 MB	/home/user/Pictures/Paris/VID_20130928_182431.mp4
       131 MB	/home/user/Pictures/Turkey/SAM_4597.AVI
       129 MB	/home/user/Pictures/Turkey/SAM_4641.AVI
       127 MB	/home/user/Desktop/tmp/Camera/VID_20130911_164440.mp4

Writing a Message to Logged in Users through the Terminal

To write a message to logged in users, you can use a write command. The write utility allows you to communicate with other users, by copying lines from your terminal to theirs.

First of all you need to check who is currently logged in, and which terminal he is logged in to.

$ who
root     pts/0        2012-04-25 12:57 (192.168.0.207)
john     pts/1        2012-04-25 13:20 (192.168.0.101)

Now you can write a messages to the user John. For example:

$ write john pts/1
Hello!

When you hit ‘Enter’, your message will be sent to that terminal.

Use Ctrl + D to terminate write

Also you can cat a file and pipe it to the write command too:

$ cat file.txt | write stan pts/1

To broadcast your message to all logged in users you can use a wall command (wall = write to all):

$ wall
Hey you people!

For wall, the message will be sent only after you hit Ctrl + D

Or you can cat a file and pipe it to the wall command:

$ cat announcement.txt | wall