find – TecAdmin https://tecadmin.net How to guide for System Administrator's and Developers Mon, 24 Oct 2022 05:00:15 +0000 en-US hourly 1 https://wordpress.org/?v=6.1.1 Mongodb – Get the last record from collection https://tecadmin.net/mongodb-get-the-last-record-from-collection/ https://tecadmin.net/mongodb-get-the-last-record-from-collection/#respond Sat, 30 Jul 2022 03:59:45 +0000 https://tecadmin.net/?p=30764 MongoDB find() method is used to select documents from a specified collection. It also set the cursor position to the selected document. The default find() method gets the documents from the start of the collection. In some situations, we need to fetch the last record from a collection. Use the following query to get the [...]

The post Mongodb – Get the last record from collection appeared first on TecAdmin.

]]>
MongoDB find() method is used to select documents from a specified collection. It also set the cursor position to the selected document.

The default find() method gets the documents from the start of the collection. In some situations, we need to fetch the last record from a collection. Use the following query to get the last records from a MongoDB collection.

db.collection.find().limit(1).sort({$natural:-1}) 

The above query provides the single last record only, You can also fetch the multiple records from the end of the MongoDB collection.

You can set the number of records with limit() method as below:

db.collection.find().limit(5).sort({$natural:-1}) 

Thanks, Hope this quick how-to tutorial helps you to fulfill your requirements.

The post Mongodb – Get the last record from collection appeared first on TecAdmin.

]]>
https://tecadmin.net/mongodb-get-the-last-record-from-collection/feed/ 0
How to Search Recently Modified Files in Linux https://tecadmin.net/how-to-search-recently-modified-files-in-linux/ https://tecadmin.net/how-to-search-recently-modified-files-in-linux/#respond Wed, 16 Feb 2022 09:24:53 +0000 https://tecadmin.net/?p=28575 This tutorial will help you to find recently modified files in Linux via command line . The find command allows us to define duration in Minutes or Days. The minutes are define with -mmin and the days value can be defined with -mtime You can also define the search criteria to find files modified within [...]

The post How to Search Recently Modified Files in Linux appeared first on TecAdmin.

]]>
This tutorial will help you to find recently modified files in Linux via command line .

The find command allows us to define duration in Minutes or Days. The minutes are define with -mmin and the days value can be defined with -mtime

You can also define the search criteria to find files modified within or before specified duration. For example, to search files modified before, use “+” (positive) with duration (eg: +1, +24 etc). To search files modified within duration use “-” (negative) sign with duration value (eg: -1, -24) etc.

Find All Modified Files Less Than Time

  1. Modified within 10 Minutes:- Search all files modified within 10 minutes in the current directory. Use -mmin -10 means the files last modified less than 10 minutes.
    find . -type f -mmin -10 
    
  2. Modified within 2 Hours:- Find all files modified within 2 hours in the current directory. Use -mmin -120 means the files last modified less than 120 minutes ie equal to 2 hours.
    find . -type f -mmin -120 
    
  3. Modified within 1 day:- Search all files modified within 24 hours in the current directory. To define range in days use -mtime. For example -mtime -1 means the files last modified les than 24 hours ago.
    find . -type f -mtime -1 
    

Find All Modified Files Before Time

The above example, find all file modified within specified duration. But you can also search files modified before specified duration with the help of below examples.

  1. Modified older than 10 Minutes:- Search all files modified before 10 minutes in the current directory. Use -mmin +10 option, which means find all files modified more than 10 minutes ago.
    find . -type f -mmin +10 
    
  2. Modified older than 2 Hours:- Find all files modified before 2 hours in the current directory. Use -mmin +120 options to search file modified older than 120 minutes (ie 2 hours).
    find . -type f -mmin +120 
    
  3. Modified older than 1 day:- Search all files modified more than 24 hours ago in the current directory. You can use -mtime option to define duration in days. For example -mtime +1 means find all files modified before 24 hours.
    find . -type f -mtime +1 
    

The post How to Search Recently Modified Files in Linux appeared first on TecAdmin.

]]>
https://tecadmin.net/how-to-search-recently-modified-files-in-linux/feed/ 0
(Resolved) -bash: /bin/mv: Argument list too long https://tecadmin.net/mv-argument-list-too-long/ https://tecadmin.net/mv-argument-list-too-long/#comments Tue, 23 Nov 2021 02:42:20 +0000 https://tecadmin.net/?p=28334 One of my development servers contains millions of files under a single directory. To free the disk space, we decided to move to them a new folder created on another disk attached to the same system. When tried to move file with mv command, received the following error. -bash: /bin/mv: Argument list too long An [...]

The post (Resolved) -bash: /bin/mv: Argument list too long appeared first on TecAdmin.

]]>
One of my development servers contains millions of files under a single directory. To free the disk space, we decided to move to them a new folder created on another disk attached to the same system. When tried to move file with mv command, received the following error.

-bash: /bin/mv: Argument list too long

An argument list too long is a common problem with a bash that can happen when you have long command line parameters or arguments. You start running a script and it throws errors like “invalid command” or “too long”. The reason for this is that the shell is trying to read past the end of your argument list and there is no end of the input pipe to wait for. A system variable ARG_MAX defines the Maximum Character Length of Arguments In a shell command.

The Solution’s

The quick solution is to use xargs command line utility or find command with -exec … {}. Both commands break a large command into more minor and complete the job without errors.

  • find with xargs

    The following command will move all files with the “.txt” extension to the target directory. Here find will search all files with the “.txt” extension in the current directory as subdirectories. PIPE (|) will take the standard output of the find command and send it to the mv command as standard input. then mv will move files to the target directory one by one.

    find . -name '*.txt' | xargs mv --target-directory=/path/to/dest_dir/ 
    
  • find with exec

    Instead of using xargs, we can also use -the exec command. Here find will search files and exec will execute the mv command for each file one by one and move the file to the destination directory.

    find . -name '*.txt' -exec mv {} /path/to/dest_dir/ \;
    

    The default above commands will navigate recursively to the sub-directories. To limit the find to the current directory only use -maxdepth followed by a limit number to sub-directories.

    find . -name '*.txt' -maxdepth 1 -exec mv {} /path/to/dest_dir/ \;
    

The “Argument list too long” is a common error while handling a large number of files in bash scripts. You can find the max limit with the command getconf ARG_MAX on the shell.

The post (Resolved) -bash: /bin/mv: Argument list too long appeared first on TecAdmin.

]]>
https://tecadmin.net/mv-argument-list-too-long/feed/ 1
How to Search files with case-insensitive names in Linux https://tecadmin.net/find-files-with-case-insensitive-names-in-linux/ https://tecadmin.net/find-files-with-case-insensitive-names-in-linux/#respond Sun, 13 Dec 2020 17:32:29 +0000 https://tecadmin.net/?p=23675 find is the basic Unix command used to search files recursively under a directory tree. It is default available in all the Linux operating systems. All the Linux command line users must be aware about uses of Linux find command. It The find command traverse under a directory tree and capable to search files or [...]

The post How to Search files with case-insensitive names in Linux appeared first on TecAdmin.

]]>
find is the basic Unix command used to search files recursively under a directory tree. It is default available in all the Linux operating systems.

All the Linux command line users must be aware about uses of Linux find command. It The find command traverse under a directory tree and capable to search files or directory based on defined search pattern. It also provides option to search files with names in uppercase or lowercase or mixed case.

In this tutorial you will learn about how to search files with case insensitive names.

Find files with Case Insensitive Names

Use -name command line option followed by the file name under a directory tree. The below command will search all files with name backup.zip under the current directory and sub directories.

find . –name backup.zip 

The above command searches files in case sensitive names.

Use -iname option to search file names in any case. Here iname means insensitive names. The following command will match all patter like Backup.zip, BACKUP.ZIP, backup.Zip or BackUp.Zip etc.

find . –iname backup.zip 

The case insensitive means any letter of a file name cane be uppercase or lowercase. In this situation use find with option -iname to search all files matching files in any case.

Conclusion

In this quick tutorial, you have learned to find files with case insensitive names in Linux.

The post How to Search files with case-insensitive names in Linux appeared first on TecAdmin.

]]>
https://tecadmin.net/find-files-with-case-insensitive-names-in-linux/feed/ 0
How to Use LIKE Statement in MongoDB https://tecadmin.net/using-sql-like-statement-in-mongodb/ https://tecadmin.net/using-sql-like-statement-in-mongodb/#respond Sat, 26 Sep 2020 17:49:20 +0000 https://tecadmin.net/?p=22697 MongoDB is an powerful Nosql database server. MongoDB uses JSON-like documents with optional schemas to store data. Its always a critical task for a develop to organize data. As it plays most important role in the application performance. In Mongodb, you can use queries similar to SQL LIKE statement to fetch data. For the examples [...]

The post How to Use LIKE Statement in MongoDB appeared first on TecAdmin.

]]>
MongoDB is an powerful Nosql database server. MongoDB uses JSON-like documents with optional schemas to store data.

Its always a critical task for a develop to organize data. As it plays most important role in the application performance. In Mongodb, you can use queries similar to SQL LIKE statement to fetch data.

For the examples used in this tutorial, we uses some dummy data as below. You can also create a database and execute below commands to insert dummy data.

db.colors.insert({ "id": 100, "color": "Pink"})
db.colors.insert({ "id": 101, "color": "Purple"})
db.colors.insert({ "id": 102, "color": "Black"})
db.colors.insert({ "id": 103, "color": "Blue"})

Using .find()

The Mongodb find() command is used to search documents from a collection. This function provides flexible options to search documents.

The default find() function retrieve all the documents in a collection. It also allows you to query a collection of documents, by passing a few simple parameters, and return a cursor.

A simple example of the .find() method look like below:

> db.colors.find()
{ "_id" : ObjectId("5f697e4ccc528930cde49f53"), "id" : 100, "color" : "Pink" }
{ "_id" : ObjectId("5f697e4fcc528930cde49f54"), "id" : 101, "color" : "Purple" }
{ "_id" : ObjectId("5f697e52cc528930cde49f55"), "id" : 102, "color" : "Black" }
{ "_id" : ObjectId("5f697e55cc528930cde49f56"), "id" : 103, "color" : "Blue" }

The above returns all the documents in a collection. But this is very uncommon on production requirements. You always required some filtered results from a database.

For example, fetch all documents contains “color: Pink”. Execute query like:

>  db.colors.find({color: "Pink"})

Mongo match exact string

Using .find() as SQL LIKE Statement

You can use regular expression for searching documents in monogdb. This will be similar to LIKE statements in SQL queries.

  1. Search String Anywhere – To search all document where color name have “Pink” anywhere in string. The second statement searches for all documents where color have "Bl" in there name.
    ### SQL Statement 
    select * from colors where color LIKE "%Pink%"
    
    ### Mongodb Statement 
    db.colors.find(color: "/Pink/")
    

    Mongodb match find with substring

  2. Search String Start With – This will match all the string start with P characters. The carrot “^” symbol is used for start with.
    ### SQL Statement 
    select * from colors where color LIKE "P%"
    
    ### Mongodb Statement 
    db.colors.find(color: "/^P/")
    

  3. Search String End With – The dollar “$” symbol is used to match string ends with specific characters. The below example matches all strings ends with “k” character.
    ### SQL Statement 
    select * from colors where color LIKE "%k"
    
    ### Mongodb Statement 
    db.colors.find(color: "/k$/")
    

  4. Search String In Any Case – The default find method search with case-sensitive. You can instruct find command to match characters in any case with “i” option as used in below example.
    ### SQL Statement 
    select * from colors where color LIKE BINARY "pink"
    
    ### Mongodb Statement 
    db.colors.find(color: "/pink/i")
    

  5. Conclusion

    In this tutorial, you have learned to to search database similar to SQL LIKE statements in Mongodb.

    The post How to Use LIKE Statement in MongoDB appeared first on TecAdmin.

    ]]> https://tecadmin.net/using-sql-like-statement-in-mongodb/feed/ 0 How to find files larger than 10MB, 100MB, 1GB in Linux https://tecadmin.net/find-all-files-larger-than-1gb-size-in-linux/ https://tecadmin.net/find-all-files-larger-than-1gb-size-in-linux/#comments Fri, 21 Aug 2020 13:12:18 +0000 https://tecadmin.net/?p=22373 If you’re looking for files that are larger than 10MB, 100MB or 1GB, the find command can be very helpful. With find, you can search for files based on size criteria. A few days back my production application goes down. After searching for half an hour, I found the application was down due to the [...]

    The post How to find files larger than 10MB, 100MB, 1GB in Linux appeared first on TecAdmin.

    ]]>
    If you’re looking for files that are larger than 10MB, 100MB or 1GB, the find command can be very helpful. With find, you can search for files based on size criteria.

    A few days back my production application goes down. After searching for half an hour, I found the application was down due to the disk full on my server. So I searched all files greater than 1 GB and then all files greater than 100 MB. There were a few log files that were large in size, which caused the disk full.

    In this tutorial, you will learn how to search file by their size using find command.

    Searching the larger files in Linux

    You can define size in KB, MB and GB formats. For example, you can define size 100K, 100M, 1G or 10G formats. Use below examples, which will help you to find files by there size and extension.

    • The following command will find all file greater than equals to 100MB under entire file system.
      find / -size +100M 
      

      This would search through the entire file system and return a list of all files that are larger than 100MB. If you only want to search a specific directory, you can replace “/” with the path to that directory. For example, if you only wanted to search your home directory, you could use this command:

      find ~/ -size +100M 
      
    • You can also use find to search for files that are larger than 1GB. To do this, you would just need to use a different size criterion. For example, to find all files that are greater than 1GB, you could use this command:
      find / -size +1G 
      

    Find files by size and extension

    Instead of searching all files, you can also search files of specific extensions greater than 1G B size. For example search, all files with extension “.log” and size are 1GB or more.

    find / -type f -name "*.log" -size +1G 
    

    Related Topics

    The post How to find files larger than 10MB, 100MB, 1GB in Linux appeared first on TecAdmin.

    ]]>
    https://tecadmin.net/find-all-files-larger-than-1gb-size-in-linux/feed/ 1
    Find All files with 777 permission in Linux https://tecadmin.net/find-files-with-permission-777-in-linux/ https://tecadmin.net/find-files-with-permission-777-in-linux/#respond Thu, 13 Aug 2020 16:24:05 +0000 https://tecadmin.net/?p=22302 Right file permission is the most crucial part of the Linux system management. A file with permission 777 is open to everyone for read and write. Any user logged in to system can write to this file. Which can be harmful for your system. In some condition’s, you may required 777 permissions like log file [...]

    The post Find All files with 777 permission in Linux appeared first on TecAdmin.

    ]]>
    Right file permission is the most crucial part of the Linux system management. A file with permission 777 is open to everyone for read and write. Any user logged in to system can write to this file. Which can be harmful for your system.

    In some condition’s, you may required 777 permissions like log file etc. But mostly we don’t required it. This tutorial will help you to search files with 777 permission on your Linux/Unix system via find command.

    Syntax:

    find /path/to/dir -perm 777 
    

    The -perm command line parameter is used with find command to search files based on permissions. You can use any permission instead of 777 to find files with that permissions only.

    For example to search all files with permission 777 under the logged in user home directory, type:

    find $HOME -perm 777 
    

    The above command will search all files and directories with permission 777 under the specified directory.

    Linux command find files with 777 permissions

    But if you don’t want to include directories in this list. Define the type with -type on command line parameter as below. This will search only files with permission 777 under the /var/www directory.

    find /var/www -perm 777 -type f 
    

    Linux command find files only with 777 permissions

    To search directory only, type:

    find /var/www -perm 777 -type d 
    

    Hope this tutorial helps you search files based on permissions and secure your Linux/Unix system.

    The post Find All files with 777 permission in Linux appeared first on TecAdmin.

    ]]>
    https://tecadmin.net/find-files-with-permission-777-in-linux/feed/ 0
    How To Find Files Modified in Last 30 Days in Linux https://tecadmin.net/find-files-modified-in-x-days/ https://tecadmin.net/find-files-modified-in-x-days/#comments Sat, 04 Jul 2020 09:13:41 +0000 https://tecadmin.net/?p=21962 find is the Unix/Linux command line utility used for searching files across the file system. Sometimes we need to search files modified in last few days. Assume you have modified multiple files in your application and forgot to keep track of the modified files. In that case, find command provides you an option to search [...]

    The post How To Find Files Modified in Last 30 Days in Linux appeared first on TecAdmin.

    ]]>
    find is the Unix/Linux command line utility used for searching files across the file system. Sometimes we need to search files modified in last few days. Assume you have modified multiple files in your application and forgot to keep track of the modified files. In that case, find command provides you an option to search files based on their modification. You can also search the files modified before X days.

    Use -mtime option with the find command to search files based on modification time followed by the number of days. Number of days can be used in two formats.

    1. Use + with number of days to search file modified older that X days
    2. Use with number of days to search file modified in last X days

    The below examples will help you to understand the search for files based on modification time.

    Find files modified in last X days

    Use below command to search all files and directories modified in last 30 days. Here dot (.) is used to search in current directory. And -30 defines to search files modified in last 30 day. Change this number with your search requirements.

    find . -mtime -30
    

    You can also customize search based on file type. Use -type followed with -f (file) or -d (directory). Below command will search for files only.

    find . -type f -mtime -30
    

    Find files modified before X days

    The below command will search all files and directories modified before 30 days. Here dot (.) is used to search in current directory. And +30 defines to search files modified before 30 day. Change this number with your search preferences.

    find . -mtime +30
    

    Customize search pattern to search for files only using -type f. Or use -type d to search for directories.

    find . -type f -mtime +30
    

    Conclusion

    This tutorial described you to find files based on modification days. You can also use more options with find command to filter more.

    The post How To Find Files Modified in Last 30 Days in Linux appeared first on TecAdmin.

    ]]>
    https://tecadmin.net/find-files-modified-in-x-days/feed/ 4
    Recursively Count Number of Files within a Directory in Linux https://tecadmin.net/linux-count-files-recursively/ https://tecadmin.net/linux-count-files-recursively/#respond Thu, 20 Feb 2020 06:52:48 +0000 https://tecadmin.net/?p=16621 Sometimes we need to find actual number of files available under a directory. But it directory contains multiple sub directories. Then it is hard to manually count number of files within a directory in Linux system using command line. find DIR_NAME -type f | wc -l find – Is a Linux/Unix command DIR_NAME – A [...]

    The post Recursively Count Number of Files within a Directory in Linux appeared first on TecAdmin.

    ]]>
    Sometimes we need to find actual number of files available under a directory. But it directory contains multiple sub directories. Then it is hard to manually count number of files within a directory in Linux system using command line.

    find DIR_NAME -type f | wc -l
    
    • find – Is a Linux/Unix command
    • DIR_NAME – A directory path to search for. Use dot (.) to start search from current directory
    • -type f – Search for files only (do not include directories)
    • Pipe (|) – Pipe sends output of one command as input to other command
    • wc -l – Count number of lines in result

    Count files within current directory

    Use the following command to count the number of available files under the current directory. Here dot (.) denotes to the current directory.

    find . -type f | wc -l
    

    Recursively Count Number of Files

    Count files in specific directory

    To count files under any other directory use the following command. Here the command will find all files under the /backup directory and print total count on screen.

    find /backup -type f | wc -l
    

    Recursive count files in linux command

    The post Recursively Count Number of Files within a Directory in Linux appeared first on TecAdmin.

    ]]>
    https://tecadmin.net/linux-count-files-recursively/feed/ 0
    How to Find all Files Containing a String in Linux https://tecadmin.net/find-all-files-containing-a-string-in-linux/ https://tecadmin.net/find-all-files-containing-a-string-in-linux/#respond Tue, 04 Nov 2014 06:25:29 +0000 https://tecadmin.net/?p=6351 How to Find all Files Containing a String in Linux. This tutorial will help you to search all files containing specific text string recursively. This tutorial uses “find” command to search string in files. Alternatively, You can also use grep command to search text. Command:- # find / -exec grep -l “string-to-search” {} ; Uses: [...]

    The post How to Find all Files Containing a String in Linux appeared first on TecAdmin.

    ]]>
    How to Find all Files Containing a String in Linux. This tutorial will help you to search all files containing specific text string recursively. This tutorial uses “find” command to search string in files. Alternatively, You can also use grep command to search text.

    Command:-


    # find / -exec grep -l “string-to-search” {} ;


    Uses:

    Below is an example command which searches text ‘redhat’ in / filesystem. This command will search for all files containing string ‘redhat’ and list file names only like below.

    # find / -exec grep -l "redhat" {} ; 
    
    /lib64/security/pam_oddjob_mkhomedir.so
    /lib64/libdevmapper.a.1.02
    /lib64/libdevmapper-event.a.1.02
    /lib64/libdevmapper.a
    /lib64/libdevmapper-event.a
    ...
    

    To Search in specific file extension files. Like search ‘redhat’ in all php files only, use following command.

    # find / -name '*.php' -exec grep -l "redhat" {} ; 
    
    /var/www/projects/index.php
    /var/www/projects/new.php
    ...
    

    The post How to Find all Files Containing a String in Linux appeared first on TecAdmin.

    ]]>
    https://tecadmin.net/find-all-files-containing-a-string-in-linux/feed/ 0