To zip the files under the "projects" directory with the specified extensions (.txt, .py, .sh, .xlsx, .R), while excluding files where the filename is solely numbers followed by .txt (e.g., 123.txt), you can use the following bash command. This command uses find to locate the files, filters out the excluded ones, and then creates a compressed tar archive (work_station_250606.tar.gz).
Command:
find projects -type f \( -name "*.py" -o -name "*.sh" -o -name "*.xlsx" -o -name "*.R" -o \( -name "*.txt" -a -not -regex '.*/[0-9]+\.txt' \) \) -print0 | xargs -0 tar -czf work_station_250606.tar.gz
Explanation:
- find projects -type f: Searches for files (
-type f) in the "projects" directory and its subdirectories.
- ( -name ".py" -o -name ".sh" -o -name ".xlsx" -o -name ".R" -o ( -name ".txt" -a -not -regex './[0-9]+.txt' ) ):
- Matches files with the extensions
.py, .sh, .xlsx, or .R.
- For
.txt files, it includes only those that do not match the regex pattern .*/[0-9]+\.txt, which excludes files like 123.txt (where the filename is only numbers followed by .txt).
- -print0: Outputs the file paths separated by null characters to handle filenames with spaces or special characters.
- xargs -0 tar -czf work_station_250606.tar.gz: Takes the null-terminated output from
find and passes it as arguments to tar. The tar -czf options create a gzipped tar archive:
-c: Create a new archive.
-z: Compress with gzip.
-f work_station_250606.tar.gz: Specifies the output file name.
How to Run:
- Open a terminal.
- Navigate to the directory where the "projects" folder is located (e.g.,
cd /path/to/your/directory).
- Copy and paste the command above, then press Enter.
- Enter your password if prompted (due to permissions).
Notes:
- This command will include the full paths of the files in the archive relative to the "projects" directory. If you want to preserve the directory structure, this is fine; otherwise, you can modify it with
tar options like --transform if needed.
- If there are no matching files, the command will still create an empty archive.
- Make sure you have the necessary permissions to read the files in "projects" and write the output file.
- If you encounter any errors (e.g., related to
find or tar), check for typos in the command or verify that the "projects" directory exists and contains files with the specified extensions.
This should accomplish what you requested! If you need any modifications, let me know.