Bash script to archive sub-folders with 7zip without archiving archives - bash

I am trying to write a bash script that will zip all sub folders into .cbz files, while leaving the top-level folders alone and that doesn't archive already zipped folders using 7zip.
Here is what I wrote:
#!/bin/bash
for folder in /home/angelucifer/Documents/Personal/MangaLib/*/*
do
7z a -mx0 -mmt2 -tzip "${folder%/}.cbz" "$folder"
rm -rf "$folder"
done
My current issue seems to be that it will archive already zipped folders, but everything else works fine.
The reason I specified a directory is to avoid accidentally archiving the contents of my home folder... again.
My intent for this script is to go into my MangaLib folder, and archive the contents of the folders inside it, without archiving those folders too, which is the purpose of the two wildcards in the address. Then, it should delete the original folder and leave only the .cba file.
Again, the issue is that I will routinely run this script to compress the subfolders of any new additions to my MangaLib folder, but the script will also compress the previously archived folders as well, which is not my intent.

Use file command to find file type. Then zip only the ones you need.
#!/bin/bash
for folder in /home/angelucifer/Documents/Personal/MangaLib/*/*
do
if ! `file $folder | grep -i zip > /dev/null 2>&1`; then
##Most of the zip utilities contain "zip" when checked for file type.
##grep for the expression that matches your case
7z a -mx0 -mmt2 -tzip "${folder%/}.cbz" "$folder"
rm -rf "$folder"
fi
done

Related

Copy whole directory but exclude all folders and subfolders with certain name

I'm not allowed to use rsync on the cluster I'm working on so I need to use cp. I want to copy a large directory including all files and subfolders etc. but without any folders that have the name "outdir".
I tried cp -r -v ./!(outdir) ../target-directory/
but it still copies all folders and contents in deeper directories with the name outdir. It only included the outdir folders in the highest directory.
I also tried cp -r ./*/!(outdir) ../target-directory/ but that one copied all files into the folder without keeping any hirarchy or folders etc.
I also tried certain find commands but it didn't work, but maybe I was just doing something stupid. I'm a beginner with bash so if you could explain your answer and what the flags etc. do that would really be helpfull, I've been trying forever now, on what I think shouldn't be that hard to do.
Instead of cp, you can use tar with option --exclude to control what you want copied or not.
The full command is:
tar --exclude="outdir" -cvpf - . | (cd TARGET_DIRECTORY; tar -xpf -)
So any path that contains the "outdir" pattern will be excluded.
Without the --exclude option, it will copy the entire structure of your current directory under TARGET_DIRECTORY.
You can replace the . in the first tar by your desired source directory.

Copy files to their corresponding destination folders

I have many files in a folder with different extensions (.txt, .ascii, .mat). I want to move them to the destination folder which would be same as file names.
For example:
I have files like a.txt, a.ascii, b.txt, b.ascii, b.mat.
I want to make folder first in the name of a and b, then I want to move files a.txt,a.ascii to folder a and b.txt,b.ascii,b.mat to folder b.
I tried the code as follows. However I need an automatic way to make folder and move the files to it.
#!/bin/sh
mkdir a b
for file in $(<list.txt)
do
cp "$file" a
done
Seems that this Bash script can do the job
#!/usr/bin/env bash
for file in $(<list.txt); do
dirn="${file%.*}"
mkdir -p "$dirn"
cp "$file" "$dirn"
done

Failed to copy list of files to another folder

I have a text file called "list.txt" that contain all the directories of the files that need to be copied to a new folder (dir_newfolder). I wrote the code like below:
for file in $(cat list.txt); do cp ${file} dir_newfolder; done
I got list of errors: cp:"file_name":No such file or directory. The file_names are the lines pulled out from the "list.txt". But when I copy each file_names from the error message and use cp to copy to the new folder. There is no error.
I am using mac os terminal.
Thanks in advance.
Copy a file or folder locally
In the Terminal app on your Mac, use the cp command to make a copy of a file.
For example, to copy a folder named Expenses in your Documents folder to another volume named Data:
% cp -R ~/Documents/Expenses /Volumes/Data/Expenses
The -R flag causes cp to copy the folder and its contents. Note that the folder name does not end with a slash, which would change how cp copies the folder.
in your case:
make sure you are providing correct path list.txt and the correct path for destiny folder, also i mentioned how to access file variable in double quotes , try this code it's working for me
for file in $(cat ~/Documents/list.txt); do cp "$file" ~/dir_newfolder; done

How to change all file type extensions at a certain location?

I'm currently using this line to move all files with a .tif extension to another folder location:
ROBOCOPY "H:\Location\User\Test Folder 1" "H:\Location\User\Test Folder 2" *tif /mov
Now I want to loop through all the files at H:\Location\User\Test Folder 2 and convert them from .tif to .pdf - It looks like the solution might be something like this, but what I don't see here is what folder location this script would be running on (and thusly I'm afraid to test it on my environment) - how do I specify for this to run only on files at H:\Location\User\Test Folder 2?
for f in *.tif; do
mv -- "$f" "${f%.tif}.pdf"
done
The *.tif glob will only find files in the current working directory. Thus, you can change the code this way to ensure that it only modifies files in the intended directory:
cd "H:\Location\User\Test Folder 2"
for f in *.tif; do
mv -- "$f" "${f%.tif}.pdf"
done

Copy command in Mac Terminal fails with "No such file or directory"

This should be straightforward, but I'm getting weird results.
I have a folder with subfolders containing ~4000 files. I'm trying to copy just the files of a certain filetype to another folder, while preserving the subfolder hierarchy.
Command:
cp -R /Users/Steve/Desktop/Hardscapes/*.LOB /Users/Steve/Desktop/Temp
fails with the message:
"/Users/Steve/Desktop/Hardscapes/*.LOB: No such file or directory".
I created the command by typing cp -R then dragging the source folder to the terminal window, adding *.LOB after the /, and dragging the destination folder to the terminal window.
Troubleshooting:
replacing *.LOB with *.* gives the same error.
cp -R /Users/Steve/Desktop/Hardscapes/ /Users/Steve/Desktop/Temp copies the entire Hardscapes folder to Temp, with all its subfolders and files.
Thanks for your help and suggestions.
EDIT: The folder Hardscapes contains only other folders. If I run the command above using one of those folders as the source, the contents are copied faithfully. The folder Hardscapes itself contains no .LOB files - they're only in the subfolders.
So maybe that's the issue, cp can't find any files corresponding to Hardscapes/*.LOB? But I thought the -R switch was supposed to tell it to look through all the subfolders.
Next I put a file named Test.LOB in the Hardscapes folder. The command above copies only that file and none of the subfolders. It looks like the -R switch is not doing its job. Do I have the syntax right?
Try this:
rsync -a --prune-empty-dirs --include '*/' --include '*.LOB' --exclude '*' /Users/Steve/Desktop/Hardscapes/ /Users/Steve/Desktop/Temp
As you already mentioned, directory Hardscapes itself contains no .LOB files. That's why your mask /Users/Steve/Desktop/Hardscapes/*.LOB results in matching no files at all.

Resources