User Tools

Site Tools


bash:files:copy_files_to_different_directory

Differences

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

Link to this comparison view

bash:files:copy_files_to_different_directory [2022/06/13 10:15] – created peterbash:files:copy_files_to_different_directory [2022/06/13 10:16] (current) peter
Line 1: Line 1:
 ====== BASH - Files - Copy files to different directory ====== ====== BASH - Files - Copy files to different directory ======
 +
 +<code bash>
 +#!/bin/bash                                                                                                                                                                                                 
 +
 +for filename in *; do
 +  if [[ -f "$filename" ]]; then
 +    base=${filename%.*}
 +    ext=${filename#$base.}
 +    mkdir -p "${ext}"
 +    mv "$filename" "${ext}"
 +  fi
 +done
 +</code>
 +
 +----
 +
 +===== Copy all files by extension to single directory =====
 +
 +<code bash>
 +find . -name \*.txt -exec cp {} someDirectory \;
 +</code>
 +
 +or
 +
 +<code bash>
 +find . -name "file*" -exec mv {} /tmp \;
 +</code>
 +
 +<WRAP info>
 +**NOTE:**  The open brackets **{}** are a placeholder for the argument which is to be used from the output of find.
 +</WRAP>
 +
 +<WRAP info>
 +**NOTE:**  Why not just use **mv file* /tmp**.
 +
 +  * Problem with Moving Large Number of Files
 +    * Linux systems have a predefined limit on the maximum number of arguments that can be used with a single command.
 +    * This limit varies from system to system based on the stack size.
 +    * Thus, if a very high number of files are being specified with the wildcard; e.g. over a hundred thousand files, it throws an error: **Argument list too long**.
 +
 +</WRAP>
 +
 +----
  
bash/files/copy_files_to_different_directory.1655115352.txt.gz · Last modified: 2022/06/13 10:15 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki