Difference between revisions of "Linux command: find"

From RHS Wiki
Jump to navigation Jump to search
Tag: visualeditor
Tag: visualeditor
Line 4: Line 4:
 
  <nowiki>find <path> -mtime -1 -ls</nowiki>
 
  <nowiki>find <path> -mtime -1 -ls</nowiki>
  
=== Last 3 accessed files ===
+
===Last 3 accessed files===
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
find . -type f -exec stat -c '%X %n' {} \; | sort -nr | awk 'NR==1,NR==3 {print $2}'
 
find . -type f -exec stat -c '%X %n' {} \; | sort -nr | awk 'NR==1,NR==3 {print $2}'
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== Last 3 modified files ===
+
===Last 3 modified files===
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
find . -type f -exec stat -c '%Y %n' {} \; | sort -nr | awk 'NR==1,NR==3 {print $2}'
 
find . -type f -exec stat -c '%Y %n' {} \; | sort -nr | awk 'NR==1,NR==3 {print $2}'
 
</syntaxhighlight>
 
</syntaxhighlight>
  
* <code>find . -type f -exec stat -c '%X %n' *</code>: prints the last access' time followed by the file's path for each file in the current directory hierarchy;
+
*<code>find . -type f -exec stat -c '%X %n' *</code>: prints the last access' time followed by the file's path for each file in the current directory hierarchy;
* <code>find . -type f -exec stat -c '%Y %n' *</code>: prints the last modification's time followed by the file's path for each file in the current directory hierarchy;
+
*<code>find . -type f -exec stat -c '%Y %n' *</code>: prints the last modification's time followed by the file's path for each file in the current directory hierarchy;
* <code>sort -nr</code>: sorts in an inverse numerical order;
+
*<code>sort -nr</code>: sorts in an inverse numerical order;
* <code>awk 'NR==1,NR==3 {print $2}'</code>: prints the second field of the first, second and third line.
+
*<code>awk 'NR==1,NR==3 {print $2}'</code>: prints the second field of the first, second and third line.
  
 
You can change the number of files to be shown by changing 3 to the desired number of files in <code>awk 'NR==1,NR==3 {print $2}'</code>.
 
You can change the number of files to be shown by changing 3 to the desired number of files in <code>awk 'NR==1,NR==3 {print $2}'</code>.
Line 31: Line 31:
 
===Exclude extension===
 
===Exclude extension===
 
  find Vídeos/popcorntime/ -type f -not -name "*.torrent" -not -name "*.jpg" -not -name "*.srt" -not -name "*.txt" -exec mv "{}" /media/rafa/HM_030/video/ \;
 
  find Vídeos/popcorntime/ -type f -not -name "*.torrent" -not -name "*.jpg" -not -name "*.srt" -not -name "*.txt" -exec mv "{}" /media/rafa/HM_030/video/ \;
 +
 +
=== Exclude 2 extensions ===
 +
<syntaxhighlight lang="bash">
 +
find ./ ! -regex  '.*\(deb\|vmdk\)$'
 +
</syntaxhighlight>
  
 
===Large Files===
 
===Large Files===
 
  find . -type f -size +10000k -exec ls -lh {} \; | awk '{ print $8 ": " $5 }'
 
  find . -type f -size +10000k -exec ls -lh {} \; | awk '{ print $8 ": " $5 }'

Revision as of 21:48, 18 March 2020

find files

find <path> -name <filename>

find files modified in the last 24h

find <path> -mtime -1 -ls

Last 3 accessed files

find . -type f -exec stat -c '%X %n' {} \; | sort -nr | awk 'NR==1,NR==3 {print $2}'

Last 3 modified files

find . -type f -exec stat -c '%Y %n' {} \; | sort -nr | awk 'NR==1,NR==3 {print $2}'
  • find . -type f -exec stat -c '%X %n' *: prints the last access' time followed by the file's path for each file in the current directory hierarchy;
  • find . -type f -exec stat -c '%Y %n' *: prints the last modification's time followed by the file's path for each file in the current directory hierarchy;
  • sort -nr: sorts in an inverse numerical order;
  • awk 'NR==1,NR==3 {print $2}': prints the second field of the first, second and third line.

You can change the number of files to be shown by changing 3 to the desired number of files in awk 'NR==1,NR==3 {print $2}'.

find case insensitive

find . -iname "fileName.txt"

find video files

find . -type f -exec file -N -i -- {} + | grep video

or if you only want the filenames ...

find . -type f -exec file -N -i -- {} + | sed -n 's!: video/[^:]*$!!p'

Exclude extension

find Vídeos/popcorntime/ -type f -not -name "*.torrent" -not -name "*.jpg" -not -name "*.srt" -not -name "*.txt" -exec mv "{}" /media/rafa/HM_030/video/ \;

Exclude 2 extensions

find ./ ! -regex  '.*\(deb\|vmdk\)$'

Large Files

find . -type f -size +10000k -exec ls -lh {} \; | awk '{ print $8 ": " $5 }'