User Tools

Site Tools


find_usage

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
find_usage [2022/07/23 20:51] – [list files in chronological order] rajufind_usage [2024/03/12 23:10] (current) – [find and sort] raju
Line 30: Line 30:
 Ref:- https://stackoverflow.com/questions/11392526/how-to-sort-the-output-of-grep-l-chronologically-by-newest-modification-date/53438441 Ref:- https://stackoverflow.com/questions/11392526/how-to-sort-the-output-of-grep-l-chronologically-by-newest-modification-date/53438441
  
-tags | find and sort, chronological+tags | find and sort, chronological, sort find results chronologically
  
 ==== find number of words in all files under a directory ==== ==== find number of words in all files under a directory ====
Line 39: Line 39:
 Ref:- https://askubuntu.com/questions/926422/how-to-count-the-total-number-of-words-from-all-the-files-in-a-directory/1286714 Ref:- https://askubuntu.com/questions/926422/how-to-count-the-total-number-of-words-from-all-the-files-in-a-directory/1286714
  
 +==== gunzip files on nfs and copy to hadoop ====
 +<code>
 +files_to_copy=`find $dir -maxdepth 1 -iname '*.avro.gz' -newer $tmp_file -print
 +for i in $files_to_copy
 +do
 +    unzipped_file=${tmp_nfs_dir}/`basename $i | sed -e 's/\.gz$//'`
 +    gunzip $i -c > $unzipped_file
 +    hadoop fs -put -f $unzipped_file $hdfs_dir
 +    echo "$unzipped_file -> $hdfs_dir"
 +    rm -f $unzipped_file
 +done
 +</code>
 +
 +tags | process the output of find
 ==== dummy ==== ==== dummy ====
   * [[grep on find results]]   * [[grep on find results]]
Line 45: Line 59:
 See: https://unix.stackexchange.com/questions/34325/sorting-the-output-of-find See: https://unix.stackexchange.com/questions/34325/sorting-the-output-of-find
  
 +==== copy all txt files in a directory to another ====
 +
 +<code>
 +find $SRC_DIR -maxdepth 1 -iname '*.txt' -exec cp -vt $DEST_DIR {} +
 +</code>
 +
 +Note: The $DEST_DIR must exist. Otherwise it will throw an error.
 +
 +The difference between this and ''cp $SRC_DIR/*.txt $DEST_DIR'' is that the latter throws an error if there are no txt files in $SRC_DIR. The find command simply completes even if there are no files.
 +
 +Sample run
 +
 +
 +
 +<code>
 +$ mkdir -p x1/x2 x3
 +$ touch x1/file1.txt x1/file1.pdf x1/file2.txt x1/x2/file3.txt
 +
 +$tree
 +.
 +├── x1
 +│   ├── file1.pdf
 +│   ├── file1.txt
 +│   ├── file2.txt
 +│   └── x2
 +│       └── file3.txt
 +└── x3
 +
 +3 directories, 4 files
 +
 +$find x1 -maxdepth 1 -iname '*.txt' -exec cp -vt x3 {} +
 +`x1/file1.txt' -> `x3/file1.txt'
 +`x1/file2.txt' -> `x3/file2.txt'
 +
 +$tree
 +.
 +├── x1
 +│   ├── file1.pdf
 +│   ├── file1.txt
 +│   ├── file2.txt
 +│   └── x2
 +│       └── file3.txt
 +└── x3
 +    ├── file1.txt
 +    └── file2.txt
 +
 +3 directories, 6 files
 +</code>
 +
 +Without the -maxdepth option, all files underneath $SRC_DIR will be copied. The directory hierarchy is not preserved. For example, while files in x1 are copied to x3 so are the files in x1/x2 etc.,
 +
 +
 +
 +<code>
 +$ rm -rf x3
 +$ mkdir x3
 +$ find x1 -iname '*.txt' -exec cp -vt x3 {} +
 +`x1/x2/file3.txt' -> `x3/file3.txt'
 +`x1/file1.txt' -> `x3/file1.txt'
 +`x1/file2.txt' -> `x3/file2.txt'
 +
 +$ tree
 +.
 +├── x1
 +│   ├── file1.pdf
 +│   ├── file1.txt
 +│   ├── file2.txt
 +│   └── x2
 +│       └── file3.txt
 +└── x3
 +    ├── file1.txt
 +    ├── file2.txt
 +    └── file3.txt
 +
 +3 directories, 7 files
 +</code>
find_usage.1658609471.txt.gz · Last modified: 2022/07/23 20:51 by raju