Find command files and Directories linux
Find command files and Directories linux
Output
./test/file22.txt
./sales/file22.txt
Please note that the results include the path. This is important if you don’t know the directory where
the file is located, or when it is in more than one place.
You can also search for the file in another directory while still in the current location. In this case,
you need to provide the path for the directory where you want to search.
Output
./test/file22.txt
./test/file30.doc
./test/file1.txt
./test/file5,doc
Output
./test/subtest/subfil.txt
./test/file22.txt
./test/file1.txt
./home1/files32.txt
./home2/file22.txt
./qa/tags.txt
./test/qatree.pdf
./test/qa.txt
./home/qa
The command returns both the files and directories matching the search criteria. To find files or
directories only, you need to specify this in the command.
Files only
Output
./test/qatree.pdf
./test/qa.txt
Directories only
Output
./home/qa
6.Case insensitive find command
All searches with -name switch are case sensitive and will not give results with capital letters. To get
all cases, use the -iname option.
Output
./test/qatree.pdf
./test/qa.txt
./test/QAtest.txt
./home/qa
Output
./test/file22.txt
/root/numeric/file22.txt
output
./test/subtest/subfil.txt
./test/qatree.pdf
./test/file22.txt
./test/qa.txt
./test/file30.doc
./books/acro.pdf
./data1/FILE22.txt
./docs/files32.txt
To look for all the files containing the word hyperconvergence”, use;
Output
/root/numeric/hci
/root/HCIP
The –i option enables the command to ignore cases and will find the text whether capitalized or not
i.e. hyperconvergence, Hyperconvergence , etc.
To look for the files in a specific directory, simply add them to the command
Output
./numeric/hci
You can find all files or directories that are smaller, equal or greater than a certain size, within a
certain range or empty. Use the appropriate size format depending on the type of files or directories
you are searching for.
c – bytes
k – kilobytes
M – Megabytes
G – Gigabytes
Output
. /Downloads/ubuntu18043.iso
./.cache/pip/http/6/e/3/8/3/6e38383f41850c3e5705d48116bc52f81b92a80500f414305057 7a9c
or
Directories
The -mtime +8 will look for txt files that are older than 8 days.
To see files that have not been accessed within the last 10 days in the home directory.
Output
./.bash_history
./.profileroot@ubuntu1804:~#
Where mode is the permission which is either numeric such as 644, 655, 700, 777, etc, or letters
such as u=x, a=r+x, etc.
You can specify the mode in the following three different ways.
Without a prefix when you want to find files with the exact permissions specified.
With “-“ for files with at least the specified permission. This returns files with the specified as well as
additional higher permissions.
Using “/” requires specifying the owner or group with the permission to the file.
Output
./file1
Output
./file2
./file1
As such, it returns two files that meet this criterion – file1 and file2. The files do not need to have the
exact 766 permissions and can have additional ones as long but must have at least the specified.
./file3
./file1
The above looks for files that are writable by either their owner or group.
This returns files that are writable by either but not necessarily both. To see files, where both have
writable permissions, use the – prefix.
Output
./file2
./file1
Output
/home/jack
/home/jack/examples.desktop
/home/jack/.gnupg
/home/jack/.gnupg/private-keys-v1.d
/home/jack/ans.cgi
Output
/home/jack/docs/file32.txt
26.Find and list files and directories together with their permissions
find -name "*.conf" | ls -l
Output
total 120
In this section, we will look at how you can act on the files that match the pattern specified in the
find command.
jack@ubuntu1804:~/ver$ ls -la
total 8
We will now look for all the PHP files (above) and replace their permissions with 755
find ver -name "*.php" -type f -exec chmod 755 {} ;The command looks for PHP files in the ver
directory and then sets their permission to 755 (rwxr-xr-x)
Results
jack@ubuntu1804:~/ver$ ls -la
total 8
You can also look for directories with 644 permissions and replace this with 755.
Output
./docs
Output
./doc
From above we can see the root and docs directories have the 755 permissions.
Ls –la command gives the following details
The command below will find the file22.txt file and copy it to the ~/tmp/images directory.
This will find and copy all the jpg files to the ~/tmp/images folder.
This will find the file hci file and copy it to the three directories of /tmp/dir1/ /tmp/dir2/ and
$HOME/3/
The command looks for all the files with names starting with uni and having any extension. It then
moves them to the directory /unifiles/
This will remove both files and directories starting with the letters til.
36.Delete by extension
Below is how you can locate and delete all txt files in the current directory. Replace the txt with
another extension such as bak, pdf or any other that you want to remove.
In case you want the system to prompt you to confirm before deleting each file, add the -i option as
below.
Output
By default, the -rm will not remove the directories and you need to use the –r option to ensures a
recursive removal. This ensures the deletion of empty directories and those containing files. The -f
option forces the removal and is used for both the files and directories.
37.Find and delete files older than n days
Find and delete backup files older than 20 days from the current directory.
Ensure that you match the directory name case or use the -iname option.
Output
In our case, we typed n for directory Dir22 which will not be deleted and y for the dir22 which will
now be removed.
or
or
Please note that deleting system or critical files from your computer can damage the operating
system or applications or lead to loss of your important data.
To avoid accidental deletions, it is best practice to use a non-root user account. Also, ensure that you
are deleting the right files that are not useful and that you have a backup of all your data files just in
case.