Wednesday, 19 June 2019

Ubuntu How to Find *.jpg in Current Folder and Subfolders

In general, here are few useful commands:

1. find all images in current and its subfolders:-
find . -name "*.jpg" > temp.txt

2. To list all images with full path:-
find $(pwd) -name "*.ppm" > temp.txt

3. find all images in current and its subfolders, then count it
find . -name "*.jpg"| wc -l

=== Appendix ===

How to find files and directories in Linux

(ps: copy from https://www.computerhope.com/issues/ch001723.htm)
In Linux operating systems, the find command may be used to search for files and directories on your computer. To proceed, select a link from the following list or go through each section in order.
Note To use find, begin by opening a terminal session to access the command line.

Basic functionality of find

Running find without any options will produce a list of every file and directory in and beneath the working directory. For instance, if your working directory is /home/hope/Documents, running find will output the following:
  • Every file in /home/hope/Documents.
  • Every subdirectory in /home/hope/Documents.
  • Every file in each of those subdirectories.
Let's see it in action. First, let's check our working directory by using the pwd command:
pwd
/home/hope/Documents
Now let's run find without any options:
find
.

./images

./images/hp

./images/hp/snape.jpg

./images/hp/harry.jpg

./images/memes

./images/memes/winteriscoming.jpg

./images/memes/goodguygary.JPG

./images/memes/picard.jpg

./gimp-2.8.16.tar.bz2

./hp-fanfic

./hp-fanfic/malfoys-revenge.doc

./hp-fanfic/weekend-at-hagreds.doc

./hp-fanfic/dumbledores-lament.doc

./archlinux-2016.02.01-dual.iso
In this example, we see a total of ten files and four subdirectories in and beneath our Documents folder.
Notice that the output starts with a single dot, which represents the working directory. Running find with no options is the same as specifying that the search should begin in the working directory, like this:
find .
The above example is the "proper" way to use find. If you try to use it on another UNIX-like operating system, such as FreeBSD, you will find that specifying a directory is required, so it's good practice to use this form of the command.

Specifying where to search

To only list files and subdirectories that are contained in the directory /home/hope/Documents/images, specify it as the first argument of the command:
find /home/hope/Documents/images
/home/hope/Documents/images

/home/hope/Documents/images/hp

/home/hope/Documents/images/hp/snape.jpg

/home/hope/Documents/images/hp/harry.jpg

/home/hope/Documents/images/memes

/home/hope/Documents/images/memes/winteriscoming.jpg

/home/hope/Documents/images/memes/goodguygary.JPG

/home/hope/Documents/images/memes/picard.jpg
Notice that the full path is also shown in the results.
If our working directory is /home/hope/Documents, we can use the following command, which finds the same files:
find ./images
But this time, the output reflects the starting location of the search and looks like this:
./images

./images/hp

./images/hp/snape.jpg

./images/hp/harry.jpg

./images/memes

./images/memes/winteriscoming.jpg

./images/memes/goodguygary.JPG

./images/memes/picard.jpg
By default, the search will look in every subdirectory of your starting location. If you want to restrict how many levels of subdirectory to search, you can use the -maxdepth option with a number.
For instance, specifying -maxdepth 1 will search only in the directory where the search begins. If any subdirectories are found, they will be listed, but not searched.
find . -maxdepth 1
.

./images

./bigfiles.txt

./gimp-2.8.16.tar.bz2

./hp-fanfic

./archlinux-2016.02.01-dual.iso
Specifying -maxdepth 2 will search the directory and one subdirectory deep:
find . -maxdepth 2
.

./images

./images/hp

./images/memes

./gimp-2.8.16.tar.bz2

./hp-fanfic

./hp-fanfic/malfoys-revenge.doc

./hp-fanfic/weekend-at-hagreds.doc

./hp-fanfic/dumbledores-lament.doc

./archlinux-2016.02.01-dual.iso
Specifying -maxdepth 3 will search one level deeper than that:
find . -maxdepth 3
.

./images

./images/hp

./images/hp/snape.jpg

./images/hp/harry.jpg

./images/memes

./images/memes/winteriscoming.jpg

./images/memes/goodguygary.JPG

./images/memes/picard.jpg

./gimp-2.8.16.tar.bz2

./hp-fanfic

./hp-fanfic/malfoys-revenge.doc

./hp-fanfic/weekend-at-hagreds.doc

./hp-fanfic/dumbledores-lament.doc

./archlinux-2016.02.01-dual.iso

Finding by name

To restrict your search results to match only files and directories that have a certain name, use the -name option and put the name in quotes:
find . -name "picard.jpg"
./images/memes/picard.jpg
You can also use wildcards as part of your file name. For instance, to find all files whose name ends in .jpg, you can use an asterisk to represent the rest of the file name. When you run the command, the shell will glob the file name into anything that matches the pattern:
find . -name "*.jpg"
./images/hp/snape.jpg

./images/hp/harry.jpg

./images/memes/winteriscoming.jpg

./images/memes/picard.jpg
Notice that our command didn't list the file whose extension (in this case, JPG) is in capital letters. That's because unlike other operating systems, such as Microsoft Windows, Linux file names are case-sensitive.
To perform a case-insensitive search instead, use the -iname option:
find . -iname "*.jpg"
./images/hp/snape.jpg

./images/hp/harry.jpg

./images/memes/winteriscoming.jpg

./images/memes/goodguygary.JPG

./images/memes/picard.jpg

Finding only files, or only directories

To list files only and omit directory names from your results, specify -type f:
find . -type f
./images/hp/snape.jpg

./images/hp/harry.jpg

./images/memes/winteriscoming.jpg

./images/memes/goodguygary.JPG

./images/memes/picard.jpg

./gimp-2.8.16.tar.bz2

./hp-fanfic/malfoys-revenge.doc

./hp-fanfic/weekend-at-hagreds.doc

./hp-fanfic/dumbledores-lament.doc

./archlinux-2016.02.01-dual.iso
To list directories only and omit file names, specify -type d:
find . -type d
.

./images

./images/hp

./images/memes

./hp-fanfic

Finding files based on size

To display only files of a certain size, you can use the -size option. To specify the size, use a plus or a minus sign (for "more than" or "less than"), a number, and a quantitative suffix such as k, M, or G.
For instance, to find files that are "bigger than 50 kilobytes", use -size +50k:
find . -size +50k
./images/memes/winteriscoming.jpg

./gimp-2.8.16.tar.bz2

./archlinux-2016.02.01-dual.iso
For files "bigger than 10 megabytes", use -size +10M:
find . -size +10M
./gimp-2.8.16.tar.bz2

./archlinux-2016.02.01-dual.iso
For "bigger than 1 gigabyte", use -size +1G:
find . -size +1G
./archlinux-2016.02.01-dual.iso
For files in a certain size range, use two -size options. For instance, to find files "bigger than 10 megabytes, but smaller than 1 gigabyte", specify -size +10M -size -1G:
find . -size +10M -size -1G
./gimp-2.8.16.tar.bz2
        

Finding files based on modification, access, or status change

The -mtime option restricts search by how many days since the file's contents were modified. To specify days in the past, use a negative number. For example, to find only those files which were modified in the past two days (48 hours ago), use -mtime -2:
find . -mtime -2
The -mmin option does the same thing, but in terms of minutes, not days. For instance, this command shows only files modified in the past half hour:
find . -mmin -30
A similar option is -ctime, which checks when a file's status was last changed, measured in days. A status change is a change in the file's metadata. For instance, changing the permissions of a file is status change.
The option -cmin will search for a status change, measured in minutes.
You can also search for when a file was last accessed — in other words, when its contents were most recently viewed. The -atime option is used to search for files based upon their most recent access time, measured in days.
The -amin option will perform the same search restriction, but measured in minutes.

Redirecting output to a text file

If you are performing a very large search, you may want to save your search results in a file, so that you can view the results later. You can do this by redirecting your find output to a file:
find . -iname "*.jpg" > images.txt
You can then open your results in a text editor, or print them with the cat command.
cat images.txt
./images/hp/snape.jpg

./images/hp/harry.jpg

./images/memes/winteriscoming.jpg

./images/memes/goodguygary.JPG

./images/memes/picard.jpg
Alternatively, you can pipe your output to the tee command, which will print the output to the screen and write it to a file:
find . -size +500M | tee bigfiles.txt
./archlinux-2016.02.01-dual.iso
cat bigfiles.txt
./archlinux-2016.02.01-dual.iso

Suppressing error messages

You may receive the error message "Permission denied" when performing a search. For instance, if you search the root directory as a normal user:
find /
find: `/var/lib/sudo/ts': Permission denied

find: `/var/lib/sudo/lectured': Permission denied

find: `/var/lib/polkit-1': Permission denied

find: `/var/lib/container': Permission denied

find: `/var/lib/gdm3/.dbus': Permission denied

find: `/var/lib/gdm3/.config/ibus': Permission denied

...
You will receive that error message if find tries to access a file that your user account doesn't have permission to read. You may be able to perform the search as the superuser (root), which has complete access to every file on the system. But it's not recommended to do things as root, unless there are no other options.
If all you need to do is hide the "Permission denied" messages, you can add 2&>1 | grep -v "Permission denied" to the end of your command, like this:
find / 2>&1 | grep -v "Permission denied"
The above example filters out the "Permission denied" messages from your search. How?
2>&1 is a special redirect that sends error messages to the standard output to pipe the combined lines of output to the grep command. grep -v then performs an inverse match on "Permission denied", displaying only lines which do not contain that string.
Redirecting and using grep to filter the error messages is a useful technique when "Permission denied" is cluttering your search results and you can't perform the search as root.

Examples

find ~/. -name "*.txt" -amin -120
Find all files in your home directory and below which end in the extension ".txt". Display only files accessed in the past two hours.
find . -name "*.zip" -size +10M -mtime -3
Find all files in the working directory and below whose name has the extension ".zip" and whose size is greater than 10 megabytes. Display only files whose contents were modified in the last 72 hours.
find . -iname "*report*" -type f -maxdepth 2
Perform a case-insensitive search for files that contain the word "report" in their name. If the search finds a directory with "report" in its name, do not display it. Search only in the working directory, and one directory level beneath it.
find / -name "*init*" 2>&1 | grep -v "Permission denied" | tee ~/initfiles.txt
Find all files on the system whose name contains "init", suppressing error messages. Display results on the screen and output them to a file in your home directory named "initfiles.txt".

No comments:

Post a Comment