Monday, March 18, 2019


Find Command: -
Part – 1:
Q) How to find a particular file or directory ?
Ans – Using find command.
Syntax –
find <path> -<option> <filename or directory name><Enter – Key>
find . –name f1<Enter – Key>
. -> refer to currently directory
Q) Whether the file file.sh is present in /var/logs/script directory or not ?
Ans - $ find /var/logs/script –type f –name file.sh<Enter – Key>
Whereas
–type f -> option search only files
-name -> option related to name of any files or directory
If you will get the file name in output, then file is present in the above directory otherwise not.
Q) Check whether the directory d1 is present inside the current directory ?
Ans - $ find . –type d –name d1<Enter – Key>
Whereas
–type d -> option search only directories
-name -> option related to name of any files or directory
If you will get directory name in output, then the directory is present inside the current directory otherwise not.
Q) Give me those files which are created within 7 days in /var/logs directory ?
Ans - $ find /var/logs –type f –ctime -7<Enter – Key>
-ctime -> creation time option in days
-7 -> within 7 days related to negative sign with 7 days.
Q) Give me those files which are modified beyond or before 7 days in current directory ?
Ans - $ find . –type f –mtime +7<Enter – Key>
-mtime -> modification time option in days
+7 -> beyond or before 7 days related to positive sign with 7 days.
Q) Give me those files which are accessed between 5-7 days in current directory ?
Ans - $ find . –type f –atime -7 –atime +5<Enter – Key>
-atime -> access time option in days
-7 -> within 7 days
+5 -> beyond or before 5 days.
Q) Give me those files which are modified between 15 – 20 minutes in current directory ?
Ans - $ find . –type f –mmin -20 –mmin +15<Enter – Key>
-mmin -> modified time option in minutes
-20 -> within 20 minutes
+15 -> beyond or before 15 minutes.
Similarly, we can use –cmin and –amin for creation time in minutes and access time in minutes respectively.
Q) Find those directories which are created within one week in /var/logs directory ?
Ans - $ find /var/logs –type d –ctime -7<Enter – Key>
-7 -> related to within one week
Q) Find those files whose size is 20 blocks in current directory ?
Ans - $ find . –type f –size 20<Enter – Key>
Q) Find those files whose size is more than 20 blocks in current directory ?
Ans - $ find . –type f –size +20<Enter – Key>
Q) Find those files whose size is less than 20 blocks in current directory ?
Ans - $ find . –type f –size -20<Enter – Key>
Q) Find those files whose size is 12 GB in current directory ?
Ans - $ find . –type f –size 20g<Enter – Key>
Q) Find those files whose size is more than 20 GB in current directory ?
Ans - $ find . –type f –size +20g<Enter – Key>
Q) Find those files whose size is less than 20 GB in current directory ?
Ans - $ find . –type f –size -20g<Enter – Key>
Q) Find those files whose size is 20 kb in current directory ?
Ans - $ find . –type f –size 20K<Enter – Key>
Q) Find those files whose size is more than 20 kb in current directory ?
Ans - $ find . –type f –size +20K<Enter – Key>
Q) Find those files whose size is less than 20 kb in current directory ?
Ans - $ find . –type f –size -20K<Enter – Key>
Q) Find those files whose size is 20 MB in current directory ?
Ans - $ find . –type f –size 20M<Enter – Key>
Q) Find those files whose size is less than 20 MB in current directory ?
Ans - $ find . –type f –size -20M<Enter – Key>
Q) Whether the file file1.csv is present in current directory or its sub – directory ?
Ans - $ find . –type f –maxdepth 2 –name file1.csv<Enter – Key>
Q) How to search for all files in current directory not in the sub – directories ?
Ans - $ find . –type f –maxdepth 1<Enter – Key>
Q) Whether the directory d1 is present beyond 2 levels of current directory ?
Ans - $ find . –type d –mindepth 2 –name d1<Enter – Key>
Q) Find those files which are created earlier than file f1 in /stat01/msgs directory ?
Ans - $ find /stat01/msgs –type f –cnewer f1<Enter – Key>
Q) Find those files which are accessed before the accessed of file f4 in current directory ?
Ans - $ find . –type f ! –anewer f4<Enter – Key>
Similarly, -mnewer option is used to find files modified earlier than modified time of a particular file.
Q) Find those files which are created between the creation time of f1 and f2. Also, creation time of f1 is less than f2 in currently directory.
Ans - $ find . –type f –cnewer f1 ! –cnewer f2<Enter – Key>
Q) Find all the directories whose permission is 752 in current directory ?
Ans - $ find . –type d –perm 753<Enter – Key>
Q) Find all the files whose permission is 654 in current folder ?
Ans - $ find . –type f –perm 654<Enter – Key>

No comments:

Post a Comment

Interview Questions: - (UNIX - 0) UNIX: -   Q) How to get inode number of a file/directory? Ans – $ ls –i <File/Directory_Name...